In [1]:
%matplotlib inline
import numpy as np
import pandas as pd
#import pandas_datareader.data as web # Not a dependency, but we'll need it now.
In [2]:
import benford as bf
In [5]:
sp = pd.read_csv('data/SPY.csv', index_col='Date', parse_dates=True)
In [5]:
#adding '_' to facilitate handling the column
#sp.rename(columns={'Adj Close':'Adj_Close'}, inplace=True)
sp['p_r'] = sp.Close/sp.Close.shift()-1 #simple returns
sp['l_r'] = np.log(sp.Close/sp.Close.shift()) #log returns
sp.tail()
Out[5]:
In [6]:
f1d = bf.first_digits(sp.l_r, digs=1, decimals=8) # digs=1 for the first digit (1-9)
In [7]:
f1d
Out[7]:
In [8]:
f2d = bf.first_digits(sp.l_r, digs=2, decimals=8) # Note the parameter digs=2!
In [9]:
f2d.head()
Out[9]:
In [10]:
f2d.tail()
Out[10]:
In [11]:
# For a significance of 5%, a confidence of 95
f2d = bf.first_digits(sp.l_r, digs=2, decimals=8, confidence=95)
In [12]:
# First Three Digits Test, now with 99% confidence level
# digs=3 for the first three digits
f3d = bf.first_digits(sp.l_r, digs=3, decimals=8, confidence=99)
In [13]:
# The First Three Digits plot is better seen and zoomed in and out without the inline plotting.
# Try %matplotlib
In [14]:
# Second Digit Test
sd = bf.second_digit(sp.l_r, decimals=8, confidence=95)
In [16]:
# Last Two Digits Test
l2d = bf.last_two_digits(sp.l_r, decimals=8, confidence=90)
In [17]:
mad1 = bf.mad(sp.l_r, test=1, decimals=8) # test=1 : MAD for the First Digits
mad1
Out[17]:
In [18]:
mad2 = bf.mad(sp.l_r, test=2, decimals=8) # test=2 : MAD for the First Two Digits
mad2
Out[18]:
In [19]:
mad3 = bf.mad(sp.l_r, test=3, decimals=8) # test=3 : MAD for the First Three Digits
mad3
Out[19]:
In [20]:
mad_sd = bf.mad(sp.l_r, test=22, decimals=8) # test=22 : MAD for the Second Digits
mad_sd
Out[20]:
In [21]:
mad_l2d = bf.mad(sp.l_r, test=-2, decimals=8) # test=-2 : MAD for the Last Two Digits
mad_l2d
Out[21]:
In [22]:
f2d = bf.first_digits(sp.l_r, digs=2, decimals=8, MAD=True, show_plot=False)
In [23]:
sd = bf.second_digit(sp.l_r, decimals=8, MAD=True, show_plot=False)
In [24]:
mant = bf.mantissas(sp.l_r, inform=True, show_plot=True)
In [25]:
mant.hist(bins=30, figsize=(12,5))
Out[25]:
In [ ]: