00001 /* ***************************************************** */ 00002 /* Compute mean and variance of distances to clusters. */ 00003 /* mean_variance_dist_clusters.c */ 00004 /* ***************************************************** */ 00005 /* Author: Christian Page, CERFACS, Toulouse, France. */ 00006 /* ***************************************************** */ 00011 /* LICENSE BEGIN 00012 00013 Copyright Cerfacs (Christian Page) (2015) 00014 00015 christian.page@cerfacs.fr 00016 00017 This software is a computer program whose purpose is to downscale climate 00018 scenarios using a statistical methodology based on weather regimes. 00019 00020 This software is governed by the CeCILL license under French law and 00021 abiding by the rules of distribution of free software. You can use, 00022 modify and/ or redistribute the software under the terms of the CeCILL 00023 license as circulated by CEA, CNRS and INRIA at the following URL 00024 "http://www.cecill.info". 00025 00026 As a counterpart to the access to the source code and rights to copy, 00027 modify and redistribute granted by the license, users are provided only 00028 with a limited warranty and the software's author, the holder of the 00029 economic rights, and the successive licensors have only limited 00030 liability. 00031 00032 In this respect, the user's attention is drawn to the risks associated 00033 with loading, using, modifying and/or developing or reproducing the 00034 software by the user in light of its specific status of free software, 00035 that may mean that it is complicated to manipulate, and that also 00036 therefore means that it is reserved for developers and experienced 00037 professionals having in-depth computer knowledge. Users are therefore 00038 encouraged to load and test the software's suitability as regards their 00039 requirements in conditions enabling the security of their systems and/or 00040 data to be ensured and, more generally, to use and operate it in the 00041 same conditions as regards security. 00042 00043 The fact that you are presently reading this means that you have had 00044 knowledge of the CeCILL license and that you accept its terms. 00045 00046 LICENSE END */ 00047 00048 00049 00050 00051 00052 00053 00054 #include <classif.h> 00055 00057 void 00058 mean_variance_dist_clusters(double *mean_dist, double *var_dist, double *pc, double *clusters, double *var_pc, 00059 double *var_pc_norm_all, int neof, int nclust, int ntime) { 00072 double *dist_pc = NULL; /* Sum of the normalized distances to clusters over all EOFs */ 00073 00074 double sum = 0.0; /* Sum of normalized distances to clusters */ 00075 double val; /* Normalized distance to cluster */ 00076 00077 int eof; /* EOF loop counter */ 00078 int nt; /* Time loop counter */ 00079 int clust; /* Cluster loop counter */ 00080 00081 /* Allocate memory */ 00082 dist_pc = (double *) malloc(ntime * sizeof(double)); 00083 if (dist_pc == NULL) alloc_error(__FILE__, __LINE__); 00084 00085 /* Loop over all clusters */ 00086 for (clust=0; clust<nclust; clust++) { 00087 /* Loop over time */ 00088 for (nt=0; nt<ntime; nt++) { 00089 /* Initialize the sum */ 00090 sum = 0.0; 00091 /* Loop over all EOFs */ 00092 for (eof=0; eof<neof; eof++) { 00093 /* Calculate normalized distance to clusters */ 00094 val = (pc[nt+eof*ntime] / sqrt(var_pc_norm_all[eof])) - (clusters[eof+clust*neof] / sqrt(var_pc[eof])); 00095 /* Calculate squared sum */ 00096 sum += (val * val); 00097 } 00098 /* Save for this timestep the square root of the sum of the squares of the distance to clusters */ 00099 dist_pc[nt] = sqrt(sum); 00100 } 00101 /* Calculate mean over time */ 00102 mean_dist[clust] = gsl_stats_mean(dist_pc, 1, ntime); 00103 /* Calculate variance over time */ 00104 var_dist[clust] = gsl_stats_variance(dist_pc, 1, ntime); 00105 00106 // printf("ctrl dist pre... %d %lf %lf %lf %lf %lf\n",clust,sqrt(sum),pc[ntime-1+clust*ntime],sqrt(var_pc_norm_all[clust]),clusters[clust+clust*neof],sqrt(var_pc[clust])); 00107 // printf("ctrl dist.... %d %lf %lf %lf %lf\n",clust,dist_pc[ntime-1],sum,mean_dist[clust],sqrt(var_dist[clust])); 00108 } 00109 00110 /* Free memory */ 00111 (void) free(dist_pc); 00112 }