From: Benjamin Auder Date: Tue, 10 Dec 2019 00:32:45 +0000 (+0100) Subject: Fix code which is now running, but computations are probably wrong X-Git-Url: https://git.auder.net/game/current/bundles/framework/css/body.css?a=commitdiff_plain;h=4bf8494d8484be5eab39dd2a83e649678a2de713;p=morpheus.git Fix code which is now running, but computations are probably wrong --- diff --git a/pkg/R/optimParams.R b/pkg/R/optimParams.R index dc294f0..4fb6615 100644 --- a/pkg/R/optimParams.R +++ b/pkg/R/optimParams.R @@ -117,11 +117,12 @@ setRefClass( 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 }, @@ -251,18 +252,21 @@ setRefClass( 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) } diff --git a/pkg/src/functions.c b/pkg/src/functions.c index c8d51dd..68f1b79 100644 --- a/pkg/src/functions.c +++ b/pkg/src/functions.c @@ -54,19 +54,39 @@ void Moments_M3(double* X, double* Y, int* pn, int* pd, double* M3) } } +#include + // 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