Skip to content

Commit f8801a1

Browse files
authored
Added tutorial files
1 parent b744566 commit f8801a1

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

Tutorials/Beta Tutorial/Beta.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# import libraries
4+
import pandas as pd
5+
import statsmodels.api as sm
6+
7+
'''
8+
Download monthly prices of Facebook and S&P 500 index from 2014 to 2017
9+
CSV file downloaded from Yahoo File
10+
start period: 02/11/2014
11+
end period: 30/11/2014
12+
period format: DD/MM/YEAR
13+
'''
14+
fb = pd.read_csv('FB.csv', parse_dates=True, index_col='Date',)
15+
sp_500 = pd.read_csv('^GSPC.csv', parse_dates=True, index_col='Date')
16+
17+
# joining the closing prices of the two datasets
18+
monthly_prices = pd.concat([fb['Close'], sp_500['Close']], axis=1)
19+
monthly_prices.columns = ['FB', '^GSPC']
20+
21+
# check the head of the dataframe
22+
print(monthly_prices.head())
23+
24+
# calculate monthly returns
25+
monthly_returns = monthly_prices.pct_change(1)
26+
clean_monthly_returns = monthly_returns.dropna(axis=0)
27+
28+
# split dependent and independent variable
29+
X = clean_monthly_returns['^GSPC']
30+
y = clean_monthly_returns['FB']
31+
32+
# Add a constant to the independent value
33+
X1 = sm.add_constant(X)
34+
35+
# make regression model
36+
model = sm.OLS(y, X1)
37+
38+
# fit model and print results
39+
results = model.fit()
40+
print(results.summary())
41+
42+
# alternatively scipy linear regression
43+
from scipy import stats
44+
slope, intercept, r_value, p_value, std_err = stats.linregress(X, y)
45+
print(slope)
46+
47+
# save OLS Regression Results as a PNG
48+
import matplotlib.pyplot as plt
49+
plt.rc('figure', figsize=(12, 7))
50+
plt.text(0.01, 0.05, str(results.summary()), {'fontsize': 10}, fontproperties = 'monospace')
51+
plt.axis('off')
52+
plt.tight_layout()
53+
plt.savefig('output.png')

Tutorials/Beta Tutorial/FB.csv

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Date,Open,High,Low,Close,Adj Close,Volume
2+
2014-11-01,75.470001,78.269997,72.510002,77.699997,77.699997,490965300
3+
2014-12-01,77.260002,82.169998,74.400002,78.019997,78.019997,534632100
4+
2015-01-01,78.580002,79.250000,73.540001,75.910004,75.910004,545698200
5+
2015-02-01,76.110001,81.370003,73.449997,78.970001,78.970001,474720300
6+
2015-03-01,79.000000,86.070000,77.260002,82.220001,82.220001,574223800
7+
2015-04-01,82.500000,85.589996,78.320000,78.769997,78.769997,541741400
8+
2015-05-01,79.239998,81.849998,76.790001,79.190002,79.190002,421283500
9+
2015-06-01,79.300003,89.400002,78.660004,85.769997,85.769997,537883500
10+
2015-07-01,86.769997,99.239998,85.230003,94.010002,94.010002,790781000
11+
2015-08-01,93.529999,98.739998,72.000000,89.730003,89.730003,794774200
12+
2015-09-01,86.849998,96.489998,85.720001,89.900002,89.900002,634526500
13+
2015-10-01,90.050003,105.120003,88.360001,101.970001,101.970001,571192300
14+
2015-11-01,101.720001,110.650002,100.470001,104.239998,104.239998,542369900
15+
2015-12-01,104.830002,107.919998,101.459999,104.660004,104.660004,440059600
16+
2016-01-01,101.949997,112.839996,89.370003,112.209999,112.209999,792410100
17+
2016-02-01,112.269997,117.589996,96.820000,106.919998,106.919998,873671800
18+
2016-03-01,107.830002,116.989998,104.400002,114.099998,114.099998,521306200
19+
2016-04-01,113.750000,120.790001,106.309998,117.580002,117.580002,741132500
20+
2016-05-01,117.830002,121.080002,115.879997,118.809998,118.809998,459696300
21+
2016-06-01,118.500000,119.440002,108.230003,114.279999,114.279999,450837700
22+
2016-07-01,114.199997,128.330002,112.970001,123.940002,123.940002,464098700
23+
2016-08-01,123.849998,126.730003,122.070000,126.120003,126.120003,365460600
24+
2016-09-01,126.379997,131.979996,125.599998,128.270004,128.270004,376355700
25+
2016-10-01,128.380005,133.500000,126.750000,130.990005,130.990005,313200300
26+
2016-11-01,131.410004,131.940002,113.550003,118.419998,118.419998,643698100
27+
2016-12-01,118.379997,122.500000,114.000000,115.050003,115.050003,408552500
28+
2017-01-01,116.029999,133.139999,115.510002,130.320007,130.320007,378873600
29+
2017-02-01,132.250000,137.179993,130.300003,135.539993,135.539993,384675700
30+
2017-03-01,136.470001,142.949997,136.080002,142.050003,142.050003,342098600
31+
2017-04-01,141.929993,151.529999,138.809998,150.250000,150.250000,275502100
32+
2017-05-01,151.740005,153.600006,144.419998,151.460007,151.460007,403586900
33+
2017-06-01,151.750000,156.500000,144.559998,150.979996,150.979996,414677400
34+
2017-07-01,151.720001,175.490005,147.800003,169.250000,169.250000,419907600
35+
2017-08-01,169.820007,173.050003,165.000000,171.970001,171.970001,303622800
36+
2017-09-01,172.399994,174.000000,161.559998,170.869995,170.869995,304818300
37+
2017-10-01,171.389999,180.800003,168.289993,180.059998,180.059998,318146600
38+
2017-11-01,182.360001,184.250000,174.000000,177.179993,177.179993,350998700

Tutorials/Beta Tutorial/^GSPC.csv

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Date,Open,High,Low,Close,Adj Close,Volume
2+
2014-11-01,2018.209961,2075.760010,2001.010010,2067.560059,2067.560059,63600190000
3+
2014-12-01,2065.780029,2093.550049,1972.560059,2058.899902,2058.899902,80743820000
4+
2015-01-01,2058.899902,2072.360107,1988.119995,1994.989990,1994.989990,77330040000
5+
2015-02-01,1996.670044,2119.590088,1980.900024,2104.500000,2104.500000,68775560000
6+
2015-03-01,2105.229980,2117.520020,2039.689941,2067.889893,2067.889893,76675850000
7+
2015-04-01,2067.629883,2125.919922,2048.379883,2085.510010,2085.510010,72060940000
8+
2015-05-01,2087.379883,2134.719971,2067.929932,2107.389893,2107.389893,65187730000
9+
2015-06-01,2108.639893,2129.870117,2056.320068,2063.110107,2063.110107,73213980000
10+
2015-07-01,2067.000000,2132.820068,2044.020020,2103.840088,2103.840088,77920590000
11+
2015-08-01,2104.489990,2112.659912,1867.010010,1972.180054,1972.180054,84626790000
12+
2015-09-01,1970.089966,2020.859985,1871.910034,1920.030029,1920.030029,79989370000
13+
2015-10-01,1919.650024,2094.320068,1893.699951,2079.360107,2079.360107,85844900000
14+
2015-11-01,2080.760010,2116.479980,2019.390015,2080.409912,2080.409912,75943590000
15+
2015-12-01,2082.929932,2104.270020,1993.260010,2043.939941,2043.939941,83649260000
16+
2016-01-01,2038.199951,2038.199951,1812.290039,1940.239990,1940.239990,92409770000
17+
2016-02-01,1936.939941,1962.959961,1810.099976,1932.229980,1932.229980,93049560000
18+
2016-03-01,1937.089966,2072.209961,1937.089966,2059.739990,2059.739990,92639420000
19+
2016-04-01,2056.620117,2111.050049,2033.800049,2065.300049,2065.300049,81124990000
20+
2016-05-01,2067.169922,2103.479980,2025.910034,2096.949951,2096.949951,78883600000
21+
2016-06-01,2093.939941,2120.550049,1991.680054,2098.860107,2098.860107,86852700000
22+
2016-07-01,2099.340088,2177.090088,2074.020020,2173.600098,2173.600098,69530250000
23+
2016-08-01,2173.149902,2193.810059,2147.580078,2170.949951,2170.949951,75610310000
24+
2016-09-01,2171.330078,2187.870117,2119.120117,2168.270020,2168.270020,77270240000
25+
2016-10-01,2164.330078,2169.600098,2114.719971,2126.149902,2126.149902,73196630000
26+
2016-11-01,2128.679932,2214.100098,2083.790039,2198.810059,2198.810059,88299760000
27+
2016-12-01,2200.169922,2277.530029,2187.439941,2238.830078,2238.830078,75251240000
28+
2017-01-01,2251.570068,2300.989990,2245.129883,2278.870117,2278.870117,70483180000
29+
2017-02-01,2285.590088,2371.540039,2271.649902,2363.639893,2363.639893,69162420000
30+
2017-03-01,2380.129883,2400.979980,2322.250000,2362.719971,2362.719971,81547770000
31+
2017-04-01,2362.340088,2398.159912,2328.949951,2384.199951,2384.199951,65265670000
32+
2017-05-01,2388.500000,2418.709961,2352.719971,2411.800049,2411.800049,79607170000
33+
2017-06-01,2415.649902,2453.820068,2405.699951,2423.409912,2423.409912,81002490000
34+
2017-07-01,2431.389893,2484.040039,2407.699951,2470.300049,2470.300049,63169400000
35+
2017-08-01,2477.100098,2490.870117,2417.350098,2471.649902,2471.649902,70616030000
36+
2017-09-01,2474.419922,2519.439941,2446.550049,2519.360107,2519.360107,66337980000
37+
2017-10-01,2521.199951,2582.979980,2520.399902,2575.260010,2575.260010,70871570000
38+
2017-11-01,2583.209961,2657.739990,2557.449951,2647.580078,2647.580078,73173260000

Tutorials/Beta Tutorial/output.png

40.5 KB
Loading

0 commit comments

Comments
 (0)