source: issm/trunk-jpl/src/c/shared/Random/random.cpp@ 26574

Last change on this file since 26574 was 26483, checked in by vverjans, 3 years ago

NEW: randomflag allows to use fixed random seed in random.cpp, zero-initialization of smbspin for SmbAutoregression

  • Property svn:executable set to *
File size: 2.8 KB
Line 
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
22void univariateNormal(IssmPDouble* prand, IssmPDouble mean, IssmPDouble sdev, int seedfixed=-1) { /*{{{*/
23
24 unsigned seed;
25 /*Random seed using time_since_epoch*/
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);
30 /*Normal Probability Distribution*/
31 std::normal_distribution<IssmPDouble> normdistri(mean,sdev);
32 *prand = normdistri(generator);
33} /*}}}*/
34void multivariateNormal(IssmDouble** prand, int dim, IssmDouble mean, IssmDouble* covariancematrix, int seedfixed=-1) { /*{{{*/
35
36 IssmPDouble* sampleStandardNormal = xNew<IssmPDouble>(dim);
37 IssmDouble* sampleMultivariateNormal = xNew<IssmDouble>(dim);
38 IssmDouble* Lchol = xNewZeroInit<IssmDouble>(dim*dim);
39
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);
43
44 /*Matrix by vector multiplication*/
45 for(int i=0;i<dim;i++){
46 /*Entry-by-entry multiplication along matrix row*/
47 IssmDouble sum=0.;
48 for(int j=0;j<dim;j++) sum += sampleStandardNormal[j]*Lchol[i*dim+j];
49 sampleMultivariateNormal[i] = mean+sum;
50 }
51
52 /*Assign output pointer and cleanup*/
53 *prand = sampleMultivariateNormal;
54 xDelete<IssmPDouble>(sampleStandardNormal);
55 xDelete<IssmDouble>(Lchol);
56} /*}}}*/
57void multivariateNormal(IssmDouble** prand, int dim, IssmDouble* mean, IssmDouble* covariancematrix, int seedfixed=-1) { /*{{{*/
58
59 IssmPDouble* sampleStandardNormal = xNew<IssmPDouble>(dim);
60 IssmDouble* sampleMultivariateNormal = xNew<IssmDouble>(dim);
61 IssmDouble* Lchol = xNewZeroInit<IssmDouble>(dim*dim);
62
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);
65 CholeskyRealPositiveDefinite(Lchol,covariancematrix,dim);
66
67 /*Matrix by vector multiplication*/
68 for(int i=0;i<dim;i++){
69 IssmDouble sum = 0.;
70 for(int j=0;j<dim;j++) sum += sampleStandardNormal[j]*Lchol[i*dim+j];
71 sampleMultivariateNormal[i] = mean[i]+sum;
72 }
73
74 /*Assign output pointer and cleanup*/
75 *prand = sampleMultivariateNormal;
76 xDelete<IssmPDouble>(sampleStandardNormal);
77 xDelete<IssmDouble>(Lchol);
78} /*}}}*/
79
80
81
Note: See TracBrowser for help on using the repository browser.