There are also a few more advanced (and still experimental) analysis methods in pyfolio based on Bayesian statistics.
The main benefit of these methods is uncertainty quantification. All the values you saw above, like the Sharpe ratio, are just single numbers. These estimates are noisy because they have been computed over a limited number of data points. So how much can you trust these numbers? You don't know because there is no sense of uncertainty. That is where Bayesian statistics helps as instead of single values, we are dealing with probability distributions that assign degrees of belief to all possible parameter values.
Lets create the Bayesian tear sheet. Under the hood this is running MCMC sampling in PyMC3
to estimate the posteriors which can take quite a while (that's the reason why we don't generate this by default in create_full_tear_sheet()
).
In [1]:
%matplotlib inline
import pyfolio as pf
In [2]:
#stock_rets = pf.utils.get_symbol_rets('FB')
import pandas as pd
stock_rets = pd.read_pickle('fb.pickle')
In [3]:
out_of_sample = stock_rets.index[-40]
In [4]:
%pdb
In [5]:
pf.create_bayesian_tear_sheet(stock_rets, live_start_date=out_of_sample)
Lets go through these row by row:
In [6]:
help(pf.bayesian.run_model)
For example, to run a model that assumes returns to be normally distributed, you can call:
In [7]:
# Run model that assumes returns to be T-distributed
trace = pf.bayesian.run_model('t', stock_rets)
The returned trace object can be directly inquired. For example might we ask what the probability of the Sharpe ratio being larger than 0 is by checking what percentage of posterior samples of the Sharpe ratio are > 0:
In [8]:
# Check what frequency of samples from the sharpe posterior are above 0.
print('Probability of Sharpe ratio > 0 = {:3}%'.format((trace['sharpe'] > 0).mean() * 100))
But we can also interact with it like with any other pymc3
trace:
In [9]:
import pymc3 as pm
pm.traceplot(trace);
For more information on Bayesian statistics, check out these resources: