-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgaussqr.cpp
300 lines (251 loc) · 8.78 KB
/
gaussqr.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright (c) 2005, Andrew Fernandes ([email protected]);
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the North Carolina State University nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "gaussqr.h"
#include <cmath>
#include <algorithm>
const integer_t gaussqr_version_major = 1;
const integer_t gaussqr_version_minor = 1;
gaussqr_result relative_error( const integer_t n , const real_t *w0 , const real_t *w1 , real_t *err )
/*
Returns the maximum relative error difference between two vectors, w0 and w1.
*/
{
if ( n < 3 || w0 == 0 || w1 == 0 || err == 0 )
return(gaussqr_illegal_argument);
*err = 0.0;
for ( integer_t i = 0; i < n; i++ ) {
*err = std::max( *err , std::fabs( (w0[i]-w1[i])/(0.5*(w0[i]+w1[i])) ) );
}
return(gaussqr_success);
}
namespace {
// raise a real_t number to an integer_t power
inline real_t pow_ri( real_t x , integer_t n )
{
real_t y = 1.0;
if ( n < 0 ) {
x = 1.0/x;
n = -n;
}
while ( n > 0 ) {
if ( n & 1 ) y *= x;
if ( n >>= 1 ) x *= x;
}
return(y);
}
inline real_t squared( const real_t &x )
// square the argument
{
return( x * x );
}
}
gaussqr_result standard_distribution_rcoeffs( const distribution_type type , const integer_t n , real_t *a , real_t *b , const real_t *p )
/*
Given one of the enumerated distriution types, and the number of desired recursion coefficients n,
this routine returns the desired recursion coefficients a and b. The parameters of the distribution
are passed in the array p, whose length is implied by the distribution type. See the cases below
for the number and meaning of the parameters (they are quite standard).
WARNING: No error checking is done on the input parameters p for validity!
*/
{
if ( n < 3 || a == 0 || b == 0 )
return(gaussqr_illegal_argument);
using std::exp;
real_t lambda = 0.0;
switch(type) {
case distribution_normal : {
const real_t &mu = p[0];
const real_t sigma2 = squared(p[1]);
lambda = 1.0;
for ( integer_t i = 0; i < n; i++ ) {
a[i] = mu;
}
for ( integer_t i = 1; i < n; i++ ) {
b[i] = i * sigma2;
}
} break;
case distribution_gamma : {
const real_t &alpha = p[0];
const real_t &beta = p[1];
lambda = beta;
for ( integer_t i = 0; i < n; i++ ) {
a[i] = alpha + 2*i;
}
for ( integer_t i = 1; i < n; i++ ) {
b[i] = i*(alpha+i-1);
}
} break;
case distribution_log_normal : {
const real_t &m = p[0];
const real_t s2 = squared(p[1]);
lambda = exp(m);
const real_t zeta = exp(s2);
for ( integer_t i = 0; i < n; i++ ) {
a[i] = pow(zeta,0.5*(2*i-1))*(pow_ri(zeta,i)*(zeta+1.0)-1.0);
}
for ( integer_t i = 1; i < n; i++ ) {
b[i] = pow_ri(zeta,3*i-2)*(pow_ri(zeta,i)-1.0);
}
} break;
case distribution_students_t : {
const real_t &nu = p[0];
lambda = 1.0;
for ( integer_t i = 0; i < n; i++ ) {
a[i] = 0.0;
}
for ( integer_t i = 1; i < n; i++ ) {
b[i] = i*nu*(nu-i+1)/((nu-2*i)*(nu-2*i+2));
}
} break;
case distribution_inverse_gamma : {
const real_t &alpha = p[0];
const real_t &beta = p[1];
lambda = beta;
for ( integer_t i = 0; i < n; i++ ) {
a[i] = (alpha+1.0)/((alpha-2*i+1)*(alpha-2*i-1));
}
for ( integer_t i = 1; i < n; i++ ) {
b[i] = i*(alpha-i+1)/((alpha-2*i)*squared(alpha-2*i+1)*(alpha-2*i+2));
}
} break;
case distribution_beta : {
const real_t &alpha = p[0];
const real_t &beta = p[1];
const real_t gamma = alpha + beta;
lambda = 1.0;
for ( integer_t i = 0; i < n; i++ ) {
if ( i == 0 && alpha == 1.0 && beta == 1.0 ) {
a[i] = 0.5;
} else {
a[i] = (alpha*gamma+(2*i-2)*alpha+2*i*beta+i*(2*i-2))/((gamma+2*i)*(gamma+2*i-2));
}
}
for ( integer_t i = 1; i < n; i++ ) {
b[i] = (i*(gamma+i-2)*(alpha+i-1)*(beta+i-1))/((gamma+2*i-1)*squared(gamma+2*i-2)*(gamma+2*i-3));
}
} break;
case distribution_fishers_f : {
const real_t &nu1 = p[0];
const real_t &nu2 = p[1];
lambda = nu2/nu1;
for ( integer_t i = 0; i < n; i++ ) {
a[i] = (nu1*nu2+2*nu1+4*i*nu2-8*i*i)/((nu2-4*i-2)*(nu2-4*i+2));
}
for ( integer_t i = 1; i < n; i++ ) {
b[i] = (2*i*(nu1+2*i-2)*(nu2-2*i+2)*(nu1+nu2-2*i))/((nu2-4*i)*squared(nu2-4*i+2)*(nu2-4*i+4));
}
} break;
default :
return(gaussqr_illegal_argument);
}
const real_t lambda2 = squared(lambda);
for ( integer_t i = 0; i < n; i++ ) {
a[i] *= lambda;
b[i] *= lambda2;
}
b[0] = 1.0;
return(gaussqr_success);
}
namespace {
// Define a polymorphic gamma function, since C++ does not provide one.
// Note that the 'tgamma' variants are C99 standard and are probably
// called something else on Windows.
inline float gamma_function( const float &x ) { return tgammaf(x); }
inline double gamma_function( const double &x ) { return tgamma (x); }
inline long double gamma_function( const long double &x ) { return tgammal(x); }
// Unfortunately, no standard library defines the beta function; we do so here.
inline real_t beta_function( const real_t &alpha , const real_t &beta ) {
return( gamma_function(alpha)*gamma_function(beta)/gamma_function(alpha+beta) );
}
}
gaussqr_result standard_distribution_pdf( const distribution_type type , const real_t x , real_t *y , const real_t *p )
/*
Given one of the enumerated distriution types and an abscissa x,
this routine returns the ordinate y of the desired density function. The parameters of the distribution
are passed in the array p, whose length is implied by the distribution type. See the cases below
for the number and meaning of the parameters (they are quite standard).
WARNING: No error checking is done on the input parameters p or abscissa x for validity!
*/
{
if ( y == 0 )
return(gaussqr_illegal_argument);
using std::exp;
using std::pow;
using std::log;
using std::sqrt;
switch(type) {
case distribution_normal : {
const real_t &mu = p[0];
const real_t sigma2 = squared(p[1]);
const real_t z = x - mu;
*y = exp( -0.5 * squared(z) / sigma2 ) / sqrt( 2.0 * sigma2 * M_PI );
} break;
case distribution_gamma : {
const real_t &alpha = p[0];
const real_t &beta = p[1];
*y = pow(x,alpha-1.0) * exp(-x/beta) / ( gamma_function(alpha) * pow(beta,alpha) );
} break;
case distribution_log_normal : {
const real_t &m = p[0];
const real_t s2 = squared(p[1]);
const real_t z = log(x) - m;
*y = exp( -0.5 * squared(z) / s2 ) / ( x * sqrt( 2.0 * s2 * M_PI ) );
} break;
case distribution_students_t : {
const real_t &nu = p[0];
const real_t tmp = 0.5*( nu + 1.0 );
*y = gamma_function(tmp)/( sqrt(nu*M_PI) * gamma_function(0.5*nu) * pow(1.0+x*x/nu,tmp) );
} break;
case distribution_inverse_gamma : {
const real_t &alpha = p[0];
const real_t &beta = p[1];
*y = pow(x,-(alpha+1.0)) * pow(beta,alpha) * exp(-beta/x) / gamma_function(alpha);
} break;
case distribution_beta : {
const real_t &alpha = p[0];
const real_t &beta = p[1];
*y = pow(x,alpha-1.0) * pow(1.0-x,beta-1.0) / beta_function(alpha,beta);
} break;
case distribution_fishers_f : {
const real_t &nu1 = p[0];
const real_t &nu2 = p[1];
const real_t nu1by2 = 0.5 * nu1;
const real_t nu2by2 = 0.5 * nu2;
*y = pow(nu1,nu1by2) * pow(nu2,nu2by2) * pow(x,nu1by2-1.0) / ( beta_function(nu1by2,nu2by2) * pow(nu1*x+nu2,nu1by2+nu2by2) );
} break;
default :
return(gaussqr_illegal_argument);
}
return(gaussqr_success);
}