{"id":2922,"date":"2025-03-18T00:41:13","date_gmt":"2025-03-17T16:41:13","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2922"},"modified":"2025-03-18T00:41:13","modified_gmt":"2025-03-17T16:41:13","slug":"python-%e9%87%8f%e5%8c%96","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/18\/python-%e9%87%8f%e5%8c%96\/","title":{"rendered":"Python \u91cf\u5316"},"content":{"rendered":"\n<p>Python \u5728\u91cf\u5316\u91d1\u878d\u9886\u57df\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u91cf\u5316\u91d1\u878d\uff08Quantitative Finance\uff09\u4e3b\u8981\u4f9d\u8d56\u4e8e\u6570\u5b66\u6a21\u578b\u3001\u7edf\u8ba1\u5b66\u65b9\u6cd5\u3001\u7b97\u6cd5\u3001\u4ee5\u53ca\u7f16\u7a0b\u6280\u80fd\u6765\u8fdb\u884c\u6570\u636e\u5206\u6790\u3001\u98ce\u9669\u7ba1\u7406\u3001\u6295\u8d44\u7b56\u7565\u8bbe\u8ba1\u7b49\u5de5\u4f5c\u3002Python \u4f5c\u4e3a\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5728\u91cf\u5316\u91d1\u878d\u9886\u57df\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>\u91cf\u5316\u91d1\u878d\u7b80\u4ecb<\/strong><\/h2>\n\n\n\n<p>\u91cf\u5316\u91d1\u878d\u662f\u901a\u8fc7\u6570\u5b66\u6a21\u578b\u548c\u8ba1\u7b97\u6280\u672f\u6765\u5206\u6790\u548c\u7ba1\u7406\u91d1\u878d\u5e02\u573a\u6570\u636e\uff0c\u8bbe\u8ba1\u4ea4\u6613\u7b56\u7565\uff0c\u8fdb\u884c\u8d44\u4ea7\u5b9a\u4ef7\u3001\u98ce\u9669\u8bc4\u4f30\u3001\u6295\u8d44\u7ec4\u5408\u4f18\u5316\u7b49\u3002\u91cf\u5316\u91d1\u878d\u4e0d\u4ec5\u9650\u4e8e\u8bc1\u5238\u4ea4\u6613\uff0c\u4e5f\u5305\u62ec\u671f\u8d27\u3001\u5916\u6c47\u3001\u884d\u751f\u54c1\u7b49\u9886\u57df\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>Python \u5728\u91cf\u5316\u91d1\u878d\u4e2d\u7684\u4f18\u52bf<\/strong><\/h2>\n\n\n\n<p>Python \u56e0\u5176\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u5f3a\u5927\u7684\u5e93\u652f\u6301\uff0c\u6210\u4e3a\u4e86\u91cf\u5316\u91d1\u878d\u4e2d\u6700\u5e38\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u3002\u4ee5\u4e0b\u662f Python \u5728\u91cf\u5316\u9886\u57df\u7684\u4e00\u4e9b\u4e3b\u8981\u4f18\u52bf\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7b80\u6d01\u6613\u7528<\/strong>\uff1aPython \u8bed\u8a00\u672c\u8eab\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\uff0c\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\u3002<\/li>\n\n\n\n<li><strong>\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b<\/strong>\uff1aPython \u63d0\u4f9b\u4e86\u591a\u79cd\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5982 <code>pandas<\/code>\u3001<code>numpy<\/code> \u7b49\uff0c\u53ef\u4ee5\u5904\u7406\u5927\u91cf\u7684\u91d1\u878d\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u4e30\u5bcc\u7684\u91d1\u878d\u5206\u6790\u5de5\u5177<\/strong>\uff1aPython \u62e5\u6709\u5927\u91cf\u4e0e\u91d1\u878d\u76f8\u5173\u7684\u5e93\uff0c\u5982 <code>QuantLib<\/code>\u3001<code>TA-Lib<\/code>\u3001<code>Zipline<\/code> \u7b49\uff0c\u80fd\u591f\u5e2e\u52a9\u5feb\u901f\u5b9e\u73b0\u590d\u6742\u7684\u91d1\u878d\u6a21\u578b\u3002<\/li>\n\n\n\n<li><strong>\u5f3a\u5927\u7684\u53ef\u89c6\u5316\u529f\u80fd<\/strong>\uff1a\u4f7f\u7528 <code>matplotlib<\/code>\u3001<code>seaborn<\/code> \u7b49\u5e93\uff0cPython \u80fd\u591f\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u5e2e\u52a9\u5206\u6790\u548c\u5c55\u793a\u91d1\u878d\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>Python \u91cf\u5316\u91d1\u878d\u5e38\u7528\u7684\u5e93\u548c\u5de5\u5177<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 <strong>Pandas<\/strong><\/h3>\n\n\n\n<p><code>pandas<\/code> \u662f Python \u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5c24\u5176\u9002\u5408\u5904\u7406\u548c\u5206\u6790\u91d1\u878d\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982 DataFrame\uff09\u548c\u8bb8\u591a\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff08\u5982\u65f6\u95f4\u5e8f\u5217\u5904\u7406\u3001\u6570\u636e\u5408\u5e76\u3001\u5206\u7ec4\u805a\u5408\u7b49\uff09\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\n\n# \u8bfb\u53d6\u5386\u53f2\u80a1\u4ef7\u6570\u636e\uff08CSV \u6587\u4ef6\uff09\ndf = pd.read_csv('stock_data.csv')\n\n# \u67e5\u770b\u6570\u636e\u7ed3\u6784\nprint(df.head())\n\n# \u8ba1\u7b97\u80a1\u4ef7\u7684\u65e5\u6536\u76ca\u7387\ndf&#91;'daily_return'] = df&#91;'Close'].pct_change()\n\n# \u8ba1\u7b97\u5e76\u8f93\u51fa\u7d2f\u8ba1\u6536\u76ca\u7387\ndf&#91;'cumulative_return'] = (1 + df&#91;'daily_return']).cumprod()\nprint(df.tail())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 <strong>NumPy<\/strong><\/h3>\n\n\n\n<p><code>numpy<\/code> \u662f\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u6838\u5fc3\u5e93\uff0c\u652f\u6301\u5927\u89c4\u6a21\u7684\u77e9\u9635\u8fd0\u7b97\u548c\u9ad8\u7ea7\u6570\u5b66\u51fd\u6570\u3002\u5728\u91cf\u5316\u91d1\u878d\u4e2d\uff0c<code>numpy<\/code> \u5e38\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u6570\u636e\u4ee5\u53ca\u6267\u884c\u5404\u79cd\u6570\u5b66\u8ba1\u7b97\uff0c\u5982\u7edf\u8ba1\u5206\u6790\u3001\u7ebf\u6027\u4ee3\u6570\u8ba1\u7b97\u7b49\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\n# \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u80a1\u4ef7\u7684\u6570\u7ec4\nprices = np.array(&#91;100, 101, 102, 105, 107])\n\n# \u8ba1\u7b97\u65e5\u6536\u76ca\u7387\nreturns = np.diff(prices) \/ prices&#91;:-1]\nprint(returns)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 <strong>Matplotlib \/ Seaborn<\/strong><\/h3>\n\n\n\n<p><code>matplotlib<\/code> \u548c <code>seaborn<\/code> \u662f Python \u4e2d\u6700\u5e38\u7528\u7684\u53ef\u89c6\u5316\u5e93\uff0c\u9002\u7528\u4e8e\u7ed8\u5236\u5404\u79cd\u91d1\u878d\u6570\u636e\u56fe\u8868\uff0c\u5982\u4ef7\u683c\u8d70\u52bf\u56fe\u3001\u6563\u70b9\u56fe\u3001\u76f4\u65b9\u56fe\u7b49\u3002\u5b83\u4eec\u80fd\u5e2e\u52a9\u91cf\u5316\u5206\u6790\u5e08\u76f4\u89c2\u5730\u5c55\u793a\u5206\u6790\u7ed3\u679c\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import matplotlib.pyplot as plt\n\n# \u5047\u8bbe\u6211\u4eec\u6709\u80a1\u4ef7\u6570\u636e\ndates = &#91;'2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04']\nprices = &#91;100, 101, 103, 105]\n\n# \u7ed8\u5236\u80a1\u4ef7\u8d70\u52bf\nplt.plot(dates, prices)\nplt.xlabel('Date')\nplt.ylabel('Price')\nplt.title('Stock Price Over Time')\nplt.xticks(rotation=45)\nplt.show()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 <strong>QuantLib<\/strong><\/h3>\n\n\n\n<p><code>QuantLib<\/code> \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u91cf\u5316\u91d1\u878d\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u671f\u6743\u5b9a\u4ef7\u3001\u98ce\u9669\u7ba1\u7406\u3001\u56fa\u5b9a\u6536\u76ca\u3001\u91d1\u878d\u884d\u751f\u54c1\u7b49\u9886\u57df\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u6a21\u578b\u3001\u91d1\u878d\u5de5\u5177\u548c\u5b9a\u4ef7\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import QuantLib as ql\n\n# \u8bbe\u7f6e\u5b9a\u4ef7\u65e5\u671f\ntoday = ql.Date(15, 3, 2025)\nql.Settings.instance().evaluationDate = today\n\n# \u4f8b\u5982\uff0c\u8ba1\u7b97\u4e00\u4e2a\u6b27\u5f0f\u770b\u6da8\u671f\u6743\u7684\u5b9a\u4ef7\nspot_price = 100\nstrike_price = 105\nvolatility = 0.2  # \u6ce2\u52a8\u7387\ninterest_rate = 0.05  # \u65e0\u98ce\u9669\u5229\u7387\nmaturity_date = ql.Date(15, 6, 2025)\n\n# \u5b9a\u4e49\u671f\u6743\npayoff = ql.PlainVanillaPayoff(ql.Option.Call, strike_price)\nexercise = ql.EuropeanExercise(maturity_date)\noption = ql.VanillaOption(payoff, exercise)\n\n# \u8bbe\u7f6e\u5e02\u573a\u73af\u5883\nspot_handle = ql.QuoteHandle(ql.SimpleQuote(spot_price))\nrate_handle = ql.YieldTermStructureHandle(ql.FlatForward(today, interest_rate, ql.Actual365Fixed()))\nvol_handle = ql.BlackVolTermStructureHandle(ql.BlackConstantVol(today, ql.NullCalendar(), ql.QuoteHandle(ql.SimpleQuote(volatility)), ql.Actual365Fixed()))\n\n# \u5b9a\u4e49Black-Scholes\u8fc7\u7a0b\nbs_process = ql.BlackScholesProcess(spot_handle, rate_handle, vol_handle)\n\n# \u5b9a\u4ef7\noption.setPricingEngine(ql.AnalyticEuropeanEngine(bs_process))\nprint('Option Price: ', option.NPV())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.5 <strong>Zipline<\/strong><\/h3>\n\n\n\n<p><code>Zipline<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u548c\u56de\u6d4b\u91cf\u5316\u4ea4\u6613\u7b56\u7565\u7684 Python \u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ad8\u6548\u7684\u56de\u6d4b\u5f15\u64ce\uff0c\u5141\u8bb8\u7528\u6237\u6784\u5efa\u81ea\u52a8\u5316\u4ea4\u6613\u7b56\u7565\u5e76\u8fdb\u884c\u56de\u6d4b\uff0c\u8bc4\u4f30\u7b56\u7565\u7684\u6709\u6548\u6027\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import zipline\nfrom zipline.api import order, record, symbol\nfrom zipline import run_algorithm\n\ndef initialize(context):\n    context.asset = symbol('AAPL')\n\ndef handle_data(context, data):\n    order(context.asset, 10)  # \u4e70\u5165 10 \u80a1 AAPL\n    record(AAPL=data.current(context.asset, 'price'))\n\n# \u8bbe\u7f6e\u56de\u6d4b\u65e5\u671f\nstart_date = pd.to_datetime('2023-01-01')\nend_date = pd.to_datetime('2023-12-31')\n\n# \u6267\u884c\u56de\u6d4b\nrun_algorithm(start=start_date, end=end_date, initialize=initialize, handle_data=handle_data, capital_base=10000)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>Python \u5728\u91cf\u5316\u91d1\u878d\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 <strong>\u91d1\u878d\u6570\u636e\u5206\u6790<\/strong><\/h3>\n\n\n\n<p>\u91cf\u5316\u5206\u6790\u5e08\u5229\u7528 Python \u5bf9\u5e02\u573a\u6570\u636e\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u3001\u5efa\u6a21\u4e0e\u9884\u6d4b\uff0c\u4f8b\u5982\uff1a\u80a1\u4ef7\u5206\u6790\u3001\u6280\u672f\u6307\u6807\u8ba1\u7b97\u3001\u5e02\u573a\u60c5\u7eea\u5206\u6790\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 <strong>\u8d44\u4ea7\u5b9a\u4ef7\u4e0e\u884d\u751f\u54c1\u5b9a\u4ef7<\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528\u6570\u5b66\u6a21\u578b\uff08\u5982 Black-Scholes \u6a21\u578b\uff09\u5b9a\u4ef7\u671f\u6743\u548c\u5176\u4ed6\u884d\u751f\u54c1\uff0c\u8bc4\u4f30\u6295\u8d44\u7ec4\u5408\u98ce\u9669\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 <strong>\u6295\u8d44\u7ec4\u5408\u4f18\u5316<\/strong><\/h3>\n\n\n\n<p>\u901a\u8fc7\u73b0\u4ee3\u6295\u8d44\u7ec4\u5408\u7406\u8bba\uff08\u5982\u5747\u503c-\u65b9\u5dee\u4f18\u5316\uff09\u6765\u9009\u62e9\u6700\u4f73\u7684\u8d44\u4ea7\u914d\u7f6e\uff0c\u6700\u5927\u5316\u6536\u76ca\u5e76\u63a7\u5236\u98ce\u9669\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 <strong>\u91cf\u5316\u4ea4\u6613\u7b56\u7565<\/strong><\/h3>\n\n\n\n<p>\u8bbe\u8ba1\u5e76\u56de\u6d4b\u81ea\u52a8\u5316\u4ea4\u6613\u7b56\u7565\uff0c\u8fdb\u884c\u9ad8\u9891\u4ea4\u6613\u3001\u7edf\u8ba1\u5957\u5229\u3001\u5e02\u573a\u4e2d\u6027\u7b56\u7565\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.5 <strong>\u98ce\u9669\u7ba1\u7406<\/strong><\/h3>\n\n\n\n<p>Python \u88ab\u7528\u4e8e\u8ba1\u7b97\u91d1\u878d\u4ea7\u54c1\u7684\u98ce\u9669\uff0c\u4f8b\u5982\u901a\u8fc7 VaR\uff08\u4ef7\u503c-at-risk\uff09\u3001CVaR\uff08\u6761\u4ef6\u4ef7\u503c-at-risk\uff09\u7b49\u6a21\u578b\u8fdb\u884c\u5e02\u573a\u98ce\u9669\u8bc4\u4f30\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>Python \u5728\u91cf\u5316\u91d1\u878d\u9886\u57df\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u5b83\u4e3a\u91d1\u878d\u6570\u636e\u5206\u6790\u3001\u8d44\u4ea7\u5b9a\u4ef7\u3001\u98ce\u9669\u7ba1\u7406\u3001\u6295\u8d44\u7ec4\u5408\u4f18\u5316\u3001\u91cf\u5316\u4ea4\u6613\u7b56\u7565\u7b49\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\u3002\u901a\u8fc7\u7ed3\u5408 Python \u4e30\u5bcc\u7684\u5e93\u548c\u5de5\u5177\uff0c\u91d1\u878d\u5de5\u7a0b\u5e08\u548c\u91cf\u5316\u5206\u6790\u5e08\u80fd\u591f\u9ad8\u6548\u5730\u8fdb\u884c\u91d1\u878d\u5e02\u573a\u7684\u5efa\u6a21\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5728\u91cf\u5316\u91d1\u878d\u9886\u57df\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u91cf\u5316\u91d1\u878d\uff08Quantitative Finance\uff09\u4e3b\u8981\u4f9d\u8d56\u4e8e\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2923,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2922","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2922"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2922\/revisions"}],"predecessor-version":[{"id":2924,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2922\/revisions\/2924"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2923"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}