void matinverse
     ( std::vector< std::vector<double> > mat,
//          double mat[3][3],
       int  order,
       std::vector< std::vector<double> > &matinv
//          double matinv[3][3]
     )
   {
      int i, j;
      std::vector< int > indexx(order+1);
      //std::vector< std::vector<double> > lu(order+1,order+1);
      //std::vector< std::vector<double> >  b(order+1,2);
	  std::vector< std::vector<double> > lu(order+1,std::vector<double>(order+1,0.0)); //DEBUG: updated the vector<vector<double>> constructor.
	  std::vector< std::vector<double> > b(order+1,std::vector<double>(2,0.0)); //DEBUG: updated the vector<vector<double>> constructor.

      matinv.resize(order);  // rows
      for (std::vector< std::vector<double> >::iterator it=matinv.begin(); it != matinv.end();++it)
          it->resize(order);

      for (i = 1; i <= order; i++)
        {
          indexx[i] = i;
          for (j = 1; j <= order; j++)
              lu[i][j] = mat[i-1][j-1];
        }
      ludecomp(lu, indexx, order);
       for (j = 1; j <= order; j++)
        {
          for (i = 1; i <= order; i++)
            {
             if (i == j)
                 b[i][1] = 1.0;
               else
                 b[i][1] = 0.0;
            }

          lubksub(lu, indexx, order, b);
          for (i = 1; i <= order; i++)
             matinv[i-1][j-1] = b[i][1];
        }
   } // procedure matinverse
示例#2
0
void lu_mtrinv( double *a, int n )
{
    int i,j,k;
    int *pivot;

    double s;
  
    pivot = (int *)ALLOCMEM(n*sizeof(int));

    /*
     *  AP = LU
     */
    ludecomp( a,n,pivot );

    for( i=0; i<n; i++ )
    {
        if ( ABS(A(i,i))<AEPS )
        {
            fprintf( stderr, "Inv: Matrix is singular.\n" );
            return;
        }
        A(i,i) = 1.0/A(i,i);
    }

    /*  
     *  INV(U)
     */
    for( i=n-2; i>=0; i-- )
        for( j=n-1; j>i; j-- )
        {
            s = -A(i,j);
            for( k=i+1; k<j; k++ ) s -= A(i,k)*A(k,j);
            A(i,j) = s;
        }

    /*
     * INV(L)
     */
    for( i=n-2; i>=0; i-- )
        for( j=n-1; j>i; j-- )
        {
            s = 0.0;
            for( k=i+1; k<=j; k++ ) s -= A(j,k)*A(k,i);
            A(j,i) = A(i,i)*s;
        }
  
    /* 
     * A  = INV(AP)
     */
    for( i=0; i<n; i++ )
        for( j=0; j<n; j++ )
        {
            s = 0.0;
            for( k=MAX(i,j); k<n; k++ )
            {
                if ( k!=i )
                    s += A(i,k)*A(k,j);
                else
                    s += A(k,j);

                A(i,j) = s;
            }
        }

    /*
     * A = INV(A) (at last)
     */
    for( i=n-1; i>=0; i-- )
        if ( pivot[i]!=i )
            for( j=0; j<n; j++ )
            {
                s = A(i,j);
                A(i,j) = A(pivot[i],j);
                A(pivot[i],j) = s;
            }

    FREEMEM((char *)pivot);
}
示例#3
0
文件: emle.c 项目: binaryWorld/ctsa
int as154_seas(double *inp, int N, int optmethod, int p, int d, int q, int s, int P, int D, int Q,
	double *phi, double *theta, double *PHI, double *THETA, double *wmean,double *var,double *loglik,double *hess) {
	int i, pq, retval, length, offset,ret;
	double *b, *tf, *x,*inp2,*dx,*thess;
	int *ipiv;
	double maxstep;
	alik_seas_object obj;
	custom_function as154_min;
	obj = alik_seas_init(p, d, q, s, P, D, Q, N);
	inp2 = (double*)malloc(sizeof(double)* (N - s*D));
	pq = obj->pq;
	b = (double*)malloc(sizeof(double)* pq);
	tf = (double*)malloc(sizeof(double)* pq);
	thess = (double*)malloc(sizeof(double)* pq*pq);
	dx = (double*)malloc(sizeof(double)* pq);
	ipiv = (int*)malloc(sizeof(int)* pq);

	length = N;

	maxstep = 1.0;


	css_seas(inp, N, optmethod, p, d, q, s, P, D, Q, phi, theta, PHI, THETA, wmean, var,loglik,hess);

	/*

	*/

	if (D > 0) {
		N = diffs(inp, N, D, s, inp2);
	}
	else {
		for (i = 0; i < N; ++i) {
			inp2[i] = inp[i];
		}
	}

	x = (double*)malloc(sizeof(double)* (N - d));

	if (d > 0) {
		N = diff(inp2, N, d, x); // No need to demean x
	}
	else {
		for (i = 0; i < N; ++i) {
			x[i] = inp2[i];
		}
	}

	obj->N = N;

	offset = obj->offset;
	for (i = 0; i < p; ++i) {
		b[i] = phi[i];
	}
	for (i = 0; i < q; ++i) {
		b[p + i] = -theta[i];
	}
	for (i = 0; i < P; ++i) {
		b[p + q + i] = PHI[i];
	}
	for (i = 0; i < Q; ++i) {
		b[p + q + P + i] = -THETA[i];
	}

	if (obj->M == 1) {
		b[p + q + P + Q] = *wmean;
	}

	obj->mean = *wmean;

	//mdisplay(b, 1, p + q + P + Q);

	for (i = 0; i < N; ++i) {
		obj->x[offset + i] = obj->x[offset + 2 * N + i] = x[i];
	}
	for (i = N; i < 2 * N; ++i) {
		obj->x[offset + i] = 0.0;
	}
	//printf("\n %d %g ", pq,maxstep);

//	custom_function as154_min = { fas154_seas, obj };
	as154_min.funcpt = fas154_seas;
	as154_min.params = obj;

	retval = fminunc(&as154_min, NULL, pq, b, maxstep, optmethod, tf);
	if (retval == 0) {
		ret = 0;
	}
	else if (retval == 15) {
		ret = 15;
	}
	else if (retval == 4) {
		ret = 4;
	}
	else {
		ret = 1;
	}

	for (i = 0; i < pq; ++i) {
		dx[i] = 1.0;
	}

	hessian_fd(&as154_min, tf, pq, dx, obj->eps, hess);

	mtranspose(hess, pq, pq, thess);

	for (i = 0; i < pq*pq; ++i) {
		thess[i] = (N - d - s*D) * 0.5 * (hess[i] + thess[i]);
	}


	ludecomp(thess, pq, ipiv);
	minverse(thess, pq, ipiv, hess);

	for (i = 0; i < p; ++i) {
		phi[i] = tf[i];
	}
	for (i = 0; i < q; ++i) {
		theta[i] = -tf[p + i];
	}
	for (i = 0; i < P; ++i) {
		PHI[i] = tf[p + q + i];
	}
	for (i = 0; i < Q; ++i) {
		THETA[i] = -tf[p + q + P + i];
	}

	if (obj->M == 1) {
		*wmean = tf[p + q + Q + P];
	}
	else {
		*wmean = 0.0;
	}

	*var = (obj->ssq) / (double) N;
	*loglik = obj->loglik;
	//printf("MEAN %g \n", mean(obj->x+N,N));
	//mdisplay(obj->x + N, 1, N);

	free(b);
	free(tf);
	free(inp2);
	free(x);
	free(dx);
	free(thess);
	free(ipiv);
	free_alik_seas(obj);
	return ret;
}
示例#4
0
文件: emle.c 项目: binaryWorld/ctsa
int as154(double *inp, int N, int optmethod, int p, int d, int q, double *phi, double *theta, double *wmean, double *var,double *resid,double *loglik,double *hess) {
	int i,pq,retval,length,ret;
	double *b,*tf,*x,*dx,*thess;
	int *ipiv;
	double maxstep;
	alik_object obj;
	custom_function as154_min;

	x = (double*)malloc(sizeof(double)* (N - d));

	length = N;
	
	maxstep = 1.0;

	obj = alik_init(p, d, q, N);


	css(inp, N, optmethod, p, d, q, phi, theta, wmean, var,resid,loglik,hess);

	if (d > 0) {
		N = diff(inp, N, d, x); // No need to demean x
	}
	else {
		for (i = 0; i < N; ++i) {
			x[i] = inp[i];
		}
	}

	obj->N = N;
	obj->mean = *wmean;
	pq = obj->pq;
	b = (double*)malloc(sizeof(double)* pq);
	tf = (double*)malloc(sizeof(double)* pq);
	thess = (double*)malloc(sizeof(double)* pq*pq);
	dx = (double*)malloc(sizeof(double)* pq);
	ipiv = (int*)malloc(sizeof(int)* pq);

	for (i = 0; i < p; ++i) {
		b[i] = phi[i];
	}
	for (i = 0; i < q; ++i) {
		b[p + i] = -theta[i];
	}

	if (obj->M == 1) {
		b[p + q] = obj->mean;
	}


	for (i = 0; i < N; ++i) {
		obj->x[i] = obj->x[2 * N + i] = x[i];
	}
	for (i = N; i < 2 * N; ++i) {
		obj->x[i] = 0.0;
	}

	//20141121ÐÞ¸Ä

	//custom_function as154_min = { fas154, obj };
	as154_min.funcpt = fas154;
	as154_min.params = obj;

	retval = fminunc(&as154_min, NULL, pq, b,maxstep, optmethod, tf);

	if (retval == 0) {
		ret = 0;
	}
	else if (retval == 15) {
		ret = 15;
	}
	else if (retval == 4) {
		ret = 4;
	}
	else {
		ret = 1;
	}

	for (i = 0; i < pq; ++i) {
		dx[i] = 1.0;
	}

	hessian_fd(&as154_min, tf, pq, dx, obj->eps, hess);
	
	mtranspose(hess, pq, pq, thess);

	for (i = 0; i < pq*pq; ++i) {
		thess[i] = (N-d) * 0.5 * (hess[i] + thess[i]);
	}
	

	ludecomp(thess, pq, ipiv);
	minverse(thess, pq, ipiv, hess);


	for (i = 0; i < p; ++i) {
		phi[i] = tf[i];
	}
	for (i = 0; i < q; ++i) {
		theta[i] = -tf[p + i];
	}
	if (obj->M == 1) {
		*wmean = tf[p + q];
	}
	else {
		*wmean = 0.0;
	}
	/*
	wmean = 0.0;
	for (i = 0; i < N; ++i) {
		wmean += (obj->x[N + i] * obj->x[N + i]);
	}*/

	*var = (obj->ssq) / (double) N;
	for (i = 0; i < N - d; ++i) {
		resid[i] = obj->x[N + i];
	}
	*loglik = obj->loglik;
	//printf("MEAN %g \n", mean(obj->x+N,N));
	//mdisplay(obj->x + N, 1, N);

	free(b);
	free(tf);
	free(x);
	free(thess);
	free(ipiv);
	free(dx);
	free_alik(obj);
	return ret;
}