-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtestNdlutil.cpp
169 lines (163 loc) · 4.73 KB
/
testNdlutil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "ndlutil.h"
#include "CMatrix.h"
#include <iostream>
int testFunctions();
int testFunction(string funcName);
int testGaussOverDiffCumGaussian();
int testLnDiffCumGaussian();
int testConsts();
int main()
{
cout << "eps is: " << ndlutil::EPS << endl;
int fail = 0;
fail+= testFunctions();
fail+= testConsts();
cout << "Total failures: " << fail << endl;
}
int testConsts()
{
int fail=0;
double diff=abs(ndlutil::LOGTWOPI-log(2*M_PI));
if(diff<ndlutil::EPS)
cout << "log(2*pi) passes." << endl;
else
{
cout << "FAILURE: log(2*pi)." << ": absolute difference " << diff << endl;
fail++;
}
diff=abs(ndlutil::HALFLOGTWOPI-.5*log(2*M_PI));
if(diff<ndlutil::EPS)
cout << ".5*log(2*pi) passes." << endl;
else
{
cout << "FAILURE: .5*log(2*pi)." << ": absolute difference " << diff << endl;
fail++;
}
diff=abs(ndlutil::HALFSQRTTWO-.5*sqrt(2.0));
if(diff<ndlutil::EPS)
cout << ".5*sqrt(2) passes." << endl;
else
{
cout << "FAILURE: .5*sqrt(2)." << ": absolute difference " << diff << endl;
fail++;
}
diff=abs(ndlutil::SQRTTWOPI-sqrt(2*M_PI));
if(diff<ndlutil::EPS)
cout << "sqrt(2*pi) passes." << endl;
else
{
cout << "FAILURE: sqrt(2*pi)." << ": absolute difference " << diff << endl;
fail++;
}
return fail;
}
//y=ndlutil::lnCumGaussSum(double u1, double u2, double w1, double w2);
int testFunctions()
{
int fail = 0;
fail+=testFunction("ngaussian");
fail+=testFunction("cumGaussian");
fail+=testFunction("invCumGaussian");
fail+=testFunction("gradLnCumGaussian");
fail+=testFunction("lnCumGaussian");
fail+=testFunction("sigmoid");
fail+=testFunction("invSigmoid");
fail+=testFunction("erfcinv");
fail+=testFunction("gamma");
fail+=testFunction("gammaln");
fail+=testFunction("digamma");
fail+=testGaussOverDiffCumGaussian();
fail+=testLnDiffCumGaussian();
return fail;
}
int testLnDiffCumGaussian()
{
int fail = 0;
string fileName = "matfiles" + ndlstrutil::dirSep() + "lnDiffCumGaussianNdlutilTest.mat";
CMatrix uprime(1, 1);
uprime.readMatlabFile(fileName, "uprime");
CMatrix u(1, 1);
u.readMatlabFile(fileName, "u");
CMatrix y(1, 1);
y.readMatlabFile(fileName, "y");
for(int i=0; i<y.getNumElements(); i++)
{
double diff = y.getVal(i)-ndlutil::lnDiffCumGaussian(u.getVal(i), uprime.getVal(i));
if(abs(diff)>ndlutil::EPS)
{
cout << "FAILURE: lnDiffCumGaussian for u = " << u.getVal(i) << " and uprime = " << uprime.getVal(i) << ": absolute difference " << diff << endl;
fail++;
}
}
if(fail==0)
cout << "lnDiffCumGaussian test passed." << endl;
return fail;
}
int testGaussOverDiffCumGaussian()
{
int fail = 0;
string fileName = "matfiles" + ndlstrutil::dirSep() + "gaussOverDiffCumGaussianNdlutilTest.mat";
CMatrix uprime(1, 1);
uprime.readMatlabFile(fileName, "uprime");
CMatrix u(1, 1);
u.readMatlabFile(fileName, "u");
CMatrix order(1, 1);
order.readMatlabFile(fileName, "order");
CMatrix y(1, 1);
y.readMatlabFile(fileName, "y");
for(int i=0; i<y.getNumElements(); i++)
{
double diff = y.getVal(i)-ndlutil::gaussOverDiffCumGaussian(u.getVal(i), uprime.getVal(i), (int)order.getVal(i));
if(abs(diff)>ndlutil::EPS)
{
cout << "FAILURE: gaussOverDiffCumGaussian for u = " << u.getVal(i) << " and uprime = " << uprime.getVal(i) << " and order = " << order.getVal(i) << ": absolute difference " << diff << endl;
fail++;
}
}
if(fail==0)
cout << "gaussOverDiffCumGaussian test passed." << endl;
return fail;
}
int testFunction(string funcName)
{
int fail = 0;
string fileName = "matfiles" + ndlstrutil::dirSep() + funcName+"NdlutilTest.mat";
CMatrix xMat(1, 1);
xMat.readMatlabFile(fileName, "x");
CMatrix yMat(1, 1);
yMat.readMatlabFile(fileName, "y");
double x=xMat.getVal(0);
double trueY=yMat.getVal(0);
double y;
if(funcName=="ngaussian")
y=ndlutil::ngaussian(x);
else if(funcName=="cumGaussian")
y=ndlutil::cumGaussian(x);
else if(funcName=="gradLnCumGaussian")
y=ndlutil::gradLnCumGaussian(x);
else if(funcName=="invCumGaussian")
y=ndlutil::invCumGaussian(x);
else if(funcName=="lnCumGaussian")
y=ndlutil::lnCumGaussian(x);
else if(funcName=="sigmoid")
y=ndlutil::sigmoid(x);
else if(funcName=="invSigmoid")
y=ndlutil::invSigmoid(x);
else if(funcName=="erfcinv")
y=ndlutil::erfcinv(x);
else if(funcName=="gamma")
y=ndlutil::gamma(x);
else if(funcName=="gammaln")
y=ndlutil::gammaln(x);
else if(funcName=="digamma")
y=ndlutil::digamma(x);
double diff = abs(y-trueY);
if(diff<=ndlutil::EPS)
cout << funcName << " test passed." << endl;
else
{
cout << "FAILURE: " << funcName << ": absolute difference " << diff << endl;
fail++;
}
return fail;
}