{"id":13740576,"url":"https://github.com/stefan-jansen/empyrical-reloaded","last_synced_at":"2025-05-08T20:31:57.997Z","repository":{"id":40288567,"uuid":"341662136","full_name":"stefan-jansen/empyrical-reloaded","owner":"stefan-jansen","description":"Common financial risk and performance metrics. Used by zipline and pyfolio.","archived":false,"fork":true,"pushed_at":"2024-11-14T23:23:24.000Z","size":2809,"stargazers_count":75,"open_issues_count":3,"forks_count":30,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-25T20:22:25.333Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://empyrical.ml4trading.io/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"quantopian/empyrical","license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stefan-jansen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-23T19:12:16.000Z","updated_at":"2025-04-20T22:03:35.000Z","dependencies_parsed_at":"2023-02-08T21:00:17.263Z","dependency_job_id":null,"html_url":"https://github.com/stefan-jansen/empyrical-reloaded","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefan-jansen%2Fempyrical-reloaded","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefan-jansen%2Fempyrical-reloaded/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefan-jansen%2Fempyrical-reloaded/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefan-jansen%2Fempyrical-reloaded/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stefan-jansen","download_url":"https://codeload.github.com/stefan-jansen/empyrical-reloaded/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253144886,"owners_count":21861142,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T04:00:50.018Z","updated_at":"2025-05-08T20:31:56.985Z","avatar_url":"https://github.com/stefan-jansen.png","language":"Python","funding_links":[],"categories":["Python","Curated List","Python：量化金融第一生态"],"sub_categories":["Risk Analysis","Analytics","五、投资组合优化与风险分析"],"readme":"\u003cp align=\"center\"\u003e\n\u003ca href=\"https://empyrical.ml4trading.io\"\u003e\n\u003cimg src=\"https://i.imgur.com/PbZNeud.png\" width=\"35%\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n![PyPI](https://img.shields.io/pypi/v/empyrical-reloaded)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/empyrical-reloaded)\n\n[![Conda Version](https://img.shields.io/conda/vn/conda-forge/empyrical-reloaded.svg)](https://anaconda.org/conda-forge/empyrical-reloaded)\n[![Conda Downloads](https://img.shields.io/conda/dn/conda-forge/empyrical-reloaded.svg)](https://anaconda.org/conda-forge/empyrical-reloaded)\n\n[![PyPI Wheels](https://github.com/stefan-jansen/empyrical-reloaded/actions/workflows/build_wheels.yml/badge.svg)](https://github.com/stefan-jansen/empyrical-reloaded/actions/workflows/build_wheels.yml)\n[![Conda packages](https://github.com/stefan-jansen/empyrical-reloaded/actions/workflows/conda_package.yml/badge.svg)](https://github.com/stefan-jansen/empyrical-reloaded/actions/workflows/conda_package.yml)\n[![CI Tests](https://github.com/stefan-jansen/empyrical-reloaded/actions/workflows/unit_tests.yml/badge.svg)](https://github.com/stefan-jansen/empyrical-reloaded/actions/workflows/unit_tests.yml)\n\nCommon financial return and risk metrics in Python.\n\n## Installation\n\nempyrical requires Python 3.9+. You can install it using `pip`:\n\n```bash\npip install empyrical-reloaded\n```\n\nor `conda` from the `conda-forge` channel\n\n```bash\nconda install empyrical-reloaded -c conda-forge\n```\n\nempyrical requires and installs the following packages while executing the above commands:\n\n- numpy\u003e=1.23.5\n- pandas\u003e=1.3.0\n- scipy\u003e=0.15.1\n\n\u003e Note that Numpy\u003e=2.0 requires pandas\u003e=2.2.2. If you are using an older version of pandas, you may need to upgrade\n\u003e accordingly, otherwise you may encounter compatibility issues.\n\nOptional dependencies include [yfinance](https://github.com/ranaroussi/yfinance) to download price data\nfrom [Yahoo! Finance](https://finance.yahoo.com/)\nand [pandas-datareader](https://pandas-datareader.readthedocs.io/en/latest/) to\naccess [Fama-French](https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html) risk factors and FRED\ntreasury yields.\n\n\u003e Note that `pandas-datareader` is not compatible with Python\u003e=3.12.\n\nTo install the optional dependencies, use:\n\n```bash\npip install empyrical-reloaded[yfinance]\n```\n\nor\n\n```bash\npip install empyrical-reloaded[datreader]\n```\n\nor\n\n```bash\npip install empyrical-reloaded[yfinance,datreader]\n```\n\n## Usage\n\n### Simple Statistics\n\nEmpyrical computes basic metrics from returns and volatility to alpha and beta, Value at Risk, and Shorpe or Sortino\nratios.\n\n```python\nimport numpy as np\nfrom empyrical import max_drawdown, alpha_beta\n\nreturns = np.array([.01, .02, .03, -.4, -.06, -.02])\nbenchmark_returns = np.array([.02, .02, .03, -.35, -.05, -.01])\n\n# calculate the max drawdown\nmax_drawdown(returns)\n\n# calculate alpha and beta\nalpha, beta = alpha_beta(returns, benchmark_returns)\n```\n\n### Rolling Measures\n\nEmpyrical also aggregates return and risk metrics for rolling windows:\n\n```python\nimport numpy as np\nfrom empyrical import roll_max_drawdown\n\nreturns = np.array([.01, .02, .03, -.4, -.06, -.02])\n\n# calculate the rolling max drawdown\nroll_max_drawdown(returns, window=3)\n```\n\n### Pandas Support\n\nEmpyrical also works with both [NumPy](https://numpy.org/) arrays and [Pandas](https://pandas.pydata.org/) data\nstructures:\n\n```python\nimport pandas as pd\nfrom empyrical import roll_up_capture, capture\n\nreturns = pd.Series([.01, .02, .03, -.4, -.06, -.02])\nfactor_returns = pd.Series([.02, .01, .03, -.01, -.02, .02])\n\n# calculate a capture ratio\ncapture(returns, factor_returns)\n-0.147387712263491\n\n```\n\n### Fama-French Risk Factors\n\nEmpyrical downloads Fama-French risk factors from 1970 onward:\n\n\u003e Note: requires optional dependency `pandas-datareader` - see installation instructions above.gst\n\n```python\nimport pandas as pd\nimport empyrical as emp\n\nrisk_factors = emp.utils.get_fama_french()\n\npd.concat([risk_factors.head(), risk_factors.tail()])\n\nMkt - RF\nSMB\nHML\nRF\nMom\nDate\n1970 - 01 - 02\n00: 00:00 + 00: 00\n0.0118\n0.0129\n0.0101\n0.00029 - 0.0340\n1970 - 01 - 05\n00: 00:00 + 00: 00\n0.0059\n0.0067\n0.0072\n0.00029 - 0.0153\n1970 - 01 - 06\n00: 00:00 + 00: 00 - 0.0074\n0.0010\n0.0021\n0.00029\n0.0038\n1970 - 01 - 07\n00: 00:00 + 00: 00 - 0.0015\n0.0040 - 0.0033\n0.00029\n0.0011\n1970 - 01 - 0\n8\n00: 00:00 + 00: 00\n0.0004\n0.0018 - 0.0017\n0.00029\n0.0033\n2024 - 03 - 22\n00: 00:00 + 00: 00 - 0.0023 - 0.0087 - 0.0053\n0.00021\n0.0043\n2024 - 03 - 25\n00: 00:00 + 00: 00 - 0.0026 - 0.0024\n0.0088\n0.00021 - 0.0034\n2024 - 03 - 26\n00: 00:00 + 00: 00 - 0.0026\n0.0009 - 0.0013\n0.00021\n0.0009\n2024 - 03 - 27\n00: 00:00 + 00: 00\n0.0088\n0.0104\n0.0091\n0.00021 - 0.0134\n2024 - 03 - 28\n00: 00:00 + 00: 00\n0.0010\n0.0029\n0.0048\n0.00021 - 0.0044\n```\n\n### Asset Prices and Benchmark Returns\n\nEmpyrical use [yfinance](https://github.com/ranaroussi/yfinance) to download price data\nfrom [Yahoo! Finance](https://finance.yahoo.com/). To obtain the S\u0026P returns since 1950, use:\n\n\u003e Note: requires optional dependency `yfinance` - see installation instructions above.\n\n```python\nimport empyrical as emp\n\nsymbol = '^GSPC'\nreturns = emp.utils.get_symbol_returns_from_yahoo(symbol,\n                                                  start='1950-01-01')\n\nimport seaborn as sns  # requires separate installation\nimport matplotlib.pyplot as plt  # requires separate installation\n\nfig, axes = plt.subplots(ncols=2, figsize=(14, 5))\n\nwith sns.axes_style('whitegrid'):\n    returns.plot(ax=axes[0], rot=0, title='Time Series', legend=False)\n    sns.histplot(returns, ax=axes[1], legend=False)\naxes[1].set_title('Histogram')\nsns.despine()\nplt.tight_layout()\nplt.suptitle('Daily S\u0026P 500 Returns')\n```\n\n\u003ca href=\"https://empyrical.ml4trading.io\"\u003e\n\u003cimg src=\"https://i.imgur.com/0PSxfSI.png\" width=\"100%\"\u003e\n\u003c/a\u003e\n\n### Documentation\n\nSee the [documentation](https://empyrical.ml4trading.io) for details on the API.\n\n## Support\n\nPlease [open an issue](https://github.com/stefan-jansen/empyrical-reloaded/issues/new) for support.\n\n## Contributing\n\nPlease contribute using [Github Flow](https://guides.github.com/introduction/flow/). Create a branch, add commits,\nand [open a pull request](https://github.com/stefan-jansen/empyrical-reloaded/compare/).\n\n## Testing\n\n- install requirements\n    - \"pytest\u003e=6.2.0\",\n\n```bash\npytest tests\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefan-jansen%2Fempyrical-reloaded","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstefan-jansen%2Fempyrical-reloaded","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefan-jansen%2Fempyrical-reloaded/lists"}