-
Notifications
You must be signed in to change notification settings - Fork 0
/
beta_functions.py
158 lines (98 loc) · 4.86 KB
/
beta_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# This function generates an array of d beta values over n time points to be used in a time series
# generating model
# 15/10/20 Andrew Melville
import pandas as pd
from brownian_motion import geo_bm, bm_std, brownian_bridge
import numpy as np
class beta_generator:
def __init__(self, beta_type, number, dimensions, freq = 10, noise = 0.0035, t=0.5):
# Initialise class variables determining vector size, dimensions, and beta generator type
self.beta_type = beta_type
self.n = number
self.d = dimensions
self.freq = freq
self.noise = noise
self.t = t # correlation in brownian bridge
# Initialise empty beta array and array of linespace to be operated on
self.beta_df = pd.DataFrame([], index = [l for l in range(self.n)], columns = [m for m in range(1,self.d+1)])
self.line = np.linspace(0,self.n,self.n)
def __call__(self):
# Call the appropriate generation function
if self.beta_type == 'sin_range':
return self.sin_range()
elif self.beta_type == 'sin_correlated':
return self.sin_correlated()
elif self.beta_type == 'linear':
return self.linear()
elif self.beta_type == 'high_freq':
return self.high_freq()
elif self.beta_type == 'geo_bm':
return self.brownian()
elif self.beta_type == 'bm_std':
return self.brownian()
elif self.beta_type == 'constant':
return self.constant()
elif self.beta_type == 'bm_copy':
return self.brownian_copy()
elif self.beta_type == 'cor_bb':
return self.correlated_bridge()
def sin_range(self):
n, d, line = self.n, self.d, self.line
# Loop through dimensions and generate the same beta vector for each
for j in range(d):
# Generate random periodic function for each beta
self.beta_df[j] = np.sin((2*np.pi*line)*(j+1)/n)
return self.beta_df
def sin_correlated(self):
d, line = self.d, self.line
# Loop through dimensions and generate the same beta vector for each
for j in range(d):
# Generate standard sin periodic function for each beta
self.beta_df[j] = np.sin((2*np.pi*line))
return self.beta_df
def linear(self):
d, line = self.d, self.line
# Loop through dimensions and generate the same beta vector for each
for j in range(d):
# Generate random periodic function for each beta
self.beta_df[j] = 2*line
return self.beta_df
def high_freq(self):
n, d, line, freq = self.n, self.d, self.line, self.freq
# Loop through dimensions and generate the same beta vector for each
for j in range(d):
# Generate standard sin periodic function for each beta
self.beta_df[j] = np.sin((freq * 2 * np.pi * line) / n)
return self.beta_df
def brownian(self):
n, d = self.n, self.d
self.beta_df = bm_std(d=d, n=n, sigma=self.noise, initial_range=[-0.1,0.6])
return self.beta_df
def hierarchical_brownian_bridge(self):
n, d = self.n, self.d
bm_hold = bm_std(d=1, n=n, sigma=self.noise, initial_range=[-0.1,0.6])
for i in range(d):
self.beta_df[i] = bm_hold
return self.beta_df
def constant(self):
n, d = self.n, self.d
self.beta_df = bm_std(d=d, n=n, sigma=self.noise, initial_range=[-0.1,0.6])
return self.beta_df
def correlated_bridge(self):
n, d, t, noise = self.n, self.d, self.t, self.noise
# Generate master bridge that all other beta series are derived from
master_bridge = t * brownian_bridge(n=n, sigma=noise, initial_range=[-0.2,0.8], final_range=[-0.2,0.8])
master_bridge = pd.DataFrame([master_bridge for i in range(d)]).transpose()
master_bridge.columns = [c+1 for c in range(d)]
# Create corrletaed beta series
for k in range(d):
# Generate seperate Brownian Bridge
hold_bb = brownian_bridge(n=n, sigma=noise, initial_range=[-0.25,0.75], final_range=[-0.25,0.75])
# Generate seperate independent brownian bridges
self.beta_df[k+1] = hold_bb
# Take convex combination
self.beta_df = (1-t)*self.beta_df + master_bridge
return self.beta_df
## Notes
# Need to add more flexible beta functions using combinations of
# sin and cos periodic functions