-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhistogram.py
More file actions
54 lines (43 loc) · 1.54 KB
/
histogram.py
File metadata and controls
54 lines (43 loc) · 1.54 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
""" Plot a histogram """
import pandas as pd
import matplotlib.pyplot as plt
from daily_returns import get_data, plot_data
def compute_daily_returns(df):
daily_returns = df.copy()
daily_returns[1:] = (df[1:] / df[:-1].values) - 1
daily_returns.ix[0, :] = 0
return daily_returns
def main():
# Read data
dates = pd.date_range("2009-01-01", "2012-12-31")
symbols = ["SPY"]
df = get_data(symbols, dates)
plot_data(df)
# Compute daily returns
daily_returns = compute_daily_returns(df)
plot_data(daily_returns, title="Daily Returns", ylabel="Daily Returns")
# Plot a histogram
daily_returns.hist(bins=20)
# Get the mean and standard deviation
mean = daily_returns["SPY"].mean()
print "mean: ", mean
std = daily_returns["SPY"].std()
print "standard deviation: ", mean
plt.axvline(mean, color="w", linestyle="dashed", linewidth=2)
plt.axvline(std, color="r", linestyle="dashed", linewidth=2)
plt.axvline(-std, color="r", linestyle="dashed", linewidth=2)
plt.show()
# Compute Kurtosis
print "kurtosis: ", daily_returns["SPY"].kurtosis()
# Plot double histogram
dates = pd.date_range("2009-01-01", "2012-12-31")
symbols = ["SPY", "XOM"]
df = get_data(symbols, dates)
plot_data(df)
daily_returns = compute_daily_returns(df)
plot_data(daily_returns, title="Daily Returns", ylabel="Daily Returns")
daily_returns["SPY"].hist(bins=20, label="SPY")
daily_returns["XOM"].hist(bins=20, label="XOM")
plt.show()
if __name__ == "__main__":
main()