alphainspect


Namealphainspect JSON
Version 0.5.0 PyPI version JSON
download
home_pageNone
Summaryfactor performance visualization
upload_time2024-12-13 14:35:34
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License Copyright (c) 2024 wukan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords quant alpha performance visualization
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # AlphaInspect

仿`alphalens`的单因子分析工具

## 安装

```commandline
pip install -i https://pypi.org/simple --upgrade alphainspect  # 官方源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade alphainspect  # 国内镜像源
```

## 使用

1. 准备数据。运行`data/prepare_data.py`
    1. date, asset。必需的两个字段
    2. factor因子值。放在因子发生时刻
    3. forward return远期收益率。计算收益率后需要回移到开始位置。为何是`shift(-n)收益率`,而不是`shift(n)因子`呢?
        1. 多期收期率。如果移动因子,会导致一个因子就要移动多次
        2. 因子一般成百上千,全移动要的工作量非常大,而收益率则少很多
    4. 推荐大家使用`expr_codegen`和`polars_ta`等项目
2. 运行`examples/factor_analysis.py`示例单因子分析
3. 运行`examples/reports_html.py`示例多进程并行输出HTML网页报表
4. 运行`examples/reports_notebook.py`示例多进程并行转Notebook报表
5. 运行`examples/events_study.py`示例事件分析

## 部分图示

![2x2](docs/img/2x2.png)
![3x2](docs/img/3x2.png)
![ic](docs/img/ic.png)
![returns](docs/img/returns.png)
![cum_returns](docs/img/cum_returns.png)
![spread](docs/img/spread.png)
![turnover](docs/img/turnover.png)
![events](docs/img/events.png)

## 累计收益的计算方法

参考 [cum_returns.md](cum_returns.md)

## `alphainspect`与`alphalens`的不同

1. 不自动计算`forward_returns`。
    1. `alphalens`设`periods=(1, 5, 10)`,然后内部计算持有1、5、10期数的收益率
    2. `alphainspect`由用户外部生成,用户可以比较同因子,不同交易方式产生的差异。例如:
        - `RETURN_OC_1`: T+1开盘入场,T+1收盘出场
        - `RETURN_CC_1`: T+0收盘入场,T+1收盘出场
        - `RETURN_OO_1`: T+1开盘入场,T+2开盘出场
        - `RETURN_OO_5`: T+1开盘入场,T+6开盘出场
2. 不做去极值、标准化、行业中性化等操作
    1. `alphalens`的各参数要弄懂还是很麻烦的,初学者如绩效达不到要求就得深入研究源代码找原因
    2. `alphainspect`用户在外可以一次性全计算好,如`F1_ORG, F1_ZS, F1_NEUT`,然后在分别传不同因子进行比较即可
3. 资金分配只用等权
    1. `alphalens`有因子加权、多空等设置
    2. `alphainspect`只提供等权一种计算方法,实现简单
4. 收益率计算方法不同
    1. `alphalens`多期简单收益率几何平均成1期,然后+1累乘
    2. ~~`alphainspect`由用户提供1期简单收益率,然后根据要求持有或调仓,得到新的权益,循环迭代下去。更精确~~
    3. `alphainspect`由用户提供每期收益率(也可以使用多期收益率的几何平均),然后累加。(分层计算速度提高1~1.5倍)

## `alphainspect`与`alphalens`的相同

1. 数据组织方式相同。都是长表,都是因子不移动,收益率计算,然后后移到与因子产生时间对齐
2. 不考虑滑点和手续费。单因子是用来合成多因子的,因手续费和滑点而错过部分单因子就可惜了,应当在因子合成后的回测阶段才考虑手续费
3. 收益计算不求精确,只为能正确评价因子绩效

## 二次开发

```commandline
git --clone https://github.com/wukan1986/alphainspect.git
cd alphainspect
pip install -e .
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "alphainspect",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "quant, alpha, performance, visualization",
    "author": null,
    "author_email": "wukan <wu-kan@163.com>",
    "download_url": "https://files.pythonhosted.org/packages/2f/a0/e2a6177c70b8085720e69de13488ce769a6bee373c9a032d862a6474dc5e/alphainspect-0.5.0.tar.gz",
    "platform": null,
    "description": "# AlphaInspect\n\n\u4eff`alphalens`\u7684\u5355\u56e0\u5b50\u5206\u6790\u5de5\u5177\n\n## \u5b89\u88c5\n\n```commandline\npip install -i https://pypi.org/simple --upgrade alphainspect  # \u5b98\u65b9\u6e90\npip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade alphainspect  # \u56fd\u5185\u955c\u50cf\u6e90\n```\n\n## \u4f7f\u7528\n\n1. \u51c6\u5907\u6570\u636e\u3002\u8fd0\u884c`data/prepare_data.py`\n    1. date, asset\u3002\u5fc5\u9700\u7684\u4e24\u4e2a\u5b57\u6bb5\n    2. factor\u56e0\u5b50\u503c\u3002\u653e\u5728\u56e0\u5b50\u53d1\u751f\u65f6\u523b\n    3. forward return\u8fdc\u671f\u6536\u76ca\u7387\u3002\u8ba1\u7b97\u6536\u76ca\u7387\u540e\u9700\u8981\u56de\u79fb\u5230\u5f00\u59cb\u4f4d\u7f6e\u3002\u4e3a\u4f55\u662f`shift(-n)\u6536\u76ca\u7387`\uff0c\u800c\u4e0d\u662f`shift(n)\u56e0\u5b50`\u5462\uff1f\n        1. \u591a\u671f\u6536\u671f\u7387\u3002\u5982\u679c\u79fb\u52a8\u56e0\u5b50\uff0c\u4f1a\u5bfc\u81f4\u4e00\u4e2a\u56e0\u5b50\u5c31\u8981\u79fb\u52a8\u591a\u6b21\n        2. \u56e0\u5b50\u4e00\u822c\u6210\u767e\u4e0a\u5343\uff0c\u5168\u79fb\u52a8\u8981\u7684\u5de5\u4f5c\u91cf\u975e\u5e38\u5927\uff0c\u800c\u6536\u76ca\u7387\u5219\u5c11\u5f88\u591a\n    4. \u63a8\u8350\u5927\u5bb6\u4f7f\u7528`expr_codegen`\u548c`polars_ta`\u7b49\u9879\u76ee\n2. \u8fd0\u884c`examples/factor_analysis.py`\u793a\u4f8b\u5355\u56e0\u5b50\u5206\u6790\n3. \u8fd0\u884c`examples/reports_html.py`\u793a\u4f8b\u591a\u8fdb\u7a0b\u5e76\u884c\u8f93\u51faHTML\u7f51\u9875\u62a5\u8868\n4. \u8fd0\u884c`examples/reports_notebook.py`\u793a\u4f8b\u591a\u8fdb\u7a0b\u5e76\u884c\u8f6cNotebook\u62a5\u8868\n5. \u8fd0\u884c`examples/events_study.py`\u793a\u4f8b\u4e8b\u4ef6\u5206\u6790\n\n## \u90e8\u5206\u56fe\u793a\n\n![2x2](docs/img/2x2.png)\n![3x2](docs/img/3x2.png)\n![ic](docs/img/ic.png)\n![returns](docs/img/returns.png)\n![cum_returns](docs/img/cum_returns.png)\n![spread](docs/img/spread.png)\n![turnover](docs/img/turnover.png)\n![events](docs/img/events.png)\n\n## \u7d2f\u8ba1\u6536\u76ca\u7684\u8ba1\u7b97\u65b9\u6cd5\n\n\u53c2\u8003 [cum_returns.md](cum_returns.md)\n\n## `alphainspect`\u4e0e`alphalens`\u7684\u4e0d\u540c\n\n1. \u4e0d\u81ea\u52a8\u8ba1\u7b97`forward_returns`\u3002\n    1. `alphalens`\u8bbe`periods=(1, 5, 10)`\uff0c\u7136\u540e\u5185\u90e8\u8ba1\u7b97\u6301\u67091\u30015\u300110\u671f\u6570\u7684\u6536\u76ca\u7387\n    2. `alphainspect`\u7531\u7528\u6237\u5916\u90e8\u751f\u6210\uff0c\u7528\u6237\u53ef\u4ee5\u6bd4\u8f83\u540c\u56e0\u5b50\uff0c\u4e0d\u540c\u4ea4\u6613\u65b9\u5f0f\u4ea7\u751f\u7684\u5dee\u5f02\u3002\u4f8b\u5982\uff1a\n        - `RETURN_OC_1`: T+1\u5f00\u76d8\u5165\u573a\uff0cT+1\u6536\u76d8\u51fa\u573a\n        - `RETURN_CC_1`: T+0\u6536\u76d8\u5165\u573a\uff0cT+1\u6536\u76d8\u51fa\u573a\n        - `RETURN_OO_1`: T+1\u5f00\u76d8\u5165\u573a\uff0cT+2\u5f00\u76d8\u51fa\u573a\n        - `RETURN_OO_5`: T+1\u5f00\u76d8\u5165\u573a\uff0cT+6\u5f00\u76d8\u51fa\u573a\n2. \u4e0d\u505a\u53bb\u6781\u503c\u3001\u6807\u51c6\u5316\u3001\u884c\u4e1a\u4e2d\u6027\u5316\u7b49\u64cd\u4f5c\n    1. `alphalens`\u7684\u5404\u53c2\u6570\u8981\u5f04\u61c2\u8fd8\u662f\u5f88\u9ebb\u70e6\u7684\uff0c\u521d\u5b66\u8005\u5982\u7ee9\u6548\u8fbe\u4e0d\u5230\u8981\u6c42\u5c31\u5f97\u6df1\u5165\u7814\u7a76\u6e90\u4ee3\u7801\u627e\u539f\u56e0\n    2. `alphainspect`\u7528\u6237\u5728\u5916\u53ef\u4ee5\u4e00\u6b21\u6027\u5168\u8ba1\u7b97\u597d\uff0c\u5982`F1_ORG, F1_ZS, F1_NEUT`\uff0c\u7136\u540e\u5728\u5206\u522b\u4f20\u4e0d\u540c\u56e0\u5b50\u8fdb\u884c\u6bd4\u8f83\u5373\u53ef\n3. \u8d44\u91d1\u5206\u914d\u53ea\u7528\u7b49\u6743\n    1. `alphalens`\u6709\u56e0\u5b50\u52a0\u6743\u3001\u591a\u7a7a\u7b49\u8bbe\u7f6e\n    2. `alphainspect`\u53ea\u63d0\u4f9b\u7b49\u6743\u4e00\u79cd\u8ba1\u7b97\u65b9\u6cd5\uff0c\u5b9e\u73b0\u7b80\u5355\n4. \u6536\u76ca\u7387\u8ba1\u7b97\u65b9\u6cd5\u4e0d\u540c\n    1. `alphalens`\u591a\u671f\u7b80\u5355\u6536\u76ca\u7387\u51e0\u4f55\u5e73\u5747\u62101\u671f\uff0c\u7136\u540e+1\u7d2f\u4e58\n    2. ~~`alphainspect`\u7531\u7528\u6237\u63d0\u4f9b1\u671f\u7b80\u5355\u6536\u76ca\u7387\uff0c\u7136\u540e\u6839\u636e\u8981\u6c42\u6301\u6709\u6216\u8c03\u4ed3\uff0c\u5f97\u5230\u65b0\u7684\u6743\u76ca\uff0c\u5faa\u73af\u8fed\u4ee3\u4e0b\u53bb\u3002\u66f4\u7cbe\u786e~~\n    3. `alphainspect`\u7531\u7528\u6237\u63d0\u4f9b\u6bcf\u671f\u6536\u76ca\u7387(\u4e5f\u53ef\u4ee5\u4f7f\u7528\u591a\u671f\u6536\u76ca\u7387\u7684\u51e0\u4f55\u5e73\u5747),\u7136\u540e\u7d2f\u52a0\u3002\uff08\u5206\u5c42\u8ba1\u7b97\u901f\u5ea6\u63d0\u9ad81~1.5\u500d\uff09\n\n## `alphainspect`\u4e0e`alphalens`\u7684\u76f8\u540c\n\n1. \u6570\u636e\u7ec4\u7ec7\u65b9\u5f0f\u76f8\u540c\u3002\u90fd\u662f\u957f\u8868\uff0c\u90fd\u662f\u56e0\u5b50\u4e0d\u79fb\u52a8\uff0c\u6536\u76ca\u7387\u8ba1\u7b97\uff0c\u7136\u540e\u540e\u79fb\u5230\u4e0e\u56e0\u5b50\u4ea7\u751f\u65f6\u95f4\u5bf9\u9f50\n2. \u4e0d\u8003\u8651\u6ed1\u70b9\u548c\u624b\u7eed\u8d39\u3002\u5355\u56e0\u5b50\u662f\u7528\u6765\u5408\u6210\u591a\u56e0\u5b50\u7684\uff0c\u56e0\u624b\u7eed\u8d39\u548c\u6ed1\u70b9\u800c\u9519\u8fc7\u90e8\u5206\u5355\u56e0\u5b50\u5c31\u53ef\u60dc\u4e86\uff0c\u5e94\u5f53\u5728\u56e0\u5b50\u5408\u6210\u540e\u7684\u56de\u6d4b\u9636\u6bb5\u624d\u8003\u8651\u624b\u7eed\u8d39\n3. \u6536\u76ca\u8ba1\u7b97\u4e0d\u6c42\u7cbe\u786e\uff0c\u53ea\u4e3a\u80fd\u6b63\u786e\u8bc4\u4ef7\u56e0\u5b50\u7ee9\u6548\n\n## \u4e8c\u6b21\u5f00\u53d1\n\n```commandline\ngit --clone https://github.com/wukan1986/alphainspect.git\ncd alphainspect\npip install -e .\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 wukan  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "factor performance visualization",
    "version": "0.5.0",
    "project_urls": null,
    "split_keywords": [
        "quant",
        " alpha",
        " performance",
        " visualization"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bcf873c025480591381ff8178f2c1cf607c15217ec8de6f18963bc947beca0b8",
                "md5": "16084d48d86bf112144e9308f4bd43fd",
                "sha256": "ae71fb2e10e1469bb057ccc41a7888183ad57d3840bc325346786a204b8b96f0"
            },
            "downloads": -1,
            "filename": "alphainspect-0.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "16084d48d86bf112144e9308f4bd43fd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 27953,
            "upload_time": "2024-12-13T14:35:32",
            "upload_time_iso_8601": "2024-12-13T14:35:32.080637Z",
            "url": "https://files.pythonhosted.org/packages/bc/f8/73c025480591381ff8178f2c1cf607c15217ec8de6f18963bc947beca0b8/alphainspect-0.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2fa0e2a6177c70b8085720e69de13488ce769a6bee373c9a032d862a6474dc5e",
                "md5": "30d8eee7dd3845ca13d3e5c914cd344a",
                "sha256": "d22aff31401988e06e0407818a14f8a1a427fc35766740f869e06b5fac0a76aa"
            },
            "downloads": -1,
            "filename": "alphainspect-0.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "30d8eee7dd3845ca13d3e5c914cd344a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 23833,
            "upload_time": "2024-12-13T14:35:34",
            "upload_time_iso_8601": "2024-12-13T14:35:34.618553Z",
            "url": "https://files.pythonhosted.org/packages/2f/a0/e2a6177c70b8085720e69de13488ce769a6bee373c9a032d862a6474dc5e/alphainspect-0.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-13 14:35:34",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "alphainspect"
}
        
Elapsed time: 0.53141s