#' 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)))
{
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"
{
"Product t(Mi - hat_Mi) W (Mi - hat_Mi) with Mi(theta)"
- A <- M(θ) - Mhat
+ A <- Moments(θ) - Mhat
t(A) %*% W %*% A
},
{
"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(θ)
{
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)
}
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
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++)
{
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++)