Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GSL.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "bias_config.h"
26 #include <Base/Common/W32Compat.hh>
27 #include <MathAlgo/GSL.hh>
28 #include <Base/Debug/Debug.hh>
29 #include <Base/Debug/Error.hh>
30 #include <Base/Debug/Exception.hh>
31 #include <Base/Common/CompareFloatingPoint.hh>
32 
33 #include <cmath>
34 
35 #ifdef BIAS_HAVE_GSL
36 
37 # include <gsl/gsl_cdf.h>
38 # include <gsl/gsl_randist.h>
39 # include <gsl/gsl_sf_gamma.h>
40 
41 #endif
42 
43 using namespace BIAS;
44 using namespace std;
45 
46 
47 double
48 BIAS::ChiSquareProbDensFun(double x, int deg_freedom)
49 {
50 #ifdef BIAS_HAVE_GSL
51  if (x<0.)
52  BEXCEPTION("ChiSquareProbDensFun: invalid argument, x must be > 0: "<<x);
53  if (deg_freedom<1)
54  BEXCEPTION("ChiSquareProbDensFun: invalid argument, deg_fred must be > 0 "
55  <<deg_freedom);
56  double res = gsl_ran_chisq_pdf(x, deg_freedom);
57  BIASASSERT(!isnan(res) && !isinf(res));
58  return res;
59 #else
60  BIASERR("recompile BIAS with gsl support enabled");
61  BIASABORT;
62  return 0.0;
63 #endif
64 }
65 
66 double
67 BIAS::ChiSquareCulmProbFun(double x, int deg_freedom)
68 {
69 #ifdef BIAS_HAVE_GSL
70  double res = gsl_cdf_chisq_P(x, deg_freedom);
71  BIASASSERT(!isnan(res) && !isinf(res));
72  return res;
73 #else
74  BIASERR("recompile BIAS with gsl support enabled");
75  BIASABORT;
76  return 0.0;
77 #endif
78 }
79 
80 double
81 BIAS::InvChiSquareCulmProbFun(double x, int deg_freedom)
82 {
83 #ifdef BIAS_HAVE_GSL
84  double res = gsl_cdf_chisq_Pinv(x, deg_freedom);
85  BIASASSERT(!isnan(res) && !isinf(res));
86  return res;
87 #else
88  BIASERR("recompile BIAS with gsl support enabled");
89  BIASABORT;
90  return 0.0;
91 #endif
92 }
93 
94 double
95 BIAS::Gamma(double x)
96 {
97 #ifdef BIAS_HAVE_GSL
98  double res = gsl_sf_gamma (x);
99  BIASASSERT(!isnan(res) && !isinf(res));
100  return res;
101 #else
102  BIASERR("recompile BIAS with gsl support enabled");
103  BIASABORT;
104  return 0.0;
105 #endif
106 }
107 
108 double
109 BIAS::InvGamma(double x)
110 {
111 #ifdef BIAS_HAVE_GSL
112  double res = gsl_sf_gammainv (x);
113  BIASASSERT(!isnan(res) && !isinf(res));
114  return res;
115 #else
116  BIASERR("recompile BIAS with gsl support enabled");
117  BIASABORT;
118  return 0.0;
119 #endif
120 }
121 
122 double
123 BIAS::BetaDistribution(double a, double b, double x)
124 {
125 #ifdef BIAS_HAVE_GSL
126  if ((a+b)<=GSL_SF_GAMMA_XMAX){// check numerically stablility
127  double res = gsl_ran_beta_pdf(x, a, b);
128  BIASASSERT(!isnan(res) && !isinf(res));
129  return res;
130  } else {
131  BEXCEPTION("a or b out of range: a="<<a<<" b="<<b<<endl);
132  }
133 #else
134  BIASERR("recompile BIAS with gsl support enabled");
135  BIASABORT;
136  return 0.0;
137 #endif
138 }
139 
140 
141 double
142 BIAS::CulmBetaDistribution(double a, double b, double x)
143 {
144 #ifdef BIAS_HAVE_GSL
145  double res = gsl_cdf_beta_P(x, a, b);
146 #ifdef BIAS_DEBUG
147  if (isnan(res) || isinf(res)){
148  cout << "a: "<<a<<" b: "<<b<<" x: "<<x<<endl;
149  }
150  cout << "a: "<<a<<" b: "<<b<<" x: "<<x<<endl;
151 #endif
152  BIASASSERT(!isnan(res) && !isinf(res));
153  return res;
154 #else
155  BIASERR("recompile BIAS with gsl support enabled");
156  BIASABORT;
157  return 0.0;
158 #endif
159 }
160 
161 
162 double
163 BIAS::CulmulativeNormalDistribution(double x, double sigma)
164 {
165 #ifdef BIAS_HAVE_GSL
166  double res = gsl_cdf_gaussian_P(x, sigma);
167 #ifdef BIAS_DEBUG
168  if (isnan(res) || isinf(res)){
169  cout << "sigma: "<<sigma<<" x: "<<x<<" res: "<<res<<endl;
170  }
171  cout << "sigma: "<<sigma<<" x: "<<x<<" res: "<<res<<endl;
172 #endif
173  BIASASSERT(!isnan(res) && !isinf(res));
174  return res;
175 #else
176  BIASERR("recompile BIAS with gsl support enabled");
177  BIASABORT;
178  return 0.0;
179 #endif
180 }
181 
182 
183 double
184 BIAS::BetaDistributionApprox(double a, double b, double x)
185 {
186 #ifdef BIAS_HAVE_GSL
187  if ((a+b)<=GSL_SF_GAMMA_XMAX){// check numerically stablility
188  double res = gsl_ran_beta_pdf(x, a, b);
189  BIASASSERT(!isnan(res) && !isinf(res));
190  return res;
191  } else if (Equal(a,b)){
192  // the beta distribution is very steep and narrow,
193  // approximate beta distribution by gaussian
194  double dist = x-0.5;
195  // The variance of the beta distribution beta(a, b, x) is given by
196  // sigma^2 = (a*b)/(a+b)^2/(a+b+1)
197  // (http://en.wikipedia.org/wiki/Beta_distribution)
198  double sigma_sq = (a*b)/(a+b)/(a+b)/(a+b+1);
199  double res = 1.0/sqrt(sigma_sq * 2.0 * M_PI) *
200  exp(-(dist*dist)/(2.0 * sigma_sq));
201  bool debug = false;
202  if (debug){
203  cout <<"approximating beta distribution by gaussian: alpha = beta = "<<a
204  <<" x = "<<x<<" sigma: "<<sqrt(sigma_sq)<<" result: "<<res<<endl;
205  }
206  return res;
207  } else {
208  BIASERR("a or b to big, cannot compute beta distribution\n");
209  BIASABORT;
210  return -1.0;
211  }
212 #else
213  BIASERR("recompile BIAS with gsl support enabled");
214  BIASABORT;
215  return 0.0;
216 #endif
217 }
218 
219 
220 
221 
222 
BIASMathAlgo_EXPORT double BetaDistribution(double a, double b, double x)
Definition: GSL.cpp:123
BIASMathAlgo_EXPORT double ChiSquareProbDensFun(double x, int deg_freedom)
Definition: GSL.cpp:48
BIASMathAlgo_EXPORT double CulmulativeNormalDistribution(double x, double sigma)
culmulative probability function of normal distribution
Definition: GSL.cpp:163
BIASMathAlgo_EXPORT double InvChiSquareCulmProbFun(double x, int deg_freedom)
Definition: GSL.cpp:81
BIASMathAlgo_EXPORT double Gamma(double x)
Definition: GSL.cpp:95
BIASMathAlgo_EXPORT double InvGamma(double x)
Definition: GSL.cpp:109
BIASMathAlgo_EXPORT double CulmBetaDistribution(double a, double b, double x)
Definition: GSL.cpp:142
BIASMathAlgo_EXPORT double ChiSquareCulmProbFun(double x, int deg_freedom)
Definition: GSL.cpp:67
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
BIASMathAlgo_EXPORT double BetaDistributionApprox(double a, double b, double x)
Definition: GSL.cpp:184