-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_means.py
33 lines (25 loc) · 1.03 KB
/
state_means.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
from models import KalmanFilterAverage, KalmanFilterRegression
############################################################
# Load pricing data for a security
#start = all_contracts.index.min()
#end = all_contracts.index.max()
#x = get_pricing('LMT', fields='price', start_date=start, end_date=end)
x = state_means(all_contracts)
############################################################
# Use the observed values of the price to get a rolling mean
state_means, _ = kf.filter(x.values)
state_means = pd.Series(state_means.flatten(), index=x.index)
# Compute the rolling mean with various lookback windows
mean30 = x.rolling(window = 30).mean()
mean60 = x.rolling(window = 60).mean()
mean90 = x.rolling(window = 90).mean()
# Plot original data and estimated mean
plt.plot(state_means)
plt.plot(x)
plt.plot(mean30)
plt.plot(mean60)
plt.plot(mean90)
plt.title('Kalman filter estimate of average')
plt.legend(['Kalman Estimate', 'X', '30-day Moving Average', '60-day Moving Average','90-day Moving Average'])
plt.xlabel('Day')
plt.ylabel('Price');