Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
polakowo
GitHub Repository: polakowo/vectorbt
Path: blob/master/tests/notebooks/returns.ipynb
1073 views
Kernel: Python 3

returns

import vectorbt as vbt
import numpy as np import pandas as pd from datetime import datetime, timedelta from numba import njit import empyrical
/Users/olegpolakow/miniconda3/lib/python3.7/site-packages/pandas_datareader/compat/__init__.py:7: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead. from pandas.util.testing import assert_frame_equal /Users/olegpolakow/miniconda3/lib/python3.7/site-packages/empyrical/utils.py:32: UserWarning: Unable to import pandas_datareader. Suppressing import error and continuing. All data reading functionality will raise errors; but has been deprecated and will be removed in a later version. warnings.warn(msg)
# Disable caching for performance testing vbt.settings.caching['enabled'] = False vbt.settings.returns['year_freq'] = '252 days' # same as empyrical
index = pd.DatetimeIndex([ datetime(2018, 1, 1), datetime(2018, 1, 2), datetime(2018, 1, 3), datetime(2018, 1, 4), datetime(2018, 1, 5) ], freq='D') columns = ['a', 'b', 'c'] ts = pd.DataFrame({ 'a': [1, 2, 3, 4, 5], 'b': [5, 4, 3, 2, 1], 'c': [1, 2, 3, 2, 1] }, index=index).astype(np.float32) print(ts)
a b c 2018-01-01 1.0 5.0 1.0 2018-01-02 2.0 4.0 2.0 2018-01-03 3.0 3.0 3.0 2018-01-04 4.0 2.0 2.0 2018-01-05 5.0 1.0 1.0
big_ts = pd.DataFrame(np.random.randint(1, 10, size=(1000, 1000)).astype(float)) big_ts.index = [datetime(2018, 1, 1) + timedelta(days=i) for i in range(1000)] big_ts.shape
(1000, 1000)
returns = ts.vbt.pct_change() print(returns) big_returns = big_ts.vbt.pct_change()
a b c 2018-01-01 NaN NaN NaN 2018-01-02 1.000000 -0.200000 1.000000 2018-01-03 0.500000 -0.250000 0.500000 2018-01-04 0.333333 -0.333333 -0.333333 2018-01-05 0.250000 -0.500000 -0.500000
np.random.seed(42) benchmark_rets = returns['a'] * np.random.uniform(0.8, 1.2, returns.shape[0]) big_benchmark_rets = big_returns[0] * np.random.uniform(0.8, 1.2, big_returns.shape[0])
# Test year frequency print(returns.vbt.returns.year_freq) print(returns['a'].vbt.returns.year_freq) print(returns.vbt.returns(year_freq='252 days').year_freq) print(returns['a'].vbt.returns(year_freq='252 days').year_freq)
252 days 00:00:00 252 days 00:00:00 252 days 00:00:00 252 days 00:00:00
print(returns.vbt.returns.ann_factor) # default print(returns.vbt.returns(year_freq='252 days').ann_factor)
252.0 252.0
print(returns['a'].vbt.returns.daily()) # already daily, do nothing print(returns.vbt.returns.daily()) %timeit big_returns.vbt.returns.daily()
2018-01-01 NaN 2018-01-02 1.000000 2018-01-03 0.500000 2018-01-04 0.333333 2018-01-05 0.250000 Freq: D, Name: a, dtype: float64 a b c 2018-01-01 NaN NaN NaN 2018-01-02 1.000000 -0.200000 1.000000 2018-01-03 0.500000 -0.250000 0.500000 2018-01-04 0.333333 -0.333333 -0.333333 2018-01-05 0.250000 -0.500000 -0.500000 The slowest run took 24.92 times longer than the fastest. This could mean that an intermediate result is being cached. 56 µs ± 106 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(returns['a'].vbt.returns.annual()) print(returns.vbt.returns.annual()) %timeit big_returns.vbt.returns.annual()
2018-01-01 4.0 Freq: 252D, Name: a, dtype: float64 a b c 2018-01-01 4.0 -0.8 2.980232e-08 5.96 ms ± 69.6 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.cum_returns(returns['a'])) %timeit empyrical.cum_returns(big_returns[0]) print(returns['a'].vbt.returns.cumulative()) %timeit big_returns[0].vbt.returns.cumulative() print(returns.vbt.returns.cumulative()) print(returns.vbt.returns.cumulative(start_value=1)) %timeit big_returns.vbt.returns.cumulative()
2018-01-01 0.0 2018-01-02 1.0 2018-01-03 2.0 2018-01-04 3.0 2018-01-05 4.0 Freq: D, dtype: float64 963 µs ± 24.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 2018-01-01 0.0 2018-01-02 1.0 2018-01-03 2.0 2018-01-04 3.0 2018-01-05 4.0 Freq: D, Name: a, dtype: float64 The slowest run took 10.08 times longer than the fastest. This could mean that an intermediate result is being cached. 161 µs ± 220 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 0.0 0.0 0.000000e+00 2018-01-02 1.0 -0.2 1.000000e+00 2018-01-03 2.0 -0.4 2.000000e+00 2018-01-04 3.0 -0.6 1.000000e+00 2018-01-05 4.0 -0.8 2.980232e-08 a b c 2018-01-01 1.0 1.0 1.0 2018-01-02 2.0 0.8 2.0 2018-01-03 3.0 0.6 3.0 2018-01-04 4.0 0.4 2.0 2018-01-05 5.0 0.2 1.0 4.31 ms ± 308 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.cum_returns_final(returns['a'])) %timeit empyrical.cum_returns_final(big_returns[0]) print(returns['a'].vbt.returns.total()) %timeit big_returns[0].vbt.returns.total() print(returns.vbt.returns.total()) %timeit big_returns.vbt.returns.total() print(returns.vbt.returns.rolling_total(3, minp=1)) %timeit big_returns.vbt.returns.rolling_total(3, minp=1)
4.000000149011612 84.9 µs ± 10.8 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 4.000000149011612 The slowest run took 7.79 times longer than the fastest. This could mean that an intermediate result is being cached. 124 µs ± 145 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a 4.000000e+00 b -8.000000e-01 c 2.980232e-08 Name: total_return, dtype: float64 3.31 ms ± 74.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 1.0 -0.20 1.0 2018-01-03 2.0 -0.40 2.0 2018-01-04 3.0 -0.60 1.0 2018-01-05 1.5 -0.75 -0.5 117 ms ± 9.41 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.annual_return(returns['a'])) %timeit empyrical.annual_return(big_returns[0]) print(returns['a'].vbt.returns.annualized()) %timeit big_returns[0].vbt.returns.annualized() print(returns.vbt.returns.annualized()) %timeit big_returns.vbt.returns.annualized() print(returns.vbt.returns.rolling_annualized(3, minp=1)) %timeit big_returns.vbt.returns.rolling_annualized(3, minp=1)
1.690786886567203e+35 88.2 µs ± 19.2 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 1.6907868865671834e+35 The slowest run took 5.69 times longer than the fastest. This could mean that an intermediate result is being cached. 160 µs ± 152 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a 1.690787e+35 b -1.000000e+00 c 1.502038e-06 Name: annualized_return, dtype: float64 3.66 ms ± 122 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 8.507059e+37 -1.0 8.507059e+37 2018-01-03 1.197252e+40 -1.0 1.197252e+40 2018-01-04 3.741454e+50 -1.0 1.934286e+25 2018-01-05 2.672771e+33 -1.0 -1.000000e+00 145 ms ± 10.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.annual_volatility(returns['a'], alpha=3.)) %timeit empyrical.annual_volatility(big_returns[0], alpha=3.) print(returns['a'].vbt.returns.annualized_volatility(levy_alpha=3.)) %timeit big_returns[0].vbt.returns.annualized_volatility(levy_alpha=3.) print(returns.vbt.returns.annualized_volatility(levy_alpha=3.)) %timeit big_returns.vbt.returns.annualized_volatility(levy_alpha=3.) print(returns.vbt.returns.rolling_annualized_volatility(3, minp=1, levy_alpha=3.)) %timeit big_returns.vbt.returns.rolling_annualized_volatility(3, minp=1, levy_alpha=3.)
2.121838249438074 24.1 µs ± 10.1 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 2.121838249438074 The slowest run took 5.02 times longer than the fastest. This could mean that an intermediate result is being cached. 172 µs ± 149 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a 2.121838 b 0.830587 c 4.466341 Name: annualized_volatility, dtype: float64 5.78 ms ± 82.4 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 NaN NaN NaN 2018-01-03 2.233170 0.223317 2.233170 2018-01-04 2.191425 0.425454 4.254544 2018-01-05 0.804033 0.804033 3.384043 262 ms ± 13.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.calmar_ratio(returns['b'])) %timeit empyrical.calmar_ratio(big_returns[0]) print(returns['b'].vbt.returns.calmar_ratio()) %timeit big_returns[0].vbt.returns.calmar_ratio() print(returns.vbt.returns.calmar_ratio()) %timeit big_returns.vbt.returns.calmar_ratio() print(returns.vbt.returns.rolling_calmar_ratio(3, minp=1)) %timeit big_returns.vbt.returns.rolling_calmar_ratio(3, minp=1)
-1.2500000139698388 143 µs ± 36.8 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) -1.2500000139698388 The slowest run took 138.16 times longer than the fastest. This could mean that an intermediate result is being cached. 2.12 ms ± 4.94 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a NaN b -1.250000 c 0.000002 Name: calmar_ratio, dtype: float64 11.5 ms ± 61.6 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 NaN -5.0 NaN 2018-01-03 NaN -2.5 NaN 2018-01-04 NaN -2.0 5.802859e+25 2018-01-05 NaN -1.5 -1.500000e+00 616 ms ± 13.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.omega_ratio(returns['c'], risk_free=0.01, required_return=0.1)) %timeit empyrical.omega_ratio(big_returns[0], risk_free=0.01, required_return=0.1) print(returns['c'].vbt.returns.omega_ratio(risk_free=0.01, required_return=0.1)) %timeit big_returns[0].vbt.returns.omega_ratio(risk_free=0.01, required_return=0.1) print(returns.vbt.returns.omega_ratio(risk_free=0.01, required_return=0.1)) %timeit big_returns.vbt.returns.omega_ratio(risk_free=0.01, required_return=0.1) print(returns.vbt.returns.rolling_omega_ratio(3, minp=1, risk_free=0.01, required_return=0.1)) %timeit big_returns.vbt.returns.rolling_omega_ratio(3, minp=1, risk_free=0.01, required_return=0.1)
1.7319528661672228 414 µs ± 31.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 1.7319528661672228 The slowest run took 136.51 times longer than the fastest. This could mean that an intermediate result is being cached. 1.99 ms ± 4.63 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a inf b 0.000000 c 1.731953 Name: omega_ratio, dtype: float64 8.65 ms ± 66.1 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 inf 0.0 inf 2018-01-03 inf 0.0 inf 2018-01-04 inf 0.0 4.303734 2018-01-05 inf 0.0 0.573267 652 ms ± 13.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.sharpe_ratio(returns['a'], risk_free=0.01)) %timeit empyrical.sharpe_ratio(big_returns[0], risk_free=0.01) print(returns['a'].vbt.returns.sharpe_ratio(risk_free=0.01)) %timeit big_returns[0].vbt.returns.sharpe_ratio(risk_free=0.01) print(returns.vbt.returns.sharpe_ratio(risk_free=0.01)) %timeit big_returns.vbt.returns.sharpe_ratio(risk_free=0.01) print(returns.vbt.returns.rolling_sharpe_ratio(3, minp=1, risk_free=0.01)) %timeit big_returns.vbt.returns.rolling_sharpe_ratio(3, minp=1, risk_free=0.01)
24.139822936194918 80.5 µs ± 19.4 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 24.139822936194918 The slowest run took 124.12 times longer than the fastest. This could mean that an intermediate result is being cached. 2 ms ± 4.62 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a 24.139823 b -39.938439 c 3.517158 Name: sharpe_ratio, dtype: float64 8.53 ms ± 70.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 NaN NaN NaN 2018-01-03 33.225918 -105.514710 33.225918 2018-01-04 27.503962 -63.894201 8.929476 2018-01-05 43.786248 -46.280396 -3.588519 387 ms ± 14.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(returns.vbt.returns.deflated_sharpe_ratio(risk_free=0.01)) %timeit big_returns.vbt.returns.deflated_sharpe_ratio(risk_free=0.01) # can specify var_sharpe and nb_trials expclicitly print(big_returns[0].vbt.returns.deflated_sharpe_ratio( risk_free=0.01, var_sharpe=np.var(big_returns.vbt.returns.sharpe_ratio(risk_free=0.01)), nb_trials=big_returns.shape[1] ))
a NaN b NaN c 0.000536 Name: deflated_sharpe_ratio, dtype: float64
/Users/olegpolakow/Documents/SourceTree/vectorbt/vectorbt/returns/metrics.py:23: RuntimeWarning: invalid value encountered in sqrt np.sqrt(1 - skew * est_sharpe + ((kurtosis - 1) / 4) * est_sharpe ** 2)) /Users/olegpolakow/miniconda3/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py:1922: RuntimeWarning: invalid value encountered in greater_equal cond2 = (x >= np.asarray(_b)) & cond0
19 ms ± 751 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 1.0
print(empyrical.downside_risk(returns['b'], required_return=0.1)) %timeit empyrical.downside_risk(big_returns[0], required_return=0.1) print(returns['b'].vbt.returns.downside_risk(required_return=0.1)) %timeit big_returns[0].vbt.returns.downside_risk(required_return=0.1) print(returns.vbt.returns.downside_risk(required_return=0.1)) %timeit big_returns.vbt.returns.downside_risk(required_return=0.1) print(returns.vbt.returns.rolling_downside_risk(3, minp=1, required_return=0.1)) %timeit big_returns.vbt.returns.rolling_downside_risk(3, minp=1, required_return=0.1)
6.920801865722236 44.2 µs ± 23.6 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 6.920801865722236 The slowest run took 115.52 times longer than the fastest. This could mean that an intermediate result is being cached. 1.69 ms ± 3.89 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a 0.000000 b 6.920802 c 5.874521 Name: downside_risk, dtype: float64 6.09 ms ± 59.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 0.0 4.762352 0.000000 2018-01-03 0.0 5.174456 0.000000 2018-01-04 0.0 5.798563 3.971565 2018-01-05 0.0 7.503555 6.783313 377 ms ± 13.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.sortino_ratio(returns['b'], required_return=0.1)) %timeit empyrical.sortino_ratio(big_returns[0], required_return=0.1) print(returns['b'].vbt.returns.sortino_ratio(required_return=0.1)) %timeit big_returns[0].vbt.returns.sortino_ratio(required_return=0.1) print(returns.vbt.returns.sortino_ratio(required_return=0.1)) %timeit big_returns.vbt.returns.sortino_ratio(required_return=0.1) print(returns.vbt.returns.rolling_sortino_ratio(3, minp=1, required_return=0.1)) %timeit big_returns.vbt.returns.rolling_sortino_ratio(3, minp=1, required_return=0.1)
-15.32336860018125 113 µs ± 33.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) -15.32336860018125 The slowest run took 123.76 times longer than the fastest. This could mean that an intermediate result is being cached. 1.85 ms ± 4.29 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a inf b -15.323369 c 2.859808 Name: sortino_ratio, dtype: float64 8.87 ms ± 72.6 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 inf -15.874508 inf 2018-01-03 inf -15.827749 inf 2018-01-04 inf -15.693543 18.330304 2018-01-05 inf -15.485994 -7.842775 491 ms ± 13.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.excess_sharpe(returns['a'], benchmark_rets)) %timeit empyrical.excess_sharpe(big_returns[0], benchmark_rets) print(returns['a'].vbt.returns.information_ratio(benchmark_rets)) # will broadcast %timeit big_returns[0].vbt.returns.information_ratio(big_benchmark_rets) print(returns.vbt.returns.information_ratio(benchmark_rets)) %timeit big_returns.vbt.returns.information_ratio(big_benchmark_rets) print(returns.vbt.returns.rolling_information_ratio(benchmark_rets, 3, minp=1)) %timeit big_returns.vbt.returns.rolling_information_ratio(big_benchmark_rets, 3, minp=1)
-0.5575108270794908 432 µs ± 73.3 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) -0.5575108270794908 144 µs ± 12.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a -0.557511 b -2.718676 c -1.185416 Name: information_ratio, dtype: float64 8.39 ms ± 224 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 NaN NaN NaN 2018-01-03 -1.197205 -2.636038 -1.197205 2018-01-04 -0.903634 -2.536990 -0.905136 2018-01-05 -0.206482 -12.233914 -1.276646 386 ms ± 15.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.beta(returns['a'], benchmark_rets)) %timeit empyrical.beta(big_returns[0], benchmark_rets) print(returns['a'].vbt.returns.beta(benchmark_rets)) %timeit big_returns[0].vbt.returns.beta(big_benchmark_rets) print(returns.vbt.returns.beta(benchmark_rets)) %timeit big_returns.vbt.returns.beta(big_benchmark_rets) print(returns.vbt.returns.rolling_beta(benchmark_rets, 3, minp=1)) %timeit big_returns.vbt.returns.rolling_beta(big_benchmark_rets, 3, minp=1)
0.7853755820643185 1.17 ms ± 451 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 0.7853755820643185 107 µs ± 6.25 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a 0.785376 b 0.252235 c 1.547239 Name: beta, dtype: float64 10 ms ± 253 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 NaN NaN NaN 2018-01-03 0.788784 0.078878 0.788784 2018-01-04 0.796948 0.140305 1.403054 2018-01-05 0.762210 0.727668 3.117926 592 ms ± 13.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.alpha(returns['a'], benchmark_rets, risk_free=0.01)) %timeit empyrical.alpha(big_returns[0], benchmark_rets, risk_free=0.01) print(returns['a'].vbt.returns.alpha(benchmark_rets, risk_free=0.01)) %timeit big_returns[0].vbt.returns.alpha(big_benchmark_rets, risk_free=0.01) print(returns.vbt.returns.alpha(benchmark_rets, risk_free=0.01)) %timeit big_returns.vbt.returns.alpha(big_benchmark_rets, risk_free=0.01) print(returns.vbt.returns.rolling_alpha(benchmark_rets, 3, minp=1, risk_free=0.01)) %timeit big_returns.vbt.returns.rolling_alpha(big_benchmark_rets, 3, minp=1, risk_free=0.01)
21533608.259557564 1.08 ms ± 185 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 21533608.259557564 The slowest run took 126.75 times longer than the fastest. This could mean that an intermediate result is being cached. 2.62 ms ± 6.07 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a 2.153361e+07 b -1.000000e+00 c -1.000000e+00 Name: alpha, dtype: float64 16.3 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 NaN NaN NaN 2018-01-03 1.221461e+07 -1.0 1.221461e+07 2018-01-04 1.606601e+06 -1.0 -1.000000e+00 2018-01-05 1.344194e+08 -1.0 -1.000000e+00 1.01 s ± 17.8 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.tail_ratio(returns['a'])) %timeit empyrical.tail_ratio(big_returns[0]) print(returns['a'].vbt.returns.tail_ratio()) %timeit big_returns[0].vbt.returns.tail_ratio() print(returns.vbt.returns.tail_ratio()) %timeit big_returns.vbt.returns.tail_ratio() print(returns.vbt.returns.rolling_tail_ratio(3, minp=1)) %timeit big_returns.vbt.returns.rolling_tail_ratio(3, minp=1)
3.5238094437960337 137 µs ± 18.6 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 3.5238094437960337 The slowest run took 481.62 times longer than the fastest. This could mean that an intermediate result is being cached. 4.87 ms ± 11.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a 3.523809 b 0.436842 c 1.947368 Name: tail_ratio, dtype: float64 23.7 ms ± 90.3 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 1.000000 1.000000 1.000000 2018-01-03 1.857143 0.818182 1.857143 2018-01-04 2.714285 0.630769 3.800000 2018-01-05 1.870968 0.534483 0.862069 2.44 s ± 64.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.value_at_risk(returns.iloc[1:]['a'], cutoff=0.05)) %timeit empyrical.value_at_risk(big_returns[0], cutoff=0.05) print(returns['a'].vbt.returns.value_at_risk(cutoff=0.05)) %timeit big_returns[0].vbt.returns.value_at_risk(cutoff=0.05) print(returns.vbt.returns.value_at_risk(cutoff=0.05)) %timeit big_returns.vbt.returns.value_at_risk(cutoff=0.05) print(returns.vbt.returns.rolling_value_at_risk(3, minp=1, cutoff=0.05)) %timeit big_returns.vbt.returns.rolling_value_at_risk(3, minp=1, cutoff=0.05)
0.26250000596046447 81.6 µs ± 32.3 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 0.26250000596046447 The slowest run took 503.96 times longer than the fastest. This could mean that an intermediate result is being cached. 4.88 ms ± 11.8 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a 0.2625 b -0.4750 c -0.4750 Name: value_at_risk, dtype: float64 13.6 ms ± 196 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 1.000000 -0.200000 1.000000 2018-01-03 0.525000 -0.247500 0.525000 2018-01-04 0.350000 -0.325000 -0.250000 2018-01-05 0.258333 -0.483333 -0.483333 1.29 s ± 36.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.conditional_value_at_risk(returns.iloc[1:]['a'], cutoff=0.05)) %timeit empyrical.conditional_value_at_risk(big_returns[0], cutoff=0.05) print(returns['a'].vbt.returns.cond_value_at_risk(cutoff=0.05)) %timeit big_returns[0].vbt.returns.cond_value_at_risk(cutoff=0.05) print(returns.vbt.returns.cond_value_at_risk(cutoff=0.05)) %timeit big_returns.vbt.returns.cond_value_at_risk(cutoff=0.05) print(returns.vbt.returns.rolling_cond_value_at_risk(3, minp=1, cutoff=0.05)) %timeit big_returns.vbt.returns.rolling_cond_value_at_risk(3, minp=1, cutoff=0.05)
0.25 The slowest run took 4.48 times longer than the fastest. This could mean that an intermediate result is being cached. 41.2 µs ± 27.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 0.25 The slowest run took 433.89 times longer than the fastest. This could mean that an intermediate result is being cached. 4.25 ms ± 10.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a 0.25 b -0.50 c -0.50 Name: cond_value_at_risk, dtype: float64 12.4 ms ± 117 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 1.000000 -0.200000 1.000000 2018-01-03 0.500000 -0.250000 0.500000 2018-01-04 0.333333 -0.333333 -0.333333 2018-01-05 0.250000 -0.500000 -0.500000 1.97 s ± 39.4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.capture(returns['a'], benchmark_rets)) %timeit empyrical.capture(big_returns[0], big_benchmark_rets) print(returns['a'].vbt.returns.capture(benchmark_rets)) %timeit big_returns[0].vbt.returns.capture(big_benchmark_rets) print(returns.vbt.returns.capture(benchmark_rets)) %timeit big_returns.vbt.returns.capture(big_benchmark_rets) print(returns.vbt.returns.rolling_capture(benchmark_rets, 3, minp=1)) %timeit big_returns.vbt.returns.rolling_capture(big_benchmark_rets, 3, minp=1)
0.00691706997447195 188 µs ± 118 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 0.006917069974471952 The slowest run took 99.74 times longer than the fastest. This could mean that an intermediate result is being cached. 2.06 ms ± 4.72 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
/Users/olegpolakow/miniconda3/lib/python3.7/site-packages/empyrical/stats.py:447: RuntimeWarning: invalid value encountered in double_scalars return ending_value ** (1 / num_years) - 1
a 6.917070e-03 b -4.091036e-38 c 6.144892e-44 Name: capture, dtype: float64 8.52 ms ± 3.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 0.000019 -2.224899e-43 1.892734e-05 2018-01-03 0.000055 -4.590579e-45 5.496077e-05 2018-01-04 0.000024 -6.439659e-56 1.245614e-30 2018-01-05 0.353571 -1.322864e-34 -1.322864e-34 297 ms ± 12.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.up_capture(returns['a'], benchmark_rets)) %timeit empyrical.up_capture(big_returns[0], big_benchmark_rets) print(returns['a'].vbt.returns.up_capture(benchmark_rets)) %timeit big_returns[0].vbt.returns.up_capture(big_benchmark_rets) print(returns.vbt.returns.up_capture(benchmark_rets)) %timeit big_returns.vbt.returns.up_capture(big_benchmark_rets) print(returns.vbt.returns.rolling_up_capture(benchmark_rets, 3, minp=1)) %timeit big_returns.vbt.returns.rolling_up_capture(big_benchmark_rets, 3, minp=1)
0.0019948153957327634 428 µs ± 55.2 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 0.0019948153957327634 The slowest run took 108.89 times longer than the fastest. This could mean that an intermediate result is being cached. 2.18 ms ± 5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a 1.994815e-03 b -1.839889e-47 c 3.454480e-53 Name: up_capture, dtype: float64 7.84 ms ± 4.83 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 3.582444e-10 -4.950174e-86 3.582444e-10 2018-01-03 4.074546e-07 -3.110292e-67 4.074546e-07 2018-01-04 2.409368e-05 -6.439659e-56 1.245614e-30 2018-01-05 3.535714e-01 -1.322864e-34 -1.322864e-34 844 ms ± 15.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.down_capture(returns['a'], benchmark_rets)) #%timeit empyrical.down_capture(big_returns[0], big_benchmark_rets) print(returns['a'].vbt.returns.down_capture(benchmark_rets)) %timeit big_returns[0].vbt.returns.down_capture(big_benchmark_rets) print(returns.vbt.returns.down_capture(benchmark_rets)) %timeit big_returns.vbt.returns.down_capture(big_benchmark_rets) print(returns.vbt.returns.rolling_down_capture(benchmark_rets, 3, minp=1)) %timeit big_returns.vbt.returns.rolling_down_capture(big_benchmark_rets, 3, minp=1)
nan nan The slowest run took 114.37 times longer than the fastest. This could mean that an intermediate result is being cached. 2.29 ms ± 5.28 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a NaN b NaN c NaN Name: down_capture, dtype: float64 7.7 ms ± 4.79 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 NaN NaN NaN 2018-01-03 NaN NaN NaN 2018-01-04 NaN NaN NaN 2018-01-05 NaN NaN NaN 844 ms ± 19.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(returns.vbt.returns.drawdown()) %timeit big_returns.vbt.returns.drawdown()
a b c 2018-01-01 0.0 0.0 0.000000 2018-01-02 0.0 -0.2 0.000000 2018-01-03 0.0 -0.4 0.000000 2018-01-04 0.0 -0.6 -0.333333 2018-01-05 0.0 -0.8 -0.666667 8.76 ms ± 831 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(empyrical.max_drawdown(returns['b'])) %timeit empyrical.max_drawdown(big_returns[0]) print(returns['b'].vbt.returns.max_drawdown()) %timeit big_returns[0].vbt.returns.max_drawdown() print(returns.vbt.returns.max_drawdown()) %timeit big_returns.vbt.returns.max_drawdown() print(returns.vbt.returns.rolling_max_drawdown(3, minp=1)) %timeit big_returns.vbt.returns.rolling_max_drawdown(3, minp=1)
-0.7999999910593033 The slowest run took 4.07 times longer than the fastest. This could mean that an intermediate result is being cached. 87.9 µs ± 62.1 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) -0.7999999910593032 The slowest run took 210.20 times longer than the fastest. This could mean that an intermediate result is being cached. 2.01 ms ± 4.76 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) a 0.000000 b -0.800000 c -0.666667 Name: max_drawdown, dtype: float64 8.07 ms ± 124 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a b c 2018-01-01 NaN NaN NaN 2018-01-02 0.0 -0.200000 0.000000 2018-01-03 0.0 -0.400000 0.000000 2018-01-04 0.0 -0.500000 -0.333333 2018-01-05 0.0 -0.666667 -0.666667 490 ms ± 14 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
print(returns.vbt.returns.drawdowns) %timeit big_returns.vbt.returns.drawdowns print(returns.vbt.returns.drawdowns.max_drawdown())
Drawdowns(**Config({ "wrapper": "<vectorbt.base.array_wrapper.ArrayWrapper object at 0x7fb4a0a65ef0> of shape (5, 3)", "records_arr": "<numpy.ndarray object at 0x7fb4865b94e0> of shape (2,)", "idx_field": "end_idx", "col_mapper": null, "ts": "<pandas.core.frame.DataFrame object at 0x7fb486d7a438> of shape (5, 3)" })) 11.3 ms ± 620 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) a NaN b -0.800000 c -0.666667 Name: max_drawdown, dtype: float64
print(returns['b'].vbt.returns.stats( settings=dict(benchmark_rets=benchmark_rets, levy_alpha=3., risk_free=0.01, required_return=0.1))) %timeit big_returns[0].vbt.returns.stats(\ silence_warnings=True,\ settings=dict(benchmark_rets=big_benchmark_rets, levy_alpha=3., risk_free=0.01, required_return=0.1)) print(returns.vbt.returns.stats( settings=dict(benchmark_rets=benchmark_rets, levy_alpha=3., risk_free=0.01, required_return=0.1))) %timeit big_returns.vbt.returns.stats(\ silence_warnings=True,\ settings=dict(benchmark_rets=big_benchmark_rets, levy_alpha=3., risk_free=0.01, required_return=0.1))
Start 2018-01-01 00:00:00 End 2018-01-05 00:00:00 Period 5 days 00:00:00 Total Return [%] -79.999999 Benchmark Return [%] 451.85973 Annualized Return [%] -100.0 Annualized Volatility [%] 83.05873 Max Drawdown [%] 79.999999 Max Drawdown Duration 4 days 00:00:00 Sharpe Ratio -39.938439 Calmar Ratio -1.25 Omega Ratio 0.0 Sortino Ratio -15.323369 Skew -1.065369 Kurtosis 0.645216 Tail Ratio 0.436842 Common Sense Ratio 0.0 Value at Risk -0.475 Alpha -1.0 Beta 0.252235 Name: b, dtype: object 48.8 ms ± 623 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) Start 2018-01-01 00:00:00 End 2018-01-05 00:00:00 Period 5 days 00:00:00 Total Return [%] 106.666673 Benchmark Return [%] 451.85973 Annualized Return [%] 5635956288557277813711347645725802496.0 Annualized Volatility [%] 247.292207 Max Drawdown [%] 73.333332 Max Drawdown Duration 3 days 00:00:00 Sharpe Ratio -4.093819 Calmar Ratio -0.624999 Omega Ratio inf Sortino Ratio inf Skew 0.256871 Kurtosis -0.254095 Tail Ratio 1.96934 Common Sense Ratio 198600359944397791384906716353134592.0 Value at Risk -0.229167 Alpha 7177868.753186 Beta 0.861616 Name: agg_func_mean, dtype: object
/Users/olegpolakow/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:6: UserWarning: Object has multiple columns. Aggregating using <function mean at 0x7fb48139f9d8>. Pass column to select a single column/group.
207 ms ± 2.43 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
returns['a'].vbt.returns.plot_cumulative(benchmark_rets).show_svg()
Image in a Jupyter notebook
returns['a'].vbt.returns.plot_cumulative(benchmark_rets, fill_to_benchmark=True).show_svg()
Image in a Jupyter notebook