Path: blob/master/finrl/applications/imitation_learning/Imitation_Sandbox.ipynb
732 views
Kernel: finrl
Installation Setup
In [61]:
Out[61]:
The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload
In [62]:
Out[62]:
%pylab is deprecated, use %matplotlib inline and import the required libraries.
Populating the interactive namespace from numpy and matplotlib
In [63]:
In [64]:
Environment configuration
A gym-style portfolio allocation environment for agents to interact. It is handy to compare the performances.
In [10]:
In [11]:
Out[11]:
Stock Dimension: 11, State Space: 11
Feature Dimension: 4
In [12]:
Out[12]:
=================================
begin_total_asset:1000000
end_total_asset:4904419.075973194
Sharpe: 0.6729768484476764
=================================
Modelling
Use a two-stage scheme (supervised learning & reinforcement learning), in analogy to AlphaGo and ChatGPT. The first stage learns from human trader logs, while the second stage leverages reinforcement learning to achieve super-human performance.
In [84]:
Out[84]:
=================================
begin_total_asset:1000000
end_total_asset:4904419.075973194
Sharpe: 0.6729768484476764
=================================
In [184]:
Regression
In [129]:
In [130]:
Out[130]:
=================================
begin_total_asset:1000000
end_total_asset:3841898.2757355147
Sharpe: 0.6109941521683996
=================================
[<matplotlib.lines.Line2D at 0x13940bc7eb0>]
[<matplotlib.lines.Line2D at 0x13940bc7f10>]
<matplotlib.legend.Legend at 0x13940bbde50>
Text(0, 0.5, 'Portfolio Value')
Text(0.5, 0, 'timesteps')
In [119]:
Out[119]:
[<matplotlib.lines.Line2D at 0x13940821c70>]
Text(0, 0.5, 'MSE')
Text(0.5, 0, 'timesteps')
In [123]:
Out[123]:
OLS Regression Results
=======================================================================================
Dep. Variable: y R-squared (uncentered): 0.783
Model: OLS Adj. R-squared (uncentered): 0.783
Method: Least Squares F-statistic: 1.096e+05
Date: Sun, 12 Mar 2023 Prob (F-statistic): 0.00
Time: 19:27:08 Log-Likelihood: 49310.
No. Observations: 30448 AIC: -9.862e+04
Df Residuals: 30447 BIC: -9.861e+04
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
x1 1.0000 0.003 331.098 0.000 0.994 1.006
==============================================================================
Omnibus: 32688.137 Durbin-Watson: 2.225
Prob(Omnibus): 0.000 Jarque-Bera (JB): 1888.263
Skew: 0.000 Prob(JB): 0.00
Kurtosis: 1.780 Cond. No. 1.00
==============================================================================
Notes:
[1] R² is computed without centering (uncentered) since the model does not contain a constant.
[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.
Tree
In [131]:
Out[131]:
In [132]:
Out[132]:
=================================
begin_total_asset:1000000
end_total_asset:3843266.9780635866
Sharpe: 0.6109682673652438
=================================
[<matplotlib.lines.Line2D at 0x13940c57730>]
[<matplotlib.lines.Line2D at 0x13940c579a0>]
<matplotlib.legend.Legend at 0x13940c5f6d0>
Text(0, 0.5, 'Portfolio Value')
Text(0.5, 0, 'timesteps')
In [128]:
Out[128]:
[<matplotlib.lines.Line2D at 0x139409c12e0>]
Text(0, 0.5, 'MSE')
Text(0.5, 0, 'timesteps')
In [135]:
Out[135]:
OLS Regression Results
=======================================================================================
Dep. Variable: y R-squared (uncentered): 0.969
Model: OLS Adj. R-squared (uncentered): 0.969
Method: Least Squares F-statistic: 8.558e+04
Date: Sun, 12 Mar 2023 Prob (F-statistic): 0.00
Time: 19:48:18 Log-Likelihood: 12158.
No. Observations: 2769 AIC: -2.431e+04
Df Residuals: 2768 BIC: -2.431e+04
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 0 0 nan nan 0 0
0 1.0460 0.004 292.549 0.000 1.039 1.053
==============================================================================
Omnibus: 367.771 Durbin-Watson: 1.964
Prob(Omnibus): 0.000 Jarque-Bera (JB): 3366.307
Skew: 0.298 Prob(JB): 0.00
Kurtosis: 8.369 Cond. No. inf
==============================================================================
Notes:
[1] R² is computed without centering (uncentered) since the model does not contain a constant.
[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[3] The smallest eigenvalue is 0. This might indicate that there are
strong multicollinearity problems or that the design matrix is singular.
LSTM
In [188]:
(Output Hidden)
In [190]:
Out[190]:
=================================
begin_total_asset:1000000
end_total_asset:4709181.10148131
Sharpe: 0.6590339018434035
=================================
In [191]:
Out[191]:
In [192]:
Out[192]:
OLS Regression Results
=======================================================================================
Dep. Variable: y R-squared (uncentered): 0.997
Model: OLS Adj. R-squared (uncentered): 0.997
Method: Least Squares F-statistic: 8.709e+05
Date: Tue, 14 Mar 2023 Prob (F-statistic): 0.00
Time: 15:29:15 Log-Likelihood: 15331.
No. Observations: 2769 AIC: -3.066e+04
Df Residuals: 2768 BIC: -3.065e+04
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 0 0 nan nan 0 0
0 0.9980 0.001 933.236 0.000 0.996 1.000
==============================================================================
Omnibus: 1059.708 Durbin-Watson: 1.956
Prob(Omnibus): 0.000 Jarque-Bera (JB): 40336.718
Skew: -1.122 Prob(JB): 0.00
Kurtosis: 21.563 Cond. No. inf
==============================================================================
Notes:
[1] R² is computed without centering (uncentered) since the model does not contain a constant.
[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[3] The smallest eigenvalue is 0. This might indicate that there are
strong multicollinearity problems or that the design matrix is singular.
First stage - Supervised Learning
Fundemental models: regression, tree, lstm and ann
Placebo tests: features (subset or random variables that has the mean and std), imitate mean-var, XLF
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Mertrics
In [ ]:
Cumulative rewards
In [ ]:
Portfolio values
In [ ]:
MSE
In [ ]:
Backtest
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Exploration to finetune (TD3+BC)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: