1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-17 17:34:35 +08:00

fix analysis bug

This commit is contained in:
Young
2020-09-25 02:47:29 +00:00
parent b2749d2e08
commit 9556d1ce38
11 changed files with 157 additions and 219 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

@@ -108,35 +108,35 @@ Graphical Result
.. image:: ../_static/img/analysis/score_ic.png .. image:: ../_static/img/analysis/score_ic.png
Usage of `analysis_position.cumulative_return` .. Usage of `analysis_position.cumulative_return`
---------------------------------------------- .. ----------------------------------------------
..
API .. API
~~~~~~~~~~~~~~~~ .. ~~~~~~~~~~~~~~~~
..
.. automodule:: qlib.contrib.report.analysis_position.cumulative_return .. .. automodule:: qlib.contrib.report.analysis_position.cumulative_return
:members: .. :members:
..
Graphical Result .. Graphical Result
~~~~~~~~~~~~~~~~~ .. ~~~~~~~~~~~~~~~~~
..
.. note:: .. .. note::
..
- Axis X: Trading day .. - Axis X: Trading day
- Axis Y: .. - Axis Y:
- Above axis Y: `(((Ref($close, -1)/$close - 1) * weight).sum() / weight.sum()).cumsum()` .. - Above axis Y: `(((Ref($close, -1)/$close - 1) * weight).sum() / weight.sum()).cumsum()`
- Below axis Y: Daily weight sum .. - Below axis Y: Daily weight sum
- In the **sell** graph, `y < 0` stands for profit; in other cases, `y > 0` stands for profit. .. - In the **sell** graph, `y < 0` stands for profit; in other cases, `y > 0` stands for profit.
- In the **buy_minus_sell** graph, the **y** value of the **weight** graph at the bottom is `buy_weight + sell_weight`. .. - In the **buy_minus_sell** graph, the **y** value of the **weight** graph at the bottom is `buy_weight + sell_weight`.
- In each graph, the **red line** in the histogram on the right represents the average. .. - In each graph, the **red line** in the histogram on the right represents the average.
..
.. image:: ../_static/img/analysis/cumulative_return_buy.png .. .. image:: ../_static/img/analysis/cumulative_return_buy.png
..
.. image:: ../_static/img/analysis/cumulative_return_sell.png .. .. image:: ../_static/img/analysis/cumulative_return_sell.png
..
.. image:: ../_static/img/analysis/cumulative_return_buy_minus_sell.png .. .. image:: ../_static/img/analysis/cumulative_return_buy_minus_sell.png
..
.. image:: ../_static/img/analysis/cumulative_return_hold.png .. .. image:: ../_static/img/analysis/cumulative_return_hold.png
Usage of `analysis_position.risk_analysis` Usage of `analysis_position.risk_analysis`
@@ -220,42 +220,42 @@ Graphical Result
.. image:: ../_static/img/analysis/risk_analysis_std.png .. image:: ../_static/img/analysis/risk_analysis_std.png
:align: center :align: center
..
Usage of `analysis_position.rank_label` .. Usage of `analysis_position.rank_label`
---------------------------------------------- .. ----------------------------------------------
..
API .. API
~~~~~ .. ~~~~~
..
.. automodule:: qlib.contrib.report.analysis_position.rank_label .. .. automodule:: qlib.contrib.report.analysis_position.rank_label
:members: .. :members:
..
..
Graphical Result .. Graphical Result
~~~~~~~~~~~~~~~~~ .. ~~~~~~~~~~~~~~~~~
..
.. note:: .. .. note::
..
- hold/sell/buy graphics: .. - hold/sell/buy graphics:
- Axis X: Trading day .. - Axis X: Trading day
- Axis Y: .. - Axis Y:
Average `ranking ratio`of `label` for stocks that is held/sold/bought on the trading day. .. Average `ranking ratio`of `label` for stocks that is held/sold/bought on the trading day.
..
In the above example, the `label` is formulated as `Ref($close, -1)/$close - 1`. The `ranking ratio` can be formulated as follows. .. In the above example, the `label` is formulated as `Ref($close, -1)/$close - 1`. The `ranking ratio` can be formulated as follows.
.. math:: .. .. math::
..
ranking\ ratio = \frac{Ascending\ Ranking\ of\ label}{Number\ of\ Stocks\ in\ the\ Portfolio} .. ranking\ ratio = \frac{Ascending\ Ranking\ of\ label}{Number\ of\ Stocks\ in\ the\ Portfolio}
..
.. image:: ../_static/img/analysis/rank_label_hold.png .. .. image:: ../_static/img/analysis/rank_label_hold.png
:align: center .. :align: center
..
.. image:: ../_static/img/analysis/rank_label_buy.png .. .. image:: ../_static/img/analysis/rank_label_buy.png
:align: center .. :align: center
..
.. image:: ../_static/img/analysis/rank_label_sell.png .. .. image:: ../_static/img/analysis/rank_label_sell.png
:align: center .. :align: center
..
..
Usage of `analysis_model.analysis_model_performance` Usage of `analysis_model.analysis_model_performance`
----------------------------------------------------- -----------------------------------------------------

View File

@@ -80,25 +80,6 @@
"positions = pickle.load(estimator_dir.joinpath('positions.pkl').open('rb'))" "positions = pickle.load(estimator_dir.joinpath('positions.pkl').open('rb'))"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# get label data from qlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from qlib.data import D\n",
"pred_df_dates = pred_df.index.get_level_values(level='datetime')\n",
"features_df = D.features(D.instruments(MARKET), ['Ref($close, -1)/$close - 1'], pred_df_dates.min(), pred_df_dates.max())\n",
"features_df.columns = ['label']"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -112,7 +93,9 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from qlib.contrib.report import analysis_model, analysis_position" "from qlib.data import D\n",
"from qlib.contrib.report import analysis_model, analysis_position\n",
"pred_df_dates = pred_df.index.get_level_values(level='datetime')"
] ]
}, },
{ {
@@ -122,6 +105,16 @@
"## analysis position" "## analysis position"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stock_ret = D.features(D.instruments(MARKET), ['Ref($close, -1)/$close - 1'], pred_df_dates.min(), pred_df_dates.max())\n",
"stock_ret.columns = ['label']"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -138,41 +131,6 @@
"analysis_position.report_graph(report_normal_df)" "analysis_position.report_graph(report_normal_df)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### score IC"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pred_label = pd.concat([features_df, pred_df], axis=1, sort=True).reindex(features_df.index)\n",
"analysis_position.score_ic_graph(pred_label)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### cumulative return"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"analysis_position.cumulative_return_graph(positions, report_normal_df, features_df)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -183,9 +141,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"scrolled": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"analysis_position.risk_analysis_graph(analysis_df, report_normal_df)" "analysis_position.risk_analysis_graph(analysis_df, report_normal_df)"
@@ -195,7 +151,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### rank label" "## analysis model"
] ]
}, },
{ {
@@ -204,14 +160,25 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"analysis_position.rank_label_graph(positions, features_df, pred_df_dates.min(), pred_df_dates.max())" "label_df = D.features(D.instruments(MARKET), ['Ref($close, -2)/Ref($close, -1) - 1'], pred_df_dates.min(), pred_df_dates.max())\n",
"label_df.columns = ['label']"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## analysis model" "### score IC"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pred_label = pd.concat([label_df, pred_df], axis=1, sort=True).reindex(label_df.index)\n",
"analysis_position.score_ic_graph(pred_label)"
] ]
}, },
{ {
@@ -224,9 +191,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"scrolled": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"analysis_model.model_performance_graph(pred_label)" "analysis_model.model_performance_graph(pred_label)"

View File

@@ -62,7 +62,6 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"scrolled": true,
"tags": [] "tags": []
}, },
"outputs": [], "outputs": [],
@@ -195,7 +194,19 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from qlib.contrib.report import analysis_model, analysis_position" "from qlib.contrib.report import analysis_model, analysis_position\n",
"from qlib.data import D\n",
"pred_df_dates = pred_score.index.get_level_values(level='datetime')\n",
"report_normal_df = report_normal\n",
"positions = positions_normal\n",
"pred_df = pred_score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## analysis position"
] ]
}, },
{ {
@@ -204,18 +215,8 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# get label data\n", "stock_ret = D.features(D.instruments(MARKET), ['Ref($close, -1)/$close - 1'], pred_df_dates.min(), pred_df_dates.max())\n",
"from qlib.data import D\n", "stock_ret.columns = ['label']"
"pred_df_dates = pred_score.index.get_level_values(level='datetime')\n",
"features_df = D.features(D.instruments(MARKET), ['Ref($close, -1)/$close - 1'], pred_df_dates.min(), pred_df_dates.max())\n",
"features_df.columns = ['label']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## analysis position"
] ]
}, },
{ {
@@ -231,7 +232,40 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"analysis_position.report_graph(report_normal)" "analysis_position.report_graph(report_normal_df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### risk analysis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"analysis_position.risk_analysis_graph(analysis_df, report_normal_df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## analysis model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"label_df = D.features(D.instruments(MARKET), ['Ref($close, -2)/Ref($close, -1) - 1'], pred_df_dates.min(), pred_df_dates.max())\n",
"label_df.columns = ['label']"
] ]
}, },
{ {
@@ -247,69 +281,10 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"pred_label = pd.concat([features_df, pred_score], axis=1, sort=True).reindex(features_df.index)\n", "pred_label = pd.concat([label_df, pred_df], axis=1, sort=True).reindex(label_df.index)\n",
"analysis_position.score_ic_graph(pred_label)" "analysis_position.score_ic_graph(pred_label)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### cumulative return"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"analysis_position.cumulative_return_graph(positions_normal, report_normal, features_df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### risk analysis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"analysis_position.risk_analysis_graph(analysis_df, report_normal)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### rank label"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"analysis_position.rank_label_graph(positions_normal, features_df, pred_df_dates.min(), pred_df_dates.max())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## analysis model"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -320,9 +295,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"scrolled": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"analysis_model.model_performance_graph(pred_label)" "analysis_model.model_performance_graph(pred_label)"
@@ -344,8 +317,7 @@
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3"
"version": "3.8.5"
}, },
"toc": { "toc": {
"base_numbering": 1, "base_numbering": 1,

View File

@@ -2,7 +2,7 @@
# Licensed under the MIT License. # Licensed under the MIT License.
__version__ = "0.5.0" __version__ = "0.5.0.dev"
import os import os
import copy import copy

View File

@@ -264,17 +264,18 @@ def model_performance_graph(
) -> [list, tuple]: ) -> [list, tuple]:
"""Model performance """Model performance
:param pred_label: index is **pd.MultiIndex**, index name is **[instrument, datetime]**; columns names is **[score, label]** :param pred_label: index is **pd.MultiIndex**, index name is **[instrument, datetime]**; columns names is **[score,
label]**. It is usually same as the label of model training(e.g. "Ref($close, -2)/Ref($close, -1) - 1")
.. code-block:: python .. code-block:: python
instrument datetime score label instrument datetime score label
SH600004 2017-12-11 -0.013502 -0.013502 SH600004 2017-12-11 -0.013502 -0.013502
2017-12-12 -0.072367 -0.072367 2017-12-12 -0.072367 -0.072367
2017-12-13 -0.068605 -0.068605 2017-12-13 -0.068605 -0.068605
2017-12-14 0.012440 0.012440 2017-12-14 0.012440 0.012440
2017-12-15 -0.102778 -0.102778 2017-12-15 -0.102778 -0.102778
:param lag: `pred.groupby(level='instrument')['score'].shift(lag)`. It will be only used in the auto-correlation computing. :param lag: `pred.groupby(level='instrument')['score'].shift(lag)`. It will be only used in the auto-correlation computing.

View File

@@ -12,7 +12,7 @@ from setuptools import find_packages, setup, Extension
NAME = "qlib" NAME = "qlib"
DESCRIPTION = "A Quantitative-research Platform" DESCRIPTION = "A Quantitative-research Platform"
REQUIRES_PYTHON = ">=3.5.0" REQUIRES_PYTHON = ">=3.5.0"
VERSION = "0.5.0" VERSION = "0.5.0.dev"
# Detect Cython # Detect Cython
try: try: