| 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 |
#include "dw_rand.h" |
| 19 |
#include "dw_matrix_rand.h" |
| 20 |
#include "dw_error.h" |
| 21 |
#include "dw_std.h" |
| 22 |
|
| 23 |
#include <math.h> |
| 24 |
|
| 25 |
|
| 26 |
/******************************************************************************/ |
| 27 |
/************************ Random Matrices and Vectors *************************/ |
| 28 |
/******************************************************************************/ |
| 29 |
/* |
| 30 |
Assumes |
| 31 |
x : m-vector |
| 32 |
|
| 33 |
Results |
| 34 |
Fills x with deviates drawn from the uniform distribution on [0,1] |
| 35 |
*/ |
| 36 |
TVector dw_UniformVector(TVector x) |
| 37 |
{ |
| 38 |
int i; |
| 39 |
if (!x) { dw_Error(NULL_ERR); return (TVector)NULL; } |
| 40 |
for (i=DimV(x)-1; i >= 0; i--) ElementV(x,i)=dw_uniform_rnd(); |
| 41 |
return x; |
| 42 |
} |
| 43 |
|
| 44 |
/* |
| 45 |
Assumes |
| 46 |
X : m x n matrix |
| 47 |
|
| 48 |
Results |
| 49 |
Fills X with deviates drawn from the uniform distribution on [0,1] |
| 50 |
*/ |
| 51 |
TMatrix dw_UniformMatrix(TMatrix X) |
| 52 |
{ |
| 53 |
int i; |
| 54 |
PRECISION *pX; |
| 55 |
if (!X) { dw_Error(NULL_ERR); return (TMatrix)NULL; } |
| 56 |
for (pX=pElementM(X), i=RowM(X)*ColM(X)-1; i >= 0; i--) pX[i]=dw_uniform_rnd(); |
| 57 |
return X; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
Assumes |
| 62 |
x : m-vector |
| 63 |
|
| 64 |
Results |
| 65 |
Fills x with independent standard normal deviates |
| 66 |
*/ |
| 67 |
TVector dw_NormalVector(TVector x) |
| 68 |
{ |
| 69 |
int i; |
| 70 |
if (!x) { dw_Error(NULL_ERR); return (TVector)NULL; } |
| 71 |
for (i=DimV(x)-1; i >= 0; i--) ElementV(x,i)=dw_gaussian_rnd(); |
| 72 |
return x; |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
Assumes |
| 77 |
X : m x n matrix |
| 78 |
|
| 79 |
Results |
| 80 |
Fills X with independent standard normal deviates |
| 81 |
*/ |
| 82 |
TMatrix dw_NormalMatrix(TMatrix X) |
| 83 |
{ |
| 84 |
int i; |
| 85 |
PRECISION *pX; |
| 86 |
if (!X) { dw_Error(NULL_ERR); return (TMatrix)NULL; } |
| 87 |
for (pX=pElementM(X), i=RowM(X)*ColM(X)-1; i >= 0; i--) pX[i]=dw_gaussian_rnd(); |
| 88 |
return X; |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
Assumes: |
| 93 |
x - n-vector or null pointer |
| 94 |
R - n x m upper triangular matrix with non-zero elements along the diagonal |
| 95 |
a - m-vector with a(i) <= b(i) |
| 96 |
b - m-vector with a(i) <= b(i) |
| 97 |
|
| 98 |
Returns: |
| 99 |
Returns x upon success and null otherwise. If x is null, then it is created. |
| 100 |
|
| 101 |
Results: |
| 102 |
The vector x is filled with a truncated normal draw. The truncation is of |
| 103 |
the form: |
| 104 |
|
| 105 |
a' <= gamma'*R <= b' |
| 106 |
|
| 107 |
where gamma is standard normal. |
| 108 |
|
| 109 |
Notes: |
| 110 |
Since R is upper triangular, the restrictions are of the form |
| 111 |
|
| 112 |
a(i) <= {gamma(0)*R(i,0) + ... + gamma(i-1)*R(i,i-1)} |
| 113 |
+ gamma(i)*R(i,i) <= b(i) |
| 114 |
|
| 115 |
Suppose the restrictions are of the form |
| 116 |
|
| 117 |
a' <= epsilon'*S <= b' |
| 118 |
|
| 119 |
where epsilon is normal with mean mu and variance Sigma. Let |
| 120 |
|
| 121 |
Sigma=T*T' |
| 122 |
|
| 123 |
be the Cholesky decomposition of Sigma and |
| 124 |
|
| 125 |
T'*S=Q*R |
| 126 |
|
| 127 |
be the QR-decomposition of T'*S. Then the above restrictions can be written |
| 128 |
as |
| 129 |
|
| 130 |
a' - mu'*S <= gamma'*R <= b' - mu'*S |
| 131 |
|
| 132 |
where gamma'=(epsilon' - mu')*inv(T')*Q or epsilon'=gamma'*Q'*T' + mu' and |
| 133 |
gamma is standard normal. |
| 134 |
*/ |
| 135 |
TVector dw_TruncatedNormalVector(TVector x, TMatrix R, TVector a, TVector b) |
| 136 |
{ |
| 137 |
int i, j, n, m; |
| 138 |
PRECISION dot, d; |
| 139 |
// Checks |
| 140 |
if (!R || !a || !b) |
| 141 |
{ |
| 142 |
dw_Error(NULL_ERR); |
| 143 |
return (TVector)NULL; |
| 144 |
} |
| 145 |
n=RowM(R); |
| 146 |
m=ColM(R); |
| 147 |
if ((m > n) || (DimV(a) != m) || (DimV(b) != m)) |
| 148 |
{ |
| 149 |
dw_Error(SIZE_ERR); |
| 150 |
return (TVector)NULL; |
| 151 |
} |
| 152 |
if (!x) |
| 153 |
{ if (!(x=CreateVector(n))) return (TVector)NULL; } |
| 154 |
else |
| 155 |
if (DimV(x) != n) |
| 156 |
{ |
| 157 |
dw_Error(SIZE_ERR); |
| 158 |
return (TVector)NULL; |
| 159 |
} |
| 160 |
|
| 161 |
// draw truncated normals |
| 162 |
// Note: dw_truncated_gaussian_rnd() makes no assumption about the ordering of the arguments. |
| 163 |
d=ElementM(R,0,0); |
| 164 |
ElementV(x,0)=dw_truncated_gaussian_rnd(ElementV(a,0)/d,ElementV(b,0)/d); |
| 165 |
for (i=1; i < m; i++) |
| 166 |
{ |
| 167 |
for (dot=ElementV(x,0)*ElementM(R,0,i), j=i-1; j > 0; j--) dot+=ElementV(x,j)*ElementM(R,j,i); |
| 168 |
d=ElementM(R,i,i); |
| 169 |
ElementV(x,i)=dw_truncated_gaussian_rnd((ElementV(a,i)-dot)/d,(ElementV(b,i)-dot)/d); |
| 170 |
} |
| 171 |
|
| 172 |
// draw unrestricted normals |
| 173 |
for ( ; i < n; i++) |
| 174 |
ElementV(x,i)=dw_gaussian_rnd(); |
| 175 |
|
| 176 |
return x; |
| 177 |
} |
| 178 |
|
| 179 |
/* |
| 180 |
Assumes |
| 181 |
x : m-vector |
| 182 |
|
| 183 |
Results |
| 184 |
Fills x with independent log normal deviates. The mean and standard |
| 185 |
deviation of the underlying normal distribution are passed. |
| 186 |
*/ |
| 187 |
TVector dw_LogNormalVector(TVector x, PRECISION mean, PRECISION standard_deviation) |
| 188 |
{ |
| 189 |
int i; |
| 190 |
if (!x) { dw_Error(NULL_ERR); return (TVector)NULL; } |
| 191 |
for (i=DimV(x)-1; i >= 0; i--) ElementV(x,i)=dw_lognormal_rnd(mean,standard_deviation); |
| 192 |
return x; |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
Computes a matrix of gamma deviates. If x, a, and b represent X(i,j), |
| 197 |
A(i,j), and B(i,j), then density of x is |
| 198 |
|
| 199 |
x^(a-1) exp(-x/b) |
| 200 |
------------------ |
| 201 |
gamma(a) b^a |
| 202 |
*/ |
| 203 |
TMatrix dw_GammaMatrix(TMatrix X, TMatrix A, TMatrix B) |
| 204 |
{ |
| 205 |
int i; |
| 206 |
PRECISION *pX, *pA, *pB; |
| 207 |
if (!A || !B) |
| 208 |
{ |
| 209 |
dw_Error(NULL_ERR); |
| 210 |
return (TMatrix)NULL; |
| 211 |
} |
| 212 |
if ((RowM(A) != RowM(B)) || (ColM(A) != ColM(B))) |
| 213 |
{ |
| 214 |
dw_Error(SIZE_ERR); |
| 215 |
return (TMatrix)NULL; |
| 216 |
} |
| 217 |
if (!X) |
| 218 |
{ |
| 219 |
if (!(X=CreateMatrix(RowM(A),ColM(A)))) |
| 220 |
return (TMatrix)NULL; |
| 221 |
} |
| 222 |
else |
| 223 |
if ((RowM(X) != RowM(A)) || (ColM(X) != ColM(A))) |
| 224 |
{ |
| 225 |
dw_Error(SIZE_ERR); |
| 226 |
return (TMatrix)NULL; |
| 227 |
} |
| 228 |
for (pX=pElementM(X), pA=pElementM(A), pB=pElementM(B), i=RowM(X)*ColM(X)-1; i >= 0; i--) |
| 229 |
pX[i]=pB[i]*dw_gamma_rnd(pA[i]); |
| 230 |
return X; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/* |
| 235 |
Assumes |
| 236 |
X : m x m matrix |
| 237 |
S : m x m non-singular matrix |
| 238 |
|
| 239 |
Results |
| 240 |
X is drawn from the Wishart distribution with parameters sigma, nu, and m, |
| 241 |
where sigma=Inverse(S'S). The pdf of X is proportional to |
| 242 |
|
| 243 |
|det(X)|^(0.5*(nu - m - 1)) |
| 244 |
---------------------------- * exp(-0.5*tr(Inverse(sigma*X)) |
| 245 |
|det(sigma)|^(0.5*nu) |
| 246 |
|
| 247 |
= |det(X)|^(0.5*(nu - m - 1)) |det(S)|^(nu) exp(-0.5*tr(S'*X*S)) |
| 248 |
*/ |
| 249 |
TMatrix dw_Wishart(TMatrix X, TMatrix S, int nu) |
| 250 |
{ |
| 251 |
int m=RowM(S); |
| 252 |
TMatrix Z; |
| 253 |
|
| 254 |
if ((m != ColM(S)) || (m != RowM(X)) || (m != ColM(X))) dw_Error(SIZE_ERR); |
| 255 |
|
| 256 |
Z=dw_NormalMatrix(CreateMatrix(m,nu)); |
| 257 |
ProductMM(Z,S,Z); |
| 258 |
ProductTransposeMM(X,Z,Z); |
| 259 |
FreeMatrix(Z); |
| 260 |
return X; |
| 261 |
} |
| 262 |
|
| 263 |
/* |
| 264 |
Assumes |
| 265 |
x : n x n matrix |
| 266 |
T : n x n upper triangular matrix |
| 267 |
|
| 268 |
Results |
| 269 |
x is drawn from the multivariate student-t distribution with parameters. |
| 270 |
The pdf of x is given by |
| 271 |
*/ |
| 272 |
TVector dw_StudentT(TVector x, TMatrix T, int nu) |
| 273 |
{ |
| 274 |
PRECISION r=0.0, s; |
| 275 |
int i, n=DimV(x); |
| 276 |
if ((n != ColM(T)) || (n != RowM(T))) dw_Error(SIZE_ERR); |
| 277 |
dw_NormalVector(x); |
| 278 |
ProductMV(x,T,x); |
| 279 |
for (i=nu; i > 0; i--) |
| 280 |
{ |
| 281 |
s=dw_gaussian_rnd(); |
| 282 |
r+=s*s; |
| 283 |
} |
| 284 |
ProductSV(x,sqrt((PRECISION)nu/r),x); |
| 285 |
return x; |
| 286 |
} |
| 287 |
|
| 288 |
TMatrix dw_UniformOrthogonal(TMatrix Q) |
| 289 |
{ |
| 290 |
TMatrix X; |
| 291 |
int i, j, err; |
| 292 |
|
| 293 |
if (!Q) |
| 294 |
{ |
| 295 |
dw_Error(NULL_ERR); |
| 296 |
return (TMatrix)NULL; |
| 297 |
} |
| 298 |
if (RowM(Q) != ColM(Q)) |
| 299 |
{ |
| 300 |
dw_Error(SIZE_ERR); |
| 301 |
return (TMatrix)NULL; |
| 302 |
} |
| 303 |
|
| 304 |
/* Uncomment to use IMSL implementation */ |
| 305 |
//imsls_d_random_orthogonal_matrix(RowM(Q),IMSLS_RETURN_USER,pElementM(Q),0); |
| 306 |
/**/ |
| 307 |
|
| 308 |
|
| 309 |
/* Uncomment to use C code implementation */ |
| 310 |
X=dw_NormalMatrix(CreateMatrix(RowM(Q),ColM(Q))); |
| 311 |
if (!(err=QR(Q,X,X))) |
| 312 |
for (i=RowM(X)-1; i >= 0; i--) |
| 313 |
if (ElementM(X,i,i) < 0) |
| 314 |
for (j=RowM(Q)-1; j >= 0; j--) ElementM(Q,j,i)=-ElementM(Q,j,i); |
| 315 |
FreeMatrix(X); |
| 316 |
if (err) return (TMatrix)NULL; |
| 317 |
/**/ |
| 318 |
|
| 319 |
return Q; |
| 320 |
} |
| 321 |
|
| 322 |
/* |
| 323 |
Assumes: |
| 324 |
x : m-vector |
| 325 |
|
| 326 |
Results: |
| 327 |
The vector x is filled with a vector drawn from the uniform distribution on |
| 328 |
the m-1 dimensional unit sphere. |
| 329 |
|
| 330 |
Returns: |
| 331 |
The vector x. |
| 332 |
|
| 333 |
Notes: |
| 334 |
The vector is obtained by drawing a m-vector from the standard normal |
| 335 |
distribution and then normalizing its length to one. |
| 336 |
*/ |
| 337 |
TVector dw_UniformUnitSphere(TVector x) |
| 338 |
{ |
| 339 |
PRECISION r; |
| 340 |
if (!x) |
| 341 |
{ |
| 342 |
dw_Error(NULL_ERR); |
| 343 |
return (TVector)NULL; |
| 344 |
} |
| 345 |
do |
| 346 |
dw_NormalVector(x); |
| 347 |
while ((r=Norm(x)) == 0.0); |
| 348 |
return ProductSV(x,1.0/r,x); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
/* |
| 353 |
Assumes: |
| 354 |
x : m-vector |
| 355 |
|
| 356 |
Results: |
| 357 |
The vector x is filled with a vector drawn from the uniform distribution on |
| 358 |
the m dimensional solid unit sphere. |
| 359 |
|
| 360 |
Returns: |
| 361 |
Upon success, returns the norm of x, upon failure returns -1.0. |
| 362 |
|
| 363 |
Notes: |
| 364 |
The vector is drawn by drawing a m-vector from the standard normal |
| 365 |
distribution and a real number u from the uniform distribution on [0,1], and |
| 366 |
normalizing the vector so its length equal to u^(1/m). |
| 367 |
*/ |
| 368 |
TVector dw_UniformUnitBall(TVector x) |
| 369 |
{ |
| 370 |
PRECISION r, s; |
| 371 |
if (!x) |
| 372 |
{ |
| 373 |
dw_Error(NULL_ERR); |
| 374 |
return (TVector)NULL; |
| 375 |
} |
| 376 |
do |
| 377 |
dw_NormalVector(x); |
| 378 |
while ((s=Norm(x)) == 0.0); |
| 379 |
ProductSV(x,(r=pow(dw_uniform_rnd(),1.0/DimV(x)))/s,x); |
| 380 |
return x; |
| 381 |
} |
| 382 |
|
| 383 |
/******************************************************************************/ |
| 384 |
/******************************************************************************/ |
| 385 |
|