-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulations.py
193 lines (133 loc) · 5.41 KB
/
simulations.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import numpy as np
def linear(n, p, noise=False, coeffs=None):
x = np.random.normal(size=(n, p))
eps = np.random.normal(size=(n, p))
if coeffs is None:
coeffs = np.array([np.exp(-0.0022 * (i + 30)) for i in range(p)])
y = x * coeffs + noise * eps
return x, y
def exponential(n, p, noise=False, coeffs=None):
x = np.random.normal(scale=3, size=(n, p))
eps = np.random.normal(scale=3, size=(n, p))
if coeffs is None:
coeffs = np.array([np.exp(-0.022 * (i + 52)) for i in range(p)])
y = np.exp(x * coeffs) - 1 + noise * eps
return x, y
def cubic(n, p, noise=False, coeffs=None):
x = np.random.normal(size=(n, p))
eps = np.random.normal(size=(n, p))
if coeffs is None:
coeffs = np.array([np.exp(-0.031 * (i + 25)) for i in range(p)])
x_coeffs = x * coeffs
y = x_coeffs**3 + x_coeffs**2 + x_coeffs + noise * eps
return x, y
def step(n, p, noise=False, coeffs=None):
x = np.random.normal(size=(n, p))
if coeffs is None:
coeffs = np.array([np.exp(-0.0457 * (i + 10)) for i in range(p)])
eps = np.random.normal(size=(n, p))
x_coeff = ((x * coeffs) > 0.5) * 1
y = x_coeff + noise * eps
return x, y
def quadratic(n, p, noise=False, coeffs=None):
x = np.random.normal(size=(n, p))
if coeffs is None:
coeffs = np.array([np.exp(-0.0325 * (i + 24)) for i in range(p)])
eps = np.random.normal(size=(n, p))
x_coeffs = x * coeffs
y = x_coeffs**2 + noise * eps
return x, y
def w_shaped(n, p, noise=False, coeffs=None):
x = np.random.normal(scale=30, size=(n, p))
if coeffs is None:
coeffs = np.array([np.exp(-0.2735 * (i + 10)) for i in range(p)])
eps = np.random.normal(scale=30, size=(n, p))
x_coeffs = x * coeffs
y = ((x_coeffs ** 4) - 7 * x_coeffs**2) + noise * eps
return x, y
def logarithmic(n, p, noise=False, coeffs=None):
rng = np.random.default_rng()
if coeffs is None:
coeffs = np.array([np.exp(-0.072 * (i + 10)) for i in range(p)])
x = rng.normal(size=(n, p))
eps = rng.normal(size=(n, p))
y = np.log((x * coeffs + 1)**2) + noise * eps
return x, y
def fourth_root(n, p, noise=False, coeffs=None):
x = np.random.normal(size=(n, p))
eps = np.random.normal(size=(n, p))
if coeffs is None:
coeffs = np.array([np.exp(-0.25 * (i + 50)) for i in range(p)])
x_coeffs = x * coeffs
y = 10 * np.abs(x_coeffs) ** 0.25 + noise * eps
return x, y
def _sin(n, p, noise=False, period=4*np.pi, coeffs=None):
rng = np.random.default_rng()
if period == 4*np.pi and coeffs is None:
coeffs = np.array([np.exp(-0.0095 * (i + 50)) for i in range(p)])
elif period == 16*np.pi and coeffs is None:
coeffs = np.array([np.exp(-0.015 * (i + 50)) for i in range(p)])
x = rng.normal(size=(n, p))
eps = rng.normal(size=(n, p))
y = np.sin(x * coeffs * period) + noise * eps
return x, y
def sin_four_pi(n, p, noise=False, coeffs=None):
return _sin(n, p, noise=noise, period=4 * np.pi, coeffs=coeffs)
def sin_sixteen_pi(n, p, noise=False, coeffs=None):
return _sin(n, p, noise=noise, period=16 * np.pi, coeffs=coeffs)
def _square_diamond(n, p, noise=False, low=-1, high=1, period=-np.pi / 2, coeffs=None):
u = np.random.uniform(low, high, size=(n, p))
v = np.random.uniform(low, high, size=(n, p))
eps = np.random.uniform(low, high, size=(n, p))
if coeffs is None:
coeffs = np.array([np.exp(-0.0042 * (i + 10)) for i in range(p)])
x = u * np.cos(period) + v * np.sin(period)
y = -u * coeffs * np.sin(period) + v * coeffs * np.cos(period) + eps * noise
return x, y
def square(n, p, noise=False, low=-1, high=1, coeffs=None):
return _square_diamond(n, p, noise=noise, low=low, high=high, period=-np.pi / 8, coeffs=coeffs)
def two_parabolas(n, p, noise=False, prob=0.5, coeffs=None):
x = np.random.normal(size=(n, p))
if coeffs is None:
coeffs = np.array([np.exp(-0.00145 * (i + 50)) for i in range(p)])
u = np.random.binomial(1, prob, size=(n, 1))
eps = np.random.normal(size=(n, p))
x_coeffs = x * coeffs
y = (x_coeffs**2) * (u - 0.5) + noise * eps
return x, y
def diamond(n, p, noise=False, low=-1, high=1, coeffs=None):
return _square_diamond(n, p, noise=noise, low=low, high=high, period=-np.pi / 4, coeffs=coeffs)
def multimodal_independence(n, p, prob=0.5, sep1=3, sep2=2):
rng = np.random.default_rng()
sig = np.identity(p)
u = rng.multivariate_normal(np.zeros(p), sig, size=n, method='cholesky')
v = rng.multivariate_normal(np.zeros(p), sig, size=n, method='cholesky')
u_2 = rng.binomial(1, prob, size=(n, p))
v_2 = rng.binomial(1, prob, size=(n, p))
x = u / sep1 + sep2 * u_2 - 1
y = v / sep1 + sep2 * v_2 - 1
return x, y
SIMS = {
"linear": linear,
"exponential": exponential,
"cubic": cubic,
"step": step,
"quadratic": quadratic,
"w_shaped": w_shaped,
"logarithmic": logarithmic,
"fourth_root": fourth_root,
"sin_four_pi": sin_four_pi,
"sin_sixteen_pi": sin_sixteen_pi,
"square": square,
"two_parabolas": two_parabolas,
"diamond": diamond,
"multimodal_independence": multimodal_independence,
}
def indep_sim(sim, n, p, **kwargs):
if sim not in SIMS.keys():
raise ValueError(
"sim_name must be one of the following: {}".format(list(SIMS.keys()))
)
else:
sim = SIMS[sim]
return sim(n, p, **kwargs)