/* nag_rand_varma (g05pjc) Example Program. * * Copyright 2008, Numerical Algorithms Group. * * Mark 9, 2009. */ /* Pre-processor includes */ #include #include #include #include #include #include #define VAR(I, J) var[(order == Nag_RowMajor)?(I*pdvar+J):(J*pdvar+I)] #define X(I, J) x[(order == Nag_RowMajor)?(I*pdx+J):(J*pdx+I)] #define PHI(i, j, l) phi[l*k*k+j*k+i] #define THETA(i, j, l) theta[l*k*k+j*k+i] int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* Integer scalar and array declarations */ Integer exit_status = 0; Integer lr, x_size, var_size, i, ip, iq, j, k, l, lstate, n, tmp1, tmp2, tmp3, tmp4, tmp5; Integer *state = 0; Integer pdx, pdvar; /* NAG structures */ NagError fail; Nag_ModeRNG mode; /* Double scalar and array declarations */ double *phi = 0, *r = 0, *theta = 0, *var = 0, *x = 0, *xmean = 0; /* Use column major order */ Nag_OrderType order = Nag_ColMajor; /* Choose the base generator */ Nag_BaseRNG genid = Nag_Basic; Integer subid = 0; /* Set the seed */ Integer seed[] = { 1762543 }; Integer lseed = 1; /* Initialise the error structure */ INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_rand_varma (g05pjc) Example Program Results\n\n"); /* Get the length of the state array */ lstate = -1; nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_init_repeatable (g05kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Read data from a file */ /* Skip heading*/ fscanf(fpin, "%*[^\n] "); /* Read in initial parameters */ fscanf(fpin, "%ld%ld%ld%ld%*[^\n] ", &k, &ip, &iq, &n); /* Calculate the size of the reference vector */ tmp1 = (ip > iq)?ip:iq; if (ip == 0) { tmp2 = k * (k + 1) / 2; } else { tmp2 = k*(k+1)/2 + (ip-1)*k*k; } tmp3 = ip + iq; if (k >= 6) { lr = (5*tmp1*tmp1+1)*k*k + (4*tmp1+3)*k + 4; } else { tmp4 = k*tmp1*(k*tmp1+2); tmp5 = k*k*tmp3*tmp3+tmp2*(tmp2+3)+k*k*(iq+1); lr = (tmp3*tmp3+1)*k*k + (4*tmp3+3)*k + ((tmp4 > tmp5)?tmp4:tmp5) + 4; } pdvar = k; pdx = (order == Nag_ColMajor)?k:n; x_size = (order == Nag_ColMajor)?pdx * n:pdx * k; var_size = pdvar * k; pdvar = k; pdx = (order == Nag_ColMajor)?k:n; x_size = (order == Nag_ColMajor)?pdx * n:pdx * k; var_size = pdvar * k; /* Allocate arrays */ if (!(phi = NAG_ALLOC(ip*k*k, double)) || !(r = NAG_ALLOC(lr, double)) || !(theta = NAG_ALLOC(iq*k*k, double)) || !(var = NAG_ALLOC(var_size, double)) || !(x = NAG_ALLOC(x_size, double)) || !(xmean = NAG_ALLOC(k, double)) || !(state = NAG_ALLOC(lstate, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read in the AR parameters */ for (l = 0; l < ip; l++) { for (i = 0; i < k; i++) { for (j = 0; j < k; j++) fscanf(fpin, "%lf ", &PHI(i, j, l)); } } fscanf(fpin, "%*[^\n] "); /* Read in the MA parameters */ if (iq > 0) { for (l = 0; l < iq; l++) { for (i = 0; i < k; i++) { for (j = 0; j < k; j++) fscanf(fpin, "%lf ", &THETA(i, j, l)); } } fscanf(fpin, "%*[^\n] "); } /* Read in the means */ for (i = 0; i < k; i++) fscanf(fpin, "%lf ", &xmean[i]); fscanf(fpin, "%*[^\n] "); /* Read in the variance / covariance matrix*/ for (i = 0; i < k; i++) { for (j = 0; j <= i; j++) fscanf(fpin, "%lf ", &VAR(i, j)); } fscanf(fpin, "%*[^\n] "); /* Initialise the generator to a repeatable sequence */ nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_init_repeatable (g05kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Generate the first realization */ mode = Nag_InitializeAndGenerate; nag_rand_varma(order, mode, n, k, xmean, ip, phi, iq, theta, var, pdvar, r, lr, state, x, pdx, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_varma (g05pjc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Display the results */ fprintf(fpout, " Realization Number 1\n"); for (i = 0; i < k; i++) { fprintf(fpout, "\n Series number %3ld\n", i+1); fprintf(fpout, " -------------\n\n "); for (j = 0; j < n; j++) fprintf(fpout, "%9.3f%s", X(i, j), (j+1)%8?" ":"\n "); } fprintf(fpout, "\n"); /* Generate a second realization */ mode = Nag_ReGenerateFromReference; nag_rand_varma(order, mode, n, k, xmean, ip, phi, iq, theta, var, pdvar, r, lr, state, x, pdx, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_rand_varma (g05pjc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Display the results */ fprintf(fpout, " Realization Number 2\n"); for (i = 0; i < k; i++) { fprintf(fpout, "\n Series number %3ld\n", i+1); fprintf(fpout, " -------------\n\n "); for (j = 0; j < n; j++) fprintf(fpout, "%9.3f%s", X(i, j), (j+1)%8?" ":"\n "); } fprintf(fpout, "\n"); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (phi) NAG_FREE(phi); if (r) NAG_FREE(r); if (theta) NAG_FREE(theta); if (var) NAG_FREE(var); if (x) NAG_FREE(x); if (xmean) NAG_FREE(xmean); if (state) NAG_FREE(state); return exit_status; }