NAG Library Function Document nag_corr_cov (g02bxc)

Similar documents
NAG Library Function Document nag_corr_cov (g02bxc)

NAG Library Function Document nag_partial_corr (g02byc)

NAG Library Function Document nag_mv_discrim (g03dac)

NAG Library Function Document nag_mv_distance_mat (g03eac)

NAG Library Function Document nag_glm_tran_model (g02gkc)

NAG Library Function Document nag_dummy_vars (g04eac)

NAG Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_all_regsn (g02eac)

NAG Library Function Document nag_mv_ordinal_multidimscale (g03fcc)

NAG Library Function Document nag_prob_lin_non_central_chi_sq (g01jcc)

NAG Library Function Document nag_real_cholesky_skyline_solve (f04mcc)

NAG Library Function Document nag_tsa_multi_part_lag_corr (g13dnc)

NAG Library Function Document nag_2d_spline_eval (e02dec)

NAG Library Function Document nag_smooth_spline_fit (g10abc)

NAG Library Function Document nag_dtrmv (f16pfc)

NAG Library Function Document nag_mv_cluster_indicator (g03ejc)

NAG Library Function Document nag_sum_sqs_update (g02btc)

NAG Library Function Document nag_dtr_load (f16qgc)

NAG Library Function Document nag_asian_geom_price (s30sac)

NAG Library Function Document nag_mv_prin_comp (g03aac)

NAG Library Function Document nag_dsymv (f16pcc)

NAG Library Function Document nag_friedman_test (g08aec)

NAG Library Function Document nag_rngs_copula_students_t (g05rbc)

NAG Library Function Document nag_dgglse (f08zac)

NAG Library Function Document nag_anova_random (g04bbc)

NAG Library Function Document nag_rgsn_matrix_multi_normal (g05lyc)

NAG Library Function Document nag_dgetri (f07ajc)

NAG Library Function Document nag_zero_nonlin_eqns_deriv_1 (c05ubc)

NAG Library Function Document nag_zeros_complex_poly (c02afc)

NAG Library Function Document nag_3d_shep_interp (e01tgc)

NAG Library Function Document nag_2_sample_ks_test (g08cdc)

NAG Library Function Document nag_superlu_diagnostic_lu (f11mmc)

NAG Library Function Document nag_mv_discrim (g03dac)

NAG Library Function Document nag_tsa_cross_spectrum_bivar (g13cec)

NAG Library Function Document nag_1d_cheb_fit_constr (e02agc)

NAG Library Function Document nag_rngs_varma_time_series (g05pcc)

NAG Library Function Document nag_dtrsyl (f08qhc)

NAG Library Function Document nag_rand_2_way_table (g05pzc)

NAG Library Function Document nag_regsn_mult_linear_upd_model (g02ddc)

NAG Library Function Document nag_zhpr2 (f16ssc)

NAG Library Function Document nag_real_sym_posdef_tridiag_lin_solve (f04bgc)

NAG Library Function Document nag_asian_geom_greeks (s30sbc)

NAG Library Function Document nag_bsm_greeks (s30abc)

NAG Library Function Document nag_1d_cheb_intg (e02ajc)

NAG Library Function Document nag_regsn_mult_linear_addrem_obs (g02dcc)

NAG C Library Function Document nag_tsa_multi_cross_corr (g13dmc)

NAG Library Function Document nag_regsn_mult_linear_delete_var (g02dfc)

NAG Library Function Document nag_regress_confid_interval (g02cbc)

NAG Library Function Document nag_dsytrf (f07mdc)

NAG Library Function Document nag_rand_bivariate_copula_frank (g05rfc)

NAG Library Function Document nag_ztbcon (f07vuc)

NAG Library Function Document nag_herm_posdef_tridiag_lin_solve (f04cgc)

NAG Library Function Document nag_real_symm_sparse_eigensystem_sol (f12fcc)

NAG Library Function Document nag_complex_gen_lin_solve (f04cac)

NAG Library Function Document nag_dummy_vars (g04eac)

NAG Library Function Document nag_opt_one_var_deriv (e04bbc)

NAG Library Function Document nag_durbin_watson_stat (g02fcc)

NAG Library Function Document nag_mesh2d_inc (d06aac)

NAG Library Function Document nag_real_symm_sparse_eigensystem_init (f12fac)

NAG Library Function Document nag_dpbtrf (f07hdc)

NAG Library Function Document nag_full_step_regsn (g02efc)

NAG Library Function Document nag_rand_arma (g05phc)

NAG Library Function Document nag_complex_tridiag_lin_solve (f04ccc)

NAG Library Function Document nag_rand_copula_clayton (g05rhc)

NAG Library Function Document nag_smooth_spline_fit (g10abc)

NAG C Library Function Document nag_binary_factor_service (g11sbc)

NAG Library Function Document nag_summary_stats_onevar_combine (g01auc)

NAG C Library Function Document nag_tsa_multi_part_lag_corr (g13dnc)

NAG Library Function Document nag_zero_cont_func_brent_rcomm (c05azc)

NAG Library Function Document nag_robust_m_corr_user_fn (g02hlc)

NAG Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_tsa_varma_diagnostic (g13dsc)

NAG Library Function Document nag_opt_check_deriv (e04hcc)

NAG Library Function Document nag_tsa_auto_corr_part (g13acc)

NAG Library Function Document nag_1d_quad_wt_alglog_1 (d01spc)

NAG Library Function Document nag_1d_quad_wt_alglog (d01apc)

NAG Library Function Document nag_robust_m_corr_user_fn_no_derr (g02hmc)

NAG Library Function Document nag_ode_bvp_fd_nonlin_fixedbc (d02gac)

NAG Library Function Document nag_1d_spline_interpolant (e01bac)

NAG Library Function Document nag_kalman_sqrt_filt_info_var (g13ecc)

NAG Library Function Document nag_prob_durbin_watson (g01epc)

NAG C Library Function Document nag_kruskal_wallis_test (g08afc)

NAG Library Function Document nag_stable_sort (m01ctc)

NAG Library Function Document nag_search_vector (m01fsc)

NAG Library Function Document nag_prob_non_central_f_dist (g01gdc)

NAG Library Routine Document G02BUF.1

NAG C Library Function Document nag_rank_regsn (g08rac)

NAG Library Function Document nag_2d_spline_eval (e02dec)

NAG Library Function Document nag_frequency_table (g01aec)

NAG C Library Function Document nag_linf_fit (e02gcc)

NAG Library Function Document nag_sum_convcorr_real (c06fkc)

NAG Library Function Document nag_dger (f16pmc)

NAG Library Function Document nag_mv_dendrogram (g03ehc)

NAG Library Function Document nag_legendre_p (s22aac)

NAG Library Function Document nag_1d_spline_evaluate (e02bbc)

NAG Library Function Document nag_quad_md_gauss (d01fbc)

NAG Library Function Document nag_outlier_peirce (g07gac)

NAG Library Function Document nag_pde_parab_1d_euler_exact (d03pxc)

NAG Library Function Document nag_opt_simplex_easy (e04cbc)

NAG C Library Function Document nag_cp_stat (g02ecc)

Transcription:

1 Purpose NAG Library Function Document nag_corr_cov () nag_corr_cov () calculates the Pearson product-moment correlation coefficients and the variancecovariance matrix for a set of data. Weights may be used. 2 Specification #include <nag.h> #include <nagg02.h> void nag_corr_cov (Integer n, Integer m, const double x[], Integer tdx, const Integer sx[], const double wt[], double *sw, double wmean[], double std[], double r[], Integer tdr, double v[], Integer tdv, NagError *fail) 3 Description For n observations on m variables a one-pass updating algorithm (see West (1979)) is used to compute the means, the standard deviations, the variance-covariance matrix, and the Pearson product-moment correlation matrix for p selected variables. Suitables weights may be used to indicate multiple observations and to remove missing values. The quantities are defined by: (a) The means (b) The variance-covariance matrix (c) The standard deviations C jk ¼ X n i¼1 x j ¼ X n w i x ij i¼1 X n w i i¼1 w i x ij x j X n i¼1 s j ¼ ð xik x k Þ w i 1 qffiffiffiffiffiffi C jj (d) The Pearson product-moment correlation coefficients j ¼ 1;...;p j ¼ 1;...;p j; k ¼ 1;...;p C jk R jk ¼ qffiffiffiffiffiffiffiffiffiffiffiffiffiffi C jj C kk j; k ¼ 1;...;p where x ij is the value of the ith observation on the jth variable and w i is the weight for the ith observation which will be 1 in the unweighted case. Note that the denominator for the variance-covariance is P n i¼1 w i 1, so the weights should be scaled so that the sum of weights reflects the true sample size..1

NAG Library Manual 4 References Chan T F, Golub G H and Leveque R J (1982) Updating Formulae and a Pairwise Algorithm for Computing Sample Variances Compstat, Physica-Verlag West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532 555 5 Arguments 1: n Integer Input On entry: the number of observations in the dataset, n. Constraint: n > 1. 2: m Integer Input On entry: the total number of variables, m. Constraint: m 1. 3: x½n tdxš const double Input On entry: the data x½i 1Š½j 1Š must contain the ith observation on the jth variable, x ij, for i ¼ 1;...;n and j ¼ 1;...;m. 4: tdx Integer Input On entry: the stride separating matrix row elements in x. Constraint: tdx m 5: sx½mš const Integer Input On entry: indicates which p variables to include in the analysis. sx½j 1Š > 0 The jth variable is to be included. sx½j 1Š ¼0 The jth variable is not to be included. sx is set to the null pointer (Integer *)0 All variables are included in the analysis, i.e., p ¼ m. Constraint: sx½iš 0, for i ¼ 1;...;m. 6: wt½nš const double Input On entry: the optional frequency weighting for each observation. wt½i 1Š contains the weight for the ith data value. Usually wt½i 1Š will be an integral value corresponding to the number of observations associated with the ith data value, or zero if the ith data value is to be ignored. If wt is set to the null pointer (double *)0 then wt is not referenced. Constraint: wt½i 1Š 0:0, for i ¼ 1;...;n. 7: sw double * Output On exit: the sum of weights if wt is not the null pointer, otherwise sw contains the number of observations, n. 8: wmean½mš double Output On exit: the sample means. wmean½j 1Š contains the mean for the jth variable..2

9: std½mš double Output On exit: the standard deviations. std½j 1Š contains the standard deviation for the jth variable. 10: r½m tdrš double Output On exit: the matrix of Pearson product-moment correlation coefficients. r½j 1Š½k 1Š contains the correlation between variables j and k, for j; k ¼ 1;...;p. 11: tdr Integer Input On entry: the stride separating matrix row elements in r. Constraint: tdr m 12: v½m tdvš double Output On exit: the variance-covariance matrix. v½j 1Š½k 1Š contains the covariance between variables j and k, for j; k ¼ 1;...;p. 13: tdv Integer Input On entry: the stride separating matrix row elements in v. Constraint: tdv m 14: fail NagError * Input/Output The NAG error argument (see Section 3.6 in the Essential Introduction). 6 Error Indicators and Warnings NE_2_INT_ARG_LT On entry, tdr ¼ hvaluei while m ¼ hvaluei. The arguments must satisfy tdr m. On entry, tdv ¼ hvaluei while m ¼ hvaluei. These arguments must satisfy tdv m. On entry, tdx ¼ hvaluei while m ¼ hvaluei. These arguments must satisfy tdx m. NE_ALLOC_FAIL Dynamic memory allocation failed. NE_INT_ARG_LE On entry, n must be greater than 1: n ¼ hvaluei. NE_INT_ARG_LT On entry, m ¼ hvaluei. Constraint: m 1. NE_NEG_SX On entry, at least one element of sx is negative. NE_NEG_WEIGHT On entry, at least one of the weights is negative. NE_POS_SX On entry, no element of sx is positive..3

NAG Library Manual NE_SW_LT_ONE On entry, the sum of weights is less than 1.0. NE_VAR_EQ_ZERO A variable has zero variance. At least one variable has zero variance. In this case v and std are as calculated, but r will contain zero for any correlation involving a variable with zero variance. 7 Accuracy For a discussion of the accuracy of the one pass algorithm see Chan et al. (1982) and West (1979). 8 Further Comments Correlation coefficients based on ranks can be computed using nag_ken_spe_corr_coeff (g02brc). 9 Example A program to calculate the means, standard deviations, variance-covariance matrix and a matrix of Pearson product-moment correlation coefficients for a set of 3 observations of 3 variables. 9.1 Program Text /* nag_corr_cov () Example Program. * * Copyright 1992 Numerical Algorithms Group. * * Mark 3, 1992. * Mark 8 revised, 2004. */ #include <nag.h> #include <nagx04.h> #include <stdio.h> #include <nag_stdlib.h> #include <nagg02.h> #define X(I, J) x[(i) *tdx + J] #define R(I, J) r[(i) *tdr + J] #define V(I, J) v[(i) *tdv + J] int main(int argc, char *argv[]) FILE *fpin, *fpout; Integer exit_status = 0, i, j, m, n, tdr, tdv, tdx, test; NagError fail; char w; double *r = 0, *std = 0, sw, *v = 0, *wmean = 0, *wt = 0, *wtptr, *x = 0; 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_corr_cov () Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); test = 0; while ((fscanf(fpin, "%ld%ld %c", &m, &n, &w)!= EOF)) if (m >= 1 && n >= 1).4

if (!(x = NAG_ALLOC(n*m, double))!(r = NAG_ALLOC(m*m, double))!(v = NAG_ALLOC(m*m, double))!(wt = NAG_ALLOC(n, double))!(wmean = NAG_ALLOC(m, double))!(std = NAG_ALLOC(m, double))) fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; tdx = m; tdr = m; tdv = m; else fprintf(fpout, "Invalid m or n.\n"); exit_status = 1; return exit_status; for (i = 0; i < n; i++) fscanf(fpin, "%lf", &wt[i]); for (i = 0; i < n; i++) for (j = 0; j < m; j++) fscanf(fpin, "%lf", &X(i, j)); if (w == w ) wtptr = wt; else wtptr = (double *) 0; /* nag_corr_cov (). * Product-moment correlation, unweighted/weighted * correlation and covariance matrix, allows variables to be * disregarded */ nag_corr_cov(n, m, x, tdx, (Integer *) 0, wtptr, &sw, wmean, std, r, tdr, v, tdv, &fail); if (fail.code!= NE_NOERROR) fprintf(fpout, "Error from nag_corr_cov ().\n%s\n", fail.message); exit_status = 1; goto END; if (wtptr) fprintf(fpout, "\ncase %ld --- Using weights\n", ++test); else fprintf(fpout, "\ncase %ld --- Not using weights\n", ++test); fprintf(fpout, "\n\n"); for (i = 0; i < n; i++) fprintf(fpout, "%6.1f%6.1f%6.1f%6.1f\n", X(i, 0), X(i, 1), X(i, 2), wt[i]); fprintf(fpout, "\n"); fprintf(fpout, "\n"); for (i = 0; i < m; i++) fprintf(fpout, "%6.1f\n", wmean[i]); fprintf(fpout, "\n\n"); for (i = 0; i < m; i++) fprintf(fpout, "%6.1f\n", std[i]); fprintf(fpout, "\n\n"); for (i = 0; i < m; i++) for (j = 0; j < m; j++) fprintf(fpout, " %7.4f ", R(i, j));.5

NAG Library Manual fprintf(fpout, "\n"); fprintf(fpout, "\n\n"); for (i = 0; i < m; i++) for (j = 0; j < m; j++) fprintf(fpout, " %7.3f ", V(i, j)); fprintf(fpout, "\n"); fprintf(fpout, "\nsum of weights %6.1f\n", sw); END: if (x) NAG_FREE(x); if (r) NAG_FREE(r); if (v) NAG_FREE(v); if (wt) NAG_FREE(wt); if (wmean) NAG_FREE(wmean); if (std) NAG_FREE(std); if (fpin!= stdin) fclose(fpin); if (fpout!= stdout) fclose(fpout); return exit_status; 9.2 Program Data nag_corr_cov () Example Program Data 3 3 w 9.1231 3.7011 4.5230 0.9310 0.0900 0.8870 0.0009 0.0099 0.0999 0.1300 1.3070 0.3700 3 3 w 0.1300 1.3070 0.3700 9.1231 3.7011 4.5230 0.9310 0.0900 0.8870 0.0009 0.0099 0.0999 3 3 u 0.717 9.370 0.013 1.119 0.133 9.700 11.100 23.510 11.117 0.900 9.013 8.710 3 3 w 0.717 19.370 0.013 1.119 0.133 9.700 11.100 23.510 11.117 0.900 9.013 78.710 3 3 u 0.717 19.370 0.013 1.119 0.133 9.700 11.100 3.510 13.117 0.900 0.013 78.710 3 3 w 0.717 19.370 0.913 1.119 0.133 9.700 17.100 93.510 13.117 30.900 0.013 78.710 9.3 Program Results nag_corr_cov () Example Program Results Case 1 --- Using weights Input data.6

0.9 0.1 0.9 9.1 0.0 0.0 0.1 3.7 0.1 1.3 0.4 4.5 0.5 0.4 0.6 0.4 0.6 0.3 1.0000-0.4932 0.9839-0.4932 1.0000-0.3298 0.9839-0.3298 1.0000 0.197-0.123 0.149-0.123 0.316-0.063 0.149-0.063 0.117 Sum of weights 17.3 Case 2 --- Using weights 9.1 3.7 4.5 0.1 0.9 0.1 0.9 1.3 0.0 0.0 0.1 0.4 1.3 0.3 1.0 3.3 1.4 1.5 1.0000 0.9908 0.9903 0.9908 1.0000 0.9624 0.9903 0.9624 1.0000 10.851 4.582 5.044 4.582 1.971 2.089 5.044 2.089 2.391 Sum of weights 1.8 Case 3 --- Not using weights 1.1 0.1 9.7 0.7 11.1 23.5 11.1 9.4 0.9 9.0 8.7 0.0 4.4 10.9 9.8 5.8 11.8 1.2.7

NAG Library Manual 1.0000 0.9193 0.9200 0.9193 1.0000 0.6915 0.9200 0.6915 1.0000 33.951 63.208 6.485 63.208 139.250 9.871 6.485 9.871 1.464 Sum of weights 3.0 Case 4 --- Using weights 1.1 0.1 9.7 0.7 11.1 23.5 11.1 19.4 0.9 9.0 78.7 0.0 10.7 22.7 11.1 1.9 4.5 1.8 1.0000 0.9985 0.0173 0.9985 1.0000 0.0716 0.0173 0.0716 1.0000 3.672 8.538 0.059 8.538 19.909 0.570 0.059 0.570 3.185 Sum of weights 20.1 Case 5 --- Not using weights 1.1 0.1 9.7 0.7 11.1 3.5 13.1 19.4 0.9 0.0 78.7 0.0 4.4 1.2 33.8 5.8 2.0 38.9 1.0000 0.9999-0.4781 0.9999 1.0000-0.4881-0.4781-0.4881 1.0000 33.951 11.567-108.343 11.567 3.941-37.687-108.343-37.687 1512.750 Sum of weights 3.0.8

Case 6 --- Using weights 1.1 0.1 9.7 0.7 17.1 93.5 13.1 19.4 30.9 0.0 78.7 0.9 17.2 86.3 15.9 4.2 25.6 13.7 1.0000-0.0461 0.7426-0.0461 1.0000-0.7033 0.7426-0.7033 1.0000 17.846-4.989 43.123-4.989 656.407-247.692 43.123-247.692 188.970 Sum of weights 21.0.9 (last)