| 1 | #ifndef _Covariance_h_ |
| 2 | #define _Covariance_h_ |
| 3 | /* Covariance.h |
| 4 | * |
| 5 | * Copyright (C) 1993-2021 David Weenink |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "CCA.h" |
| 22 | #include "PCA.h" |
| 23 | #include "SSCP.h" |
| 24 | #include "TableOfReal_extensions.h" |
| 25 | |
| 26 | Thing_define (Covariance, SSCP) { |
| 27 | }; |
| 28 | |
| 29 | Collection_define (CovarianceList, OrderedOf, Covariance) { |
| 30 | SSCPList asSSCPList () { |
| 31 | return reinterpret_cast <SSCPList> (this); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | autoTableOfReal Covariance_TableOfReal_extractDistanceQuantileRange (Covariance me, TableOfReal thee, double qlow, double qhigh); |
| 36 | /* |
| 37 | Select from a TableOfReal the rows whose Mahalanobis distance to the centroid |
| 38 | (from the SSCP) is in the quantile [qlow, qhigh]. |
| 39 | */ |
| 40 | |
| 41 | autoTableOfReal Covariance_TableOfReal_mahalanobis (Covariance me, TableOfReal thee, bool useTableCentroid); |
| 42 | /* |
| 43 | Calculate the Mahalanobis distance: sqrt ((x-m)'S**-1 (x-m)) |
| 44 | use the m-vector (centroid) from the covariance unless useTableColumnMeans is true. |
| 45 | */ |
| 46 | autoTableOfReal Covariance_TableOfReal_scaledResiduals (Covariance me, TableOfReal thee, bool useTableCentroid); |
| 47 | |
| 48 | autoCovariance TableOfReal_to_Covariance (TableOfReal me); |
| 49 | |
| 50 | autoTableOfReal Covariance_to_TableOfReal_randomSampling (Covariance me, integer numberOfData); |
| 51 | /* Generate a table with data based on the covariance matrix */ |
| 52 | |
| 53 | void Covariance_PCA_generateOneVector_inline (Covariance me, PCA thee, VECVU vec, VEC buf); |
| 54 | /* |
| 55 | A convenience function to avoid the calculation of the PCA each time we want to generate a random vector |
| 56 | The PCA must be the result of a previous SSCP_to_PCA call ! |
| 57 | The covariance matrix must not be singular or diagonal! |
| 58 | Returns the random sampled vector in vec (which should be of size my numberOfColumns). |
| 59 | Preconditions: |
| 60 | 1. Covariance may not be in diagonal representation (1 row) |
| 61 | 2. Dimensions of me and PCA agree |
| 62 | 3. vec is of length my numberOfColumns |
| 63 | 4. buf, a vector of length my numberOfColumns, is needed so the routine cannot fail |
| 64 | */ |
| 65 | |
| 66 | autoCovariance Covariance_create (integer dimension); |
| 67 | |
| 68 | autoCovariance Covariance_createSimple (constVECVU const& covariances, constVECVU const& centroid, integer numberOfObservations); |
| 69 | |
| 70 | autoCovariance Covariance_create_reduceStorage (integer dimension, kSSCPstorage storage); |
| 71 | /* |
| 72 | storage full: complete matrix |
| 73 | storage diagonal: only diagonal |
| 74 | |
| 75 | See also SSCP_expand () for usage. |
| 76 | */ |
| 77 | |
| 78 | /* Precondition ||vector|| = 1 */ |
| 79 | void Covariance_getMarginalDensityParameters (Covariance me, constVECVU const& vector, double *out_mean, double *out_stdev); |
| 80 | |
| 81 | double Covariance_getMarginalProbabilityAtPosition (Covariance me, constVECVU const& vector, double x); |
| 82 | |
| 83 | double Covariance_getProbabilityAtPosition_string (Covariance me, conststring32 xpos); |
| 84 | |
| 85 | double Covariance_getProbabilityAtPosition (Covariance me, constVEC x); |
| 86 | /* evaluate the pdf(x,mu,Sigma) at x */ |
| 87 | |
| 88 | autoCovariance SSCP_to_Covariance (SSCP me, integer numberOfConstraints); |
| 89 | |
| 90 | autoSSCP Covariance_to_SSCP (Covariance me); |
| 91 | |
| 92 | void Covariance_difference (Covariance me, Covariance thee, double *out_prob, double *out_chisq, double *out_df); |
| 93 | |
| 94 | void Covariance_getSignificanceOfOneMean (Covariance me, integer index, double mu, double *out_probability, double *out_t, double *out_df); |
| 95 | |
| 96 | void Covariance_getSignificanceOfMeansDifference (Covariance me, integer index1, integer index2, double mu, int paired, int equalVariances, double *out_probability, double *out_t, double *out_ndf); |
| 97 | |
| 98 | void Covariance_getSignificanceOfOneVariance (Covariance me, integer index, double sigmasq, double *out_probability, double *out_chisq, double *out_ndf); |
| 99 | |
| 100 | void Covariance_getSignificanceOfVariancesRatio (Covariance me, integer index1, integer index2, double ratio, double *out_probability, double *out_f, double *out_df); |
| 101 | |
| 102 | double Covariances_getMultivariateCentroidDifference (Covariance me, Covariance thee, int equalCovariances, double *out_prob, double *out_fisher, double *out_df1, double *out_df2); |
| 103 | /* Are the centroids of me and thee different? |
| 104 | Assumption: the two covariances are equal. (we pool me and thee, dimension p). |
| 105 | Two sample test of Morrison (1990), page 141: |
| 106 | T^2 = n1*n2/(n1+n2) (m1-m2)'S^-1(m1-m2) # weighted Mahalanobis distance |
| 107 | f = T^2 * (n1+n2-p-1)/((n1+n2-2)*p) |
| 108 | f has Fisher distribution with p and n1+n2-p-1 degrees of freedom. |
| 109 | */ |
| 110 | |
| 111 | void Covariances_equality (CovarianceList me, int method, double *out_prob, double *out_chisq, double *out_df); |
| 112 | /* |
| 113 | Equality of covariance. |
| 114 | method = 1 : Bartlett (Morrison, 1990) |
| 115 | method = 2 : Wald (Schott, 2001) |
| 116 | */ |
| 117 | |
| 118 | double Covariance_TableOfReal_normalityTest_BHEP (Covariance me, TableOfReal data, constVEC const& responsibilities, double *inout_beta, double *out_tnb, double *out_lnmu, double *out_lnvar, bool *out_covarianceIsSingular); |
| 119 | /* |
| 120 | Multivariate normality test of nxp data matrix according to the method described in |
| 121 | Henze & Wagner (1997), A new approach to the BHEP tests for multivariate normality, |
| 122 | Journal of Multivariate Analysis 62, 1-23. |
| 123 | The test statistic is returned in tnb, together with the lognormal mean 'lnmu' and the lognormal variance 'lnvar'. |
| 124 | */ |
| 125 | |
| 126 | autoCovariance CovarianceList_to_Covariance_pool (CovarianceList me); |
| 127 | autoCovariance CovarianceList_to_Covariance_between (CovarianceList me); |
| 128 | autoCovariance CovarianceList_to_Covariance_within (CovarianceList me); |
| 129 | |
| 130 | #endif // _Covariance_h_ |