| 1 |
|
| /* This program simulates fractional Gaussian noise or fractional */ |
| 2 |
|
| /* Brownian motion using the Hosking method. */ |
| 3 |
|
| /* The C-package Ranlib is used, available via Netlib */ |
| 4 |
|
| /* (http://www.netlib.org). */ |
| 5 |
|
| |
| 6 |
|
| /* Reference: */ |
| 7 |
|
| /* J.R.M. Hosking (1984), */ |
| 8 |
|
| /* Modeling persistence in hydrological time series using fractional */ |
| 9 |
|
| /* brownian differencing, */ |
| 10 |
|
| /* Water Resources Research, Vol. 20, pp. 1898--1908. */ |
| 11 |
|
| |
| 12 |
|
| /* Copyright Ton Dieker */ |
| 13 |
|
| /* Centre of Mathematics and Computer Science (CWI) Amsterdam */ |
| 14 |
|
| /* April 2002 */ |
| 15 |
|
| |
| 16 |
|
| /* ton@cwi.nl */ |
| 17 |
|
| |
| 18 |
|
| |
| 19 |
|
| #include "hosking.h" |
| 20 |
|
| |
| 21 |
|
| /* the autocovariance function of fractional Gaussian noise */ |
| 22 |
|
| extern double covariance(long i, double H); |
| 23 |
|
| |
| 24 |
|
| |
| 25 |
1 |
| void hosking(long *n, double *H, double *L, int *cum, |
| 26 |
|
| long *seed1, long *seed2, double *output) { |
| 27 |
|
| /* function that generates a fractional Brownian motion or fractional */ |
| 28 |
|
| /* Gaussian noise sample using the Hosking method. */ |
| 29 |
|
| /* Input: *n determines the sample size N by N=2^(*n) */ |
| 30 |
|
| /* *H the Hurst parameter of the trace */ |
| 31 |
|
| /* *L the sample is generated on [0,L] */ |
| 32 |
|
| /* *cum = 0: fractional Gaussian noise is produced */ |
| 33 |
|
| /* = 1: fractional Brownian motion is produced */ |
| 34 |
|
| /* *seed1 seed1 for the random generator */ |
| 35 |
|
| /* *seed2 seed2 for the random generator */ |
| 36 |
|
| /* Output: *seed1 new seed1 of the random generator */ |
| 37 |
|
| /* *seed2 new seed2 of the random generator */ |
| 38 |
|
| /* *output the resulting sample is stored in this array */ |
| 39 |
1 |
| long i, j, generator, m = pow(2,*n); |
| 40 |
1 |
| double *phi = (double *) calloc(m, sizeof(double)); |
| 41 |
1 |
| double *psi = (double *) calloc(m, sizeof(double)); |
| 42 |
1 |
| double *cov = (double *) calloc(m, sizeof(double)); |
| 43 |
|
| double v, scaling; |
| 44 |
|
| |
| 45 |
|
| /* set random generator and seeds */ |
| 46 |
1 |
| snorm(); |
| 47 |
1 |
| generator = 1; |
| 48 |
1 |
| gscgn(1, &generator); |
| 49 |
1 |
| setall(*seed1,*seed2); |
| 50 |
|
| |
| 51 |
|
| /* initialization */ |
| 52 |
1 |
| output[0] = snorm(); |
| 53 |
1 |
| v = 1; |
| 54 |
1 |
| phi[0] = 0; |
| 55 |
65 |
▶ | for (i=0; i<m; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<m; i++) |
64 |
1 |
65 |
|
| 56 |
64 |
| cov[i] = covariance(i, *H); |
| 57 |
|
| |
| 58 |
|
| /* simulation */ |
| 59 |
64 |
▶ | for(i=1; i<m; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=1; i<m; i++) { |
63 |
1 |
64 |
|
| 60 |
63 |
| phi[i-1] = cov[i]; |
| 61 |
2016 |
▶ | for (j=0; j<i-1; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<i-1; j++) { |
1953 |
63 |
2016 |
|
| 62 |
1953 |
| psi[j] = phi[j]; |
| 63 |
1953 |
| phi[i-1] -= psi[j]*cov[i-j-1]; |
| 64 |
|
| } |
| 65 |
63 |
| phi[i-1] /= v; |
| 66 |
2016 |
▶ | for (j=0; j<i-1; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<i-1; j++) { |
1953 |
63 |
2016 |
|
| 67 |
1953 |
| phi[j] = psi[j] - phi[i-1]*psi[i-j-2]; |
| 68 |
|
| } |
| 69 |
63 |
| v *= (1-phi[i-1]*phi[i-1]); |
| 70 |
|
| |
| 71 |
63 |
| output[i] = 0; |
| 72 |
2079 |
▶ | for (j=0; j<i; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<i; j++) { |
2016 |
63 |
2079 |
|
| 73 |
2016 |
| output[i] += phi[j]*output[i-j-1]; |
| 74 |
|
| } |
| 75 |
63 |
| output[i] += sqrt(v)*snorm(); |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
|
| /* rescale to obtain a sample of size 2^(*n) on [0,L] */ |
| 79 |
1 |
| scaling = pow(*L/m,*H); |
| 80 |
65 |
▶ | for(i=0;i<m;i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0;i<m;i++) { |
64 |
1 |
65 |
|
| 81 |
64 |
| output[i] = scaling*(output[i]); |
| 82 |
64 |
▶ | if (*cum && i>0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*cum && i>0) { |
0 |
64 |
64 |
| if (*cum && i>0) { |
0 |
0 |
0 |
|
| 83 |
0 |
| output[i] += output[i-1]; |
| 84 |
|
| } |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
|
| /* store the new random seeds and free memory */ |
| 88 |
1 |
| getsd(seed1,seed2); |
| 89 |
|
| |
| 90 |
1 |
| free(phi); |
| 91 |
1 |
| free(psi); |
| 92 |
1 |
| free(cov); |
| 93 |
1 |
| } |