/* nag_moments_ratio_quad_forms (g01nbc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include int main(void) { /* Scalars */ double abserr, beta, y0, eps; Integer exit_status, i, j, l1, l2, lmax, n, pda, pdb, pdsigma; NagError fail; Nag_OrderType order; /* Arrays */ double *a=0, *b=0, *c=0, *ela=0, *emu=0, *rmom=0, *sigma=0; #ifdef NAG_COLUMN_MAJOR #define A(I,J) a[(J-1)*pda + I - 1] #define B(I,J) b[(J-1)*pdb + I - 1] #define SIGMA(I,J) sigma[(J-1)*pdsigma + I - 1] order = Nag_ColMajor; #else #define A(I,J) a[(I-1)*pda + J - 1] #define B(I,J) b[(I-1)*pdb + J - 1] #define SIGMA(I,J) sigma[(I-1)*pdsigma + J - 1] order = Nag_RowMajor; #endif INIT_FAIL(fail); exit_status = 0; Vprintf("nag_moments_ratio_quad_forms (g01nbc) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); Vscanf("%lf%lf%*[^\n] ", &beta, &y0); Vscanf("%ld%ld%ld%*[^\n] ", &n, &l1, &l2); /* Allocate memory */ if ( !(a = NAG_ALLOC(n * n, double)) || !(b = NAG_ALLOC(n * n, double)) || !(c = NAG_ALLOC(n * n, double)) || !(ela = NAG_ALLOC(n, double)) || !(emu = NAG_ALLOC(n, double)) || !(rmom = NAG_ALLOC(l2-l1+1, double)) || !(sigma = NAG_ALLOC(n * n, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } pda = n; pdb = n; pdsigma = n; /* Compute A, EMU, and SIGMA for simple autoregression */ for (i = 1; i <= n; ++i) { for (j = i; j <= n; ++j) { A(j, i) = 0.0; B(j, i) = 0.0; } } for (i = 1; i <= n - 1; ++i) { A(i + 1, i) = 0.5; B(i, i) = 1.0; } emu[0] = y0 * beta; for (i = 1; i <= n - 1; ++i) emu[i] = beta * emu[i - 1]; SIGMA(1, 1) = 1.0; for (i = 2; i <= n; ++i) SIGMA(i, i) = beta * beta * SIGMA(i - 1, i - 1) + 1.0; for (i = 1; i <= n; ++i) { for (j = i + 1; j <= n; ++j) SIGMA(j, i) = beta * SIGMA(j - 1, i); } eps = 0.0; /* nag_moments_ratio_quad_forms (g01nbc). * Moments of ratios of quadratic forms in Normal variables, * and related statistics */ nag_moments_ratio_quad_forms(order, Nag_RatioMoments, Nag_MeanInclude, n, a, n, b, n, c, n, ela, emu, sigma, n, l1, l2, &lmax, rmom, &abserr, eps, &fail); if (fail.code == NE_NOERROR || fail.code == NE_SOME_MOMENTS || fail.code == NE_ACCURACY) { Vprintf("\n"); Vprintf(" n = %3ld beta = %6.3f y0 = %6.3f\n", n, beta, y0); Vprintf("\n"); Vprintf(" Moments\n"); Vprintf("\n"); j = 0; for (i = l1; i <= lmax; ++i) { ++j; Vprintf("%3ld%12.3e\n", i, rmom[j - 1]); } } else { Vprintf("Error from nag_moments_ratio_quad_forms (g01nbc).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (c) NAG_FREE(c); if (ela) NAG_FREE(ela); if (emu) NAG_FREE(emu); if (rmom) NAG_FREE(rmom); if (sigma) NAG_FREE(sigma); return exit_status; }