// betasp => n x q // Xsp => N x q // XB => N x 1 void comb_XB_sp(int *n, int *r, int *T, int *q, double *Xsp, double *betasp, int *constant, double *XB) { int i, j, l, t, n1, r1, T1, q1; n1=*n; r1=*r; T1=*T; q1=*q; double *XB1, *dump, *I; XB1 = (double *) malloc((size_t)((n1*n1)*sizeof(double))); dump = (double *) malloc((size_t)((n1)*sizeof(double))); I = (double *) malloc((size_t)((n1)*sizeof(double))); for(l=0; l<r1; l++){ for(t=0; t<T1; t++){ for(i=0; i<n1; i++){ I[i] = 0.0; } for(j=0; j<q1; j++){ extract_X_sp2(t, l, j, n, r, T, Xsp, XB1); // n x n for(i=0; i<n1; i++){ dump[i] = betasp[i+j*n1]; // n x 1 } MProd(dump, constant, n, XB1, n, dump); // n x 1 MAdd(I, n, constant, dump, I); // n x 1 } put_together1(l, t, n, r, T, XB, I); } } free(XB1); free(dump); free(I); return; }
// betatp => T x p ?? // Xtp => N x p // XB => N x 1 void comb_XB_tp(int *n, int *r, int *T, int *p, double *Xtp, double *betatp, int *constant, double *XB) { int t, l, n1, r1, T1, p1; n1 =*n; r1 =*r; T1 =*T; p1 =*p; double *X1, *beta, *XB1; X1 = (double *) malloc((size_t)((n1*p1)*sizeof(double))); beta = (double *) malloc((size_t)((p1)*sizeof(double))); XB1 = (double *) malloc((size_t)((n1)*sizeof(double))); for(l=0; l<r1; l++){ for(t=0; t<T1; t++){ extract_X(t, l, n, r, T, p, Xtp, X1); // nrT x p into n x p extract_beta_t(t, T, p, betatp, beta); // p x T into p x 1 MProd(beta, constant, p, X1, n, XB1); // n x 1 put_together1(l, t, n, r, T, XB, XB1); // int i; // for(i=0; i< n1*p1; i++){ // Rprintf(" X1: %4.4f, \n", X1[i]); // } // for(i=0; i< p1; i++){ // Rprintf(" beta: %4.4f, \n", beta[i]); // } // for(i=0; i< n1; i++){ // Rprintf(" XB: %4.4f, \n", XB1[i]); // } } } free(X1); free(beta); free(XB1); return; }
// K-step Forecasts without its void zlt_fore_gpp(int *cov, int *K, int *n, int *m, int *r, int *p, int *rT, int *T, int *rK, int *nrK, double *dnm, double *dm, double *phi, double *nu, double *sig_e, double *sig_eta, double *beta, double *rho, double *wp, double *foreX, int *constant, double *foreZ) { int l, k, t, i, T1, K1, r1, n1, m1, col; T1 =*T; K1 =*K; r1 =*r; n1 =*n; m1 =*m; col =*constant; double *C, *det, *I, *A, *mu, *XB, *XB1; double *wp1, *Aw, *eta, *eps, *zfore; C = (double *) malloc((size_t)((n1*m1)*sizeof(double))); // Sigeta = (double *) malloc((size_t)((m1*m1)*sizeof(double))); // Sinv = (double *) malloc((size_t)((m1*m1)*sizeof(double))); det = (double *) malloc((size_t)((col)*sizeof(double))); I = (double *) malloc((size_t)((m1*col)*sizeof(double))); A = (double *) malloc((size_t)((m1*n1)*sizeof(double))); mu = (double *) malloc((size_t)((col)*sizeof(double))); XB = (double *) malloc((size_t)((n1*r1*K1*col)*sizeof(double))); XB1 = (double *) malloc((size_t)((n1*col)*sizeof(double))); wp1 = (double *) malloc((size_t)((m1*col)*sizeof(double))); Aw = (double *) malloc((size_t)((n1*col)*sizeof(double))); eta = (double *) malloc((size_t)((m1*col)*sizeof(double))); eps = (double *) malloc((size_t)((col)*sizeof(double))); zfore = (double *) malloc((size_t)((n1*col)*sizeof(double))); double *S, *C12c, *s21, *sig; S = (double *) malloc((size_t)((m1*m1)*sizeof(double))); C12c = (double *) malloc((size_t)((m1*col)*sizeof(double))); s21 = (double *) malloc((size_t)((col)*sizeof(double))); sig = (double *) malloc((size_t)((col)*sizeof(double))); /* // exponential covariance if(cov[0] == 1){ for(i = 0; i < (m1*m1); i++){ S[i] = exp(-1.0*phi[0]*dm[i]); // Sigeta[i] = sig_eta[0]*S[i]; } // MInv(Sigeta, Sinv_eta, m, det); for(i=0; i < m1*n1; i++){ C[i] = exp(-1.0*phi[0]*dnm[i]); } } // gaussian covariance if(cov[0] == 2){ for(i = 0; i < (m1*m1); i++){ S[i] = exp(-1.0*phi[0]*phi[0]*dm[i]*dm[i]); // Sigeta[i] = sig_eta[0]*S[i]; } // MInv(Sigeta, Sinv_eta, m, det); for(i=0; i < m1*n1; i++){ C[i] = exp(-1.0*phi[0]*phi[0]*dnm[i]*dnm[i]); } } // spherical covariance if(cov[0] == 3){ for(i = 0; i < (m1*m1); i++){ if(dm[i] > 0 && dm[i] <= 1.0/phi[0]){ S[i] = (1.0-1.5*phi[0]*dm[i]+0.5*(phi[0]*dm[i])*(phi[0]*dm[i])*(phi[0]*dm[i])); // Sigeta[i] = sig_eta[0]*S[i]; } else if(dm[i] >= 1.0/phi[0]){ S[i] = 0.0; // Sigeta[i] = 0.0; } else{ S[i] = 1.0; // Sigeta[i] = 1.0*sig_eta[0]; } } // MInv(Sigeta, Sinv_eta, m, det); for(i=0; i < m1*n1; i++){ if(dnm[i] > 0 && dnm[i] <= 1.0/phi[0]){ C[i] = 1.0-1.5*phi[0]*dnm[i]+0.5*(phi[0]*dnm[i])*(phi[0]*dnm[i])*(phi[0]*dnm[i]); } else if(dnm[i] >= 1.0/phi[0]){ C[i] = 0.0; } else{ C[i] = 1.0; } } } // matern covariance, nu = 3/2 if(cov[0] == 4){ for(i = 0; i < (m1*m1); i++){ S[i] = ((1.0+phi[0]*dm[i])*exp(-1.0*phi[0]*dm[i])); // Sigeta[i] = sig_eta[0]*S[i]; } // MInv(Sigeta, Sinv_eta, m, det); for(i=0; i < m1*n1; i++){ C[i] = (1.0+phi[0]*dnm[i])*exp(-1.0*phi[0]*dnm[i]); } } */ covF(cov, m, m, phi, nu, dm, S); covF(cov, n, m, phi, nu, dnm, C); MInv(S, S, m, det); // m x m MProd(S, m, m, C, n, A); // n x m for(i=0; i<m1; i++){ I[i] = 0.0; } mu[0] = 0.0; MProd(beta, constant, p, foreX, nrK, XB); // nrK x 1 for(l=0; l<r1; l++){ for(k=0; k<1; k++){ t = (T1-1); extract_alt2(l, k, n, rK, K, XB, XB1); // n x 1 for(i=0; i<m1; i++){ wp1[i] = wp[i+t*m1+l*m1*T1]; } MProd(wp1, constant, m, A, n, Aw); // n x 1 for(i=0; i<n1; i++){ extn_12(i, m, C, C12c); // m x 1 xTay(C12c, S, C12c, m, s21); // 1 x 1 if(s21[0] > 1.0){ s21[0] = 1.0-pow(1,-320); } if(s21[0] == 1.0){ s21[0] = 1.0-pow(1,-320); } sig[0] = sig_eta[0] * (1.0 - s21[0]); mu[0] = 0.0; mvrnormal(constant, mu, sig_e, constant, eps); mu[0] = Aw[i]; mvrnormal(constant, mu, sig, constant, eta); zfore[i] = XB1[i] + eta[0] + eps[0]; } put_together1(l, k, n, r, K, foreZ, zfore); } for(k=1; k<K1; k++){ for(i=0; i<m1; i++){ wp1[i] = wp1[i]; // m x 1 } MProd(wp1, constant, m, A, n, Aw); // n x 1 extract_alt2(l, k, n, rK, K, XB, XB1); // n x 1 mvrnormal(constant, mu, sig_e, constant, eps); for(i=0; i<n1; i++){ extn_12(i, m, C, C12c); // m x 1 xTay(C12c, S, C12c, m, s21); // 1 x 1 if(s21[0] > 1.0){ s21[0] = 1.0-pow(1,-320); } if(s21[0] == 1.0){ s21[0] = 1.0-pow(1,-320); } sig[0] = sig_eta[0] * (1.0 - s21[0]); mu[0] = 0.0; mvrnormal(constant, mu, sig_e, constant, eps); mu[0] = Aw[i]; mvrnormal(constant, mu, sig, constant, eta); zfore[i] = XB1[i] + eta[0] + eps[0]; } put_together1(l, k, n, r, K, foreZ, zfore); } } free(S); free(det); free(C); free(I); free(A); free(mu); free(XB); free(XB1); free(wp1); free(Aw); free(eta); free(eps); free(zfore); free(C12c); free(s21); free(sig); return; }
// conditional posterior for o_lt void o_gp(int *n, int *r, int *T, int *rT, int *p, double *prior_omu, double *prior_osig, double *sig_e, double *sig_eta, double *S, double *Qeta, double *XB, double *z, int *constant, double *opost) { int i, l, t, r1, nn, row, T1, col, p1; r1 = *r; row = *n; T1 = *T; nn = row * row; col = *constant; p1 = *p; double *o_1, *de_tT, *det1, *chi_tT, *mean1, *XB1, *QXB1, *zT; o_1 = (double *) malloc((size_t)((row)*sizeof(double))); de_tT = (double *) malloc((size_t)((nn)*sizeof(double))); det1 = (double *) malloc((size_t)((col)*sizeof(double))); chi_tT = (double *) malloc((size_t)((row)*sizeof(double))); mean1 = (double *) malloc((size_t)((row)*sizeof(double))); XB1 = (double *) malloc((size_t)((row)*sizeof(double))); QXB1 = (double *) malloc((size_t)((row)*sizeof(double))); zT = (double *) malloc((size_t)((row)*sizeof(double))); // for 1 <= t <= T, the delta part for(i=0; i < nn; i++) { de_tT[i] = (1.0/sig_e[0]) + Qeta[i] + 1.0/prior_osig[0]; } MInv(de_tT, de_tT, n, det1); // n x n double *term1, *I, *term2, *zt; term1 = (double *) malloc((size_t)((nn)*sizeof(double))); I = (double *) malloc((size_t)((row)*sizeof(double))); term2 = (double *) malloc((size_t)((row)*sizeof(double))); zt = (double *) malloc((size_t)((row)*sizeof(double))); // term1 and term2 for(i=0; i < nn; i++) { term1[i] = (sig_eta[0]/sig_e[0])* S[i]; } for(i=0; i < row; i++) { I[i]= 1.0; } MProd(I, constant, n, term1, n, term2); for(l=0; l < r1; l++) { for(t=0; t < T1; t++) { extract_alt2(l, t, n, rT, T, XB, XB1); extract_alt2(l, t, n, rT, T, z, zT); MProd(zT, constant, n, term1, n, zt); for(i=0; i < row; i++) { mean1[i] = (XB1[i]+zt[i])/(1+term2[i]) + prior_omu[0]; } mvrnormal(constant, mean1, de_tT, n, o_1); // random generator put_together1(l, t, n, r, T, opost, o_1); } } // End of loop year free(o_1); free(de_tT); free(det1); free(chi_tT); free(mean1); free(XB1); free(QXB1); free(zT); free(term1); free(I); free(term2); free(zt); return; }