-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathestimating_returns.py
More file actions
45 lines (39 loc) · 1.46 KB
/
Copy pathestimating_returns.py
File metadata and controls
45 lines (39 loc) · 1.46 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
# Import necessary libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
import datetime as dt
from pandas_datareader import DataReader
# Set parameters
stock_ticker = 'AAPL'
higher_bound = 0.3
lower_bound = 0.2
num_of_years = 40
# Set start and end dates for data
end_date = dt.datetime.now()
start_date = end_date - dt.timedelta(days=int(365.25 * num_of_years))
# Retrieve data from Yahoo Finance
stock_data = DataReader(stock_ticker, 'yahoo', start_date, end_date)
stock_data = stock_data.reset_index()
# Extract stock returns
open_prices = stock_data['Open'].tolist()
open_prices = open_prices[::253] # take every 253rd value starting from the first
df_returns = pd.DataFrame({'Open': open_prices})
df_returns['Return'] = df_returns['Open'].pct_change()
df_returns.dropna(inplace=True)
returns = df_returns['Return'].tolist()
# Plot normal distribution
x = np.linspace(min(returns), 1, 100)
mean = np.mean(returns)
std = np.std(returns)
norm_dist = norm.pdf(x, mean, std)
plt.plot(x, norm_dist)
plt.title(f'Normal Distribution of Returns for {stock_ticker.upper()}')
plt.xlabel('Returns')
plt.ylabel('Frequency')
plt.show()
# Estimate the probability of returns falling between lower_bound and higher_bound
norm_cdf = norm(mean, std).cdf
prob = round(norm_cdf(higher_bound) - norm_cdf(lower_bound), 4)
print(f'The probability of returns falling between {lower_bound} and {higher_bound} for {stock_ticker.upper()} is: {prob}')