computeW = function(θ)
{
- dim <- d + d^2 + d^3
- W <<- solve( matrix( .C("Compute_Omega",
+ #require(MASS)
+ dd <- d + d^2 + d^3
+ W <<- MASS::ginv( matrix( .C("Compute_Omega",
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) )
+ W=as.double(W), PACKAGE="morpheus")$W, nrow=dd, ncol=dd ) )
NULL #avoid returning W
},
else if (any(is.na(θ0$b)))
stop("θ0$b cannot have missing values")
- op_res = constrOptim( linArgs(θ0), .self$f, .self$grad_f,
- ui=cbind(
- rbind( rep(-1,K-1), diag(K-1) ),
- matrix(0, nrow=K, ncol=(d+1)*K) ),
- ci=c(-1,rep(0,K-1)) )
-
- # debug:
- 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 <= epsilon ?
+ # TODO: stopping condition? N iterations? Delta <= epsilon ?
+ for (loop in 1:10)
+ {
+ op_res = constrOptim( linArgs(θ0), .self$f, .self$grad_f,
+ ui=cbind(
+ rbind( rep(-1,K-1), diag(K-1) ),
+ matrix(0, nrow=K, ncol=(d+1)*K) ),
+ ci=c(-1,rep(0,K-1)) )
+
+ computeW(expArgs(op_res$par))
+ # debug:
+ #print(W)
+ print(op_res$value)
+ print(expArgs(op_res$par))
+ }
expArgs(op_res$par)
}
}
}
+#include <stdio.h>
+
// W = 1/N sum( t(g(Zi,theta)) g(Zi,theta) )
// with g(Zi, theta) = i-th contribution to all moments (size dim) - real moments
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;
+
+//printf("X: \n");
+//for (int kk=0; kk<d*n; kk++) printf("%f ",X[kk]);
+//printf("\n");
+//printf("Y: \n");
+//for (int kk=0; kk<n; kk++) printf("%f ",Y[kk]);
+//printf("\n");
+//printf("M: \n");
+//for (int kk=0; kk<dim; kk++) printf("%f ",M[kk]);
+//printf("\n");
+
+ // (Re)Initialize W:
+ for (int j=0; j<dim; j++)
+ {
+ for (int k=0; k<dim; k++)
+ W[j*dim+k] = 0.0;
+ }
+
//double* W = (double*)calloc(dim*dim,sizeof(double));
- double* g = (double*)malloc(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[j];
for (int j=d; j<d+(d*d); j++)
{
int idx1 = (j-d) % d; //num row
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)] - M[i];
+ g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)] - M[j];
}
for (int j=d+d*d; j<dim; j++)
{
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)] - M[i];
+ g[j] += Y[i] * X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)]*X[mi(i,idx3,n,d)] - M[j];
}
+
+//printf("i=%i, g=: \n", i);
+//for (int kk=0; kk<d; kk++) printf("%f ",g[kk]);
+//printf("\n");
+
// Add 1/n t(gi) %*% gi to W
for (int j=0; j<dim; j++)
{
}
}
free(g);
+
+// for (int j=0; j<dim; j++)
+// {
+// printf("\n");
+// for (int k=0; k<dim; k++)
+// printf("%f ",W[j*dim+k]);
+// }
}