1 | /*!\file: random
|
---|
2 | * \brief random number generating functions
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*Headers*/
|
---|
6 | /*{{{*/
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <sys/types.h>
|
---|
9 | #include <math.h>
|
---|
10 | #include <float.h> /* DBL_EPSILON */
|
---|
11 | #include <chrono>
|
---|
12 | #include <cstdarg>
|
---|
13 | #include <iostream>
|
---|
14 | #include <random>
|
---|
15 |
|
---|
16 | #include "../Matrix/matrix.h"
|
---|
17 | #include "../Exceptions/exceptions.h"
|
---|
18 | #include "../MemOps/MemOps.h"
|
---|
19 | #include "../io/io.h"
|
---|
20 | /*}}}*/
|
---|
21 |
|
---|
22 | void univariateNormal(IssmPDouble* prand, IssmPDouble mean, IssmPDouble sdev) { /*{{{*/
|
---|
23 |
|
---|
24 | /*Random seed using time_since_epoch*/
|
---|
25 | unsigned seed = std::chrono::steady_clock::now().time_since_epoch().count();
|
---|
26 | std::default_random_engine generator(seed);
|
---|
27 | /*Normal Probability Distribution*/
|
---|
28 | std::normal_distribution<IssmPDouble> normdistri(mean,sdev);
|
---|
29 | *prand = normdistri(generator);
|
---|
30 | } /*}}}*/
|
---|
31 | void multivariateNormal(IssmDouble** prand, int dim, IssmDouble mean, IssmDouble* covariancematrix) { /*{{{*/
|
---|
32 |
|
---|
33 | IssmPDouble* sampleStandardNormal = xNew<IssmPDouble>(dim);
|
---|
34 | IssmDouble* sampleMultivariateNormal = xNew<IssmDouble>(dim);
|
---|
35 | IssmDouble* Lchol = xNewZeroInit<IssmDouble>(dim*dim);
|
---|
36 |
|
---|
37 | for(int i=0;i<dim;i++) univariateNormal(&(sampleStandardNormal[i]),0.0,1.0);
|
---|
38 | CholeskyRealPositiveDefinite(Lchol,covariancematrix,dim);
|
---|
39 |
|
---|
40 | /*Matrix by vector multiplication*/
|
---|
41 | for(int i=0;i<dim;i++){
|
---|
42 | /*Entry-by-entry multiplication along matrix row*/
|
---|
43 | IssmDouble sum=0.;
|
---|
44 | for(int j=0;j<dim;j++) sum += sampleStandardNormal[j]*Lchol[i*dim+j];
|
---|
45 | sampleMultivariateNormal[i] = mean+sum;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /*Assign output pointer and cleanup*/
|
---|
49 | *prand = sampleMultivariateNormal;
|
---|
50 | xDelete<IssmPDouble>(sampleStandardNormal);
|
---|
51 | xDelete<IssmDouble>(Lchol);
|
---|
52 | } /*}}}*/
|
---|
53 | void multivariateNormal(IssmDouble** prand, int dim, IssmDouble* mean, IssmDouble* covariancematrix) { /*{{{*/
|
---|
54 |
|
---|
55 | IssmPDouble* sampleStandardNormal = xNew<IssmPDouble>(dim);
|
---|
56 | IssmDouble* sampleMultivariateNormal = xNew<IssmDouble>(dim);
|
---|
57 | IssmDouble* Lchol = xNewZeroInit<IssmDouble>(dim*dim);
|
---|
58 | for(int i=0;i<dim;i++) univariateNormal(&(sampleStandardNormal[i]),0.0,1.0);
|
---|
59 |
|
---|
60 | CholeskyRealPositiveDefinite(Lchol,covariancematrix,dim);
|
---|
61 |
|
---|
62 | /*Matrix by vector multiplication*/
|
---|
63 | for(int i=0;i<dim;i++){
|
---|
64 | IssmDouble sum = 0.;
|
---|
65 | for(int j=0;j<dim;j++) sum += sampleStandardNormal[j]*Lchol[i*dim+j];
|
---|
66 | sampleMultivariateNormal[i] = mean[i]+sum;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*Assign output pointer and cleanup*/
|
---|
70 | *prand = sampleMultivariateNormal;
|
---|
71 | xDelete<IssmPDouble>(sampleStandardNormal);
|
---|
72 | xDelete<IssmDouble>(Lchol);
|
---|
73 | } /*}}}*/
|
---|
74 |
|
---|
75 |
|
---|
76 |
|
---|