{"id":51,"date":"2025-08-31T11:35:35","date_gmt":"2025-08-31T03:35:35","guid":{"rendered":"http:\/\/www.028168.xyz\/?p=51"},"modified":"2025-08-31T11:35:36","modified_gmt":"2025-08-31T03:35:36","slug":"%e9%87%8f%e5%8c%96%e4%ba%a4%e6%98%93%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%89%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.028168.xyz\/index.php\/2025\/08\/31\/%e9%87%8f%e5%8c%96%e4%ba%a4%e6%98%93%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%89%ef%bc%89\/","title":{"rendered":"\u91cf\u5316\u4ea4\u6613\u57fa\u7840\uff08\u4e09\uff09"},"content":{"rendered":"\n<p>\u8f6c\u81ea\u5fae\u4fe1\u516c\u4f17\u53f7 \u300a\u6570\u636e\u79d1\u5b66\u5b9e\u6218\u300b<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# \u4ece Python \u5217\u8868\u521b\u5efa\nprice_list = &#x5B;143.73, 145.83, 143.68, 144.02, 143.5, 142.62]\nprice_array = np.array(price_list)\nprint(f&quot;\u4e00\u7ef4\u6570\u7ec4\uff1a{price_array}&quot;)\n\n# \u4ece\u5217\u8868\u7684\u5217\u8868\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\uff08\u77e9\u9635\uff09\nohlc_data = np.array(&#x5B;&#x5B;143.73, 145.90, 143.50, 145.83],\n                      &#x5B;145.83, 146.20, 143.68, 143.68]])\nprint(f&quot;\\n\u4e8c\u7ef4\u6570\u7ec4\uff08\u77e9\u9635\uff09\uff1a\\n{ohlc_data}&quot;)\n\n# \u4e13\u7528\u6570\u7ec4\u521b\u5efa\u51fd\u6570\nzeros_array = np.zeros(5)  # 5\u4e2a\u96f6\u7684\u6570\u7ec4\nones_matrix = np.ones((2, 3))  # 2x3\u7684\u51681\u77e9\u9635\nprice_range = np.linspace(100, 110, 11)  # 100\u5230110\u4e4b\u95f4\u5747\u5300\u5206\u5e03\u768411\u4e2a\u70b9\nrandom_returns = np.random.randn(10)  # 10\u4e2a\u6765\u81ea\u6807\u51c6\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\n\nprint(f&quot;\\n\u5168\u96f6\u6570\u7ec4\uff1a{zeros_array}&quot;)\nprint(f&quot;\\n\u5168\u4e00\u77e9\u9635\uff1a\\n{ones_matrix}&quot;)\nprint(f&quot;\\n\u7ebf\u6027\u95f4\u9694\u6570\u7ec4\uff1a{price_range}&quot;)\nprint(f&quot;\\n\u6a21\u62df\u968f\u673a\u6536\u76ca\u7387\uff1a{random_returns}&quot;)\n\nimport time\n\n# \u6a21\u62df\u4e00\u4e2a\u5305\u542b5000\u4e2a\u8d44\u4ea7\u7684\u5927\u578b\u6295\u8d44\u7ec4\u5408\nnum_assets = 5000\n\n# \u751f\u6210\u968f\u673a\u6743\u91cd\uff08\u603b\u548c\u4e3a1\uff09\nweights = np.random.random(num_assets)\nweights \/= np.sum(weights)\n\n# \u4e3a\u6bcf\u4e2a\u8d44\u4ea7\u751f\u6210\u968f\u673a\u6536\u76ca\u7387\nreturns = np.random.randn(num_assets) * 0.01  # \u5c0f\u7684\u65e5\u6536\u76ca\u7387\n\n# --- \u65b9\u6cd51\uff1aPython \u5faa\u73af ---\nstart_time_loop = time.time()\nportfolio_return_loop = 0.0\nfor i in range(num_assets):\n    portfolio_return_loop += weights&#x5B;i] * returns&#x5B;i]\nend_time_loop = time.time()\ntime_loop = (end_time_loop - start_time_loop) * 1000  # \u6beb\u79d2\n\nprint(f&quot;\u6295\u8d44\u7ec4\u5408\u6536\u76ca\u7387\uff08\u5faa\u73af\u6cd5\uff09\uff1a{portfolio_return_loop:.6f}&quot;)\nprint(f&quot;\u8017\u65f6\uff08\u5faa\u73af\u6cd5\uff09\uff1a{time_loop:.4f} \u6beb\u79d2&quot;)\n\n# --- \u65b9\u6cd52\uff1aNumPy \u5411\u91cf\u5316\u70b9\u79ef ---\nstart_time_np = time.time()\nportfolio_return_np = np.dot(weights, returns)\nend_time_np = time.time()\ntime_np = (end_time_np - start_time_np) * 1000  # \u6beb\u79d2\n\nprint(f&quot;\\n\u6295\u8d44\u7ec4\u5408\u6536\u76ca\u7387\uff08NumPy\u6cd5\uff09\uff1a{portfolio_return_np:.6f}&quot;)\nprint(f&quot;\u8017\u65f6\uff08NumPy\u6cd5\uff09\uff1a{time_np:.4f} \u6beb\u79d2&quot;)\n\n# --- \u6027\u80fd\u6bd4\u8f83 ---\nprint(f&quot;\\nNumPy \u65b9\u6cd5\u5927\u7ea6\u6bd4\u5faa\u73af\u5feb {time_loop\/time_np:.0f} \u500d\u3002&quot;)\n\n\npip install numpy-financial\n\nimport numpy_financial as npf\n\n# \u793a\u4f8b\uff1a\u8ba1\u7b97\u9879\u76ee\u7684\u51c0\u73b0\u503c\uff08NPV\uff09\n# \u4e00\u4e2a\u9879\u76ee\u9700\u898110\u4e07\u7f8e\u5143\u7684\u521d\u59cb\u6295\u8d44\n# \u9884\u8ba1\u57284\u5e74\u5185\u4ea7\u751f3\u4e07\u30014\u4e07\u30015\u4e07\u548c6\u4e07\u7f8e\u5143\u7684\u73b0\u91d1\u6d41\n# \u6298\u73b0\u7387\u4e3a8%\nrate = 0.08\ncash_flows = np.array(&#x5B;-100000, 30000, 40000, 50000, 60000])\n\n# npv\u51fd\u6570\u8ba1\u7b97\u672a\u6765\u73b0\u91d1\u6d41\u7684\u51c0\u73b0\u503c\uff08\u4ece\u7b2c1\u5e74\u5f00\u59cb\uff09\n# \u6240\u4ee5\u6211\u4eec\u8ba1\u7b97\u540e\u518d\u52a0\u4e0a\u521d\u59cb\u6295\u8d44\nnet_present_value = npf.npv(rate, cash_flows&#x5B;1:]) + cash_flows&#x5B;0]\nprint(f&quot;\u9879\u76ee\u73b0\u91d1\u6d41\uff1a{cash_flows}&quot;)\nprint(f&quot;\u6298\u73b0\u7387\uff1a{rate:.2%}&quot;)\nprint(f&quot;\u51c0\u73b0\u503c\uff08NPV\uff09\uff1a${net_present_value:,.2f}&quot;)\n\n# \u793a\u4f8b\uff1a\u8ba1\u7b97\u5185\u90e8\u6536\u76ca\u7387\uff08IRR\uff09\ninternal_rate_of_return = npf.irr(cash_flows)\nprint(f&quot;\u5185\u90e8\u6536\u76ca\u7387\uff08IRR\uff09\uff1a{internal_rate_of_return:.2%}&quot;)\n\nimport pandas as pd\n\n# \u521b\u5efa\u80a1\u7968\u4ef7\u683c\u7684 Series\naapl_prices = pd.Series(&#x5B;171.5, 172.3, 170.9, 173.1],\n                        index=&#x5B;'2023-11-01', '2023-11-02', '2023-11-03', '2023-11-04'])\nprint(&quot;Pandas Series:&quot;)\nprint(aapl_prices)\nprint(f&quot;\\n2023-11-02 \u7684\u4ef7\u683c\uff1a{aapl_prices&#x5B;'2023-11-02']}&quot;)\n\n# \u4ece\u5b57\u5178\u521b\u5efa DataFrame\ndata = {'Open': &#x5B;171.5, 172.3, 170.9, 173.1],\n        'High': &#x5B;172.8, 173.5, 171.2, 174.0],\n        'Low': &#x5B;170.1, 171.8, 170.5, 172.5],\n        'Close': &#x5B;172.3, 170.9, 173.1, 173.9],\n        'Volume': &#x5B;5.2e7, 4.8e7, 5.5e7, 4.9e7]}\n        \ndates = pd.to_datetime(&#x5B;'2023-11-01', '2023-11-02', '2023-11-03', '2023-11-04'])\ndf = pd.DataFrame(data, index=dates)\n\nprint(&quot;\\nPandas DataFrame:&quot;)\nprint(df)\n\n# \u901a\u8fc7\u6807\u7b7e\uff08\u65e5\u671f\uff09\u9009\u62e9\u5355\u884c\nprint(&quot;\\n\u4f7f\u7528 .loc \u83b7\u53d6 2023-11-02 \u7684\u6570\u636e\uff1a&quot;)\nprint(df.loc&#x5B;'2023-11-02'])\n\n# \u901a\u8fc7\u884c\u5217\u6807\u7b7e\u9009\u62e9\u5355\u4e2a\u503c\nclose_price = df.loc&#x5B;'2023-11-03', 'Close']\nprint(f&quot;\\n2023-11-03 \u7684\u6536\u76d8\u4ef7\uff1a{close_price}&quot;)\n\n# \u9009\u62e9\u884c\u548c\u7279\u5b9a\u5217\u7684\u5207\u7247\nprint(&quot;\\n\u4f7f\u7528 .loc \u9009\u62e9\u884c\u548c\u5217\u7684\u5207\u7247\uff1a&quot;)\nprint(df.loc&#x5B;'2023-11-02':'2023-11-04', &#x5B;'Open', 'Close']])\n\n# \u9009\u62e9\u7b2c\u4e00\u884c\uff08\u7d22\u5f15\u4e3a0\uff09\nprint(&quot;\\n\u4f7f\u7528 .iloc \u83b7\u53d6\u7b2c\u4e00\u884c\u6570\u636e\uff1a&quot;)\nprint(df.iloc&#x5B;0])\n\n# \u9009\u62e9\u7b2c3\u884c\u3001\u7b2c4\u5217\u7684\u503c\uff08\u7d22\u5f15\u5747\u4e3a3\uff09\nvolume_val = df.iloc&#x5B;3, 4]\nprint(f&quot;\\n\u7b2c4\u5929\u7684\u6210\u4ea4\u91cf\uff1a{volume_val}&quot;)\n\n# \u9009\u62e9\u7b2c1\u884c\u548c\u7b2c2\u884c\uff0c\u4ee5\u53ca\u7b2c0\u5217\u548c\u7b2c3\u5217\nprint(&quot;\\n\u4f7f\u7528 .iloc \u9009\u62e9\u884c\u548c\u5217\u7684\u5207\u7247\uff1a&quot;)\nprint(df.iloc&#x5B;1:3, &#x5B;0, 3]])\n\n# \u627e\u51fa\u6240\u6709\u6536\u76d8\u4ef7\u9ad8\u4e8e172\u7684\u65e5\u5b50\nhigh_close_days = df&#x5B;df&#x5B;'Close'] &gt; 172]\nprint(&quot;\\n\u6536\u76d8\u4ef7 &gt; 172 \u7684\u65e5\u5b50\uff1a&quot;)\nprint(high_close_days)\n\n# \u7ec4\u5408\u591a\u4e2a\u6761\u4ef6\uff1a\u627e\u51fa\u6210\u4ea4\u91cf\u9ad8\u4e14\u4ef7\u683c\u533a\u95f4\u5927\u7684\u65e5\u5b50\nhigh_volume_threshold = 5.0e7\nlarge_range_threshold = 2.0\nactive_days = df&#x5B;(df&#x5B;'Volume'] &gt; high_volume_threshold) &amp; \n                ((df&#x5B;'High'] - df&#x5B;'Low']) &gt; large_range_threshold)]\nprint(&quot;\\n\u9ad8\u6210\u4ea4\u91cf\u3001\u5927\u4ef7\u683c\u533a\u95f4\u7684\u65e5\u5b50\uff1a&quot;)\nprint(active_days)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"593\" src=\"http:\/\/www.028168.xyz\/wp-content\/uploads\/2025\/08\/DataFrame\u64cd\u4f5c-1024x593.png\" alt=\"\" class=\"wp-image-52\" srcset=\"http:\/\/www.028168.xyz\/wp-content\/uploads\/2025\/08\/DataFrame\u64cd\u4f5c-1024x593.png 1024w, http:\/\/www.028168.xyz\/wp-content\/uploads\/2025\/08\/DataFrame\u64cd\u4f5c-300x174.png 300w, http:\/\/www.028168.xyz\/wp-content\/uploads\/2025\/08\/DataFrame\u64cd\u4f5c-768x445.png 768w, http:\/\/www.028168.xyz\/wp-content\/uploads\/2025\/08\/DataFrame\u64cd\u4f5c.png 1405w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u81ea\u5fae\u4fe1\u516c\u4f17\u53f7 \u300a\u6570\u636e\u79d1\u5b66\u5b9e\u6218\u300b<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-51","post","type-post","status-publish","format-aside","hentry","category-7","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":1,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":53,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts\/51\/revisions\/53"}],"wp:attachment":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}