In [1]:
%matplotlib inline
from matplotlib import pyplot as plt

In [21]:
import scipy.stats as stats

In [2]:
import pandas as pd
import numpy as np

In [3]:
import quantquotedaily as qqd

In [4]:
aapl = qqd.get_daily_data('aapl')

In [5]:
aapl.head(10)


Out[5]:
time open high low close volume
date
1998-01-02 0 3.31397 3.95098 3.28236 3.95098 24947201.10
1998-01-05 0 4.01177 4.02635 3.69325 3.89020 22344145.08
1998-01-06 0 3.87561 4.98432 3.58628 4.60502 63150252.55
1998-01-07 0 4.57341 4.68040 4.20871 4.24032 36978255.52
1998-01-08 0 4.24032 4.52965 4.11875 4.39107 27687622.95
1998-01-09 0 4.40808 4.71201 4.25491 4.43726 31624883.38
1998-01-12 0 4.24032 4.52965 4.16495 4.46886 18300352.79
1998-01-13 0 4.52965 4.77279 4.49804 4.76306 22182098.34
1998-01-14 0 4.83357 4.84816 4.68040 4.78738 20340344.31
1998-01-15 0 4.66581 4.80196 4.52965 4.66581 19157062.33

10 rows × 6 columns


In [7]:
aapl.close.plot()


Out[7]:
<matplotlib.axes.AxesSubplot at 0x10a3e7b10>

Returns


In [6]:
qqd.calc_returns(aapl);

In [7]:
aapl.r.hist(bins=50, range=(-0.2,0.2))


Out[7]:
<matplotlib.axes.AxesSubplot at 0x109b37b10>

In [8]:
aapl.lnr.hist(bins=100, range=(-0.2,0.2))


Out[8]:
<matplotlib.axes.AxesSubplot at 0x109b7fd10>

In [28]:
plt.scatter(aapl.r, aapl.lnr)
plt.plot([-1,1],[-1,1])
plt.xlim(-0.2,0.2)
plt.ylim(-0.5,0.5)


Out[28]:
(-0.5, 0.5)

In [33]:
stats.norm.fit(aapl.lnr.dropna())


Out[33]:
(0.0012089039986481202, 0.030832829289784307)

In [31]:
stats.norm.fit(aapl.r.dropna())


Out[31]:
(0.001671542449499148, 0.02976971381632253)

KDE


In [23]:
kde = stats.gaussian_kde(aapl.r.dropna())

In [35]:
new_data = kde.resample(5000)

In [36]:
new_data = new_data.reshape((5000,))

In [37]:
plt.hist(new_data, bins=50, range=(-0.2,0.2));


Resampling


In [20]:
aapl2 = qqd.resample(aapl, '1w')

In [12]:
aapl.head(10)


Out[12]:
time open high low close volume r lnr
date
1998-01-02 0 3.31397 3.95098 3.28236 3.95098 24947201.10 NaN NaN
1998-01-05 0 4.01177 4.02635 3.69325 3.89020 22344145.08 -0.015384 -0.015503
1998-01-06 0 3.87561 4.98432 3.58628 4.60502 63150252.55 0.183749 0.168686
1998-01-07 0 4.57341 4.68040 4.20871 4.24032 36978255.52 -0.079196 -0.082508
1998-01-08 0 4.24032 4.52965 4.11875 4.39107 27687622.95 0.035552 0.034934
1998-01-09 0 4.40808 4.71201 4.25491 4.43726 31624883.38 0.010519 0.010464
1998-01-12 0 4.24032 4.52965 4.16495 4.46886 18300352.79 0.007122 0.007096
1998-01-13 0 4.52965 4.77279 4.49804 4.76306 22182098.34 0.065833 0.063757
1998-01-14 0 4.83357 4.84816 4.68040 4.78738 20340344.31 0.005106 0.005093
1998-01-15 0 4.66581 4.80196 4.52965 4.66581 19157062.33 -0.025394 -0.025722

10 rows × 8 columns


In [21]:
aapl2.head()


Out[21]:
high close open low volume
date
1998-01-04 3.95098 3.95098 3.31397 3.28236 2.494720e+07
1998-01-11 4.98432 4.43726 4.01177 3.58628 1.817852e+08
1998-01-18 4.84816 4.55883 4.24032 4.16495 8.862352e+07
1998-01-25 4.80196 4.74118 4.63420 4.51263 3.514019e+07
1998-02-01 4.78738 4.46886 4.72659 4.43726 2.718831e+07

5 rows × 5 columns


In [22]:
aapl2.close.plot()


Out[22]:
<matplotlib.axes.AxesSubplot at 0x10ab35610>

In [32]:
d = aapl.groupby(aapl.index.year)

In [33]:
d.describe()


Out[33]:
time open high low close volume r lnr
1998 count 252 252.000000 252.000000 252.000000 252.000000 2.520000e+02 251.000000 251.000000
mean 0 7.419265 7.602371 7.240461 7.428672 1.567724e+07 0.004318 0.003687
std 0 1.648343 1.671362 1.592288 1.613596 1.122263e+07 0.035694 0.035247
min 0 3.313970 3.950980 3.282360 3.890200 3.816357e+06 -0.091417 -0.095869
25% 0 6.500283 6.625490 6.378713 6.503930 8.512158e+06 -0.017632 -0.017789
50% 0 7.430280 7.499580 7.279530 7.416910 1.282936e+07 0.003561 0.003554
75% 0 8.802797 8.996090 8.616790 8.771795 2.040769e+07 0.023264 0.022997
max 0 10.576500 10.637300 10.272600 10.454900 8.603934e+07 0.183749 0.168686
1999 count 252 252.000000 252.000000 252.000000 252.000000 2.520000e+02 252.000000 252.000000
mean 0 14.019418 14.369465 13.689033 14.042785 1.865969e+07 0.004321 0.003646
std 0 5.268781 5.417448 5.129673 5.296875 1.317318e+07 0.036711 0.036593
min 0 7.841190 8.145110 7.780400 7.794990 4.070945e+06 -0.124813 -0.133318
25% 0 9.961378 10.053175 9.721853 9.920018 1.075410e+07 -0.019225 -0.019412
50% 0 11.716750 12.035250 11.541700 11.951400 1.477826e+07 0.006494 0.006473
75% 0 17.434750 18.250500 17.031150 17.817700 2.149257e+07 0.026913 0.026557
max 0 28.340100 28.690300 27.717700 28.583300 1.138997e+08 0.143592 0.134174
2000 count 252 252.000000 252.000000 252.000000 252.000000 2.520000e+02 252.000000 252.000000
mean 0 22.266540 22.979985 21.582661 22.226876 1.612676e+07 -0.003134 -0.004936
std 0 8.123415 8.411349 7.931134 8.201387 1.690185e+07 0.054636 0.063506
min 0 6.700870 7.114210 6.627930 6.807850 2.473292e+06 -0.518150 -0.730122
25% 0 13.455250 13.826050 12.113050 12.346525 9.381832e+06 -0.032777 -0.033326
50% 0 25.279000 25.954900 24.413500 25.120950 1.315161e+07 -0.001169 -0.001170
75% 0 28.010650 28.933400 27.379750 27.938975 1.732548e+07 0.027596 0.027222
max 0 34.632500 36.563000 34.221600 35.011800 2.339738e+08 0.136189 0.127680
2001 count 248 248.000000 248.000000 248.000000 248.000000 2.480000e+02 248.000000 248.000000
mean 0 9.806657 10.091759 9.541758 9.831893 1.291858e+07 0.002354 0.001588
std 0 1.221303 1.231999 1.174382 1.207529 7.241276e+06 0.039250 0.039131
min 0 7.021810 7.415700 7.021810 7.264950 1.795692e+06 -0.170786 -0.187277
25% 0 8.996090 9.281775 8.807655 9.038635 8.597192e+06 -0.022850 -0.023115
50% 0 9.684170 9.997790 9.484795 9.727935 1.109640e+07 -0.000956 -0.000956
75% 0 10.542475 10.966725 10.264125 10.566750 1.529788e+07 0.024569 0.024272
max 0 12.964100 13.187800 12.526500 12.930000 6.332733e+07 0.126309 0.118946
2002 count 252 252.000000 252.000000 252.000000 252.000000 2.520000e+02 252.000000 252.000000
mean 0 9.301611 9.491722 9.104130 9.304332 1.024220e+07 -0.001245 -0.001694
std 0 2.086215 2.123877 2.043067 2.096483 5.521571e+06 0.029797 0.030082
min 0 6.584170 6.734910 6.496630 6.618200 1.437792e+06 -0.149379 -0.161788
25% 0 7.240638 7.386520 7.093535 7.258868 7.197468e+06 -0.017522 -0.017678
50% 0 8.419855 8.643535 8.203460 8.405260 8.987814e+06 -0.004435 -0.004445
75% 0 11.527125 11.812825 11.310700 11.572100 1.198920e+07 0.017719 0.017564
max 0 12.609100 12.720900 12.341600 12.720900 5.893970e+07 0.084116 0.080765
2003 count 252 252.000000 252.000000 252.000000 252.000000 2.520000e+02 252.000000 252.000000
mean 0 9.008940 9.160747 8.855108 9.015578 9.450008e+06 0.001850 0.001588
std 0 1.608430 1.629331 1.576092 1.608173 6.560157e+06 0.022987 0.022843
min 0 6.316710 6.413970 6.185420 6.370200 2.583868e+06 -0.080139 -0.083532
25% 0 7.150678 7.267378 7.040045 7.147025 6.121533e+06 -0.012786 -0.012868
50% 0 9.453190 9.569895 9.239225 9.370515 7.745569e+06 0.001927 0.001925
75% 0 10.294400 10.431850 10.047700 10.243425 1.047963e+07 0.014126 0.014027
max 0 12.079000 12.161700 11.952700 12.059600 5.271461e+07 0.112803 0.106882
2004 count 252 252.000000 252.000000 252.000000 252.000000 2.520000e+02 252.000000 252.000000
mean 0 17.219969 17.518462 16.984854 17.275673 1.623744e+07 0.004691 0.004373
std 0 6.427732 6.507148 6.309313 6.408346 1.211499e+07 0.025204 0.024721
min 0 10.416000 10.576500 10.304200 10.323600 4.495727e+06 -0.056557 -0.058219
25% 0 12.990875 13.207225 12.787825 13.024900 9.312342e+06 -0.008151 -0.008185
50% 0 14.921300 15.208250 14.760900 14.987000 1.261312e+07 0.002560 0.002556
75% 0 18.779950 18.968375 18.600025 18.793300 1.781018e+07 0.015223 0.015109
max 0 33.553000 33.830100 32.779800 33.280700 9.282869e+07 0.132460 0.124392
2005 count 252 252.000000 252.000000 252.000000 252.000000 2.520000e+02 252.000000 252.000000
mean 0 45.351764 45.996662 44.703306 45.384723 2.485658e+07 0.003487 0.003187
std 0 11.066300 11.165470 10.993105 11.113568 1.561800e+07 0.024379 0.024331
min 0 31.024400 31.564100 30.440800 30.771500 5.881751e+06 -0.092548 -0.097114
... ... ... ... ... ... ... ...

128 rows × 8 columns


In [ ]: