static double uiqi(float *x, float *y, int n) { double mx, my, sx, sy, sxy = 0; meanvar(&mx, &sx, x, n); meanvar(&my, &sy, y, n); for (int i = 0; i < n; i++) sxy += (x[i] - mx)*(y[i] - my); sxy /= n - 1; double q = (4 * sxy * mx * my); q /= (sx + sy) * (mx*mx + my*my); return q; }
static double ssim(float *x, float *y, int n) { double mx, my, sx, sy, sxy = 0; meanvar(&mx, &sx, x, n); meanvar(&my, &sy, y, n); for (int i = 0; i < n; i++) sxy += (x[i] - mx)*(y[i] - my); sxy /= n - 1; double Lx = dynamic_range(x, n); double Ly = dynamic_range(x, n); double L = (Lx + Ly)/2; double C1 = SSIM_K1 * L; double C2 = SSIM_K2 * L; C1 *= C1; C2 *= C2; double r = (2*mx*my + C1) * (2*sxy + C2); r /= (mx*mx + my*my + C1) * (sx + sx + C2); return r; }
void ttest(float X[], unsigned int nx, float Y[], unsigned int ny, \ float *t, float *p) { // Simple t-test, assuming equal variance // Sokal & Rohlf, Box 9.6 // For unequal variance, need to code up Box 13.3 double sumv; float mnx, mny, varx, vary; unsigned long int df; meanvar(X, nx, &mnx, &varx); meanvar(Y, ny, &mny, &vary); df = nx + ny - 2; sumv = ( ( (nx-1) * varx ) + ( ( ny-1) * vary) ) / df; *t = (mnx-mny) / sqrt( sumv * ( (float) (nx+ny) / (float) (nx*ny) ) ); *p = ttable(*t, df, 2); }
//ComTraitMetric - calculate metrics of trait variation in each sample void ComTraitMetric(sample S, traits T, int SwapMethod, int XVAR) { //initialize variables int samp,trait, rec, run; int *STattach; int **rankLowSTMetricRnd; int **rankHighSTMetricRnd; float **STMeanObs; float ***STMeanRnd; float **MeanSTMetricRnd; float **STMetricObs; float ***STMetricRnd; float **StDevSTMetricRnd; float **SESSTMetricRnd; float *sampleTraits; float *randomMetrics; float mean, var, metric; rankLowSTMetricRnd = imatrix(0,S.nsamples-1,0,T.ntraits-1); rankHighSTMetricRnd = imatrix(0,S.nsamples-1,0,T.ntraits-1); STMeanObs = matrix(0,S.nsamples-1,0,T.ntraits-1); STMeanRnd = f3tensor(0,S.nsamples-1,0,T.ntraits-1,0,RUNS); MeanSTMetricRnd = matrix(0,S.nsamples-1,0,T.ntraits-1); STMetricObs = matrix(0,S.nsamples-1,0,T.ntraits-1); STMetricRnd = f3tensor(0,S.nsamples-1,0,T.ntraits-1,0,RUNS); StDevSTMetricRnd = matrix(0,S.nsamples-1,0,T.ntraits-1); SESSTMetricRnd = matrix(0,S.nsamples-1,0,T.ntraits-1); // attach sample to traits STattach = ivector(0, T.ntaxa-1); AttachSampleToTraits(S,T,STattach); //loop through samples and traits //calculate trait metric of each sample //store in an array for (trait = 0; trait < T.ntraits; trait++) { for (samp = 0; samp < S.nsamples; samp++) { //(re)dimension a vector to fill with trait values for this sample sampleTraits = vector(0,S.srec[samp]-1); //fill the vector for (rec = 0;rec < S.srec[samp];rec++) { sampleTraits[rec] = T.tr[STattach[ S.id[samp][rec] ]][trait]; } //calculate the metric of the trait vector for this sample/trait combo meanvar(sampleTraits, S.srec[samp], &mean, &metric); STMeanObs[samp][trait] = mean; traitMetric(sampleTraits, S.srec[samp], &metric, XVAR); STMetricObs[samp][trait] = metric; free_vector(sampleTraits,0,S.srec[samp]-1); } } // if BURNIN > 0 and independent or trial swap null, randomize and discard (burn in) if ((BURNIN > 0) && (SwapMethod==3 || SwapMethod==4)) { //burn in using appropriate algorithm if (SwapMethod == 3) { IndependentSwap(S, BURNIN); } else { TrialSwap(S, BURNIN); } } //now repeat calculations for randomized matrices for (run = 0;run<RUNS;run++) { //Swap the sample or trait labels switch (SwapMethod) { case 0: //SwapMethod 0 = shuffle traits across species //(sample remains unshuffled) TraitsAttachShuffle(S,T,STattach); break; case 1: //SwapMethod 1 = samples become random draws from sample taxa list //(maintains sample species richnesses but not species frequencies) RandomizeSampleTaxaShuffle(S); break; case 2: //SwapMethod 2 = samples become random draws from traits //note this is redundant for traits but included for completeness RandomizeSampleTaxaShuffle(S); TraitsAttachShuffle(S,T,STattach); break; case 3: //SwapMethod 3 = independent checkerboard swap of sample matrix //(maintains species frequencies and sample species richnesses) IndependentSwap(S,SWAPS); break; case 4: //SwapMethod 4 = trial swap of sample matrix //(maintains species frequencies and sample species richnesses) TrialSwap(S, SWAPS); break; default: printf("Please use -m command line switch to specify a randomization method.\n"); printf("See documentation for a list of possible null models.\n"); exit(EXIT_FAILURE); break; } for (trait = 0; trait < T.ntraits; trait++) { for (samp = 0; samp < S.nsamples; samp++) { //(re)dimension a vector to fill with trait values for this sample sampleTraits = vector(0,S.srec[samp]-1); //fill the vector for (rec = 0;rec < S.srec[samp];rec++) { sampleTraits[rec] = T.tr[STattach[ S.id[samp][rec] ]][trait]; } //calculate the metric of the trait vector for this sample/trait combo meanvar(sampleTraits, S.srec[samp], &mean, &metric); STMeanRnd[samp][trait][run] = mean; traitMetric(sampleTraits, S.srec[samp], &metric, XVAR); STMetricRnd[samp][trait][run] = metric; free_vector(sampleTraits,0,S.srec[samp]-1); } } } //calculate summary statistics for randomized data for (trait = 0;trait < T.ntraits;trait++) { for (samp = 0;samp < S.nsamples;samp++) { randomMetrics = vector(0,RUNS-1); for (run = 0;run < RUNS;run++) { randomMetrics[run] = STMetricRnd[samp][trait][run]; if (randomMetrics[run] > STMetricObs[samp][trait]) rankHighSTMetricRnd[samp][trait]++; if (randomMetrics[run] < STMetricObs[samp][trait]) rankLowSTMetricRnd[samp][trait]++; } meanvar(randomMetrics,RUNS,&mean,&var); MeanSTMetricRnd[samp][trait] = mean; StDevSTMetricRnd[samp][trait] = sqrt(var); SESSTMetricRnd[samp][trait] = (STMetricObs[samp][trait] - MeanSTMetricRnd[samp][trait]) \ / StDevSTMetricRnd[samp][trait]; free_vector(randomMetrics,0,RUNS-1); } } //summarize and output //print header printf("Phylocom output: randomization method %d, %d runs, trait metric %d (",SwapMethod,RUNS,XVAR); switch (XVAR) { case 1: printf("variance)\n"); break; case 2: printf("MPD)\n"); break; case 3: printf("MNTD)\n"); break; case 4: printf("range)\n"); break; default: printf(")\n"); break; } //print results with separate entry for each trait/plot printf("Trait\tSample\tNTaxa\tMean\tMetric\tMeanRndMetric\tSDRndMetric\tSESMetric\trankLow\trankHigh\truns\n"); for (trait = 0;trait < T.ntraits;trait++) { for (samp = 0;samp < S.nsamples;samp++) { printf("%s\t%s\t%d\t%f\t%f\t%f\t%f\t%f\t%d\t%d\t%d\n",T.trname[trait],S.pname[samp],S.srec[samp],STMeanObs[samp][trait],STMetricObs[samp][trait],MeanSTMetricRnd[samp][trait],StDevSTMetricRnd[samp][trait],SESSTMetricRnd[samp][trait],rankLowSTMetricRnd[samp][trait],rankHighSTMetricRnd[samp][trait],RUNS); } } }