dd <- d + d^2 + d^3
M <- Moments(θ)
Omega <- matrix( .C("Compute_Omega",
- X=as.double(X), Y=as.double(Y), M=as.double(M),
+ X=as.double(X), Y=as.integer(Y), M=as.double(M),
pn=as.integer(n), pd=as.integer(d),
W=as.double(W), PACKAGE="morpheus")$W, nrow=dd, ncol=dd )
MASS::ginv(Omega)
else if (!is.numeric(θ0$b) || length(θ0$b) != K || any(is.na(θ0$b)))
stop("θ0$b: length K, no NA")
# TODO: stopping condition? N iterations? Delta <= epsilon ?
- for (loop in 1:10)
+ for (loop in 1:2)
{
op_res = constrOptim( linArgs(θ0), .self$f, .self$grad_f,
ui=cbind(
// 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)
+//void Compute_Omega(double* X, int* Y, double* M, int* pn, int* pd, double* W)
+//{
+// int n=*pn, d=*pd;
+// int dim = d + d*d + d*d*d;
+// //double* W = (double*)malloc(dim*dim*sizeof(double));
+//
+// // (Re)Initialize W:
+// for (int j=0; j<dim; j++)
+// {
+// for (int k=0; k<dim; k++)
+// W[j*dim+k] = 0.0;
+// }
+// double* g = (double*)malloc(dim*sizeof(double));
+// for (int i=0; i<n; i++)
+// {
+// // g == gi:
+// for (int j=0; j<d; j++)
+// 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
+// int idx2 = ((j-d) - idx1) / d; //num col
+// 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[j];
+// }
+// for (int j=d+d*d; j<dim; j++)
+// {
+// int idx1 = (j-d-d*d) % d; //num row
+// 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;
+// 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)] - M[j];
+// }
+// // Add 1/n t(gi) %*% gi to W
+// for (int j=0; j<dim; j++)
+// {
+// for (int k=0; k<dim; k++)
+// W[j*dim+k] += g[j] * g[k] / n;
+// }
+// }
+// free(g);
+//}
+
+// Optimisation attempt:
+void Compute_Omega(double* X, int* Y, double* M, int* pn, int* pd, double* W)
{
int n=*pn, d=*pd;
int dim = d + d*d + d*d*d;
double* g = (double*)malloc(dim*sizeof(double));
for (int i=0; i<n; i++)
{
+ printf("i: %i\n",i);
// g == gi:
for (int j=0; j<d; j++)
- g[j] = Y[i] * X[mi(i,j,n,d)] - M[j];
+ g[j] = (Y[i] ? X[mi(i,j,n,d)] - M[j] : 0.0);
for (int j=d; j<d+(d*d); j++)
{
int idx1 = (j-d) % d; //num row
int idx2 = ((j-d) - idx1) / d; //num col
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[j];
+ if (Y[i])
+ {
+ if (idx1 == idx2)
+ g[j]--;
+ g[j] += X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)] - M[j];
+ }
}
for (int j=d+d*d; j<dim; j++)
{
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;
- 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)] - M[j];
+ if (Y[i])
+ {
+ if (idx1 == idx2)
+ g[j] -= X[mi(i,idx3,n,d)];
+ if (idx1 == idx3)
+ g[j] -= X[mi(i,idx2,n,d)];
+ if (idx2 == idx3)
+ g[j] -= X[mi(i,idx1,n,d)];
+ g[j] += X[mi(i,idx1,n,d)]*X[mi(i,idx2,n,d)]*X[mi(i,idx3,n,d)] - M[j];
+ }
}
// Add 1/n t(gi) %*% gi to W
for (int j=0; j<dim; j++)
-optimBeta <- function(N, n, K, p, beta, b, link, weights, ncores)
+optimBeta <- function(N, n, K, p, beta, b, link, ncores)
{
library(morpheus)
res <- multiRun(
- list(n=n, p=p, beta=beta, b=b, optargs=list(K=K, link=link, weights=weights)),
+ list(n=n, p=p, beta=beta, b=b, optargs=list(K=K, link=link)),
list(
# morpheus
function(fargs) {
d <- 2
n <- 1e4
ncores <- 1
-strw <- "1-1-1"
-weights <- c(1,1,1)
cmd_args <- commandArgs()
for (arg in cmd_args)
d <- as.integer(spl[2])
} else if (spl[1] == "link") {
link <- spl[2]
- } else if (spl[1] == "weights") {
- strw <- spl[2]
- weights <- as.numeric(unlist(strsplit(spl[2], ",")))
}
}
}
beta <- matrix( c(1,2,-1,0,3,4,-1,-3,0,2,2,-3,0,1,0,-1,-4,3,2,0, -1,1,3,-1,0,0,2,0,1,-2,1,2,-1,0,3,4,-1,-3,0,2, 2,-3,0,1,0,-1,-4,3,2,0,1,1,2,2,-2,-2,3,1,0,0), ncol=K )
}
-mr <- optimBeta(N, n, K, p, beta, b, link, weights, ncores)
+mr <- optimBeta(N, n, K, p, beta, b, link, ncores)
mr_params <- list("N"=N, "nc"=ncores, "n"=n, "K"=K, "d"=d, "link"=link,
- "p"=c(p,1-sum(p)), "beta"=beta, "b"=b, "weights"=weights)
+ "p"=c(p,1-sum(p)), "beta"=beta, "b"=b)
-save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,"_",strw,".RData",sep=""))
+save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,".RData",sep=""))
N=100
n=1e5
-nc=10
+nc=50
for d in 2 5 10; do
for link in "logit" "probit"; do
- for weights in "1,1,0"; do
- R --slave --args N=$N n=$n nc=$nc d=$d link=$link weights=$weights <accuracy.R >out_${n}_${link}_${d}_${weights} 2>&1
- done
+ R --slave --args N=$N n=$n nc=$nc d=$d link=$link <accuracy.R >out_${n}_${link}_${d}_${weights} 2>&1
done
done
-
-#for d in 2 5; do
-# for n in 5000 10000 100000 500000 1000000; do
-# for link in "logit" "probit"; do
-# R --slave --args N=$N n=$n nc=$nc d=$d link=$link <accuracy.R >out_$n$link$d 2>&1
-# done
-# done
-#done