-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathToolkit.py
More file actions
87 lines (76 loc) · 2.26 KB
/
Toolkit.py
File metadata and controls
87 lines (76 loc) · 2.26 KB
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
import numpy as np
def Wiener(dt=1,X0=0,num_steps=10000, mu=0, sigma=1):
'''
dt: Time step
X0: Starting Point
num_steps: Number of steps
mu: Mean of Gaussian
sigma: STD of Gaussian
'''
# create result array
res = np.zeros(num_steps)
# initialize start value
res[0] = X0
# calculate and store time series
for t in range(1,num_steps):
res[t] = res[t-1] + np.random.normal(mu,sigma)*dt
# return time series
return res
def OU(dt=1, X0=0, num_steps= 10000, alpha=0.5, mu=0, sigma=0.5):
'''
dt: Time step
X0: Starting Point
num_steps: Number of steps
alpha: oscillation parameter
mu: Mean of Gaussian
sigma: STD of Gaussian
'''
res = np.zeros(num_steps)
res[0] = X0
for t in range(1,num_steps):
res[t] = alpha*res[t-1]*dt + sigma*np.random.normal(mu,sigma)
return res
def WhiteNoise(X0=0,num_steps=1000, mu=0, sigma=1,a=1.):
'''
X0: Starting Point
num_steps: Number of steps
mu: Mean of Gaussian
sigma: STD of Gaussian
a: amplification parameter
'''
# create result array
res = np.zeros(num_steps)
# initialize start value
res[0] = X0
# calculate and store time series
for t in range(1,num_steps):
res[t] = a*np.random.normal(mu,sigma)
# return time series
return res
def AutocorrelationFunction(x, lag=20):
'''
x: Input Data
lag: Time lag
'''
return np.array([1]+[np.corrcoef(x[:-i], x[i:])[0,1] for i in range(1, lag)])
def MovingAverage(timeseries,lag=3):
'''
Calculates The Simple Moving Average (SMA) of a timeseries with a certain lag.
timeseries: The timeseries data to impliment the Simple Moving Average (SMA).
lag: The lag to use for the Simple Moving Average (SMA).
'''
ma = np.empty(len(timeseries))
ma[:] = np.nan
for i in range(lag,len(timeseries) - lag):
ma[i] = np.mean(timeseries[i-lag//2:i+lag//2+1])
return ma
def VarianceFunction(x, lag):
return [np.var((np.cumsum(np.insert(x, 0, 0))[i:] - np.cumsum(np.insert(x, 0, 0))[:-i] )/ float(i))/np.var(x)
for i in range(1, lag + 1)]
def sin(t,T=2*np.pi,A=1):
'''
A: Amplitude
T: Period
t: time-steps
'''
return A*np.sin(2*np.pi/T*t)