Example #1
0
void newton_fd(double (*funcpt)(double *,int),double *xi,int N,double *x) {
	int it,i;
	double *jac,*hess,*L,*xf;
	double stop,result;
	
	jac = (double*) malloc(sizeof(double) *N);
	xf = (double*) malloc(sizeof(double) *N);
	hess = (double*) malloc(sizeof(double) *N * N);
	L = (double*) malloc(sizeof(double) *N * N);
	
	it = 0;
	stop = 1.0;
	
	while (stop > 1e-05 && it < 100) {
		//result = funcpt(xi,N,jac,hess);
		jacobian_fd(funcpt,xi,N,jac);
		hessian_fd(funcpt,xi,N,hess);
		//mdisplay(jac,1,N);
		//mdisplay(hess,N,N);
		modelhess2(hess,N,L);
		//mdisplay(L,N,N);
		scale(jac,1,N,-1.0);
		linsolve_lower(L,N,jac,x);
		madd(x,xi,x,1,N);
		msub(x,xi,xf,1,N);
		stop = array_max_abs(xf,N);
		for(i=0;i < N;++i) {
			xi[i] = x[i];
		}
		it++;
		//printf("result %lf \n",result);
		printf("Values obtained %lf , %lf after %d Iterations \n",x[0],x[1],it);
	}
	
	free(jac);
	free(hess);
	free(xf);
	free(L);
}
Example #2
0
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;
}
Example #3
0
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;
}
Example #4
0
int newton_min_func(double (*funcpt)(double *,int),double *xi,int N,double *dx,double fsval,double *xf) {
	int rcode,iter;
	int i,siter,retval;
	double gtol,stol;
	double fx,num,den,stop0,maxstep,fxf;
	double *jac,*hess,*scheck,*xc,*L,*step;
	
	jac = (double*) malloc(sizeof(double) *N);
	scheck = (double*) malloc(sizeof(double) *N);
	xc = (double*) malloc(sizeof(double) *N);
	step = (double*) malloc(sizeof(double) *N);
	hess = (double*) malloc(sizeof(double) *N * N);
	L = (double*) malloc(sizeof(double) *N * N);
	
	rcode = 0;
	iter = 0;
	siter = (int) SETITER;
	gtol = pow((double) FDVAL,1.0/3.0);
	stol = gtol * gtol;
	fx = funcpt(xi,N);
	jacobian_fd(funcpt,xi,N,jac);
	
	//set values
	maxstep = 1.0;
	
	for(i = 0; i < N;++i) {
		dx[i] = 1.0 / dx[i];
	}
	
	//Check Stop0
	if (fabs(fx) > fabs(fsval)) {
			den = fabs(fx);
	} else {
			den = fabs(fsval);
	}
	for(i = 0; i < N;++i) {
		if (fabs(xi[i]) > 1.0 / fabs(dx[i])) {
			num = fabs(xi[i]);
		} else {
			num = 1.0 / fabs(dx[i]);
		}
		scheck[i] = fabs(jac[i]) * num / den;
	}
	
	stop0 = array_max_abs(scheck,N);
	
	if (stop0 <= gtol * 1e-03) {
		rcode = 1;
		for(i = 0; i < N;++i) {
			xf[i] = xi[i];
		}
		return rcode;
	}
	
	hessian_fd(funcpt,xi,N,hess);
	
	for(i = 0; i < N;++i) {
		xc[i] = xi[i];
	}
	
	while (rcode == 0 && iter < siter) {
		modelhess(hess,N,dx,L);
		scale(jac,1,N,-1.0);
		linsolve_lower(L,N,jac,step);
		scale(jac,1,N,-1.0);
		retval = lnsrch(funcpt,xc,jac,step,N,maxstep,dx,stol,xf); 
		fxf = funcpt(xf,N);
		jacobian_fd(funcpt,xf,N,jac);
		rcode = stopcheck(fxf,N,xc,xf,jac,dx,fsval,gtol,stol,retval);
		hessian_fd(funcpt,xf,N,hess);
		for(i = 0; i < N;++i) {
			xc[i] = xf[i];
		}
	}
	
	free(jac);
	free(hess);
	free(scheck);
	free(xc);
	free(L);
	free(step);
	return rcode;
}