[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 <cstdarg>
|
---|
| 12 | #include <iostream>
|
---|
| 13 |
|
---|
| 14 | #include "../Matrix/matrix.h"
|
---|
| 15 | #include "../Exceptions/exceptions.h"
|
---|
| 16 | #include "../MemOps/MemOps.h"
|
---|
| 17 | #include "../io/io.h"
|
---|
[26621] | 18 | #include "./randomgenerator.h"
|
---|
[26477] | 19 | /*}}}*/
|
---|
| 20 |
|
---|
[26621] | 21 | void univariateNormal(IssmPDouble* prand, IssmPDouble mean, IssmPDouble sdev, int seed=-1) { /*{{{*/
|
---|
[26482] | 22 |
|
---|
[26621] | 23 | /*Seed the pseudo-random number generator*/
|
---|
| 24 | rnd::linear_congruential_engine randomengine;
|
---|
| 25 | randomengine.seed(seed);
|
---|
| 26 | /*Normal distribution*/
|
---|
| 27 | rnd::normal_distribution distriNormal(mean,sdev);
|
---|
[26657] | 28 | /*Assign output pointer and cleanup*/
|
---|
[26621] | 29 | *prand = distriNormal.generator(randomengine);
|
---|
[26657] | 30 | randomengine.free_resources();
|
---|
[26477] | 31 | } /*}}}*/
|
---|
[26621] | 32 | void multivariateNormal(IssmDouble** prand, int dim, IssmDouble mean, IssmDouble* covariancematrix, int seed=-1) { /*{{{*/
|
---|
[26482] | 33 |
|
---|
| 34 | IssmPDouble* sampleStandardNormal = xNew<IssmPDouble>(dim);
|
---|
[26477] | 35 | IssmDouble* sampleMultivariateNormal = xNew<IssmDouble>(dim);
|
---|
| 36 | IssmDouble* Lchol = xNewZeroInit<IssmDouble>(dim*dim);
|
---|
[26479] | 37 |
|
---|
[26621] | 38 | /*True randomness if seed<0, otherwise random seed is fixed at seed*/
|
---|
| 39 | /*Seed the pseudo-random number generator, repeatedly calling univariateNormal does not ensure randomness*/
|
---|
| 40 | rnd::linear_congruential_engine randomengine;
|
---|
| 41 | randomengine.seed(seed);
|
---|
| 42 | /*Normal distribution*/
|
---|
| 43 | rnd::normal_distribution distriNormal(0.0,1.0);
|
---|
| 44 | for(int i=0;i<dim;i++){
|
---|
| 45 | sampleStandardNormal[i] = distriNormal.generator(randomengine);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /*Cholsesky decomposition of the covariance matrix*/
|
---|
[26483] | 49 | CholeskyRealPositiveDefinite(Lchol,covariancematrix,dim);
|
---|
[26482] | 50 |
|
---|
| 51 | /*Matrix by vector multiplication*/
|
---|
| 52 | for(int i=0;i<dim;i++){
|
---|
| 53 | /*Entry-by-entry multiplication along matrix row*/
|
---|
[26479] | 54 | IssmDouble sum=0.;
|
---|
[26482] | 55 | for(int j=0;j<dim;j++) sum += sampleStandardNormal[j]*Lchol[i*dim+j];
|
---|
| 56 | sampleMultivariateNormal[i] = mean+sum;
|
---|
[26477] | 57 | }
|
---|
[26479] | 58 |
|
---|
| 59 | /*Assign output pointer and cleanup*/
|
---|
[26477] | 60 | *prand = sampleMultivariateNormal;
|
---|
[26479] | 61 | xDelete<IssmPDouble>(sampleStandardNormal);
|
---|
[26477] | 62 | xDelete<IssmDouble>(Lchol);
|
---|
[26657] | 63 | randomengine.free_resources();
|
---|
[26477] | 64 | } /*}}}*/
|
---|
[26621] | 65 | void multivariateNormal(IssmDouble** prand, int dim, IssmDouble* mean, IssmDouble* covariancematrix, int seed=-1) { /*{{{*/
|
---|
[26482] | 66 |
|
---|
| 67 | IssmPDouble* sampleStandardNormal = xNew<IssmPDouble>(dim);
|
---|
[26477] | 68 | IssmDouble* sampleMultivariateNormal = xNew<IssmDouble>(dim);
|
---|
| 69 | IssmDouble* Lchol = xNewZeroInit<IssmDouble>(dim*dim);
|
---|
[26621] | 70 |
|
---|
| 71 | /*True randomness if seed<0, otherwise random seed is fixed at seed*/
|
---|
| 72 | /*Seed the pseudo-random number generator, repeatedly calling univariateNormal does not ensure randomness*/
|
---|
| 73 | rnd::linear_congruential_engine randomengine;
|
---|
| 74 | randomengine.seed(seed);
|
---|
| 75 | /*Normal distribution*/
|
---|
| 76 | rnd::normal_distribution distriNormal(0.0,1.0);
|
---|
| 77 | for(int i=0;i<dim;i++){
|
---|
| 78 | sampleStandardNormal[i] = distriNormal.generator(randomengine);
|
---|
| 79 | }
|
---|
[26479] | 80 |
|
---|
[26621] | 81 | /*Cholsesky decomposition of the covariance matrix*/
|
---|
[26477] | 82 | CholeskyRealPositiveDefinite(Lchol,covariancematrix,dim);
|
---|
[26479] | 83 |
|
---|
[26482] | 84 | /*Matrix by vector multiplication*/
|
---|
| 85 | for(int i=0;i<dim;i++){
|
---|
[26479] | 86 | IssmDouble sum = 0.;
|
---|
[26482] | 87 | for(int j=0;j<dim;j++) sum += sampleStandardNormal[j]*Lchol[i*dim+j];
|
---|
| 88 | sampleMultivariateNormal[i] = mean[i]+sum;
|
---|
[26477] | 89 | }
|
---|
[26482] | 90 |
|
---|
| 91 | /*Assign output pointer and cleanup*/
|
---|
[26477] | 92 | *prand = sampleMultivariateNormal;
|
---|
[26479] | 93 | xDelete<IssmPDouble>(sampleStandardNormal);
|
---|
[26477] | 94 | xDelete<IssmDouble>(Lchol);
|
---|
[26657] | 95 | randomengine.free_resources();
|
---|
[26477] | 96 | } /*}}}*/
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 |
|
---|