{"id":1171278,"date":"2025-01-15T16:33:22","date_gmt":"2025-01-15T08:33:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171278.html"},"modified":"2025-01-15T16:33:25","modified_gmt":"2025-01-15T08:33:25","slug":"%e5%a4%8f%e6%99%ae%e7%8e%87%e5%a6%82%e4%bd%95%e5%9c%a8python%e8%ae%a1%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171278.html","title":{"rendered":"\u590f\u666e\u7387\u5982\u4f55\u5728python\u8ba1\u7b97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26073631\/b697f72e-b987-483e-bbda-533cdd9e5cce.webp\" alt=\"\u590f\u666e\u7387\u5982\u4f55\u5728python\u8ba1\u7b97\" \/><\/p>\n<p><p> <strong>\u590f\u666e\u7387\u8ba1\u7b97\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><strong>\u590f\u666e\u7387\uff08Sharpe Ratio\uff09\u662f\u8861\u91cf\u6295\u8d44\u7ec4\u5408\u8868\u73b0\u7684\u4e00\u4e2a\u91cd\u8981\u6307\u6807\uff0c\u901a\u8fc7\u8ba1\u7b97\u6295\u8d44\u7ec4\u5408\u7684\u8d85\u989d\u56de\u62a5\u4e0e\u5176\u6ce2\u52a8\u7387\u7684\u6bd4\u7387\u6765\u8bc4\u4f30\u6295\u8d44\u7ec4\u5408\u7684\u98ce\u9669\u8c03\u6574\u56de\u62a5\u3002\u901a\u5e38\uff0c\u590f\u666e\u7387\u8d8a\u9ad8\uff0c\u8868\u793a\u6295\u8d44\u7ec4\u5408\u7684\u98ce\u9669\u8c03\u6574\u56de\u62a5\u8d8a\u597d\u3002<\/strong> \u5728Python\u4e2d\u8ba1\u7b97\u590f\u666e\u7387\u7684\u65b9\u6cd5\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u83b7\u53d6\u6570\u636e\u3001\u8ba1\u7b97\u65e5\u6536\u76ca\u7387\u3001\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387\u548c\u5e74\u5316\u6ce2\u52a8\u7387\u3001\u8ba1\u7b97\u65e0\u98ce\u9669\u5229\u7387\u3001\u6700\u540e\u8ba1\u7b97\u590f\u666e\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u83b7\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u8981\u8ba1\u7b97\u590f\u666e\u7387\uff0c\u9996\u5148\u9700\u8981\u83b7\u53d6\u6295\u8d44\u7ec4\u5408\u7684\u5386\u53f2\u4ef7\u683c\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5404\u79cd\u5e93\u6765\u83b7\u53d6\u6570\u636e\uff0c\u5982pandas\u3001numpy\u3001yfinance\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528yfinance\u5e93\u83b7\u53d6\u80a1\u7968\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>ticker = &#39;AAPL&#39;<\/p>\n<p>data = yf.download(ticker, start=&#39;2020-01-01&#39;, end=&#39;2023-01-01&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8ba1\u7b97\u65e5\u6536\u76ca\u7387<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u4e86\u5386\u53f2\u6570\u636e\u540e\uff0c\u9700\u8981\u8ba1\u7b97\u6bcf\u4e2a\u4ea4\u6613\u65e5\u7684\u6536\u76ca\u7387\u3002\u65e5\u6536\u76ca\u7387\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u6bcf\u5929\u6536\u76d8\u4ef7\u7684\u53d8\u5316\u7387\u6765\u83b7\u5f97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u65e5\u6536\u76ca\u7387<\/p>\n<p>data[&#39;d<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ly_return&#39;] = data[&#39;Adj Close&#39;].pct_change()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387\u548c\u5e74\u5316\u6ce2\u52a8\u7387<\/h3>\n<\/p>\n<p><p>\u5e74\u5316\u6536\u76ca\u7387\u548c\u5e74\u5316\u6ce2\u52a8\u7387\u662f\u8ba1\u7b97\u590f\u666e\u7387\u7684\u5173\u952e\u56e0\u7d20\u4e4b\u4e00\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387<\/p>\n<p>annual_return = data[&#39;daily_return&#39;].mean() * 252<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6ce2\u52a8\u7387<\/strong><\/h2>\n<p>annual_volatility = data[&#39;daily_return&#39;].std() * (252  0.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8ba1\u7b97\u65e0\u98ce\u9669\u5229\u7387<\/h3>\n<\/p>\n<p><p>\u65e0\u98ce\u9669\u5229\u7387\u901a\u5e38\u4f7f\u7528\u7684\u662f\u56fd\u503a\u6536\u76ca\u7387\uff0c\u53ef\u4ee5\u4ece\u5b98\u65b9\u91d1\u878d\u6570\u636e\u6e90\u83b7\u53d6\uff0c\u6216\u8005\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528\u4e00\u4e2a\u5047\u5b9a\u7684\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u5b9a\u65e0\u98ce\u9669\u5229\u7387<\/p>\n<p>risk_free_rate = 0.02<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8ba1\u7b97\u590f\u666e\u7387<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u590f\u666e\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u590f\u666e\u7387<\/p>\n<p>sharpe_ratio = (annual_return - risk_free_rate) \/ annual_volatility<\/p>\n<p>print(f&#39;Sharpe Ratio: {sharpe_ratio}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387\u548c\u5e74\u5316\u6ce2\u52a8\u7387<\/h3>\n<\/p>\n<p><p><strong>\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387\uff1a<\/strong> \u5e74\u5316\u6536\u76ca\u7387\u662f\u6307\u6295\u8d44\u5728\u4e00\u5e74\u5185\u7684\u5e73\u5747\u6536\u76ca\u7387\u3002\u901a\u8fc7\u5c06\u65e5\u6536\u76ca\u7387\u7684\u5747\u503c\u4e58\u4ee5\u4e00\u5e74\u5185\u7684\u4ea4\u6613\u5929\u6570\uff08\u901a\u5e38\u4e3a252\u5929\uff09\u6765\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387\u3002\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ \\text{Annual Return} = \\text{Mean Daily Return} \\times 252 ]<\/p>\n<\/p>\n<p><p><strong>\u8ba1\u7b97\u5e74\u5316\u6ce2\u52a8\u7387\uff1a<\/strong> \u5e74\u5316\u6ce2\u52a8\u7387\u662f\u6307\u6295\u8d44\u5728\u4e00\u5e74\u5185\u7684\u6ce2\u52a8\u7a0b\u5ea6\uff0c\u4e5f\u79f0\u4e3a\u6807\u51c6\u5dee\u3002\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u65e5\u6536\u76ca\u7387\u7684\u6807\u51c6\u5dee\uff0c\u7136\u540e\u4e58\u4ee5\u4e00\u5e74\u5185\u4ea4\u6613\u5929\u6570\u7684\u5e73\u65b9\u6839\uff08\u901a\u5e38\u4e3a252\u5929\u7684\u5e73\u65b9\u6839\uff09\uff0c\u6765\u83b7\u5f97\u5e74\u5316\u6ce2\u52a8\u7387\u3002\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ \\text{Annual Volatility} = \\text{Standard Deviation of Daily Returns} \\times \\sqrt{252} ]<\/p>\n<\/p>\n<p><p>\u5e74\u5316\u6536\u76ca\u7387\u548c\u5e74\u5316\u6ce2\u52a8\u7387\u5bf9\u4e8e\u8bc4\u4f30\u6295\u8d44\u7ec4\u5408\u7684\u8868\u73b0\u81f3\u5173\u91cd\u8981\u3002\u901a\u8fc7\u8fd9\u4e9b\u8ba1\u7b97\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u6295\u8d44\u7ec4\u5408\u7684\u9884\u671f\u56de\u62a5\u548c\u98ce\u9669\uff0c\u4ece\u800c\u505a\u51fa\u66f4\u660e\u667a\u7684\u6295\u8d44\u51b3\u7b56\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u83b7\u53d6\u6570\u636e\u7684\u8be6\u7ec6\u6b65\u9aa4<\/h3>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u6570\u636e\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u4ece\u53ef\u9760\u7684\u91d1\u878d\u6570\u636e\u6e90\u83b7\u53d6\u6295\u8d44\u7ec4\u5408\u7684\u5386\u53f2\u4ef7\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Python\u7684yfinance\u5e93\u83b7\u53d6\u80a1\u7968\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u5b9a\u4e49\u80a1\u7968\u4ee3\u7801\u548c\u65f6\u95f4\u8303\u56f4<\/strong><\/h2>\n<p>ticker = &#39;AAPL&#39;<\/p>\n<p>start_date = &#39;2020-01-01&#39;<\/p>\n<p>end_date = &#39;2023-01-01&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = yf.download(ticker, start=start_date, end=end_date)<\/p>\n<h2><strong>\u67e5\u770b\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528yfinance\u5e93\u6765\u4e0b\u8f7d\u82f9\u679c\u516c\u53f8\uff08AAPL\uff09\u7684\u5386\u53f2\u4ef7\u683c\u6570\u636e\uff0c\u65f6\u95f4\u8303\u56f4\u662f\u4ece2020\u5e741\u67081\u65e5\u52302023\u5e741\u67081\u65e5\u3002\u4e0b\u8f7d\u7684\u6570\u636e\u4fdd\u5b58\u5728\u4e00\u4e2apandas DataFrame\u4e2d\uff0c\u5305\u542b\u6bcf\u4e2a\u4ea4\u6613\u65e5\u7684\u5f00\u76d8\u4ef7\u3001\u6700\u9ad8\u4ef7\u3001\u6700\u4f4e\u4ef7\u3001\u6536\u76d8\u4ef7\u3001\u8c03\u6574\u6536\u76d8\u4ef7\u548c\u4ea4\u6613\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8ba1\u7b97\u65e5\u6536\u76ca\u7387\u7684\u8be6\u7ec6\u6b65\u9aa4<\/h3>\n<\/p>\n<p><p>\u8ba1\u7b97\u65e5\u6536\u76ca\u7387\u662f\u590f\u666e\u7387\u8ba1\u7b97\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\u3002\u65e5\u6536\u76ca\u7387\u8868\u793a\u6bcf\u4e2a\u4ea4\u6613\u65e5\u7684\u4ef7\u683c\u53d8\u5316\u7387\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u6bcf\u5929\u6536\u76d8\u4ef7\u7684\u53d8\u5316\u7387\u6765\u83b7\u5f97\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u8ba1\u7b97\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u65e5\u6536\u76ca\u7387<\/p>\n<p>data[&#39;daily_return&#39;] = data[&#39;Adj Close&#39;].pct_change()<\/p>\n<h2><strong>\u67e5\u770b\u6570\u636e<\/strong><\/h2>\n<p>print(data[[&#39;Adj Close&#39;, &#39;daily_return&#39;]].head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pandas\u7684<code>pct_change<\/code>\u65b9\u6cd5\u8ba1\u7b97\u6bcf\u65e5\u8c03\u6574\u6536\u76d8\u4ef7\u7684\u53d8\u5316\u7387\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u65b0\u7684\u5217&#39;daily_return&#39;\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387\u548c\u5e74\u5316\u6ce2\u52a8\u7387\u7684\u8be6\u7ec6\u6b65\u9aa4<\/h3>\n<\/p>\n<p><p>\u5e74\u5316\u6536\u76ca\u7387\u548c\u5e74\u5316\u6ce2\u52a8\u7387\u662f\u8861\u91cf\u6295\u8d44\u8868\u73b0\u7684\u4e24\u4e2a\u91cd\u8981\u6307\u6807\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u8ba1\u7b97\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387<\/p>\n<p>annual_return = data[&#39;daily_return&#39;].mean() * 252<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6ce2\u52a8\u7387<\/strong><\/h2>\n<p>annual_volatility = data[&#39;daily_return&#39;].std() * (252  0.5)<\/p>\n<h2><strong>\u67e5\u770b\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&#39;Annual Return: {annual_return}&#39;)<\/p>\n<p>print(f&#39;Annual Volatility: {annual_volatility}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8ba1\u7b97\u65e5\u6536\u76ca\u7387\u7684\u5e73\u5747\u503c\uff0c\u5e76\u4e58\u4ee5\u4e00\u5e74\u4e2d\u7684\u4ea4\u6613\u5929\u6570\uff08252\u5929\uff09\uff0c\u5f97\u5230\u5e74\u5316\u6536\u76ca\u7387\u3002\u7136\u540e\uff0c\u6211\u4eec\u8ba1\u7b97\u65e5\u6536\u76ca\u7387\u7684\u6807\u51c6\u5dee\uff0c\u5e76\u4e58\u4ee5\u4e00\u5e74\u4e2d\u4ea4\u6613\u5929\u6570\u7684\u5e73\u65b9\u6839\uff0c\u5f97\u5230\u5e74\u5316\u6ce2\u52a8\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8ba1\u7b97\u65e0\u98ce\u9669\u5229\u7387\u7684\u8be6\u7ec6\u6b65\u9aa4<\/h3>\n<\/p>\n<p><p>\u65e0\u98ce\u9669\u5229\u7387\u901a\u5e38\u4f7f\u7528\u7684\u662f\u56fd\u503a\u6536\u76ca\u7387\uff0c\u53ef\u4ee5\u4ece\u5b98\u65b9\u91d1\u878d\u6570\u636e\u6e90\u83b7\u53d6\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5047\u8bbe\u4e00\u4e2a\u65e0\u98ce\u9669\u5229\u7387\u4e3a2%\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u5b9a\u65e0\u98ce\u9669\u5229\u7387<\/p>\n<p>risk_free_rate = 0.02<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u8be5\u4ece\u53ef\u9760\u7684\u91d1\u878d\u6570\u636e\u6e90\u83b7\u53d6\u6700\u65b0\u7684\u65e0\u98ce\u9669\u5229\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8ba1\u7b97\u590f\u666e\u7387\u7684\u8be6\u7ec6\u6b65\u9aa4<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u590f\u666e\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u590f\u666e\u7387<\/p>\n<p>sharpe_ratio = (annual_return - risk_free_rate) \/ annual_volatility<\/p>\n<h2><strong>\u67e5\u770b\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&#39;Sharpe Ratio: {sharpe_ratio}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5e74\u5316\u6536\u76ca\u7387\u51cf\u53bb\u65e0\u98ce\u9669\u5229\u7387\uff0c\u5f97\u5230\u8d85\u989d\u56de\u62a5\uff0c\u7136\u540e\u5c06\u5176\u9664\u4ee5\u5e74\u5316\u6ce2\u52a8\u7387\uff0c\u5f97\u5230\u590f\u666e\u7387\u3002\u590f\u666e\u7387\u8d8a\u9ad8\uff0c\u8868\u793a\u6295\u8d44\u7ec4\u5408\u7684\u98ce\u9669\u8c03\u6574\u56de\u62a5\u8d8a\u597d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5168\u9762\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u590f\u666e\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u5b9a\u4e49\u80a1\u7968\u4ee3\u7801\u548c\u65f6\u95f4\u8303\u56f4<\/strong><\/h2>\n<p>ticker = &#39;AAPL&#39;<\/p>\n<p>start_date = &#39;2020-01-01&#39;<\/p>\n<p>end_date = &#39;2023-01-01&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = yf.download(ticker, start=start_date, end=end_date)<\/p>\n<h2><strong>\u8ba1\u7b97\u65e5\u6536\u76ca\u7387<\/strong><\/h2>\n<p>data[&#39;daily_return&#39;] = data[&#39;Adj Close&#39;].pct_change()<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387<\/strong><\/h2>\n<p>annual_return = data[&#39;daily_return&#39;].mean() * 252<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6ce2\u52a8\u7387<\/strong><\/h2>\n<p>annual_volatility = data[&#39;daily_return&#39;].std() * (252  0.5)<\/p>\n<h2><strong>\u5047\u5b9a\u65e0\u98ce\u9669\u5229\u7387<\/strong><\/h2>\n<p>risk_free_rate = 0.02<\/p>\n<h2><strong>\u8ba1\u7b97\u590f\u666e\u7387<\/strong><\/h2>\n<p>sharpe_ratio = (annual_return - risk_free_rate) \/ annual_volatility<\/p>\n<h2><strong>\u67e5\u770b\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&#39;Annual Return: {annual_return}&#39;)<\/p>\n<p>print(f&#39;Annual Volatility: {annual_volatility}&#39;)<\/p>\n<p>print(f&#39;Sharpe Ratio: {sharpe_ratio}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5168\u9762\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u4ece\u6570\u636e\u83b7\u53d6\u5230\u590f\u666e\u7387\u8ba1\u7b97\u7684\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8ba1\u7b97\u51fa\u4efb\u610f\u80a1\u7968\u6216\u6295\u8d44\u7ec4\u5408\u7684\u590f\u666e\u7387\uff0c\u4ee5\u8861\u91cf\u5176\u98ce\u9669\u8c03\u6574\u56de\u62a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u591a\u79cd\u6295\u8d44\u7ec4\u5408\u8ba1\u7b97\u590f\u666e\u7387<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6295\u8d44\u8005\u901a\u5e38\u4f1a\u540c\u65f6\u6295\u8d44\u4e8e\u591a\u4e2a\u80a1\u7968\u6216\u8d44\u4ea7\uff0c\u5f62\u6210\u4e00\u4e2a\u6295\u8d44\u7ec4\u5408\u3002\u8ba1\u7b97\u6574\u4e2a\u6295\u8d44\u7ec4\u5408\u7684\u590f\u666e\u7387\u53ef\u4ee5\u5e2e\u52a9\u6295\u8d44\u8005\u66f4\u597d\u5730\u8bc4\u4f30\u5176\u6574\u4f53\u8868\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u8ba1\u7b97\u591a\u79cd\u80a1\u7968\u7ec4\u5408\u7684\u590f\u666e\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u5b9a\u4e49\u80a1\u7968\u4ee3\u7801\u548c\u65f6\u95f4\u8303\u56f4<\/strong><\/h2>\n<p>tickers = [&#39;AAPL&#39;, &#39;MSFT&#39;, &#39;GOOGL&#39;]<\/p>\n<p>start_date = &#39;2020-01-01&#39;<\/p>\n<p>end_date = &#39;2023-01-01&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = yf.download(tickers, start=start_date, end=end_date)[&#39;Adj Close&#39;]<\/p>\n<h2><strong>\u8ba1\u7b97\u65e5\u6536\u76ca\u7387<\/strong><\/h2>\n<p>daily_returns = data.pct_change()<\/p>\n<h2><strong>\u8ba1\u7b97\u6295\u8d44\u7ec4\u5408\u7684\u65e5\u6536\u76ca\u7387\uff08\u5047\u8bbe\u7b49\u6743\u91cd\uff09<\/strong><\/h2>\n<p>portfolio_daily_return = daily_returns.mean(axis=1)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387<\/strong><\/h2>\n<p>annual_return = portfolio_daily_return.mean() * 252<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6ce2\u52a8\u7387<\/strong><\/h2>\n<p>annual_volatility = portfolio_daily_return.std() * (252  0.5)<\/p>\n<h2><strong>\u5047\u5b9a\u65e0\u98ce\u9669\u5229\u7387<\/strong><\/h2>\n<p>risk_free_rate = 0.02<\/p>\n<h2><strong>\u8ba1\u7b97\u590f\u666e\u7387<\/strong><\/h2>\n<p>sharpe_ratio = (annual_return - risk_free_rate) \/ annual_volatility<\/p>\n<h2><strong>\u67e5\u770b\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&#39;Annual Return: {annual_return}&#39;)<\/p>\n<p>print(f&#39;Annual Volatility: {annual_volatility}&#39;)<\/p>\n<p>print(f&#39;Sharpe Ratio: {sharpe_ratio}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e0b\u8f7d\u4e86\u82f9\u679c\u516c\u53f8\uff08AAPL\uff09\u3001\u5fae\u8f6f\u516c\u53f8\uff08MSFT\uff09\u548c\u8c37\u6b4c\u516c\u53f8\uff08GOOGL\uff09\u7684\u5386\u53f2\u4ef7\u683c\u6570\u636e\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u80a1\u7968\u7684\u65e5\u6536\u76ca\u7387\u3002\u7136\u540e\uff0c\u6211\u4eec\u5047\u8bbe\u6295\u8d44\u7ec4\u5408\u4e2d\u6bcf\u4e2a\u80a1\u7968\u7684\u6743\u91cd\u76f8\u7b49\uff0c\u8ba1\u7b97\u6574\u4e2a\u6295\u8d44\u7ec4\u5408\u7684\u65e5\u6536\u76ca\u7387\u3002\u63a5\u7740\uff0c\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387\u548c\u5e74\u5316\u6ce2\u52a8\u7387\uff0c\u5e76\u6700\u7ec8\u8ba1\u7b97\u51fa\u6295\u8d44\u7ec4\u5408\u7684\u590f\u666e\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u8003\u8651\u4e0d\u540c\u7684\u6743\u91cd\u5206\u914d<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u6295\u8d44\u4e2d\uff0c\u4e0d\u540c\u7684\u8d44\u4ea7\u901a\u5e38\u4f1a\u6709\u4e0d\u540c\u7684\u6743\u91cd\u5206\u914d\uff0c\u4ee5\u5b9e\u73b0\u7279\u5b9a\u7684\u6295\u8d44\u76ee\u6807\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u8ba1\u7b97\u5177\u6709\u4e0d\u540c\u6743\u91cd\u5206\u914d\u7684\u6295\u8d44\u7ec4\u5408\u7684\u590f\u666e\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<p>import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u80a1\u7968\u4ee3\u7801\u548c\u65f6\u95f4\u8303\u56f4<\/strong><\/h2>\n<p>tickers = [&#39;AAPL&#39;, &#39;MSFT&#39;, &#39;GOOGL&#39;]<\/p>\n<p>weights = [0.4, 0.4, 0.2]<\/p>\n<p>start_date = &#39;2020-01-01&#39;<\/p>\n<p>end_date = &#39;2023-01-01&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = yf.download(tickers, start=start_date, end=end_date)[&#39;Adj Close&#39;]<\/p>\n<h2><strong>\u8ba1\u7b97\u65e5\u6536\u76ca\u7387<\/strong><\/h2>\n<p>daily_returns = data.pct_change()<\/p>\n<h2><strong>\u8ba1\u7b97\u6295\u8d44\u7ec4\u5408\u7684\u65e5\u6536\u76ca\u7387<\/strong><\/h2>\n<p>portfolio_daily_return = (daily_returns * weights).sum(axis=1)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387<\/strong><\/h2>\n<p>annual_return = portfolio_daily_return.mean() * 252<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6ce2\u52a8\u7387<\/strong><\/h2>\n<p>annual_volatility = portfolio_daily_return.std() * (252  0.5)<\/p>\n<h2><strong>\u5047\u5b9a\u65e0\u98ce\u9669\u5229\u7387<\/strong><\/h2>\n<p>risk_free_rate = 0.02<\/p>\n<h2><strong>\u8ba1\u7b97\u590f\u666e\u7387<\/strong><\/h2>\n<p>sharpe_ratio = (annual_return - risk_free_rate) \/ annual_volatility<\/p>\n<h2><strong>\u67e5\u770b\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&#39;Annual Return: {annual_return}&#39;)<\/p>\n<p>print(f&#39;Annual Volatility: {annual_volatility}&#39;)<\/p>\n<p>print(f&#39;Sharpe Ratio: {sharpe_ratio}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u80a1\u7968\u5206\u914d\u4e86\u4e0d\u540c\u7684\u6743\u91cd\uff08AAPL\u548cMSFT\u5404\u536040%\uff0cGOOGL\u536020%\uff09\uff0c\u5e76\u8ba1\u7b97\u6574\u4e2a\u6295\u8d44\u7ec4\u5408\u7684\u65e5\u6536\u76ca\u7387\u3002\u63a5\u7740\uff0c\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387\u548c\u5e74\u5316\u6ce2\u52a8\u7387\uff0c\u5e76\u6700\u7ec8\u8ba1\u7b97\u51fa\u6295\u8d44\u7ec4\u5408\u7684\u590f\u666e\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f18\u5316\u6295\u8d44\u7ec4\u5408\u7684\u590f\u666e\u7387<\/h3>\n<\/p>\n<p><p>\u6295\u8d44\u8005\u901a\u5e38\u5e0c\u671b\u901a\u8fc7\u4f18\u5316\u8d44\u4ea7\u914d\u7f6e\u6765\u6700\u5927\u5316\u590f\u666e\u7387\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528scipy.optimize\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f18\u5316\u6295\u8d44\u7ec4\u5408\u7684\u6743\u91cd\u4ee5\u6700\u5927\u5316\u590f\u666e\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<p>import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<p>from scipy.optimize import minimize<\/p>\n<h2><strong>\u5b9a\u4e49\u80a1\u7968\u4ee3\u7801\u548c\u65f6\u95f4\u8303\u56f4<\/strong><\/h2>\n<p>tickers = [&#39;AAPL&#39;, &#39;MSFT&#39;, &#39;GOOGL&#39;]<\/p>\n<p>start_date = &#39;2020-01-01&#39;<\/p>\n<p>end_date = &#39;2023-01-01&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = yf.download(tickers, start=start_date, end=end_date)[&#39;Adj Close&#39;]<\/p>\n<h2><strong>\u8ba1\u7b97\u65e5\u6536\u76ca\u7387<\/strong><\/h2>\n<p>daily_returns = data.pct_change().dropna()<\/p>\n<h2><strong>\u5b9a\u4e49\u76ee\u6807\u51fd\u6570\uff08\u8d1f\u590f\u666e\u7387\uff09<\/strong><\/h2>\n<p>def negative_sharpe(weights, daily_returns, risk_free_rate=0.02):<\/p>\n<p>    portfolio_return = np.sum(daily_returns.mean() * weights) * 252<\/p>\n<p>    portfolio_volatility = np.sqrt(np.dot(weights.T, np.dot(daily_returns.cov() * 252, weights)))<\/p>\n<p>    sharpe_ratio = (portfolio_return - risk_free_rate) \/ portfolio_volatility<\/p>\n<p>    return -sharpe_ratio<\/p>\n<h2><strong>\u7ea6\u675f\u6761\u4ef6\uff1a\u6240\u6709\u6743\u91cd\u4e4b\u548c\u4e3a1<\/strong><\/h2>\n<p>constraints = ({&#39;type&#39;: &#39;eq&#39;, &#39;fun&#39;: lambda weights: np.sum(weights) - 1})<\/p>\n<h2><strong>\u8fb9\u754c\u6761\u4ef6\uff1a\u6bcf\u4e2a\u6743\u91cd\u57280\u548c1\u4e4b\u95f4<\/strong><\/h2>\n<p>bounds = tuple((0, 1) for _ in range(len(tickers)))<\/p>\n<h2><strong>\u521d\u59cb\u6743\u91cd<\/strong><\/h2>\n<p>initial_weights = [1.0 \/ len(tickers)] * len(tickers)<\/p>\n<h2><strong>\u4f18\u5316\u6743\u91cd<\/strong><\/h2>\n<p>result = minimize(negative_sharpe, initial_weights, args=(daily_returns,), method=&#39;SLSQP&#39;, bounds=bounds, constraints=constraints)<\/p>\n<h2><strong>\u6700\u4f18\u6743\u91cd<\/strong><\/h2>\n<p>optimal_weights = result.x<\/p>\n<h2><strong>\u67e5\u770b\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&#39;Optimal Weights: {optimal_weights}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u76ee\u6807\u51fd\u6570\uff0c\u5373\u8d1f\u590f\u666e\u7387\uff0c\u7136\u540e\u4f7f\u7528scipy.optimize\u5e93\u7684minimize\u51fd\u6570\u6765\u4f18\u5316\u6295\u8d44\u7ec4\u5408\u7684\u6743\u91cd\u3002\u76ee\u6807\u662f\u6700\u5c0f\u5316\u8d1f\u590f\u666e\u7387\uff0c\u5373\u6700\u5927\u5316\u590f\u666e\u7387\u3002\u6211\u4eec\u8fd8\u8bbe\u7f6e\u4e86\u7ea6\u675f\u6761\u4ef6\uff0c\u786e\u4fdd\u6240\u6709\u6743\u91cd\u4e4b\u548c\u4e3a1\uff0c\u5e76\u8bbe\u7f6e\u4e86\u6bcf\u4e2a\u6743\u91cd\u57280\u548c1\u4e4b\u95f4\u7684\u8fb9\u754c\u6761\u4ef6\u3002\u4f18\u5316\u540e\uff0c\u6211\u4eec\u5f97\u5230\u4e86\u6700\u4f18\u7684\u6743\u91cd\u5206\u914d\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u4e0d\u540c\u7684\u65e0\u98ce\u9669\u5229\u7387<\/h3>\n<\/p>\n<p><p>\u65e0\u98ce\u9669\u5229\u7387\u5728\u590f\u666e\u7387\u8ba1\u7b97\u4e2d\u8d77\u7740\u91cd\u8981\u4f5c\u7528\uff0c\u4e0d\u540c\u7684\u65e0\u98ce\u9669\u5229\u7387\u4f1a\u5f71\u54cd\u590f\u666e\u7387\u7684\u7ed3\u679c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5f53\u524d\u5e02\u573a\u73af\u5883\u9009\u62e9\u9002\u5f53\u7684\u65e0\u98ce\u9669\u5229\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7684\u65e0\u98ce\u9669\u5229\u7387\u8ba1\u7b97\u590f\u666e\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u5b9a\u4e49\u80a1\u7968\u4ee3\u7801\u548c\u65f6\u95f4\u8303\u56f4<\/strong><\/h2>\n<p>ticker = &#39;AAPL&#39;<\/p>\n<p>start_date = &#39;2020-01-01&#39;<\/p>\n<p>end_date = &#39;2023-01-01&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = yf.download(ticker, start=start_date, end=end_date)<\/p>\n<h2><strong>\u8ba1\u7b97\u65e5\u6536\u76ca\u7387<\/strong><\/h2>\n<p>data[&#39;daily_return&#39;] = data[&#39;Adj Close&#39;].pct_change()<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387<\/strong><\/h2>\n<p>annual_return = data[&#39;daily_return&#39;].mean() * 252<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6ce2\u52a8\u7387<\/strong><\/h2>\n<p>annual_volatility = data[&#39;daily_return&#39;].std() * (252  0.5)<\/p>\n<h2><strong>\u4e0d\u540c\u7684\u65e0\u98ce\u9669\u5229\u7387<\/strong><\/h2>\n<p>risk_free_rates = [0.01, 0.02, 0.03]<\/p>\n<h2><strong>\u8ba1\u7b97\u4e0d\u540c\u65e0\u98ce\u9669\u5229\u7387\u4e0b\u7684\u590f\u666e\u7387<\/strong><\/h2>\n<p>for rf in risk_free_rates:<\/p>\n<p>    sharpe_ratio = (annual_return - rf) \/ annual_volatility<\/p>\n<p>    print(f&#39;Risk-Free Rate: {rf}, Sharpe Ratio: {sharpe_ratio}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u65e0\u98ce\u9669\u5229\u7387\uff081%\u30012%\u548c3%\uff09\uff0c\u5e76\u8ba1\u7b97\u4e86\u6bcf\u79cd\u65e0\u98ce\u9669\u5229\u7387\u4e0b\u7684\u590f\u666e\u7387\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u4e0d\u540c\u7684\u65e0\u98ce\u9669\u5229\u7387\u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u590f\u666e\u7387\u7ed3\u679c\uff0c\u6295\u8d44\u8005\u5e94\u6839\u636e\u5f53\u524d\u5e02\u573a\u73af\u5883\u9009\u62e9\u9002\u5f53\u7684\u65e0\u98ce\u9669\u5229\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u8003\u8651\u4ea4\u6613\u6210\u672c\u548c\u7a0e\u6536<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u6295\u8d44\u4e2d\uff0c\u4ea4\u6613\u6210\u672c\u548c\u7a0e\u6536\u4f1a\u5f71\u54cd\u6295\u8d44\u7ec4\u5408\u7684\u5b9e\u9645\u6536\u76ca\u7387\u3002\u56e0\u6b64\uff0c\u5728\u8ba1\u7b97\u590f\u666e\u7387\u65f6\uff0c\u8003\u8651\u4ea4\u6613\u6210\u672c\u548c\u7a0e\u6536\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u8ba1\u7b97\u590f\u666e\u7387\u65f6\u8003\u8651\u4ea4\u6613\u6210\u672c\u548c\u7a0e\u6536\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yfinance as yf<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u5b9a\u4e49\u80a1\u7968\u4ee3\u7801\u548c\u65f6\u95f4\u8303\u56f4<\/strong><\/h2>\n<p>ticker = &#39;AAPL&#39;<\/p>\n<p>start_date = &#39;2020-01-01&#39;<\/p>\n<p>end_date = &#39;2023-01-01&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = yf.download(ticker, start=start_date, end=end_date)<\/p>\n<h2><strong>\u8ba1\u7b97\u65e5\u6536\u76ca\u7387<\/strong><\/h2>\n<p>data[&#39;daily_return&#39;] = data[&#39;Adj Close&#39;].pct_change()<\/p>\n<h2><strong>\u5047\u5b9a\u4ea4\u6613\u6210\u672c\u548c\u7a0e\u6536\uff08\u6bcf\u6b21\u4ea4\u6613\u7684\u767e\u5206\u6bd4\uff09<\/strong><\/h2>\n<p>transaction_cost = 0.001<\/p>\n<p>tax_rate = 0.2<\/p>\n<h2><strong>\u8ba1\u7b97\u6263\u9664\u4ea4\u6613\u6210\u672c\u548c\u7a0e\u6536\u540e\u7684\u65e5\u6536\u76ca\u7387<\/strong><\/h2>\n<p>data[&#39;net_daily_return&#39;] = data[&#39;daily_return&#39;] - transaction_cost<\/p>\n<p>data[&#39;net_daily_return&#39;] = data[&#39;net_daily_return&#39;] * (1 - tax_rate)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387<\/strong><\/h2>\n<p>annual_return = data[&#39;net_daily_return&#39;].mean()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662f\u590f\u666e\u7387\uff0c\u5b83\u7684\u91cd\u8981\u6027\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u590f\u666e\u7387\u662f\u8bc4\u4f30\u6295\u8d44\u56de\u62a5\u4e0e\u98ce\u9669\u4e4b\u95f4\u5173\u7cfb\u7684\u5de5\u5177\uff0c\u901a\u5e38\u7528\u4e8e\u6bd4\u8f83\u4e0d\u540c\u6295\u8d44\u7684\u8868\u73b0\u3002\u5b83\u901a\u8fc7\u8ba1\u7b97\u6bcf\u5355\u4f4d\u98ce\u9669\u6240\u83b7\u5f97\u7684\u8d85\u989d\u56de\u62a5\u6765\u5e2e\u52a9\u6295\u8d44\u8005\u4e86\u89e3\u6295\u8d44\u7684\u98ce\u9669\u8c03\u6574\u540e\u8868\u73b0\u3002\u8f83\u9ad8\u7684\u590f\u666e\u7387\u901a\u5e38\u8868\u793a\u66f4\u597d\u7684\u98ce\u9669\u8c03\u6574\u56de\u62a5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8ba1\u7b97\u590f\u666e\u7387\u9700\u8981\u54ea\u4e9b\u5e93\u548c\u6570\u636e\uff1f<\/strong><br \/>\u8ba1\u7b97\u590f\u666e\u7387\u901a\u5e38\u9700\u8981\u4f7f\u7528NumPy\u548cPandas\u5e93\u6765\u5904\u7406\u6570\u636e\uff0c\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u6570\u636e\u65b9\u9762\uff0c\u60a8\u9700\u8981\u6295\u8d44\u7ec4\u5408\u7684\u6536\u76ca\u7387\u6570\u636e\u548c\u65e0\u98ce\u9669\u5229\u7387\u3002\u6536\u76ca\u7387\u6570\u636e\u53ef\u4ee5\u4ece\u91d1\u878d\u6570\u636e\u63d0\u4f9b\u5546\u83b7\u53d6\uff0c\u5e38\u89c1\u7684\u683c\u5f0f\u5305\u62ecCSV\u6216Excel\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u590f\u666e\u7387\u7684\u8ba1\u7b97\uff1f<\/strong><br \/>\u5b9e\u73b0\u590f\u666e\u7387\u7684\u8ba1\u7b97\u6b65\u9aa4\u5305\u62ec\uff1a\u9996\u5148\uff0c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u5982NumPy\u548cPandas\u3002\u5176\u6b21\uff0c\u8bfb\u53d6\u6536\u76ca\u7387\u6570\u636e\u5e76\u8ba1\u7b97\u8d85\u989d\u6536\u76ca\uff0c\u5373\u6295\u8d44\u7ec4\u5408\u6536\u76ca\u7387\u51cf\u53bb\u65e0\u98ce\u9669\u5229\u7387\u3002\u63a5\u4e0b\u6765\uff0c\u8ba1\u7b97\u8d85\u989d\u6536\u76ca\u7684\u6807\u51c6\u5dee\uff0c\u6700\u540e\u4f7f\u7528\u516c\u5f0f\u8ba1\u7b97\u590f\u666e\u7387\uff1a\u590f\u666e\u7387 = \u8d85\u989d\u6536\u76ca\u7684\u5747\u503c \/ \u8d85\u989d\u6536\u76ca\u7684\u6807\u51c6\u5dee\u3002\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u51fd\u6570\u6765\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u793a\u4f8b\u4ee3\u7801\u53ef\u4ee5\u53c2\u8003\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u590f\u666e\u7387\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport pandas as pd\n\n# \u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846df\uff0c\u5305\u542b\u6295\u8d44\u7ec4\u5408\u7684\u65e5\u6536\u76ca\u7387\n# df[&#39;returns&#39;]\u662f\u6295\u8d44\u7ec4\u5408\u7684\u6536\u76ca\u7387\uff0crf\u662f\u65e0\u98ce\u9669\u5229\u7387\nrf = 0.01  # \u65e0\u98ce\u9669\u5229\u7387\ndf[&#39;excess_returns&#39;] = df[&#39;returns&#39;] - rf\n\n# \u8ba1\u7b97\u590f\u666e\u7387\nsharpe_ratio = df[&#39;excess_returns&#39;].mean() \/ df[&#39;excess_returns&#39;].std()\nprint(f&quot;\u590f\u666e\u7387: {sharpe_ratio}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u51fa\u590f\u666e\u7387\uff0c\u8fdb\u4e00\u6b65\u8bc4\u4f30\u60a8\u7684\u6295\u8d44\u7ec4\u5408\u8868\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u590f\u666e\u7387\u8ba1\u7b97\u65b9\u6cd5 \u590f\u666e\u7387\uff08Sharpe Ratio\uff09\u662f\u8861\u91cf\u6295\u8d44\u7ec4\u5408\u8868\u73b0\u7684\u4e00\u4e2a\u91cd\u8981\u6307\u6807\uff0c\u901a\u8fc7\u8ba1\u7b97\u6295\u8d44\u7ec4\u5408\u7684\u8d85\u989d\u56de\u62a5 [&hellip;]","protected":false},"author":3,"featured_media":1171286,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171278"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1171278"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171278\/revisions"}],"predecessor-version":[{"id":1171287,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171278\/revisions\/1171287"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1171286"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1171278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1171278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1171278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}