-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-netflix_project.py
185 lines (118 loc) · 3.57 KB
/
01-netflix_project.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
import pandas as pd
import numpy as np
import math
import matplotlib.pyplot as plt
import csv
import datetime as datetime
import matplotlib.dates as mdates
import seaborn; seaborn.set()
import numpy.fft as ft
from scipy.optimize import curve_fit
df = pd.read_csv('NFLX.csv')
df.head()
df.info()
df['Date'] = pd.to_datetime(df['Date'])
df.info()
dfc = df.copy()
dfc.set_index('Date', inplace=True)
dfM = dfc.resample('M').mean()
dfW = dfc.resample('W-MON').mean()
dfW.head()
dfM.head()
fig, ax = plt.subplots(figsize=(16, 9))
plt.plot(df['Date'],df['Close'], c='blue', label='Close Price')
plt.legend(loc='upper left')
plt.title('Netflix Stock')
plt.xlabel('Year')
plt.ylabel('Price (USD)')
plt.show()
fig, ax = plt.subplots(figsize=(16, 9))
plt.plot(dfW['Close'], label='Weekly Close Price')
plt.plot(dfM['Close'], label='Monthly Close Price')
plt.legend(loc='upper left')
plt.title('Netflix Stock - Means')
plt.xlabel('Year')
plt.ylabel('Price (USD)')
plt.show()
N = len(df['Date'])
c_values = df['Close'].values
d_valuess = df['Date'].values
d_values = []
l_return = []
for i in range(N-1):
l_return.append(c_values[i + 1]/c_values[i] - 1)
d_values.append(d_valuess[i])
fig, ax = plt.subplots(figsize=(16, 9))
plt.plot(d_values,l_return, c='midnightblue', label='Linear Return')
plt.legend(loc='upper left')
plt.title('Daily Linear Return ($R_t$) of Netflix Stock')
plt.xlabel('Year')
plt.ylabel('Percent')
plt.show()
log_return = []
for i in range(N-1):
log_return.append(np.log(c_values[i+1]) - np.log(c_values[i]))
fig, ax = plt.subplots(figsize=(16, 9))
plt.plot(d_values, log_return, c='navy', label='Log Return')
plt.legend(loc='upper left')
plt.title('Daily Log Return ($r_t$) of Netflix Stock')
plt.xlabel('Year')
plt.ylabel('Percent')
plt.show()
diff = []
for i in range(N-1):
diff.append(log_return[i]-l_return[i])
fig, ax = plt.subplots(figsize=(16, 9))
plt.plot(d_values, diff, c='darkblue', label='Difference Between Returns')
plt.legend(loc='upper left')
plt.title('Difference Between Daily Linear Return and Log Return of Netflix Stock')
plt.xlabel('Year')
plt.ylabel('Percent')
plt.plot()
fig, ax = plt.subplots(figsize=(16, 9))
x = np.arange(0, 10, 0.1)
y = np.log(1+x)
z = 1*x
ax.plot(y, color='red', label='$f(x) = log(1+x)$')
ax.plot(z, color='black', label='$f(x) = x$')
plt.legend(loc='best')
plt.title('Linear and Logarithm Function')
plt.xlabel('X')
plt.ylabel('f(x)')
plt.show()
n_return = []
mean = np.average(log_return)
stdev = np.std(log_return)
for i in range(N-1):
n_return.append((log_return[i]-mean)/stdev)
fig, ax = plt.subplots(figsize=(16, 9))
plt.plot(d_values,n_return, c='royalblue', label='N Return')
plt.legend(loc='upper left')
plt.title('Daily Normalized Return of Netflix Stock')
plt.xlabel('Year')
plt.ylabel('Percent')
plt.plot()
fig, ax = plt.subplots(figsize=(16, 9))
plt.hist(log_return, bins=100, density=True)
plt.show()
def normal_dist(x, mu, msd, A):
return (A/msd*np.sqrt(2*np.pi))*(np.exp(-0.5*((x-mu)/msd)**2))
fig, ax = plt.subplots(figsize=(16, 9))
count, bins, ignored = plt.hist(log_return, bins=100, density=True)
bins = np.array(bins)
bins = bins[:-1]
fit_values, co_matrix = curve_fit(normal_dist, bins, count)
L, M, N = fit_values
plt.plot(bins, normal_dist(bins, L, M, N), '--r')
plt.show()
r_squared_l = []
for i in range(len(count)):
r_squared_l.append((normal_dist(bins[i],L,M,N) - count[i])**2)
R_squared = sum(r_squared_l)/len(r_squared_l) - 1
print(R_squared)
fig, ax = plt.subplots(figsize=(16, 9))
f = ft.fft(log_return)
s = f*np.conj(f)
c = ft.ifft(s).real
plt.plot(d_values,c)
plt.show()