[go: up one dir, main page]

Menu

[7446bb]: / src / prior.cc  Maximize  Restore  History

Download this file

108 lines (92 with data), 2.4 kB

  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
/*
* See COPYING file distributed along with the psignifit package for
* the copyright and license terms
*/
#include "prior.h"
#include <iostream>
void GaussPrior::shrink ( double xmin, double xmax ) {
double s ( 0.5*(xmax-xmin) ), m ( 0.5*(xmin+xmax) );
if ( s<std() ) {
mu = m;
sg = s;
var = sg*sg;
twovar = 2*var;
rng = GaussRandom ( mu, sg );
normalization = 1./(sqrt(2*M_PI)*sg);
}
}
double GaussPrior::ppf ( double p, double start ) const {
if ( p<=0 || p>=1 )
throw BadArgumentError ( "Requested probability is outside the range" );
unsigned int i;
double x,d;
if (start==NULL)
x = mu;
else
x = start;
for ( i=0; i<20; i++ ) {
d = (cdf ( x ) - p)/pdf ( x );
x -= d;
if ( fabs(d) < 1e-7 )
break;
}
return x;
}
void BetaPrior::shrink ( double xmin, double xmax ) {
double s ( 0.5*(xmax-xmin) ), m ( 0.5*(xmin+xmax) );
if ( s<std() ) {
beta = m*(1-m)*(1-m)/(s*s) - 1 + m;
alpha = m*beta/(1-m);
normalization = betaf(alpha,beta);
rng = BetaRandom ( alpha, beta );
}
}
double BetaPrior::ppf ( double p, double start ) const {
if ( p<=0 || p>=1 )
throw BadArgumentError ( "Requested probability is outside the range" );
double x, d, sx;
unsigned int i;
if ( start == NULL )
x = log ( mean() / ( 1-mean() ) );
else if ( start<=0 || start>=1 ) {
throw BadArgumentError ( "Beta Distribution can not be evaluated outside the unit interval" );
} else
x = log ( start / (1-start) );
for ( i=0; i<20; i++ ) {
sx = 1./(1+exp(-x));
d = (cdf ( sx ) - p) / ( pdf ( sx ) * sx * (1-sx) );
x -= d;
if ( fabs ( d ) < 1e-7 )
break;
}
return 1./(1+exp(-x));
}
void GammaPrior::shrink ( double xmin, double xmax ) {
double xr ( xmin/xmax );
k = ((1+xr)/(1-xr));
k *= k;
theta = xmax / (k+sqrt(k));
normalization = pow(theta,k)*exp(gammaln(k));
rng = GammaRandom ( k, theta );
}
double GammaPrior::ppf ( double p, double start ) const {
if ( p<=0 || p>=1 )
throw BadArgumentError ( "Requested probability is outside the range" );
double x,d;
unsigned int i;
if ( start == NULL )
x = sqrt ( mean() );
else
x = sqrt ( start );
for ( i=0; i<20; i++ ) {
d = (cdf ( x*x ) - p)/(2*pdf( x*x )*x);
x -= d;
if ( fabs ( d ) < 1e-7 )
break;
}
return x*x;
}
void nGammaPrior::shrink ( double xmin, double xmax ) {
double ymin(-xmax), ymax(-xmin);
GammaPrior::shrink ( ymin, ymax );
}