Portfolio simulation summary

In [1]:
import pickle
from simple_back_test import *

import pandas as pd
import numpy as np
In [6]:
ff = "../../data/portfolio_simulations_many.pickle"
runs_per_cap = pickle.load( open(ff, 'rb'))
runs_per_cap = runs_per_cap[3]
runs_per_cap.ps.portfolio_summary
Out[6]:
average_positions average_position_size average_portfolio_return portfolio_std cumulative_return total_number_tracks max_drawdown sharpe annualised_ret annual_volatility sortino downside_risk tail_risk
0 33.839479 306983.384995 0.000467 0.001877 0.238642 156 -0.01027 3.949519 0.124107 0.029804 11.876812 0.009911 3.376873
In [3]:
all_summaries = convert_to_summary_df(runs_per_cap)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-c00a5e9cf47e> in <module>()
      2 runs_per_cap = pickle.load( open(ff, 'rb'))
      3 
----> 4 all_summaries = convert_to_summary_df(runs_per_cap)

~/workspace/whitelist/thundr/datascience/modelling/backtesting/simple_back_test.py in convert_to_summary_df(runs)
    104 
    105 def convert_to_summary_df(runs):
--> 106     aa = list(itertools.chain.from_iterable(runs.values()))
    107 
    108     bb = pd.concat(list(map(convert_df, aa)), ignore_index=True)

AttributeError: 'list' object has no attribute 'values'
In [16]:
all_summaries.head()
all_summaries['cc'] = all_summaries['capital'].astype('category')
In [17]:
all_summaries.groupby('cc').apply( lambda x: display(x.describe()))
average_positions average_position_size average_portfolio_return portfolio_std cumulative_return max_drawdown sharpe annualised_ret annual_volatility sortino downside_risk tail_risk capital
count 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 7.000000 100.0
mean 11.227572 76261.179200 0.003186 0.029285 2.923035 -0.112575 1.738981 1.067099 0.464886 12.285392 0.087883 inf 50000.0
std 2.387138 16357.811663 0.000859 0.005941 1.334308 0.054738 0.424342 0.390467 0.094315 9.728410 0.037878 NaN 0.0
min 6.881720 46324.010802 0.000388 0.017816 0.090291 -0.279671 0.310099 0.047962 0.282812 0.459498 0.017895 inf 50000.0
25% 9.677419 64819.824511 0.002654 0.025122 1.935018 -0.142298 1.478694 0.792317 0.398802 6.436859 0.062798 inf 50000.0
50% 10.967742 74948.548828 0.003254 0.028624 2.854072 -0.109124 1.740068 1.077449 0.454388 10.405729 0.083313 NaN 50000.0
75% 12.903226 86202.950005 0.003707 0.031923 3.601407 -0.071364 2.003689 1.286883 0.506764 15.333557 0.111164 inf 50000.0
max 17.204301 119485.022168 0.005179 0.049274 6.619313 -0.018933 3.038153 2.005671 0.782194 72.937218 0.212767 inf 50000.0
average_positions average_position_size average_portfolio_return portfolio_std cumulative_return max_drawdown sharpe annualised_ret annual_volatility sortino downside_risk tail_risk capital
count 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 7.000000 100.0
mean 11.227572 76261.179200 0.003186 0.029285 2.923035 -0.112575 1.738981 1.067099 0.464886 12.285392 0.087883 inf 50000.0
std 2.387138 16357.811663 0.000859 0.005941 1.334308 0.054738 0.424342 0.390467 0.094315 9.728410 0.037878 NaN 0.0
min 6.881720 46324.010802 0.000388 0.017816 0.090291 -0.279671 0.310099 0.047962 0.282812 0.459498 0.017895 inf 50000.0
25% 9.677419 64819.824511 0.002654 0.025122 1.935018 -0.142298 1.478694 0.792317 0.398802 6.436859 0.062798 inf 50000.0
50% 10.967742 74948.548828 0.003254 0.028624 2.854072 -0.109124 1.740068 1.077449 0.454388 10.405729 0.083313 NaN 50000.0
75% 12.903226 86202.950005 0.003707 0.031923 3.601407 -0.071364 2.003689 1.286883 0.506764 15.333557 0.111164 inf 50000.0
max 17.204301 119485.022168 0.005179 0.049274 6.619313 -0.018933 3.038153 2.005671 0.782194 72.937218 0.212767 inf 50000.0
average_positions average_position_size average_portfolio_return portfolio_std cumulative_return max_drawdown sharpe annualised_ret annual_volatility sortino downside_risk tail_risk capital
count 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 85.000000 100.0
mean 22.095755 152105.583576 0.003102 0.020401 2.997059 -0.075536 2.438061 1.099897 0.323859 17.271529 0.060525 inf 100000.0
std 4.202118 29768.122340 0.000614 0.003056 1.099452 0.055530 0.470058 0.312612 0.048510 11.947575 0.028424 NaN 0.0
min 10.967742 75796.184773 0.001644 0.014937 0.913678 -0.344750 1.117171 0.421526 0.237123 2.690172 0.015151 inf 100000.0
25% 19.386123 133127.178638 0.002590 0.018518 2.066658 -0.094928 2.085404 0.835444 0.293967 9.897639 0.042804 NaN 100000.0
50% 21.935484 151008.499414 0.003170 0.020063 2.992918 -0.060960 2.457380 1.117651 0.318490 13.571511 0.059796 NaN 100000.0
75% 24.354839 169166.858804 0.003417 0.021696 3.471873 -0.042117 2.789298 1.251763 0.344417 21.010622 0.071644 NaN 100000.0
max 34.838710 243069.963378 0.004669 0.032978 6.175693 -0.015566 3.491875 1.909531 0.523504 61.634420 0.169604 inf 100000.0
average_positions average_position_size average_portfolio_return portfolio_std cumulative_return max_drawdown sharpe annualised_ret annual_volatility sortino downside_risk tail_risk capital
count 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 1.000000e+02 100.0
mean 57.014906 397606.320751 0.003110 0.012709 3.125492 -0.037299 3.894212 1.149219 0.201744 23.192545 0.037259 inf 250000.0
std 6.076068 42456.304857 0.000352 0.000990 0.660183 0.017999 0.423458 0.186585 0.015720 8.662841 0.010520 NaN 0.0
min 39.784946 280732.172990 0.002077 0.010466 1.530757 -0.122648 2.636441 0.654001 0.166145 8.166482 0.017061 6.247094e+00 250000.0
25% 53.118280 370699.642483 0.002867 0.012096 2.637426 -0.044308 3.607025 1.013326 0.192012 16.905889 0.030618 7.239463e+01 250000.0
50% 56.881720 397630.079189 0.003123 0.012659 3.091242 -0.032778 3.877471 1.145784 0.200956 22.243098 0.035643 1.149488e+14 250000.0
75% 60.752688 424563.055292 0.003352 0.013125 3.550711 -0.026424 4.179165 1.273194 0.208349 27.362399 0.043842 2.340766e+14 250000.0
max 69.462366 484395.413443 0.003892 0.016192 4.841764 -0.010759 4.760423 1.602660 0.257042 54.078001 0.067758 inf 250000.0
average_positions average_position_size average_portfolio_return portfolio_std cumulative_return max_drawdown sharpe annualised_ret annual_volatility sortino downside_risk tail_risk capital
count 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.000000 100.0
mean 108.142173 757858.917202 0.003004 0.008665 2.974238 -0.024289 5.517803 1.109975 0.137545 29.262258 0.027377 8.434089 500000.0
std 5.975605 42080.579071 0.000225 0.000540 0.414133 0.009613 0.447654 0.118912 0.008571 8.139811 0.005945 3.278267 0.0
min 94.838710 657017.077313 0.002479 0.007387 2.106593 -0.067696 4.318615 0.848359 0.117259 14.526830 0.014125 4.174481 500000.0
25% 104.731183 733825.599755 0.002865 0.008225 2.718569 -0.027682 5.259526 1.037539 0.130569 23.953173 0.023157 6.215086 500000.0
50% 107.982749 757549.169910 0.003017 0.008654 2.971819 -0.022286 5.522452 1.114192 0.137384 27.795763 0.027407 7.598986 500000.0
75% 112.365591 784202.891142 0.003140 0.009025 3.212689 -0.018426 5.836667 1.180731 0.143268 34.350296 0.030205 9.980531 500000.0
max 123.870968 878138.554750 0.003627 0.010137 4.252630 -0.010073 6.492623 1.461723 0.160922 62.807220 0.048422 25.779303 500000.0
Out[17]:
In [14]:
from plotnine import *
In [21]:
ggplot(all_summaries, aes('annualised_ret')) + geom_density(aes(color = 'cc'), alpha = 0.5)
/home/sergiusz/anaconda3/envs/whitelist/lib/python3.6/site-packages/plotnine/utils.py:281: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  ndistinct = ids.apply(len_unique, axis=0).as_matrix()
/home/sergiusz/anaconda3/envs/whitelist/lib/python3.6/site-packages/pandas/core/generic.py:4384: FutureWarning: Attribute 'is_copy' is deprecated and will be removed in a future version.
  object.__getattribute__(self, name)
/home/sergiusz/anaconda3/envs/whitelist/lib/python3.6/site-packages/pandas/core/generic.py:4385: FutureWarning: Attribute 'is_copy' is deprecated and will be removed in a future version.
  return object.__setattr__(self, name, value)
Out[21]:
<ggplot: (8747888107882)>
In [22]:
ggplot(all_summaries, aes('annualised_ret')) + geom_histogram(aes(fill = 'cc')) + facet_wrap('~cc')
/home/sergiusz/anaconda3/envs/whitelist/lib/python3.6/site-packages/plotnine/utils.py:281: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  ndistinct = ids.apply(len_unique, axis=0).as_matrix()
/home/sergiusz/anaconda3/envs/whitelist/lib/python3.6/site-packages/plotnine/stats/stat_bin.py:90: UserWarning: 'stat_bin()' using 'bins = 27'. Pick better value with 'binwidth'.
  warn(msg.format(params['bins']))
/home/sergiusz/anaconda3/envs/whitelist/lib/python3.6/site-packages/pandas/core/generic.py:4384: FutureWarning: Attribute 'is_copy' is deprecated and will be removed in a future version.
  object.__getattribute__(self, name)
/home/sergiusz/anaconda3/envs/whitelist/lib/python3.6/site-packages/pandas/core/generic.py:4385: FutureWarning: Attribute 'is_copy' is deprecated and will be removed in a future version.
  return object.__setattr__(self, name, value)
/home/sergiusz/anaconda3/envs/whitelist/lib/python3.6/site-packages/plotnine/positions/position.py:188: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  intervals = data[xminmax].drop_duplicates().as_matrix().flatten()
Out[22]:
<ggplot: (-9223363289036985933)>