| 1 |
/* |
| 2 |
* Copyright (C) 1996-2011 Daniel Waggoner |
| 3 |
* |
| 4 |
* This free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* It is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* If you did not received a copy of the GNU General Public License |
| 15 |
* with this software, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
/*******************************************************************************/ |
| 19 |
/************************** Elliptical Test Routines ***************************/ |
| 20 |
/*******************************************************************************/ |
| 21 |
#include "dw_matrix_sort.h" |
| 22 |
#include "dw_elliptical.h" |
| 23 |
#include "dw_rand.h" |
| 24 |
#include "dw_matrix_rand.h" |
| 25 |
#include "dw_parse_cmd.h" |
| 26 |
#include "dw_std.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <math.h> |
| 30 |
|
| 31 |
static void TestSimulateEllipticalVariance(TElliptical *elliptical, int count, FILE *f_out); |
| 32 |
static void TestSimulateEllipticalCummulativeDensity(TElliptical *elliptical, int count, FILE *f_out); |
| 33 |
|
| 34 |
// Prints the true and estimated cummulative densities |
| 35 |
static void TestSimulateEllipticalCummulativeDensity(TElliptical *elliptical, int count, FILE *f_out) |
| 36 |
{ |
| 37 |
TVector R; |
| 38 |
int d=200, i, j; |
| 39 |
PRECISION min, max, inc, x; |
| 40 |
|
| 41 |
if (!f_out) |
| 42 |
{ |
| 43 |
printf("Null file pointer\n"); |
| 44 |
dw_exit(0); |
| 45 |
} |
| 46 |
|
| 47 |
PrintEllipticalInfo(f_out,elliptical); |
| 48 |
|
| 49 |
if (elliptical->cummulative_radius) |
| 50 |
{ |
| 51 |
R=CreateVector(count); |
| 52 |
for (i=0; i < count; i++) |
| 53 |
ElementV(R,i)=DrawElliptical((PRECISION*)NULL,elliptical); |
| 54 |
SortVectorAscending(R,R); |
| 55 |
|
| 56 |
min=ElementV(R,0); |
| 57 |
max=ElementV(R,count-1); |
| 58 |
inc=(max-min)/(PRECISION)(d-1); |
| 59 |
|
| 60 |
for (x=min, i=j=0; i < d; x+=inc, i++) |
| 61 |
{ |
| 62 |
for ( ; j < count; j++) |
| 63 |
if (ElementV(R,j) >= x) break; |
| 64 |
fprintf(f_out,"%lf,%lf,%lf\n",x,(double)j/(double)count,CummulativeDensityElliptical_Radius(x,elliptical)); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
fprintf(f_out,"\n\n"); |
| 69 |
} |
| 70 |
|
| 71 |
// Prints the difference, in terms of norms, between estimated and |
| 72 |
// actual variances and means. |
| 73 |
static void TestSimulateEllipticalVariance(TElliptical *elliptical, int count, FILE *f_out) |
| 74 |
{ |
| 75 |
TVector mean, v; |
| 76 |
TMatrix variance, x; |
| 77 |
int i; |
| 78 |
|
| 79 |
if (!f_out) |
| 80 |
{ |
| 81 |
printf("Null file pointer\n"); |
| 82 |
dw_exit(0); |
| 83 |
} |
| 84 |
|
| 85 |
PrintEllipticalInfo(f_out,elliptical); |
| 86 |
|
| 87 |
// Create matrices and vectors |
| 88 |
InitializeVector(mean=CreateVector(elliptical->dim),0.0); |
| 89 |
InitializeMatrix(variance=CreateMatrix(elliptical->dim,elliptical->dim),0.0); |
| 90 |
v=CreateVector(elliptical->dim); |
| 91 |
x=CreateMatrix(elliptical->dim,elliptical->dim); |
| 92 |
|
| 93 |
// Simulate mean and variance |
| 94 |
for (i=0; i < count; i++) |
| 95 |
{ |
| 96 |
DrawElliptical(pElementV(v),elliptical); |
| 97 |
AddVV(mean,mean,v); |
| 98 |
OuterProduct(x,v,v); |
| 99 |
AddMM(variance,variance,x); |
| 100 |
} |
| 101 |
ProductVS(mean,mean,1/(PRECISION)count); |
| 102 |
ProductMS(variance,variance,1/(PRECISION)count); |
| 103 |
OuterProduct(x,mean,mean); |
| 104 |
SubtractMM(variance,variance,x); |
| 105 |
|
| 106 |
// Difference in centers |
| 107 |
fprintf(f_out,"acutal mean\n"); |
| 108 |
dw_PrintVector(f_out,elliptical->center,"%lf "); |
| 109 |
fprintf(f_out,"difference between estimated and actual mean divided by standard deviation\n"); |
| 110 |
SubtractVV(v,elliptical->center,mean); |
| 111 |
for (i=0; i < elliptical->dim; i++) |
| 112 |
fprintf(f_out,"%8.5lf ",ElementV(v,i)/sqrt(ElementM(variance,i,i)/count)); |
| 113 |
fprintf(f_out,"\n"); |
| 114 |
|
| 115 |
// Difference in variances |
| 116 |
ProductMS(x,elliptical->scale,sqrt(elliptical->variance_scale)); |
| 117 |
ProductTransposeMM(x,x,x); |
| 118 |
SubtractMM(x,x,variance); |
| 119 |
fprintf(f_out,"norm of the difference between estimated and actual variance: %lf\n",MatrixNorm(x)); |
| 120 |
|
| 121 |
// Prints the variance matrices |
| 122 |
fprintf(f_out,"Actual Variance:\n"); |
| 123 |
ProductMS(x,elliptical->scale,sqrt(elliptical->variance_scale)); |
| 124 |
ProductTransposeMM(x,x,x); |
| 125 |
dw_PrintMatrix(f_out,x,"%lg "); |
| 126 |
fprintf(f_out,"difference between estimated and actual variance:\n"); |
| 127 |
SubtractMM(x,x,variance); |
| 128 |
dw_PrintMatrix(f_out,x,"%lg "); |
| 129 |
|
| 130 |
fprintf(f_out,"\n\n"); |
| 131 |
|
| 132 |
// Clean up |
| 133 |
FreeMatrix(variance); |
| 134 |
FreeVector(mean); |
| 135 |
FreeVector(v); |
| 136 |
FreeMatrix(x); |
| 137 |
} |
| 138 |
|
| 139 |
int main(int nargs, char **args) |
| 140 |
{ |
| 141 |
TMatrix base_scale, scale, variance; |
| 142 |
TVector center; |
| 143 |
PRECISION lower_bound, upper_bound, power, *table; |
| 144 |
int i, seed, dim, count, table_size, cummulative; |
| 145 |
TElliptical *elliptical; |
| 146 |
FILE *f_out; |
| 147 |
|
| 148 |
count=dw_ParseInteger_String(nargs,args,"ndraws",10000); |
| 149 |
dim=dw_ParseInteger_String(nargs,args,"dim",3); |
| 150 |
lower_bound=dw_ParseFloating_String(nargs,args,"lower_bound",2.0); |
| 151 |
if (lower_bound < 0.0) lower_bound=0.0; |
| 152 |
upper_bound=dw_ParseFloating_String(nargs,args,"upper_bound",8.0); |
| 153 |
if (lower_bound >= upper_bound) upper_bound=lower_bound+1.0; |
| 154 |
power=dw_ParseFloating_String(nargs,args,"power",5.0); |
| 155 |
table_size=dw_ParseInteger_String(nargs,args,"table_size",10); |
| 156 |
cummulative=(dw_FindArgument_String(nargs,args,"cummulative") == -1) ? 0 : 1; |
| 157 |
seed=dw_ParseInteger_String(nargs,args,"seed",0); |
| 158 |
|
| 159 |
f_out=cummulative ? fopen("cummulative.csv","wt") : stdout; |
| 160 |
|
| 161 |
dw_initialize_generator(seed); |
| 162 |
|
| 163 |
dw_NormalVector(center=CreateVector(dim)); |
| 164 |
dw_NormalMatrix(base_scale=CreateMatrix(dim,dim)); |
| 165 |
variance=CreateMatrix(dim,dim); |
| 166 |
scale=CreateMatrix(dim,dim); |
| 167 |
|
| 168 |
fprintf(f_out,"Mean and Variance test\n"); |
| 169 |
fprintf(f_out,"Count = %d\n",count); |
| 170 |
|
| 171 |
// Gaussian family |
| 172 |
variance=ProductTransposeMM((TMatrix)NULL,base_scale,base_scale); |
| 173 |
ProductMS(variance,variance,1.0/VarianceScale_TruncatedGaussian(dim,lower_bound,upper_bound)); |
| 174 |
elliptical=CreateElliptical_TruncatedGaussian(dim,center,variance,lower_bound,upper_bound); |
| 175 |
if (cummulative) |
| 176 |
TestSimulateEllipticalCummulativeDensity(elliptical,count,f_out); |
| 177 |
else |
| 178 |
TestSimulateEllipticalVariance(elliptical,count,f_out); |
| 179 |
FreeElliptical(elliptical); |
| 180 |
|
| 181 |
variance=ProductTransposeMM((TMatrix)NULL,base_scale,base_scale); |
| 182 |
elliptical=CreateElliptical_Gaussian(dim,center,variance); |
| 183 |
if (cummulative) |
| 184 |
TestSimulateEllipticalCummulativeDensity(elliptical,count,f_out); |
| 185 |
else |
| 186 |
TestSimulateEllipticalVariance(elliptical,count,f_out); |
| 187 |
FreeElliptical(elliptical); |
| 188 |
|
| 189 |
// Truncated Power family |
| 190 |
ProductMS(scale,base_scale,1.0/sqrt(VarianceScale_TruncatedPower(dim,lower_bound,upper_bound,power))); |
| 191 |
elliptical=CreateElliptical_TruncatedPower(dim,center,scale,lower_bound,upper_bound,power); |
| 192 |
if (cummulative) |
| 193 |
TestSimulateEllipticalCummulativeDensity(elliptical,count,f_out); |
| 194 |
else |
| 195 |
TestSimulateEllipticalVariance(elliptical,count,f_out); |
| 196 |
FreeElliptical(elliptical); |
| 197 |
|
| 198 |
ProductMS(scale,base_scale,1.0/sqrt(VarianceScale_TruncatedPower(dim,lower_bound,upper_bound,dim))); |
| 199 |
elliptical=CreateElliptical_Uniform(dim,center,scale,lower_bound,upper_bound); |
| 200 |
if (cummulative) |
| 201 |
TestSimulateEllipticalCummulativeDensity(elliptical,count,f_out); |
| 202 |
else |
| 203 |
TestSimulateEllipticalVariance(elliptical,count,f_out); |
| 204 |
FreeElliptical(elliptical); |
| 205 |
|
| 206 |
ProductMS(scale,base_scale,1.0/sqrt(VarianceScale_TruncatedPower(dim,0.0,upper_bound,power))); |
| 207 |
elliptical=CreateElliptical_Power(dim,center,scale,upper_bound,power); |
| 208 |
if (cummulative) |
| 209 |
TestSimulateEllipticalCummulativeDensity(elliptical,count,f_out); |
| 210 |
else |
| 211 |
TestSimulateEllipticalVariance(elliptical,count,f_out); |
| 212 |
FreeElliptical(elliptical); |
| 213 |
|
| 214 |
// Step |
| 215 |
table=(PRECISION*)dw_malloc((table_size+1)*sizeof(PRECISION)); |
| 216 |
table[0]=lower_bound; |
| 217 |
for (i=1; i < table_size; i++) |
| 218 |
table[i]=table[i-1]+dw_uniform_rnd()*(upper_bound-table[i-1]); |
| 219 |
table[table_size]=upper_bound; |
| 220 |
ProductMS(scale,base_scale,1.0/sqrt(VarianceScale_Step(dim,table,table_size))); |
| 221 |
elliptical=CreateElliptical_Step(dim,center,scale,table,table_size); |
| 222 |
dw_free(table); |
| 223 |
if (cummulative) |
| 224 |
TestSimulateEllipticalCummulativeDensity(elliptical,count,f_out); |
| 225 |
else |
| 226 |
TestSimulateEllipticalVariance(elliptical,count,f_out); |
| 227 |
FreeElliptical(elliptical); |
| 228 |
|
| 229 |
// Clean up |
| 230 |
FreeMatrix(variance); |
| 231 |
FreeVector(center); |
| 232 |
FreeMatrix(scale); |
| 233 |
FreeMatrix(base_scale); |
| 234 |
|
| 235 |
return 0; |
| 236 |
} |
| 237 |
/*******************************************************************************/ |
| 238 |
/*******************************************************************************/ |
| 239 |
/*******************************************************************************/ |