summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/math/example/skew_normal_example.cpp
blob: 5858aff3582dc66eb803a9d66d5c6229d636133a (plain)
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
// Copyright Benjamin Sobotta 2012

//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)


#ifdef _MSC_VER
#  pragma warning (disable : 4512) // assignment operator could not be generated
#  pragma warning (disable : 4127) // conditional expression is constant.
#endif

#include <boost/math/distributions/skew_normal.hpp>
using boost::math::skew_normal_distribution;
using boost::math::skew_normal;
#include <iostream>
#include <cmath>
#include <utility>

void check(const double loc, const double sc, const double sh,
     const double * const cumulants, const std::pair<double, double> qu,
     const double x, const double tpdf, const double tcdf)
{
  using namespace boost::math;

  skew_normal D(loc, sc, sh);
  
  const double sk = cumulants[2] / (std::pow(cumulants[1], 1.5));
  const double kt = cumulants[3] / (cumulants[1] * cumulants[1]);

  // checks against tabulated values
  std::cout << "mean: table=" << cumulants[0] << "\tcompute=" << mean(D) << "\tdiff=" << fabs(cumulants[0]-mean(D)) << std::endl;
  std::cout << "var: table=" << cumulants[1] << "\tcompute=" << variance(D) << "\tdiff=" << fabs(cumulants[1]-variance(D)) << std::endl;
  std::cout << "skew: table=" << sk << "\tcompute=" << skewness(D) << "\tdiff=" << fabs(sk-skewness(D)) << std::endl;
  std::cout << "kur.: table=" << kt << "\tcompute=" << kurtosis_excess(D) << "\tdiff=" << fabs(kt-kurtosis_excess(D)) << std::endl;
  std::cout << "mode: table=" << "N/A" << "\tcompute=" << mode(D) << "\tdiff=" << "N/A" << std::endl;

  const double q = quantile(D, qu.first);
  const double cq = quantile(complement(D, qu.first));

  std::cout << "quantile(" << qu.first << "): table=" << qu.second << "\tcompute=" << q << "\tdiff=" << fabs(qu.second-q) << std::endl;

  // consistency
  std::cout << "cdf(quantile)=" << cdf(D, q) << "\tp=" << qu.first << "\tdiff=" << fabs(qu.first-cdf(D, q)) << std::endl;
  std::cout << "ccdf(cquantile)=" << cdf(complement(D,cq)) << "\tp=" << qu.first << "\tdiff=" << fabs(qu.first-cdf(complement(D,cq))) << std::endl;

  // PDF & CDF
  std::cout << "pdf(" << x << "): table=" << tpdf << "\tcompute=" << pdf(D,x) << "\tdiff=" << fabs(tpdf-pdf(D,x)) << std::endl;
  std::cout << "cdf(" << x << "): table=" << tcdf << "\tcompute=" << cdf(D,x) << "\tdiff=" << fabs(tcdf-cdf(D,x)) << std::endl;
  std::cout << "================================\n";
}

int main()
{
  using namespace boost::math;

  double sc = 0.0,loc,sh,x,dsn,qsn,psn,p;
  std::cout << std::setprecision(20);

  double cumulants[4];


  /* R:
     > install.packages("sn")
     Warning in install.packages("sn") :
     'lib = "/usr/lib64/R/library"' is not writable
     Would you like to create a personal library
     '~/R/x86_64-unknown-linux-gnu-library/2.12'
     to install packages into?  (y/n) y
     --- Please select a CRAN mirror for use in this session ---
     Loading Tcl/Tk interface ... done
     also installing the dependency mnormt

     trying URL 'http://mirrors.dotsrc.org/cran/src/contrib/mnormt_1.4-5.tar.gz'
     Content type 'application/x-gzip' length 34049 bytes (33 Kb)
     opened URL
     ==================================================
     downloaded 33 Kb

     trying URL 'http://mirrors.dotsrc.org/cran/src/contrib/sn_0.4-17.tar.gz'
     Content type 'application/x-gzip' length 65451 bytes (63 Kb)
     opened URL
     ==================================================
     downloaded 63 Kb


     > library(sn)
     > options(digits=22)


     > sn.cumulants(1.1, 2.2, -3.3)
     [1] -0.5799089925398568  2.0179057767837230 -2.0347951542374196
     [4]  2.2553488991015072
     > qsn(0.3, 1.1, 2.2, -3.3)
     [1] -1.180104068086876
     > psn(0.4, 1.1, 2.2, -3.3)
     [1] 0.733918618927874
     > dsn(0.4, 1.1, 2.2, -3.3)
     [1] 0.2941401101565995

   */

  //1 st
  loc = 1.1; sc = 2.2; sh = -3.3;
  std::cout << "location: " << loc << "\tscale: " << sc << "\tshape: " << sh << std::endl;
  cumulants[0] = -0.5799089925398568;
  cumulants[1] =  2.0179057767837230;
  cumulants[2] = -2.0347951542374196;
  cumulants[3] =  2.2553488991015072;
  x = 0.4;
  p = 0.3;
  qsn = -1.180104068086876;
  psn = 0.733918618927874;
  dsn = 0.2941401101565995;

  check(loc, sc, sh, cumulants, std::make_pair(p,qsn), x, dsn, psn);

  /* R:

     > sn.cumulants(1.1, .02, .03)
     [1] 1.1004785154529559e+00 3.9977102296128255e-04 4.7027439329779991e-11
     [4] 1.4847542790693825e-14
     > qsn(0.01, 1.1, .02, .03)
     [1] 1.053964962950150
     > psn(1.3, 1.1, .02, .03)
     [1] 1
     > dsn(1.3, 1.1, .02, .03)
     [1] 4.754580380601393e-21

   */

  // 2nd
  loc = 1.1; sc = .02; sh = .03;
  std::cout << "location: " << loc << "\tscale: " << sc << "\tshape: " << sh << std::endl;
  cumulants[0] = 1.1004785154529559e+00;
  cumulants[1] = 3.9977102296128255e-04;
  cumulants[2] = 4.7027439329779991e-11;
  cumulants[3] = 1.4847542790693825e-14;
  x = 1.3;
  p = 0.01;
  qsn = 1.053964962950150;
  psn = 1;
  dsn = 4.754580380601393e-21;

  check(loc, sc, sh, cumulants, std::make_pair(p,qsn), x, dsn, psn);

  /* R:

     > sn.cumulants(10.1, 5, -.03)
     [1]  9.980371136761052e+00  2.498568893508016e+01 -7.348037395278123e-04
     [4]  5.799821402614775e-05
     > qsn(.8, 10.1, 5, -.03)
     [1] 14.18727407491953
     > psn(-1.3, 10.1, 5, -.03)
     [1] 0.01201290665838824
     > dsn(-1.3, 10.1, 5, -.03)
     [1] 0.006254346348897927


  */

  // 3rd
  loc = 10.1; sc = 5; sh = -.03;
  std::cout << "location: " << loc << "\tscale: " << sc << "\tshape: " << sh << std::endl;
  cumulants[0] = 9.980371136761052e+00;
  cumulants[1] = 2.498568893508016e+01;
  cumulants[2] = -7.348037395278123e-04;
  cumulants[3] = 5.799821402614775e-05;
  x = -1.3;
  p = 0.8;
  qsn = 14.18727407491953;
  psn = 0.01201290665838824;
  dsn = 0.006254346348897927;

  check(loc, sc, sh, cumulants, std::make_pair(p,qsn), x, dsn, psn);


  /* R:

     > sn.cumulants(-10.1, 5, 30)
     [1] -6.112791696741384  9.102169946425548 27.206345266148194 71.572537838642916
     > qsn(.8, -10.1, 5, 30)
     [1] -3.692242172277
     > psn(-1.3, -10.1, 5, 30)
     [1] 0.921592193425035
     > dsn(-1.3, -10.1, 5, 30)
     [1] 0.0339105445232089

  */

  // 4th
  loc = -10.1; sc = 5; sh = 30;
  std::cout << "location: " << loc << "\tscale: " << sc << "\tshape: " << sh << std::endl;
  cumulants[0] = -6.112791696741384;
  cumulants[1] = 9.102169946425548;
  cumulants[2] = 27.206345266148194;
  cumulants[3] = 71.572537838642916;
  x = -1.3;
  p = 0.8;
  qsn = -3.692242172277;
  psn = 0.921592193425035;
  dsn = 0.0339105445232089;

  check(loc, sc, sh, cumulants, std::make_pair(p,qsn), x, dsn, psn);
  
  
  /* R:

     > sn.cumulants(0,1,5)
     [1] 0.7823901817554269 0.3878656034927102 0.2055576317962637 0.1061119471655128
     > qsn(0.5,0,1,5)
     [1] 0.674471117502844
     > psn(-0.5, 0,1,5)
     [1] 0.0002731513884140924
     > dsn(-0.5, 0,1,5)
     [1] 0.00437241570403263

  */

  // 5th
  loc = 0; sc = 1; sh = 5;
  std::cout << "location: " << loc << "\tscale: " << sc << "\tshape: " << sh << std::endl;
  cumulants[0] = 0.7823901817554269;
  cumulants[1] = 0.3878656034927102;
  cumulants[2] = 0.2055576317962637;
  cumulants[3] = 0.1061119471655128;
  x = -0.5;
  p = 0.5;
  qsn = 0.674471117502844;
  psn = 0.0002731513884140924;
  dsn = 0.00437241570403263;

  check(loc, sc, sh, cumulants, std::make_pair(p,qsn), x, dsn, psn);

  /* R:

     > sn.cumulants(0,1,1e5)
     [1] 0.7978845607629713 0.3633802276960805 0.2180136141122883 0.1147706820312645
     > qsn(0.5,0,1,1e5)
     [1] 0.6744897501960818
     > psn(-0.5, 0,1,1e5)
     [1] 0
     > dsn(-0.5, 0,1,1e5)
     [1] 0

  */

  // 6th
  loc = 0; sc = 1; sh = 1e5;
  std::cout << "location: " << loc << "\tscale: " << sc << "\tshape: " << sh << std::endl;
  cumulants[0] = 0.7978845607629713;
  cumulants[1] = 0.3633802276960805;
  cumulants[2] = 0.2180136141122883;
  cumulants[3] = 0.1147706820312645;
  x = -0.5;
  p = 0.5;
  qsn = 0.6744897501960818;
  psn = 0.;
  dsn = 0.;

  check(loc, sc, sh, cumulants, std::make_pair(p,qsn), x, dsn, psn);

  return 0;
}


// EOF