[26477] | 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 |
|
---|
[26483] | 22 | void univariateNormal(IssmPDouble* prand, IssmPDouble mean, IssmPDouble sdev, int seedfixed=-1) { /*{{{*/
|
---|
[26482] | 23 |
|
---|
[26483] | 24 | unsigned seed;
|
---|
[26482] | 25 | /*Random seed using time_since_epoch*/
|
---|
[26483] | 26 | if(seedfixed==-1) seed = std::chrono::steady_clock::now().time_since_epoch().count();
|
---|
| 27 | /*Seed fixed by input argument*/
|
---|
| 28 | else seed = seedfixed;
|
---|
| 29 | std::default_random_engine generator(seed);
|
---|
[26482] | 30 | /*Normal Probability Distribution*/
|
---|
| 31 | std::normal_distribution<IssmPDouble> normdistri(mean,sdev);
|
---|
[26477] | 32 | *prand = normdistri(generator);
|
---|
| 33 | } /*}}}*/
|
---|
[26483] | 34 | void multivariateNormal(IssmDouble** prand, int dim, IssmDouble mean, IssmDouble* covariancematrix, int seedfixed=-1) { /*{{{*/
|
---|
[26482] | 35 |
|
---|
| 36 | IssmPDouble* sampleStandardNormal = xNew<IssmPDouble>(dim);
|
---|
[26477] | 37 | IssmDouble* sampleMultivariateNormal = xNew<IssmDouble>(dim);
|
---|
| 38 | IssmDouble* Lchol = xNewZeroInit<IssmDouble>(dim*dim);
|
---|
[26479] | 39 |
|
---|
[26483] | 40 | /*True randomness if seedfixed==-1, otherwise random seed is fixed at seedfixed*/
|
---|
| 41 | for(int i=0;i<dim;i++) univariateNormal(&(sampleStandardNormal[i]),0.0,1.0,seedfixed);
|
---|
| 42 | CholeskyRealPositiveDefinite(Lchol,covariancematrix,dim);
|
---|
[26482] | 43 |
|
---|
| 44 | /*Matrix by vector multiplication*/
|
---|
| 45 | for(int i=0;i<dim;i++){
|
---|
| 46 | /*Entry-by-entry multiplication along matrix row*/
|
---|
[26479] | 47 | IssmDouble sum=0.;
|
---|
[26482] | 48 | for(int j=0;j<dim;j++) sum += sampleStandardNormal[j]*Lchol[i*dim+j];
|
---|
| 49 | sampleMultivariateNormal[i] = mean+sum;
|
---|
[26477] | 50 | }
|
---|
[26479] | 51 |
|
---|
| 52 | /*Assign output pointer and cleanup*/
|
---|
[26477] | 53 | *prand = sampleMultivariateNormal;
|
---|
[26479] | 54 | xDelete<IssmPDouble>(sampleStandardNormal);
|
---|
[26477] | 55 | xDelete<IssmDouble>(Lchol);
|
---|
| 56 | } /*}}}*/
|
---|
[26483] | 57 | void multivariateNormal(IssmDouble** prand, int dim, IssmDouble* mean, IssmDouble* covariancematrix, int seedfixed=-1) { /*{{{*/
|
---|
[26482] | 58 |
|
---|
| 59 | IssmPDouble* sampleStandardNormal = xNew<IssmPDouble>(dim);
|
---|
[26477] | 60 | IssmDouble* sampleMultivariateNormal = xNew<IssmDouble>(dim);
|
---|
| 61 | IssmDouble* Lchol = xNewZeroInit<IssmDouble>(dim*dim);
|
---|
[26479] | 62 |
|
---|
[26483] | 63 | /*True randomness if seedfixed==-1, otherwise random seed is fixed at seedfixed*/
|
---|
| 64 | for(int i=0;i<dim;i++) univariateNormal(&(sampleStandardNormal[i]),0.0,1.0,seedfixed);
|
---|
[26477] | 65 | CholeskyRealPositiveDefinite(Lchol,covariancematrix,dim);
|
---|
[26479] | 66 |
|
---|
[26482] | 67 | /*Matrix by vector multiplication*/
|
---|
| 68 | for(int i=0;i<dim;i++){
|
---|
[26479] | 69 | IssmDouble sum = 0.;
|
---|
[26482] | 70 | for(int j=0;j<dim;j++) sum += sampleStandardNormal[j]*Lchol[i*dim+j];
|
---|
| 71 | sampleMultivariateNormal[i] = mean[i]+sum;
|
---|
[26477] | 72 | }
|
---|
[26482] | 73 |
|
---|
| 74 | /*Assign output pointer and cleanup*/
|
---|
[26477] | 75 | *prand = sampleMultivariateNormal;
|
---|
[26479] | 76 | xDelete<IssmPDouble>(sampleStandardNormal);
|
---|
[26477] | 77 | xDelete<IssmDouble>(Lchol);
|
---|
| 78 | } /*}}}*/
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 |
|
---|