| 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_elliptical.h" |
| 19 |
#include "dw_rand.h" |
| 20 |
#include "dw_matrix_rand.h" |
| 21 |
#include "dw_error.h" |
| 22 |
#include "dw_ascii.h" |
| 23 |
#include "dw_std.h" |
| 24 |
|
| 25 |
#include <math.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#define PI 3.14159265358979324 |
| 30 |
static PRECISION logdensity_draw(PRECISION *draw, TElliptical *elliptical); |
| 31 |
|
| 32 |
void FreeElliptical(TElliptical *elliptical) |
| 33 |
{ |
| 34 |
if (elliptical) |
| 35 |
{ |
| 36 |
FreeVector(elliptical->center); |
| 37 |
FreeMatrix(elliptical->scale); |
| 38 |
FreeMatrix(elliptical->quadratic_form); |
| 39 |
FreeVector(elliptical->draw); |
| 40 |
|
| 41 |
if (elliptical->data && elliptical->free) elliptical->free(elliptical->data); |
| 42 |
|
| 43 |
dw_free(elliptical); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
static TElliptical* CreateElliptical(int dim, TVector center, TMatrix scale) |
| 48 |
{ |
| 49 |
TElliptical* elliptical; |
| 50 |
|
| 51 |
if ((dim <= 0) || (center && DimV(center) != dim) || (scale && ((RowM(scale) != dim) || (ColM(scale) != dim)))) |
| 52 |
{ |
| 53 |
dw_Error(SIZE_ERR); |
| 54 |
return (TElliptical*)NULL; |
| 55 |
} |
| 56 |
|
| 57 |
elliptical=(TElliptical*)dw_malloc(sizeof(TElliptical)); |
| 58 |
|
| 59 |
elliptical->dim=dim; |
| 60 |
elliptical->center=center ? EquateVector((TVector)NULL,center) : InitializeVector(CreateVector(dim),0.0); |
| 61 |
elliptical->scale=scale ? EquateMatrix((TMatrix)NULL,scale) : IdentityMatrix((TMatrix)NULL,dim); |
| 62 |
elliptical->draw=CreateVector(dim); |
| 63 |
|
| 64 |
elliptical->quadratic_form=ProductTransposeMM((TMatrix)NULL,scale,scale); |
| 65 |
Inverse_LU(elliptical->quadratic_form,elliptical->quadratic_form); |
| 66 |
ForceSymmetric(elliptical->quadratic_form); |
| 67 |
|
| 68 |
elliptical->logabsdet=-LogAbsDeterminant_LU(elliptical->scale); |
| 69 |
|
| 70 |
elliptical->data=(void*)NULL; |
| 71 |
elliptical->type=(char*)NULL; |
| 72 |
elliptical->logdensity_radius=NULL; |
| 73 |
elliptical->logdensity_draw=NULL; |
| 74 |
elliptical->cummulative_radius=NULL; |
| 75 |
elliptical->draw_vector=NULL; |
| 76 |
elliptical->print_info=NULL; |
| 77 |
elliptical->free=NULL; |
| 78 |
|
| 79 |
return elliptical; |
| 80 |
} |
| 81 |
|
| 82 |
PRECISION RadiusElliptical(PRECISION *draw, TElliptical *elliptical) |
| 83 |
{ |
| 84 |
memcpy(pElementV(elliptical->draw),draw,elliptical->dim*sizeof(PRECISION)); |
| 85 |
SubtractVV(elliptical->draw,elliptical->draw,elliptical->center); |
| 86 |
return sqrt(InnerProduct(elliptical->draw,elliptical->draw,elliptical->quadratic_form)); |
| 87 |
} |
| 88 |
|
| 89 |
void PrintEllipticalInfo_full(FILE *f_out, TElliptical *elliptical) |
| 90 |
{ |
| 91 |
PrintEllipticalInfo(f_out,elliptical); |
| 92 |
fprintf(f_out,"Variance scale: %lg\n",elliptical->variance_scale); |
| 93 |
fprintf(f_out,"Center:\n"); |
| 94 |
dw_PrintVector(f_out,elliptical->center,"%lg "); |
| 95 |
fprintf(f_out,"Scale:\n"); |
| 96 |
dw_PrintMatrix(f_out,elliptical->scale,"%lg "); |
| 97 |
} |
| 98 |
|
| 99 |
static PRECISION logdensity_draw(PRECISION *draw, TElliptical *elliptical) |
| 100 |
{ |
| 101 |
return elliptical->logdensity_radius(RadiusElliptical(draw,elliptical),elliptical); |
| 102 |
} |
| 103 |
|
| 104 |
/*******************************************************************************/ |
| 105 |
/***************************** Truncated Gaussian ******************************/ |
| 106 |
/*******************************************************************************/ |
| 107 |
static void FreeElliptical_gaussian(TElliptical_gaussian *data) |
| 108 |
{ |
| 109 |
if (data) |
| 110 |
{ |
| 111 |
FreeMatrix(data->variance); |
| 112 |
dw_free(data); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
PRECISION VarianceScale_TruncatedGaussian(int dim, PRECISION lower_bound, PRECISION upper_bound) |
| 117 |
{ |
| 118 |
PRECISION a, b, c, d; |
| 119 |
if (upper_bound >= PLUS_INFINITY) |
| 120 |
a=c=1.0; |
| 121 |
else |
| 122 |
{ |
| 123 |
a=dw_chi_square_cdf(upper_bound*upper_bound,dim); |
| 124 |
c=dw_chi_square_cdf(upper_bound*upper_bound,dim+2); |
| 125 |
} |
| 126 |
if (lower_bound <= 0.0) |
| 127 |
b=d=0.0; |
| 128 |
else |
| 129 |
{ |
| 130 |
b=dw_chi_square_cdf(lower_bound*lower_bound,dim); |
| 131 |
d=dw_chi_square_cdf(lower_bound*lower_bound,dim+2); |
| 132 |
} |
| 133 |
return (c-d)/(a-b); |
| 134 |
} |
| 135 |
|
| 136 |
static PRECISION logdensity_radius_truncated_gaussian(PRECISION radius, TElliptical *elliptical) |
| 137 |
{ |
| 138 |
return -0.5*radius*radius + ((TElliptical_gaussian*)(elliptical->data))->logconstant; |
| 139 |
} |
| 140 |
|
| 141 |
static PRECISION cummulative_radius_truncated_gaussian(PRECISION radius, TElliptical *elliptical) |
| 142 |
{ |
| 143 |
TElliptical_gaussian *d=(TElliptical_gaussian*)elliptical->data; |
| 144 |
return (radius <= d->lower_bound) ? 0.0 : (radius < d->upper_bound) ? |
| 145 |
(dw_chi_square_cdf(radius*radius,elliptical->dim) - d->cumulative_lower_bound)/d->probability : 1.0; |
| 146 |
} |
| 147 |
|
| 148 |
static PRECISION draw_truncated_gaussian(PRECISION *draw, TElliptical *elliptical) |
| 149 |
{ |
| 150 |
PRECISION r, s; |
| 151 |
TElliptical_gaussian *d=(TElliptical_gaussian*)elliptical->data; |
| 152 |
do |
| 153 |
dw_NormalVector(elliptical->draw); |
| 154 |
while ((s=Norm(elliptical->draw)) == 0.0); |
| 155 |
r=sqrt(dw_chi_square_invcdf(d->cumulative_lower_bound + dw_uniform_rnd()*d->probability,elliptical->dim)); |
| 156 |
ProductVS(elliptical->draw,elliptical->draw,r/s); |
| 157 |
ProductMV(elliptical->draw,elliptical->scale,elliptical->draw); |
| 158 |
AddVV(elliptical->draw,elliptical->draw,elliptical->center); |
| 159 |
if (draw && (draw != pElementV(elliptical->draw))) |
| 160 |
memcpy(draw,pElementV(elliptical->draw),elliptical->dim*sizeof(PRECISION)); |
| 161 |
return r; |
| 162 |
} |
| 163 |
|
| 164 |
static void print_info_truncated_gaussian(FILE *f_out, TElliptical *elliptical) |
| 165 |
{ |
| 166 |
TElliptical_gaussian *d=(TElliptical_gaussian*)elliptical->data; |
| 167 |
if (f_out) fprintf(f_out,"%s\n dim=%d\n lower bound=%lg\n upper bound=%lg\n", |
| 168 |
elliptical->type,elliptical->dim,d->lower_bound,d->upper_bound); |
| 169 |
} |
| 170 |
|
| 171 |
/* |
| 172 |
Let z be a normal random vector with given center and variance. Truncate z so |
| 173 |
that it is on the elliptical annulus given by the set of all z such that |
| 174 |
|
| 175 |
r = sqrt((z - center)' * Inverse(variance) * (z - center)) |
| 176 |
|
| 177 |
is between lower_bound and upper_bound. Choose scale so that variance=scale*scale'. |
| 178 |
The density is given by |
| 179 |
|
| 180 |
Gamma(dim/2) f(r) |
| 181 |
--------------------------- ----------- |
| 182 |
2 pi^(dim/2) |det(scale)| r^(dim-1) |
| 183 |
|
| 184 |
where the density of r is given by |
| 185 |
|
| 186 |
|
| 187 |
r^(dim-1)*exp(-(r)^2/2) |
| 188 |
f(r) = ------------------------------------------------------------------------ |
| 189 |
2^(dim/2-1)*Gamma(dim/2)*(c(upper_bound^2,dim) - c(lower_bound^2,dim)) |
| 190 |
|
| 191 |
where c(x,n) is the cummulative chi-square density function with n degrees of |
| 192 |
freedom. The random variable r^2 is has a truncated chi-square distribution. |
| 193 |
|
| 194 |
Drawing z |
| 195 |
The vector z can be obtained by drawing y from the standard dim-dimensional |
| 196 |
Gaussian distribtuion, r from the distribution on [lower_bound,upper_bound] |
| 197 |
with density f(r), and defining z = r*scale*(y/norm(y)) + center. |
| 198 |
|
| 199 |
Draws of r can be obtained (perhaps not most efficiently) by drawing u from |
| 200 |
the uniform on [0,1] and defining r by |
| 201 |
|
| 202 |
r = sqrt(c_inverse(c(lower_bound^2,dim) + u*(c(upper_bound^2,dim) - c(lower_bound^2,n)),dim)) |
| 203 |
|
| 204 |
The variance_scale = sigma^2 * E[r^2] is |
| 205 |
|
| 206 |
(c(upper_bound^2,dim+2) - c(lower_bound^2,dim+2)) |
| 207 |
--------------------------------------------------- |
| 208 |
c(upper_bound^2,dim) - c(lower_bound^2,dim) |
| 209 |
|
| 210 |
|
| 211 |
Notes: c(r,n) = dw_chi_square_cdf(r,n) and c_inverse(u,n) = dw_chi_square_invcdf(u,n). |
| 212 |
The GSL functions gsl_cdf_chisq_P() and gls_cdf_chisq_Pinv() are used to compute |
| 213 |
these functions. |
| 214 |
*/ |
| 215 |
TElliptical* CreateElliptical_TruncatedGaussian(int dim, TVector mean, TMatrix variance, PRECISION lower_bound, PRECISION upper_bound) |
| 216 |
{ |
| 217 |
TElliptical* elliptical; |
| 218 |
TElliptical_gaussian *data; |
| 219 |
TMatrix scale; |
| 220 |
|
| 221 |
if (variance) |
| 222 |
{ |
| 223 |
scale=CholeskyLT((TMatrix)NULL,variance); |
| 224 |
Transpose(scale,scale); |
| 225 |
elliptical=CreateElliptical(dim,mean,scale); |
| 226 |
FreeMatrix(scale); |
| 227 |
} |
| 228 |
else |
| 229 |
elliptical=CreateElliptical(dim,mean,(TMatrix)NULL); |
| 230 |
|
| 231 |
elliptical->variance_scale=VarianceScale_TruncatedGaussian(dim,lower_bound,upper_bound); |
| 232 |
elliptical->type="truncated gaussian"; |
| 233 |
|
| 234 |
data=(TElliptical_gaussian*)dw_malloc(sizeof(TElliptical_gaussian)); |
| 235 |
data->lower_bound=lower_bound; |
| 236 |
data->upper_bound=upper_bound; |
| 237 |
data->cumulative_lower_bound=(lower_bound > 0.0) ? dw_chi_square_cdf(lower_bound*lower_bound,dim) : 0.0; |
| 238 |
data->cumulative_upper_bound=(upper_bound < PLUS_INFINITY) ? dw_chi_square_cdf(upper_bound*upper_bound,dim) : 1.0; |
| 239 |
data->probability=data->cumulative_upper_bound - data->cumulative_lower_bound; |
| 240 |
if (data->probability <= 0) |
| 241 |
{ |
| 242 |
dw_UserError("Truncated gaussian defined only on positive probability regions\n"); |
| 243 |
dw_free(data); |
| 244 |
return (TElliptical*)NULL; |
| 245 |
} |
| 246 |
data->logconstant=-0.5*dim*log(2.0*PI) + elliptical->logabsdet - log(data->probability); |
| 247 |
data->variance=variance ? EquateMatrix((TMatrix)NULL,variance) : IdentityMatrix((TMatrix)NULL,dim); |
| 248 |
|
| 249 |
elliptical->data=(void*)data; |
| 250 |
elliptical->logdensity_radius=logdensity_radius_truncated_gaussian; |
| 251 |
elliptical->logdensity_draw=logdensity_draw; |
| 252 |
elliptical->cummulative_radius=cummulative_radius_truncated_gaussian; |
| 253 |
elliptical->draw_vector=draw_truncated_gaussian; |
| 254 |
elliptical->print_info=print_info_truncated_gaussian; |
| 255 |
elliptical->free=(void (*)(void*))FreeElliptical_gaussian; |
| 256 |
|
| 257 |
return elliptical; |
| 258 |
} |
| 259 |
/*******************************************************************************/ |
| 260 |
/********************************** Gaussian ***********************************/ |
| 261 |
/*******************************************************************************/ |
| 262 |
static PRECISION draw_gaussian(PRECISION *draw, TElliptical *elliptical) |
| 263 |
{ |
| 264 |
PRECISION r; |
| 265 |
dw_NormalVector(elliptical->draw); |
| 266 |
r=sqrt(DotProduct(elliptical->draw,elliptical->draw)); |
| 267 |
ProductMV(elliptical->draw,elliptical->scale,elliptical->draw); |
| 268 |
AddVV(elliptical->draw,elliptical->draw,elliptical->center); |
| 269 |
if (draw && (draw != pElementV(elliptical->draw))) |
| 270 |
memcpy(draw,pElementV(elliptical->draw),elliptical->dim*sizeof(PRECISION)); |
| 271 |
return r; |
| 272 |
} |
| 273 |
|
| 274 |
static void print_info_gaussian(FILE *f_out, TElliptical *elliptical) |
| 275 |
{ |
| 276 |
if (f_out) fprintf(f_out,"%s\n dim=%d\n",elliptical->type,elliptical->dim); |
| 277 |
} |
| 278 |
|
| 279 |
/* |
| 280 |
Creates a Gaussian elliptical distribution with given mean and and variance. |
| 281 |
If mean is null, then it is taken to be the identity and if variance is null |
| 282 |
then it is taken to be the identity. |
| 283 |
*/ |
| 284 |
TElliptical* CreateElliptical_Gaussian(int dim, TVector mean, TMatrix variance) |
| 285 |
{ |
| 286 |
TElliptical* elliptical; |
| 287 |
if ((elliptical=CreateElliptical_TruncatedGaussian(dim,mean,variance,0.0,PLUS_INFINITY))) |
| 288 |
{ |
| 289 |
elliptical->type="gaussian"; |
| 290 |
elliptical->draw_vector=draw_gaussian; |
| 291 |
elliptical->print_info=print_info_gaussian; |
| 292 |
} |
| 293 |
return elliptical; |
| 294 |
} |
| 295 |
/*******************************************************************************/ |
| 296 |
/*******************************************************************************/ |
| 297 |
/*******************************************************************************/ |
| 298 |
|
| 299 |
/*******************************************************************************/ |
| 300 |
/******************************* Truncated Power *******************************/ |
| 301 |
/*******************************************************************************/ |
| 302 |
PRECISION VarianceScale_TruncatedPower(int dim, PRECISION lower_bound, PRECISION upper_bound, PRECISION power) |
| 303 |
{ |
| 304 |
return power*(pow(upper_bound,power+2.0) - pow(lower_bound,power+2.0)) |
| 305 |
/(dim*(power+2.0)*(pow(upper_bound,power) - pow(lower_bound,power))); |
| 306 |
} |
| 307 |
|
| 308 |
static PRECISION logdensity_radius_truncated_power(PRECISION radius, TElliptical *elliptical) |
| 309 |
{ |
| 310 |
TElliptical_power *d=(TElliptical_power*)elliptical->data; |
| 311 |
return ((radius <= d->lower_bound) || (radius > d->upper_bound)) ? MINUS_INFINITY : |
| 312 |
(d->power - elliptical->dim)*log(radius) + d->logconstant; |
| 313 |
} |
| 314 |
|
| 315 |
static PRECISION cummulative_radius_truncated_power(PRECISION radius, TElliptical *elliptical) |
| 316 |
{ |
| 317 |
TElliptical_power *d=(TElliptical_power*)elliptical->data; |
| 318 |
return (radius <= d->lower_bound) ? 0.0 : (radius < d->upper_bound) ? (pow(radius,d->power) - d->shift)/d->multiplier : 1.0; |
| 319 |
} |
| 320 |
|
| 321 |
static PRECISION draw_truncated_power(PRECISION *draw, TElliptical *elliptical) |
| 322 |
{ |
| 323 |
PRECISION r, s; |
| 324 |
TElliptical_power *d=(TElliptical_power*)elliptical->data; |
| 325 |
do |
| 326 |
dw_NormalVector(elliptical->draw); |
| 327 |
while ((s=Norm(elliptical->draw)) == 0.0); |
| 328 |
ProductSV(elliptical->draw,(r=pow(d->multiplier*dw_uniform_rnd() + d->shift,1.0/d->power))/s,elliptical->draw); |
| 329 |
ProductMV(elliptical->draw,elliptical->scale,elliptical->draw); |
| 330 |
AddVV(elliptical->draw,elliptical->draw,elliptical->center); |
| 331 |
if (draw && (draw != pElementV(elliptical->draw))) |
| 332 |
memcpy(draw,pElementV(elliptical->draw),elliptical->dim*sizeof(PRECISION)); |
| 333 |
return r; |
| 334 |
} |
| 335 |
|
| 336 |
static void print_info_truncated_power(FILE *f_out, TElliptical *elliptical) |
| 337 |
{ |
| 338 |
TElliptical_power *d=(TElliptical_power*)elliptical->data; |
| 339 |
if (f_out) fprintf(f_out,"%s\n dim=%d\n lower bound=%lg\n upper bound=%lg\n power=%lg\n", |
| 340 |
elliptical->type,elliptical->dim,d->lower_bound,d->upper_bound,d->power); |
| 341 |
} |
| 342 |
|
| 343 |
/* |
| 344 |
Creates a power distribution on the elliptical annulus which is given by the |
| 345 |
set of all z such that |
| 346 |
|
| 347 |
r = sqrt((z - center)' * Inverse(scale * scale') * (z - center)) |
| 348 |
|
| 349 |
is between lower_bound and upper_bound. The density is given by |
| 350 |
|
| 351 |
Gamma(dim/2) f(r) |
| 352 |
--------------------------- ----------- |
| 353 |
2 pi^(dim/2) |det(scale)| r^(dim-1) |
| 354 |
|
| 355 |
where f(r) = power * r^(power-1) / (upper_bound^power - lower_bound^power) for |
| 356 |
lower_bound <= r <= upper_bound and power > 0. |
| 357 |
|
| 358 |
Drawing z |
| 359 |
The vector z can be obtained by drawing y from the standard dim-dimensional |
| 360 |
Gaussian distribtuion, r from the distribution on [lower_bound,upper_bound] |
| 361 |
with density f(r), and defining z = r*scale*(y/norm(y)) + center. |
| 362 |
|
| 363 |
Since the cumulative density of r is |
| 364 |
|
| 365 |
(r^power - lower_bound^power) / (upper_bound^power - lower_bound^power) |
| 366 |
|
| 367 |
a draw of r can be obtained by drawing u from the uniform on [0,1] and |
| 368 |
defining by |
| 369 |
|
| 370 |
r = (multiplier*u + shift)^(1/power). |
| 371 |
|
| 372 |
where multiplier = (upper_bound^power - lower_bound^power) and |
| 373 |
shift = lower_bound^power |
| 374 |
|
| 375 |
The variance_scale = sigma^2 * E[r^2] is |
| 376 |
|
| 377 |
power*(upper_bound^(power+2) - lower_bound^(power+2)) |
| 378 |
------------------------------------------------------- |
| 379 |
dim*(power+2)*(upper_bound^power - lower_bound^power) |
| 380 |
*/ |
| 381 |
TElliptical* CreateElliptical_TruncatedPower(int dim, TVector center, TMatrix scale, PRECISION lower_bound, |
| 382 |
PRECISION upper_bound, PRECISION power) |
| 383 |
{ |
| 384 |
TElliptical* elliptical; |
| 385 |
TElliptical_power *data; |
| 386 |
|
| 387 |
if ((lower_bound < 0) || (upper_bound <= lower_bound) || (power <= 0)) |
| 388 |
{ |
| 389 |
dw_Error(ARG_ERR); |
| 390 |
return (TElliptical*)NULL; |
| 391 |
} |
| 392 |
|
| 393 |
if ((elliptical=CreateElliptical(dim,center,scale))) |
| 394 |
{ |
| 395 |
elliptical->variance_scale=VarianceScale_TruncatedPower(dim,lower_bound,upper_bound,power); |
| 396 |
elliptical->type="truncated power"; |
| 397 |
|
| 398 |
data=(TElliptical_power*)dw_malloc(sizeof(TElliptical_power)); |
| 399 |
|
| 400 |
data->lower_bound=lower_bound; |
| 401 |
data->upper_bound=upper_bound; |
| 402 |
data->power=power; |
| 403 |
data->shift=pow(lower_bound,power); |
| 404 |
data->multiplier=pow(upper_bound,power) - data->shift; |
| 405 |
data->logconstant=log(0.5*power) + dw_log_gamma(0.5*dim) - 0.5*dim*log(PI) - log(data->multiplier) + elliptical->logabsdet; |
| 406 |
|
| 407 |
elliptical->data=(void*)data; |
| 408 |
elliptical->logdensity_radius=logdensity_radius_truncated_power; |
| 409 |
elliptical->logdensity_draw=logdensity_draw; |
| 410 |
elliptical->cummulative_radius=cummulative_radius_truncated_power; |
| 411 |
elliptical->draw_vector=draw_truncated_power; |
| 412 |
elliptical->print_info=print_info_truncated_power; |
| 413 |
elliptical->free=(void (*)(void*))dw_free; |
| 414 |
} |
| 415 |
|
| 416 |
return elliptical; |
| 417 |
} |
| 418 |
|
| 419 |
/*******************************************************************************/ |
| 420 |
/************************************ Power ************************************/ |
| 421 |
/*******************************************************************************/ |
| 422 |
static void print_info_power(FILE *f_out, TElliptical *elliptical) |
| 423 |
{ |
| 424 |
TElliptical_power *d=(TElliptical_power*)elliptical->data; |
| 425 |
if (f_out) fprintf(f_out,"%s\n dim=%d\n upper bound=%lg\n power=%lg\n", |
| 426 |
elliptical->type,elliptical->dim,d->upper_bound,d->power); |
| 427 |
} |
| 428 |
|
| 429 |
/* |
| 430 |
The power elliptical is a special case of the truncated power distribution |
| 431 |
with lower_bound = 0. |
| 432 |
*/ |
| 433 |
TElliptical* CreateElliptical_Power(int dim, TVector center, TMatrix scale, PRECISION bound, PRECISION power) |
| 434 |
{ |
| 435 |
TElliptical *elliptical; |
| 436 |
if ((elliptical=CreateElliptical_TruncatedPower(dim,center,scale,0.0,bound,power))) |
| 437 |
{ |
| 438 |
elliptical->print_info=print_info_power; |
| 439 |
elliptical->type="power"; |
| 440 |
} |
| 441 |
return elliptical; |
| 442 |
} |
| 443 |
|
| 444 |
/*******************************************************************************/ |
| 445 |
/*********************************** Uniform ***********************************/ |
| 446 |
/*******************************************************************************/ |
| 447 |
static void print_info_uniform(FILE *f_out, TElliptical *elliptical) |
| 448 |
{ |
| 449 |
TElliptical_power *d=(TElliptical_power*)elliptical->data; |
| 450 |
if (f_out) fprintf(f_out,"%s\n dim=%d\n lower bound=%lg\n upper bound=%lg\n", |
| 451 |
elliptical->type,elliptical->dim,d->lower_bound,d->upper_bound); |
| 452 |
} |
| 453 |
|
| 454 |
static PRECISION logdensity_radius_uniform(PRECISION radius, TElliptical *elliptical) |
| 455 |
{ |
| 456 |
TElliptical_power *d=(TElliptical_power*)elliptical->data; |
| 457 |
return ((radius < d->lower_bound) || (radius > d->upper_bound)) ? MINUS_INFINITY : d->logconstant; |
| 458 |
} |
| 459 |
|
| 460 |
/* |
| 461 |
Creates a uniform distribution on the elliptical annulus which is given by the |
| 462 |
set of all z such that |
| 463 |
|
| 464 |
r = sqrt((z - center)' * Inverse(scale * scale') * (z - center)) |
| 465 |
|
| 466 |
is between lower_bound and upper_bound. This is a special case of the |
| 467 |
truncated power distribution with power = dim. |
| 468 |
*/ |
| 469 |
TElliptical* CreateElliptical_Uniform(int dim, TVector center, TMatrix scale, PRECISION lower_bound, PRECISION upper_bound) |
| 470 |
{ |
| 471 |
TElliptical* elliptical; |
| 472 |
if ((elliptical=CreateElliptical_TruncatedPower(dim,center,scale,lower_bound,upper_bound,dim))) |
| 473 |
{ |
| 474 |
elliptical->type="uniform"; |
| 475 |
elliptical->logdensity_radius=logdensity_radius_uniform; |
| 476 |
elliptical->print_info=print_info_uniform; |
| 477 |
} |
| 478 |
return elliptical; |
| 479 |
} |
| 480 |
/*******************************************************************************/ |
| 481 |
/*******************************************************************************/ |
| 482 |
/*******************************************************************************/ |
| 483 |
|
| 484 |
|
| 485 |
/*******************************************************************************/ |
| 486 |
/************************************ Step *************************************/ |
| 487 |
/*******************************************************************************/ |
| 488 |
PRECISION VarianceScale_Step(int dim, PRECISION *t, int m) |
| 489 |
{ |
| 490 |
int i; |
| 491 |
PRECISION var=0.0; |
| 492 |
for (i=0; i < m; i++) |
| 493 |
var+=(t[i+1]*t[i+1]*t[i+1] - t[i]*t[i]*t[i])/(t[i+1]-t[i]); |
| 494 |
return var/(3.0*(PRECISION)(m*dim)); |
| 495 |
} |
| 496 |
|
| 497 |
static void free_step(TElliptical_step *data) |
| 498 |
{ |
| 499 |
if (data) |
| 500 |
{ |
| 501 |
if (data->t) dw_free(data->t); |
| 502 |
dw_free(data); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
static PRECISION logdensity_radius_step(PRECISION radius, TElliptical *elliptical) |
| 507 |
{ |
| 508 |
TElliptical_step *d=(TElliptical_step*)elliptical->data; |
| 509 |
int min=0, max=d->m, mid; |
| 510 |
if ((radius < d->t[min]) || (radius > d->t[max])) |
| 511 |
return MINUS_INFINITY; |
| 512 |
else |
| 513 |
{ |
| 514 |
while (max - min > 1) |
| 515 |
if (radius > d->t[mid=(min + max)/2]) |
| 516 |
min=mid; |
| 517 |
else |
| 518 |
max=mid; |
| 519 |
return (1 - elliptical->dim)*log(radius) - log((PRECISION)d->m*(d->t[max] - d->t[min])) + d->logconstant; |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
static PRECISION cummulative_radius_step(PRECISION radius, TElliptical *elliptical) |
| 524 |
{ |
| 525 |
TElliptical_step *d=(TElliptical_step*)elliptical->data; |
| 526 |
int i; |
| 527 |
if (radius <= d->t[0]) return 0.0; |
| 528 |
for (i=1; i <= d->m; i++) |
| 529 |
if (radius <= d->t[i]) return ((PRECISION)(i-1) + (radius - d->t[i-1])/(d->t[i] - d->t[i-1]))/(PRECISION)d->m; |
| 530 |
return 1.0; |
| 531 |
} |
| 532 |
|
| 533 |
static PRECISION draw_step(PRECISION *draw, TElliptical *elliptical) |
| 534 |
{ |
| 535 |
PRECISION r, s; |
| 536 |
int i; |
| 537 |
TElliptical_step *d=( TElliptical_step*)elliptical->data; |
| 538 |
do |
| 539 |
dw_NormalVector(elliptical->draw); |
| 540 |
while ((s=Norm(elliptical->draw)) == 0.0); |
| 541 |
i=(int)floor(dw_uniform_rnd()*(PRECISION)(d->m)); |
| 542 |
r=(i < d->m) ? d->t[i] + dw_uniform_rnd()*(d->t[i+1] - d->t[i]) : d->t[d->m]; |
| 543 |
ProductSV(elliptical->draw,r/s,elliptical->draw); |
| 544 |
ProductMV(elliptical->draw,elliptical->scale,elliptical->draw); |
| 545 |
AddVV(elliptical->draw,elliptical->draw,elliptical->center); |
| 546 |
if (draw && (draw != pElementV(elliptical->draw))) |
| 547 |
memcpy(draw,pElementV(elliptical->draw),elliptical->dim*sizeof(PRECISION)); |
| 548 |
return r; |
| 549 |
} |
| 550 |
|
| 551 |
static void print_info_step(FILE *f_out, TElliptical *elliptical) |
| 552 |
{ |
| 553 |
TElliptical_step *d=(TElliptical_step*)(elliptical->data); |
| 554 |
int i; |
| 555 |
if (f_out) |
| 556 |
{ |
| 557 |
fprintf(f_out,"%s\n dim=%d\n lower bound=%lg\n upper bound=%lg\n table size=%d\n table=",elliptical->type,elliptical->dim,d->t[0],d->t[d->m],d->m); |
| 558 |
for (i=0; i <= d->m; i++) fprintf(f_out,"%lg ",d->t[i]); |
| 559 |
fprintf(f_out,"\n"); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
/* |
| 564 |
Creates a elliptical power distribution on the elliptical annulus which is |
| 565 |
given by the set of all z such that |
| 566 |
|
| 567 |
r = sqrt((z - center)' * Inverse(scale * scale') * (z - center)) |
| 568 |
|
| 569 |
is between table[0] and table[m]. The density is given by |
| 570 |
|
| 571 |
Gamma(dim/2) h(r) |
| 572 |
--------------------------- ----------- |
| 573 |
2 pi^(dim/2) |det(scale)| r^(dim-1) |
| 574 |
|
| 575 |
where h(r) is a step function. The value of h(r) is |
| 576 |
|
| 577 |
1/((table[i] - table[i-1])*m) |
| 578 |
|
| 579 |
if r is between table [i-1] and table[i]. |
| 580 |
|
| 581 |
Note: |
| 582 |
The vector x can be obtained by drawing y from the standard n-dimensional |
| 583 |
Gaussian distribtuion, r from the distribution on [table[0],table[m]] with |
| 584 |
density h(r), and defining x = r*scale*y/norm(y) + center. |
| 585 |
|
| 586 |
Because the probability of being between table[i-1] and table[i] is 1/m, and |
| 587 |
the density is constant over each of these intervals, r can be simulated by |
| 588 |
randomly choosing, with equal probability, an index i between 1 and m, |
| 589 |
drawing u from the uniform distribution on [0,1], and defining r by |
| 590 |
|
| 591 |
r = table[i-1] + u*(table[i] - table[i-1]) |
| 592 |
|
| 593 |
The elements in table must be non-decreasing. |
| 594 |
*/ |
| 595 |
TElliptical* CreateElliptical_Step(int dim, TVector center, TMatrix scale, PRECISION *table, int m) |
| 596 |
{ |
| 597 |
TElliptical* elliptical; |
| 598 |
TElliptical_step *data; |
| 599 |
|
| 600 |
if ((elliptical=CreateElliptical(dim,center,scale))) |
| 601 |
{ |
| 602 |
elliptical->variance_scale=VarianceScale_Step(dim,table,m); |
| 603 |
elliptical->type="step"; |
| 604 |
|
| 605 |
data=(TElliptical_step*)dw_malloc(sizeof(TElliptical_step)); |
| 606 |
data->t=(PRECISION*)dw_malloc((m+1)*sizeof(PRECISION)); |
| 607 |
memcpy(data->t,table,(m+1)*sizeof(PRECISION)); |
| 608 |
data->m=m; |
| 609 |
data->logconstant=log(0.5) + dw_log_gamma(0.5*dim) - 0.5*dim*log(PI) + elliptical->logabsdet; |
| 610 |
|
| 611 |
elliptical->data=(void*)data; |
| 612 |
elliptical->logdensity_radius=logdensity_radius_step; |
| 613 |
elliptical->cummulative_radius=cummulative_radius_step; |
| 614 |
elliptical->logdensity_draw=logdensity_draw; |
| 615 |
elliptical->draw_vector=draw_step; |
| 616 |
elliptical->print_info=print_info_step; |
| 617 |
elliptical->free=(void (*)(void*))free_step; |
| 618 |
} |
| 619 |
|
| 620 |
return elliptical; |
| 621 |
} |
| 622 |
/*******************************************************************************/ |
| 623 |
/*******************************************************************************/ |
| 624 |
/*******************************************************************************/ |
| 625 |
#undef PI |