From b389a46aa58d9e7f0bb45b7be72e01ebfba26d8e Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 9 Dec 2019 15:36:05 +0100
Subject: [PATCH] Fix typos and mistakes

---
 pkg/R/optimParams.R | 17 +++++++++--------
 pkg/src/functions.c |  9 ++++-----
 to-cran.sh          |  1 +
 3 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/pkg/R/optimParams.R b/pkg/R/optimParams.R
index c061fcf..a5818ed 100644
--- a/pkg/R/optimParams.R
+++ b/pkg/R/optimParams.R
@@ -31,7 +31,7 @@
 #' o$f( o$linArgs(par0) )
 #' o$f( o$linArgs(par1) )
 #' @export
-optimParams = function(X, Y, K, link=c("logit","probit"))
+optimParams <- function(X, Y, K, link=c("logit","probit"))
 {
 	# Check arguments
   if (!is.matrix(X) || any(is.na(X)))
@@ -119,13 +119,13 @@ setRefClass(
     {
       dim <- d + d^2 + d^3
       W <<- solve( matrix( .C("Compute_Omega",
-        X=as.double(X), Y=as.double(Y), M=as.double(M(θ)),
+        X=as.double(X), Y=as.double(Y), M=as.double(Moments(θ)),
         pn=as.integer(n), pd=as.integer(d),
         W=as.double(W), PACKAGE="morpheus")$W, nrow=dim, ncol=dim) )
       NULL #avoid returning W
     },
 
-    M <- function(θ)
+    Moments = function(θ)
     {
       "Vector of moments, of size d+d^2+d^3"
 
@@ -148,7 +148,7 @@ setRefClass(
     {
 			"Product t(Mi - hat_Mi) W (Mi - hat_Mi) with Mi(theta)"
 
-			A <- M(θ) - Mhat
+			A <- Moments(θ) - Mhat
       t(A) %*% W %*% A
     },
 
@@ -156,8 +156,8 @@ setRefClass(
 		{
 			"Gradient of f, dimension (K-1) + d*K + K = (d+2)*K - 1"
 
-      -2 * t(grad_M(θ)) %*% getW(θ) %*% (Mhat - M(θ))
-    }
+      -2 * t(grad_M(θ)) %*% W %*% (Mhat - Moments(θ))
+    },
 
     grad_M = function(θ)
     {
@@ -256,10 +256,11 @@ setRefClass(
 				ci=c(-1,rep(0,K-1)) )
 
       # debug:
-      print(computeW(expArgs(op_res$par)))
+      #computeW(expArgs(op_res$par))
+      #print(W)
       # We get a first non-trivial estimation of W
       # TODO: loop, this redefine f, so that we can call constrOptim again...
-      # Stopping condition? N iterations? Delta <= ε ?
+      # Stopping condition? N iterations? Delta <= epsilon ?
 
 			expArgs(op_res$par)
 		}
diff --git a/pkg/src/functions.c b/pkg/src/functions.c
index 8731b9f..c8d51dd 100644
--- a/pkg/src/functions.c
+++ b/pkg/src/functions.c
@@ -59,14 +59,14 @@ void Moments_M3(double* X, double* Y, int* pn, int* pd, double* M3)
 void Compute_Omega(double* X, double* Y, double* M, int* pn, int* pd, double* W)
 {
 	int n=*pn, d=*pd;
-  //int dim = d+d*d+d*d*d
+  int dim = d + d*d + d*d*d;
   //double* W = (double*)calloc(dim*dim,sizeof(double));
   double* g = (double*)malloc(dim * sizeof(double));
   for (int i=0; i<n; i++)
   {
     // Fill gi:
     for (int j=0; j<d; j++)
-      g[j] = Y[i] * X[mi(i,j,n,d)] - M[i]
+      g[j] = Y[i] * X[mi(i,j,n,d)] - M[i];
     for (int j=d; j<d+(d*d); j++)
     {
       int idx1 = (j-d) % d; //num row
@@ -74,7 +74,7 @@ void Compute_Omega(double* X, double* Y, double* M, int* pn, int* pd, double* W)
       g[j] = 0.0;
       if (idx1 == idx2)
         g[j] -= Y[i];
-			g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)];
+			g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)] - M[i];
     }
     for (int j=d+d*d; j<dim; j++)
     {
@@ -82,14 +82,13 @@ void Compute_Omega(double* X, double* Y, double* M, int* pn, int* pd, double* W)
       int idx2 = ((j-d-d*d - idx1) / d) %d; //num col
       int idx3 = (((j-d-d*d - idx1) / d) - idx2) / d; //num "depth"
       g[j] = 0.0;
-      double tensor_elt = Y[i]*X[mi(i,k,n,d)] / n;
       if (idx1 == idx2)
         g[j] -= Y[i] * X[mi(i,idx3,n,d)];
       if (idx1 == idx3)
         g[j] -= Y[i] * X[mi(i,idx2,n,d)];
       if (idx2 == idx3)
         g[j] -= Y[i] * X[mi(i,idx1,n,d)];
-      g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)]*X[mi(i,idx3,n,d)];
+      g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)]*X[mi(i,idx3,n,d)] - M[i];
     }
     // Add 1/n t(gi) %*% gi to W
     for (int j=0; j<dim; j++)
diff --git a/to-cran.sh b/to-cran.sh
index 16d4407..f03587a 100755
--- a/to-cran.sh
+++ b/to-cran.sh
@@ -11,6 +11,7 @@ for file in `find -name *.R`; do
 		sed -i 's/β/beta/g' $file
 		sed -i 's/ρ/rho/g' $file
 		sed -i 's/λ/lambda/g' $file
+		sed -i 's/θ/theta/g' $file
 		sed -i 's/Σ/Sigma/g' $file
 	fi
 done
-- 
2.44.0