{"id":1377,"date":"2024-08-30T07:32:00","date_gmt":"2024-08-29T23:32:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1377"},"modified":"2024-08-28T17:25:38","modified_gmt":"2024-08-28T09:25:38","slug":"%e5%8f%91%e8%b4%a2%e4%b8%8e%e4%ba%8f%e9%92%b1%e4%b8%80%e5%bf%b5%e9%97%b4%ef%bc%8c%e8%bf%98%e5%be%97%e7%9c%8b%e8%82%a1%e5%b8%82%e9%a2%84%e6%b5%8b%e4%b8%ad%e5%a6%82%e4%bd%95%e8%af%84%e4%bc%b0%e6%b7%b1","status":"publish","type":"post","link":"https:\/\/laoyulaoyu.com\/index.php\/2024\/08\/30\/%e5%8f%91%e8%b4%a2%e4%b8%8e%e4%ba%8f%e9%92%b1%e4%b8%80%e5%bf%b5%e9%97%b4%ef%bc%8c%e8%bf%98%e5%be%97%e7%9c%8b%e8%82%a1%e5%b8%82%e9%a2%84%e6%b5%8b%e4%b8%ad%e5%a6%82%e4%bd%95%e8%af%84%e4%bc%b0%e6%b7%b1\/","title":{"rendered":"\u53d1\u8d22\u4e0e\u4e8f\u94b1\u4e00\u5ff5\u95f4\uff0c\u8fd8\u5f97\u770b\u6df1\u5ea6\u5b66\u4e60\u5982\u4f55\u9a8c\u8bc1\u80a1\u5e02\u9884\u6d4b\u7ed3\u679c"},"content":{"rendered":"\n<p>\u4f5c\u8005\uff1a<a href=\"https:\/\/www.laoyulaoyu.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u8001\u4f59\u635e\u9c7c<\/a><\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002<\/mark><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-34-1024x299.png\" alt=\"\" class=\"wp-image-1570\"\/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-verse\"><strong>\u5199\u5728\u524d\u9762\u7684\u8bdd\uff1a<\/strong>\u4f20\u7edf\u7684\u9a8c\u8bc1\u6307\u6807\uff08\u5982 RMSE\u3001MSE \u6216\u5206\u4f4d\u6570\u635f\u5931\uff09\u4e3b\u8981\u7528\u4e8e\u8861\u91cf\u9884\u6d4b\u4e2d\u7684\u5e73\u5747\u8bef\u5dee\u5e45\u5ea6\u3002\u7136\u800c\uff0c\u5728\u8bad\u7ec3\u7528\u4e8e\u80a1\u7968\u5e02\u573a\u9884\u6d4b\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u65f6\uff0c\u5b83\u4eec\u5728\u63d0\u4f9b\u5bf9\u4ea4\u6613\u98ce\u9669\u548c\u56de\u62a5\u7684\u6709\u610f\u4e49\u7684\u89c1\u89e3\u65b9\u9762\u5b58\u5728\u4e0d\u8db3\u3002\u5728\u9884\u6d4b\u80a1\u7968\u5e02\u573a\u65f6\uff0c\u5b83\u4eec\u6ca1\u6709\u6e05\u695a\u5730\u8bf4\u660e\u6240\u6d89\u53ca\u7684\u65f6\u95f4\u6216\u6ce2\u52a8\u6027\uff0c\u5b83\u4eec\u4e0d\u9002\u5408\u4ea4\u6613\u8005\u7684\u9700\u6c42\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5728\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u4e2d\u5f00\u53d1\u7528\u4e8e\u80a1\u7968\u9884\u6d4b\u7684\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807<\/mark>\u53d8\u5f97\u5f88\u91cd\u8981\u7684\u539f\u56e0\u3002<\/pre>\n<\/blockquote>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u672c\u6587\u5bb9\u7684\u5feb\u901f\u6458\u8981\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5f00\u53d1\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\u7684\u91cd\u8981\u6027<\/strong>&nbsp;\u2014&nbsp;<em>\u4e3a\u4ec0\u4e48\u6807\u51c6\u6307\u6807\u8fbe\u4e0d\u5230\u8981\u6c42\uff0c\u4ee5\u53ca\u521b\u5efa\u9002\u5408\u80a1\u7968\u5e02\u573a\u9884\u6d4b\u7684\u5b9a\u5236\u9a8c\u8bc1\u6307\u6807\u7684\u91cd\u8981\u6027<\/em><\/li>\n\n\n\n<li><strong>\u5b9e\u65bd\u6307\u5357&nbsp;<\/strong>\u2014<strong>&nbsp;<\/strong><em>\u6709\u5173\u5b9e\u65bd\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\u7684\u8be6\u7ec6\u5206\u6b65\u8bf4\u660e\u3002\u6211\u4eec\u5c06\u4f7f\u7528&nbsp;<\/em><em>NeuralForecast<\/em><em>&nbsp;\u6765\u5b9e\u73b0\u8be5\u6307\u6807<\/em><\/li>\n\n\n\n<li><strong>\u8bad\u7ec3\u6a21\u578b&nbsp;<\/strong>\u2014&nbsp;<em>\u914d\u7f6e\u6a21\u578b\u3001\u51c6\u5907\u6570\u636e\u5e76\u4f7f\u7528\u6307\u6807\u8bad\u7ec3\u6a21\u578b\u4ee5\u8fdb\u884c\u80a1\u7968\u5e02\u573a\u9884\u6d4b\u3002\u6211\u4eec\u5c06\u4f7f\u7528&nbsp;<\/em><em>NeuralForecast \u4e2d\u7684<\/em>&nbsp;<em>Temporal Fusion Transformer \uff08TFT\uff09<\/em>&nbsp;\u5b9e\u73b0<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\uff1f<\/strong><\/h2>\n\n\n\n<p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u4e0d\u540c\u7684\u8bad\u7ec3\u635f\u5931\u51fd\u6570\u548c\u9a8c\u8bc1\u6307\u6807\u5177\u6709\u72ec\u7279\u7684\u7528\u9014\u3002\u8fd9\u79cd\u533a\u522b\u6709\u52a9\u4e8e\u6211\u4eec\u6355\u6349\u5b66\u4e60\u8fc7\u7a0b\u7684\u7ec6\u8282\u548c\u6a21\u578b\u7684\u5b9e\u9645\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u57f9\u8bad\u635f\u5931\u7684\u4f5c\u7528<\/strong><\/h3>\n\n\n\n<p>\u8003\u8651\u4e00\u4e2a\u5206\u7c7b\u4efb\u52a1\u3002\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528\u4ea4\u53c9\u71b5\u635f\u5931\u51fd\u6570\u3002\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a<strong>\u4ea4\u53c9\u71b5\u635f\u5931\u5728\u6bd4\u8f83\u7c7b\u7684\u9884\u6d4b\u6982\u7387\u5206\u5e03\u4e0e\u5b9e\u9645\u5206\u5e03\u65f6\u975e\u5e38\u6709\u6548<\/strong>\u3002\u8fd9\u79cd\u6bd4\u8f83\u5bf9\u4e8e\u6a21\u578b\u6709\u6548\u5730\u4ece\u6570\u636e\u4e2d\u5b66\u4e60\u81f3\u5173\u91cd\u8981\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u4e3a\u4ec0\u4e48\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528 F1 \u5206\u6570\u6216\u51c6\u786e\u6027\u4e4b\u7c7b\u7684\u4e1c\u897f\u8fdb\u884c\u8bad\u7ec3\u5462\uff1f\u539f\u56e0\u5728\u4e8e\u5bf9\u53ef\u533a\u5206\u6027\u7684\u9700\u8981\u3002\u6df1\u5ea6\u5b66\u4e60\u4e2d\u7684\u8bad\u7ec3\u635f\u5931\u51fd\u6570\u5fc5\u987b\u662f\u53ef\u5fae\u5206\u7684\uff0c\u4ee5\u4fbf\u4f18\u5316\u7b97\u6cd5\u901a\u8fc7\u68af\u5ea6\u4e0b\u964d\u6765\u8c03\u6574\u6a21\u578b\u7684\u6743\u91cd\u3002\u50cf F1-Score \u8fd9\u6837\u7684\u6307\u6807\u867d\u7136\u975e\u5e38\u9002\u5408\u8bc4\u4f30\u6700\u7ec8\u8868\u73b0\uff0c\u4f46\u65e0\u6cd5\u533a\u5206\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 \u9a8c\u8bc1\u6307\u6807\u7684\u91cd\u8981\u6027<\/strong><\/h3>\n\n\n\n<p>\u5728\u9a8c\u8bc1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u91cd\u70b9\u5173\u6ce8\u6a21\u578b\u5728\u5b9e\u9645\u573a\u666f\u4e2d\u7684\u8868\u73b0\uff0c\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\uff0cF1 \u5206\u6570\u7b49\u6307\u6807\u53d8\u5f97\u5f88\u91cd\u8981\u3002F1 \u5206\u6570\u5bf9\u4e8e\u5206\u7c7b\u4efb\u52a1\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u540c\u65f6\u8003\u8651\u4e86\u7cbe\u786e\u5ea6\u548c\u53ec\u56de\u7387\uff0c\u4ece\u800c\u63d0\u4f9b\u4e86\u6a21\u578b\u6027\u80fd\u7684\u5e73\u8861\u89c6\u56fe\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u4e0d\u5e73\u8861\u6570\u636e\u96c6\u7684\u60c5\u51b5\u4e0b\u3002\u6b64\u5916\uff0c\u4e0e\u4ea4\u53c9\u71b5\u635f\u5931\u76f8\u6bd4\uff0cF1 \u5206\u6570\u66f4\u6613\u4e8e\u4eba\u7c7b\u9605\u8bfb\u548c\u76f4\u89c2\uff0c\u56e0\u4e3a\u5b83\u76f4\u63a5\u53cd\u6620\u4e86\u6a21\u578b\u6b63\u786e\u5206\u7c7b\u9633\u6027\u5b9e\u4f8b\u7684\u80fd\u529b\uff0c\u540c\u65f6\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u5047\u9633\u6027\u548c\u5047\u9634\u6027\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4f7f\u7528\u5783\u573e\u90ae\u4ef6\u5206\u7c7b\u4efb\u52a1\u7684\u793a\u4f8b\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u5143\u5206\u7c7b\u4efb\u52a1\u6765\u68c0\u6d4b\u7535\u5b50\u90ae\u4ef6\u662f\u5783\u573e\u90ae\u4ef6\uff08\u6b63\u7c7b\uff09\u8fd8\u662f\u975e\u5783\u573e\u90ae\u4ef6\uff08\u8d1f\u7c7b\uff09\u3002\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u5047\u8bbe\u5728\u6d4b\u8bd5\u6570\u636e\u96c6\u4e0a\u8bc4\u4f30\u6a21\u578b\u540e\uff0c\u6211\u4eec\u6709\u4ee5\u4e0b\u6df7\u6dc6\u77e9\u9635\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-35.png\" alt=\"\" class=\"wp-image-1571\"\/><\/figure>\n\n\n\n<p>\u4ece\u6df7\u6dc6\u77e9\u9635\u4e2d\uff0c\u6211\u4eec\u8ba1\u7b97\u51fa\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bef\u62a5 \uff08FP\uff09\uff1a<\/strong>5\uff08\u7535\u5b50\u90ae\u4ef6\u88ab\u9519\u8bef\u5730\u8bc6\u522b\u4e3a\u5783\u573e\u90ae\u4ef6\uff09<\/li>\n\n\n\n<li><strong>\u6f0f\u62a5 \uff08FN\uff09<\/strong>\uff1a10 \uff08\u5783\u573e\u90ae\u4ef6\u88ab\u9519\u8bef\u5730\u8bc6\u522b\u4e3a\u975e\u5783\u573e\u90ae\u4ef6\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u4f7f\u7528\u4ee5\u4e0b\u503c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-36.png\" alt=\"\" class=\"wp-image-1572\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-37.png\" alt=\"\" class=\"wp-image-1573\"\/><\/figure>\n\n\n\n<p>\u56e0\u6b64\uff0cF1 \u5206\u6570\u4e3a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-38.png\" alt=\"\" class=\"wp-image-1574\"\/><\/figure>\n\n\n\n<p>\u8fd9\u4e2a 84% \u7684 F1 \u5206\u6570\u4e3a\u6a21\u578b\u7684\u51c6\u786e\u6027\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6e05\u6670\u3001\u6613\u61c2\u7684\u8861\u91cf\u6807\u51c6\u3002\u5b83\u7a81\u51fa\u4e86\u6a21\u578b\u9884\u6d4b\u4e2d\u7cbe\u786e\u5ea6\u548c\u53ec\u56de\u7387\u4e4b\u95f4\u7684\u5e73\u8861\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3 \u80a1\u5e02\u9884\u6d4b\u5462\uff1f<\/strong><\/h3>\n\n\n\n<p>\u62e5\u6709\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\u4e5f\u9002\u7528\u4e8e\u80a1\u7968\u5e02\u573a\u9884\u6d4b\u3002\u5bf9\u4e8e\u591a\u4e2a\u65f6\u95f4\u5e8f\u5217\u95ee\u9898\uff0c\u4f7f\u7528 MSE\u3001RMSE \u548c\u5206\u4f4d\u6570\u635f\u5931\u4f5c\u4e3a\u9a8c\u8bc1\u6307\u6807\u662f\u5b8c\u5168\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u53ef\u9760\u7684\u65b9\u6cd5\u6765\u8bc4\u4f30\u9884\u6d4b\u6a21\u578b\u7684\u6574\u4f53\u51c6\u786e\u6027\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\uff0c\u5bf9\u4e8e\u65e5\u5185\u4ea4\u6613\uff0c\u4f7f\u7528\u5747\u65b9\u8bef\u5dee \uff08MSE\uff09 \u548c\u5747\u65b9\u6839\u8bef\u5dee \uff08RMSE\uff09 \u7b49\u6307\u6807\u53ef\u80fd\u4f1a\u6709\u95ee\u9898\u3002\u5b83\u4eec\u7684\u4fe1\u606f\u91cf\u8f83\u5c11\uff0c\u4e5f\u66f4\u96be\u89e3\u91ca\u4ea4\u6613\u51b3\u7b56\u3002\u8fd9\u4e9b\u6307\u6807\u4e3b\u8981\u7528\u4e8e\u8861\u91cf\u9884\u6d4b\u4e2d\u7684\u5e73\u5747\u8bef\u5dee\u5e45\u5ea6\uff0c\u4f46\u5b83\u4eec\u5e76\u4e0d\u80fd\u6e05\u695a\u5730\u4e86\u89e3\u4ea4\u6613\u7684\u65f6\u95f4\u6216\u6ce2\u52a8\u6027\u3002\u65e5\u5185\u4ea4\u6613\u8005\u5bf9\u5176\u4ea4\u6613\u7684\u98ce\u9669\/\u56de\u62a5\u66f4\u611f\u5174\u8da3\u3002\u56e0\u6b64\uff0c<strong>\u867d\u7136 RMSE \u6216 MSE \u53ef\u7528\u4e8e\u8bad\u7ec3\uff0c\u4f46\u590f\u666e\u6bd4\u7387\u6216\u7d22\u8482\u8bfa\u6bd4\u7387\u7b49\u9a8c\u8bc1\u6307\u6807\u66f4\u76f8\u5173<\/strong>\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0cRMSE \u53ef\u4ee5\u6700\u5c0f\u5316\u9884\u6d4b\u80a1\u7968\u4ef7\u683c\u548c\u5b9e\u9645\u80a1\u7968\u4ef7\u683c\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u4f46\u4e0e\u6b64\u6307\u6807\u76f8\u5173\u7684\u4ea4\u6613\u6ca1\u6709\u98ce\u9669\u6216\u56de\u62a5\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c<strong>\u590f\u666e\u6bd4\u7387\u65e2\u8003\u8651\u4e86\u56de\u62a5\u53c8\u8003\u8651\u4e86\u98ce\u9669\uff0c\u63d0\u4f9b\u4e86\u66f4\u76f4\u89c2\u7684\u4ea4\u6613\u8868\u73b0\u8861\u91cf\u6807\u51c6\u3002<\/strong>\u5982\u679c\u6a21\u578b\u9884\u6d4b\u9ad8\u56de\u62a5\u4f46\u98ce\u9669\u9ad8\uff0c\u4ea4\u6613\u8005\u53ef\u80fd\u66f4\u559c\u6b22\u7565\u4f4e\u7684\u56de\u62a5\u548c\u663e\u7740\u964d\u4f4e\u7684\u98ce\u9669\uff0c\u590f\u666e\u6bd4\u7387\u4f1a\u5f3a\u8c03\u8fd9\u4e00\u70b9\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7NeuralForecast\u7684\u65f6\u95f4\u878d\u5408\u8f6c\u6362\u5668\uff08TFT\uff09\u6a21\u578b\uff0c\u5e76\u5728NeuralForecast\u6846\u67b6\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u6211\u4eec\u7684\u6a21\u578b\u7b26\u5408\u4ea4\u6613\u8005\u7684\u9700\u6c42\u3002\u8fd9\u79cd\u65b9\u6cd5\u6709\u52a9\u4e8e\u9009\u62e9\u5177\u6709\u4e1a\u52a1\u4ee3\u8868\u6027\u7684\u9a8c\u8bc1\u6307\u6807\uff0c\u4f7f\u6a21\u578b\u5728\u80a1\u7968\u9884\u6d4b\u4e2d\u66f4\u52a0\u5b9e\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u7528\u4e8e\u8bad\u7ec3\u548c\u6d4b\u8bd5\u6a21\u578b\u7684\u914d\u7f6e<\/strong><\/h2>\n\n\n\n<p>\u6211\u4eec\u51e0\u4e4e\u6ca1\u6709\u5bf9 TFT \u6a21\u578b\u8fdb\u884c\u7279\u5f81\u5de5\u7a0b\uff0c\u4e5f\u6ca1\u6709\u8fdb\u884c\u964d\u7ef4\u3001\u4ea4\u53c9\u9a8c\u8bc1\u6216\u8d85\u53c2\u6570\u4f18\u5316\u3002\u4e3b\u8981\u76ee\u6807\u662f\u5c55\u793a\u7528\u4e8e\u80a1\u7968\u9884\u6d4b\u7684\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u4f7f\u7528\u7684\u6570\u636e<\/strong><\/h3>\n\n\n\n<p>\u4e3a\u4e86\u4fdd\u6301\u6a21\u578b\u7684\u7b80\u5355\u6027\uff0c\u6211\u4eec\u5c06\u53ea\u5173\u6ce8\u4e00\u4e2a\u5355\u53d8\u91cf\u65f6\u95f4\u5e8f\u5217\uff1aSPY\uff08SPDR S&amp;P 500 ETF Trust\uff09\u7684\u6bcf\u65e5\u56de\u62a5\u7387\u3002\u8be5\u6a21\u578b\u4f7f\u7528 2005 \u5e74\u81f3 2024 \u5e74\u7684\u5386\u53f2\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\uff0c\u6db5\u76d6\u8bad\u7ec3\u671f\u548c\u6d4b\u8bd5\u671f\u3002\u6211\u4eec\u4f7f\u7528\u6bcf\u5929\u7684\u5f00\u76d8\u4ef7\u548c\u6536\u76d8\u4ef7\u6765\u8ba1\u7b97\u6bcf\u65e5\u56de\u62a5\u3002\u8fd9\u4e3a\u6a21\u578b\u589e\u52a0\u4e86\u4e00\u5c42\u771f\u5b9e\u611f\u3002\u65e5\u5185\u4ea4\u6613\u8005\u53ef\u80fd\u4f1a\u5728\u65e9\u4e0a\u5f00\u4ed3\uff0c\u5e76\u5728\u4e00\u5929\u7ed3\u675f\u65f6\u5e73\u4ed3\u3002\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u5728\u521d\u59cb\u6a21\u578b\u4e2d\u7701\u7565\u4e86\u4f63\u91d1\u548c\u6ed1\u70b9\u7b49\u56e0\u7d20\uff0c\u4f46\u8fd9\u4e9b\u56e0\u7d20\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u7eb3\u5165\u3002<\/p>\n\n\n\n<p>\u9884\u6d4b\u56de\u62a5\u4f7f\u6211\u4eec\u80fd\u591f\u5904\u7406\u7a33\u6001\u6570\u636e\u3002\u56de\u62a5\u901a\u5e38\u662f\u9759\u6b62\u7684\u2014\u2014\u6216\u8005\u81f3\u5c11\u662f\u5fae\u5f31\u7684\u9759\u6b62\u2014\u2014\u4e0e\u4ef7\u683c\u4e0d\u540c\uff0c\u4ef7\u683c\u662f\u975e\u9759\u6b62\u7684\u3002\u6b64\u5916\uff0c\u867d\u7136\u9884\u6d4b\u4ef7\u683c\u53ef\u80fd\u4e0e\u7b56\u7565\u53d6\u51b3\u4e8e\u672a\u6765\u4ef7\u683c\u7684\u671f\u6743\u4ea4\u6613\u8005\u6709\u5173\uff0c\u4f46\u5bf9\u4e8e\u65e5\u5185\u4ea4\u6613\u8005\u6765\u8bf4\uff0c\u91cd\u70b9\u4e3b\u8981\u662f\u8d44\u4ea7\u53ef\u80fd\u4ea7\u751f\u7684\u6f5c\u5728\u56de\u62a5\uff0c\u800c\u4e0d\u662f\u5176\u4ef7\u683c\u3002\u4ec5\u4ec5\u77e5\u9053\u4ef7\u683c\uff0c\u800c\u6ca1\u6709\u989d\u5916\u7684\u80cc\u666f\u4fe1\u606f\uff0c\u5982\u5386\u53f2\u4ef7\u683c\uff0c\u5bf9\u4ea4\u6613\u8005\u6765\u8bf4\u51e0\u4e4e\u6ca1\u6709\u4ef7\u503c\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u6211\u4eec\u7684\u6a21\u578b\u8fd8\u7ed3\u5408\u4e86\u6bcf\u65e5\u5916\u751f\u53d8\u91cf\uff0c\u4f8b\u5982 VIX\uff08\u6ce2\u52a8\u7387\u6307\u6570\uff09\uff0c\u5373 5 \u5e74\u76c8\u4e8f\u5e73\u8861\u901a\u8d27\u81a8\u80c0\u7387\uff0c\u4ee5\u63d0\u9ad8\u6211\u4eec\u7684\u6a21\u578b\u6027\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u914d\u7f6e<\/strong><\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u7528\u4e8e\u8bad\u7ec3\u548c\u6d4b\u8bd5\u6a21\u578b\u7684 YAML \u914d\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><br>start_date :'2005-07-01'<br>end_date :'2024-05-23'<br>max_missing_data :0.02<br>min_nb_trades :60<br>train_test_split : &#91;0.9, 0.10]<br>val_proportion_size :0.15<br>output :<br> &nbsp;-source :'yahoo'<br> &nbsp; &nbsp;data :<br> &nbsp; &nbsp; &nbsp;-'SPY'<br>historic_variables:<br> &nbsp;-source:'fred'<br> &nbsp; &nbsp;data:<br> &nbsp; &nbsp; &nbsp;-'T5YIE'<br> &nbsp; &nbsp; &nbsp;-'T10YIE'<br> &nbsp; &nbsp; &nbsp;-'T10Y3M'<br> &nbsp; &nbsp; &nbsp;-'DGS10'<br> &nbsp; &nbsp; &nbsp;-'DGS2'<br> &nbsp; &nbsp; &nbsp;-'DTB3'<br> &nbsp; &nbsp; &nbsp;-'DEXUSNZ'<br> &nbsp; &nbsp; &nbsp;-'VIXCLS'<br> &nbsp; &nbsp; &nbsp;-'T10Y2Y'<br> &nbsp; &nbsp; &nbsp;-'NASDAQCOM'<br> &nbsp; &nbsp; &nbsp;-'DCOILWTICO'<br> &nbsp;-source:'yahoo'<br> &nbsp; &nbsp;data:<br> &nbsp; &nbsp; &nbsp;-\"GC=F\"<br> &nbsp; &nbsp; &nbsp;-'MSFT'<br> &nbsp; &nbsp; &nbsp;-'GOOGL'<br> &nbsp; &nbsp; &nbsp;-'AAPL'<br> &nbsp; &nbsp; &nbsp;-'AMZN'<br>future_variables :<br> &nbsp;-'day'<br> &nbsp;-'month'<br><br>TFT_parameters:<br> &nbsp;h:1<br> &nbsp;input_size:64<br> &nbsp;max_steps:500<br> &nbsp;val_check_steps:1<br> &nbsp;batch_size:32<br> &nbsp;inference_windows_batch_size:-1<br> &nbsp;valid_batch_size:2000<br> &nbsp;learning_rate:0.0005<br> &nbsp;scaler_type :'robust'<br> &nbsp;random_seed:42<br> &nbsp;loss:'HuberMQLoss'<br> &nbsp;hidden_size:256<br> &nbsp;n_head:8<br> &nbsp;dropout:0.1<br> &nbsp;attn_dropout :0.1<br> &nbsp;gradient_clip_val:1<br><br>other_parameters :<br> &nbsp;confidence_level:0.6<br> &nbsp;quantiles : &#91; 0.05, 0.4, 0.5, 0.6, 0.95 ]<br> &nbsp;callbacks :<br> &nbsp; &nbsp;EarlyStopping :<br> &nbsp; &nbsp; &nbsp;monitor :'valid_loss'<br> &nbsp; &nbsp; &nbsp;patience :20<br> &nbsp; &nbsp; &nbsp;verbose :True<br> &nbsp; &nbsp; &nbsp;mode :'min'<br> &nbsp; &nbsp;ModelCheckPoint :<br> &nbsp; &nbsp; &nbsp;monitor :'valid_loss'<br> &nbsp; &nbsp; &nbsp;mode :'min'<br> &nbsp; &nbsp; &nbsp;save_top_k :1<br> &nbsp; &nbsp; &nbsp;verbose :True<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8be5\u6a21\u578b\u5728&nbsp;<strong>2005-07-01 \u5230 2024-05-23<\/strong>\uff08<strong>start_date<\/strong>&nbsp;\u5e74\u548c&nbsp;<strong>end_date<\/strong>\uff09\u671f\u95f4\u4f7f\u7528\u6bcf\u65e5\u4ea4\u6613\u65e5\u8fdb\u884c\u4e86\u8bad\u7ec3\u548c\u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li><strong>max_missing_data \uff1a0.02<\/strong>&nbsp;\u2014 \u4efb\u4f55\u7ed9\u5b9a\u7684\u5916\u751f\u53d8\u91cf\u5141\u8bb8\u7684\u7f3a\u5931\u6570\u636e\u70b9\u7684\u6700\u5927\u767e\u5206\u6bd4\u3002\u5982\u679c\u8d85\u8fc7\u8be5\u503c\uff0c\u6211\u4eec\u5c06\u653e\u5f03\u8be5\u529f\u80fd\u3002<\/li>\n\n\n\n<li><strong>min_nb_trades \uff1a60<\/strong>&nbsp;\u2014 \u6b64\u53d8\u91cf\u8bbe\u7f6e\u9a8c\u8bc1\u548c\u6d4b\u8bd5\u671f\u95f4\u6240\u9700\u7684\u6700\u5c0f\u4ea4\u6613\u6570\u91cf\u3002\u7531\u4e8e\u6211\u4eec\u4f7f\u7528\u5206\u4f4d\u6570\u7684\u6982\u7387\uff0c\u56e0\u6b64\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u4e0d\u4f1a\u8fdb\u884c\u4ea4\u6613\u3002\u6709\u5173\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u4e0b\u9762\u7684\u90e8\u5206\uff0c<em>\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u6700\u5c0f\u4ea4\u6613\u6570\u91cf\uff1f<\/em><\/li>\n\n\n\n<li><strong>train_test_split \uff1a[0.9\uff0c 0.10]<\/strong>&nbsp;\u2014 \u6570\u636e\u6bd4\u4f8b\u5206\u4e3a\u8bad\u7ec3\u96c6 \uff0890%\uff09 \u548c\u6d4b\u8bd5\u96c6 \uff0810%\uff09\u3002<\/li>\n\n\n\n<li><strong>val_proportion_size \uff1a0.15<\/strong>&nbsp;\u2014 \u4e3a\u9a8c\u8bc1\u76ee\u7684\u5206\u914d\u7684\u8bad\u7ec3\u6570\u636e\u6bd4\u4f8b \uff0815%\uff09\u300215% * 90% = \u603b\u6570\u636e\u96c6\u7684 13.5%<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\uff1a&nbsp;<\/strong>\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u9884\u6d4b\u4e86\u4e00\u4e2a\u5355\u4e00\u7684\u65f6\u95f4\u5e8f\u5217\uff1aSPY\uff08\u6807\u51c6\u666e\u5c14500 ETF\uff09\u3002<\/li>\n\n\n\n<li><strong>historic_variables \uff1a<\/strong>&nbsp;\u8fc7\u53bb\u7684\u5916\u751f\u53d8\u91cf\uff08\u6bcf\u65e5\u57fa\u7840\u6570\u636e\uff09\uff0c\u53ef\u80fd\u4f1a\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u4f7f\u7528\u7684\u53d8\u91cf\u6570\u91cf\u4fdd\u6301\u6700\u5c11\uff0c\u56e0\u4e3a\u6a21\u578b\u662f\u5728\u6ca1\u6709 GPU \u7684\u672c\u5730\u8ba1\u7b97\u673a\u4e0a\u8bad\u7ec3\u548c\u6d4b\u8bd5\u7684\u3002\u6211\u4eec\u4e13\u6ce8\u4e8e\u6700\u6709\u7528\u7684\u53d8\u91cf\u3002\u6211\u4eec\u80af\u5b9a\u53ef\u4ee5\u589e\u52a0\u5916\u751f\u53d8\u91cf\u7684\u6570\u91cf\uff0c\u5e76\u5e94\u7528\u7279\u5f81\u5de5\u7a0b\u6765\u63d0\u9ad8\u6a21\u578b\u6027\u80fd\u3002<\/li>\n\n\n\n<li>\u5723\u8def\u6613\u65af\u8054\u90a6\u50a8\u5907\u59d4\u5458\u4f1a\u7ecf\u6d4e\u6570\u636e\uff08fred\uff09\u4e2d\u7684\u4e00\u4e9b\u4f8b\u5b50<strong>\uff1aVIXCLS<\/strong>&nbsp;&#8211; VIX\uff0c\u8861\u91cf\u5e02\u573a\u6ce2\u52a8\u6027\u7684\u6307\u6807\uff0c<strong>T10Y3M<\/strong>&nbsp;&#8211; 10\u5e74\u51cf\u53bb3\u4e2a\u6708\u56fd\u503a\u56fa\u5b9a\u671f\u9650\uff0c<strong>T5YIE<\/strong>&nbsp;&#8211; 5\u5e74\u76c8\u4e8f\u5e73\u8861\u901a\u8d27\u81a8\u80c0\u7387\u3002\u6765\u81ea\u96c5\u864e\u8d22\u7ecf<strong>\uff08yahoo\uff09\uff1aGC=F<\/strong>&nbsp;\u2014 \u9ec4\u91d1\u671f\u8d27\u6bcf\u65e5\u4ef7\u683c\uff0c<strong>MSFT<\/strong>&nbsp;\u2014 Microsoft\u80a1\u7968\u6bcf\u65e5\u4ef7\u683c\u3002\u8fd9\u4e24\u4e2a\u6765\u6e90\u90fd\u53ef\u4ee5\u514d\u8d39\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong>future_variables \uff1a<\/strong>&nbsp;\u9884\u6d4b\u65f6\u5df2\u77e5\u7684\u672a\u6765\u5916\u751f\u53d8\u91cf\uff0c\u4f8b\u5982<strong>\u65e5<\/strong>\u548c<strong>\u6708<\/strong>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u8ba4\u4e3a\u6295\u8d44\u7ec4\u5408\u7ecf\u7406\u5728\u6bcf\u4e2a\u6708\u5e95\u91cd\u65b0\u5e73\u8861\u4ed6\u4eec\u7684\u6295\u8d44\u7ec4\u5408\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f71\u54cd\u5f53\u65f6 SPY \u7684\u6bcf\u65e5\u56de\u62a5\u7387\u3002\u8003\u8651\u6708\u5e95\uff08\u4f8b\u5982\uff0c\u7b2c 1 \u4e2a\u6708\u548c\u7b2c 31 \u5929\uff09\u7b49\u53d8\u91cf\u53ef\u4ee5\u6355\u6349\u8fd9\u4e9b\u5f71\u54cd\uff0c\u5e76\u6709\u52a9\u4e8e\u505a\u51fa\u66f4\u597d\u7684\u9884\u6d4b\u3002<\/li>\n\n\n\n<li><strong>TFT_parameters \uff1a&nbsp;<\/strong>\u6709\u5173 TFT \u53c2\u6570\u548c\u8d85\u53c2\u6570\u7684\u5b9a\u4e49\uff0c\u8bf7\u53c2\u9605\u6b64\u9875\u9762\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u8d85\u53c2\u6570\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 NeuralForecast \u63d0\u4f9b\u7684\u9ed8\u8ba4\u503c\u3002<\/li>\n\n\n\n<li><strong>h \uff1a1<\/strong>&nbsp;\u2014 \u9884\u6d4b\u8303\u56f4\uff0c\u5373\u6a21\u578b\u9884\u6d4b\u7684\u63d0\u524d\u591a\u5c11\u6b65\u3002\u6211\u4eec\u5c06\u5176\u8bbe\u7f6e\u4e3a 1\uff0c\u56e0\u4e3a\u6211\u4eec\u60f3\u8981\u8868\u73b0\u5f97\u50cf\u65e5\u5185\u4ea4\u6613\u8005\u3002\u5728\u6bcf\u5929\u7ed3\u675f\u65f6\uff0c\u6211\u4eec\u60f3\u77e5\u9053\u6211\u4eec\u662f\u5426\u5e94\u8be5\u4e3a\u7b2c\u4e8c\u5929\u505a\u591a\u3001\u505a\u7a7a\u6216\u4e0d\u505a\u4ed3\u3002\u8fd9\u4f7f\u5f97\u65e5\u5e38\u51b3\u7b56\u6210\u4e3a\u53ef\u80fd\u3002<\/li>\n\n\n\n<li><strong>input_size \uff1a64<\/strong>&nbsp;\u2014 \u7528\u4f5c\u6a21\u578b\u8f93\u5165\u7684\u8fc7\u53bb\u65f6\u95f4\u6b65\u957f\u6570\uff0c\u76f8\u5f53\u4e8e\u5927\u7ea6 3 \u4e2a\u6708\u7684\u4ea4\u6613\u65e5\u3002<\/li>\n\n\n\n<li><strong>val_check_steps \uff1a1<\/strong>&nbsp;\u2014 \u8bad\u7ec3\u671f\u95f4\u8ba1\u7b97\u9a8c\u8bc1\u6307\u6807\u7684\u9891\u7387\uff08\u4ee5\u6b65\u957f\u4e3a\u5355\u4f4d\uff09\u3002\u8fd9\u662f\u975e\u5e38\u5c0f\u7684\u3002\u5bf9\u4e8e\u66f4\u5927\u7684\u6570\u636e\u96c6\u548c\u66f4\u590d\u6742\u7684\u6a21\u578b\uff08\u4f8b\u5982\u66f4\u5927\u7684hidden_size\uff09\uff0c\u5b83\u53ef\u4ee5\u589e\u52a0\u5230 10\u300150\u3001100\uff0c\u56e0\u4e3a\u8ba1\u7b97\u6210\u672c\u4f1a\u5f88\u9ad8\u3002<\/li>\n\n\n\n<li><strong>valid_batch_size \uff1a2000&nbsp;<\/strong>\u2014\u9a8c\u8bc1\u671f\u95f4\u7684\u6279\u5904\u7406\u5927\u5c0f\u8bbe\u7f6e\u4e3a 2000\uff0c\u4ee5\u786e\u4fdd\u5728\u8ba1\u7b97\u9a8c\u8bc1\u5ea6\u91cf\u65f6\u4e00\u6b21\u5904\u7406\u6240\u6709\u9a8c\u8bc1\u6570\u636e\u3002\u8fd9\u662f\u56e0\u4e3a\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\u7684\u6784\u5efa\u65b9\u5f0f\u4f7f\u6211\u4eec\u53ea\u9700\u8981\u5728\u4e00\u4e2a\u6279\u5904\u7406\u4e2d\u4f20\u9012\u6240\u6709\u9a8c\u8bc1\u6570\u636e\u3002\u7a0d\u540e\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/li>\n\n\n\n<li><strong>scaler_type \uff1a&#8217;robust&#8217;<\/strong>&nbsp;\u2014 \u7528\u4e8e\u5728\u8bad\u7ec3\u8fed\u4ee3\u671f\u95f4\u5206\u522b\u5bf9\u6bcf\u4e2a\u8f93\u5165\u7a97\u53e3\u7684\u6570\u636e\u8fdb\u884c\u5f52\u4e00\u5316 \uff08TemporalNorm\uff09 \u7684\u7f29\u653e\u5668\u7c7b\u578b\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u4f7f\u7528\u9c81\u68d2\u5b9a\u6807\u5668\uff0c\u8fd9\u662f\u7528\u4e8e\u65f6\u95f4\u878d\u5408\u8f6c\u6362\u5668 \uff08TFT\uff09 \u7684\u9ed8\u8ba4\u5b9a\u6807\u5668\u3002\u5b83\u5bf9\u5f02\u5e38\u503c\u6709\u6548\uff0c\u5f02\u5e38\u503c\u5728\u7ecf\u6d4e\u548c\u91d1\u878d\u65f6\u95f4\u5e8f\u5217\u4e2d\u5f88\u5e38\u89c1\u3002<\/li>\n\n\n\n<li><strong>loss \uff1a&#8217;HuberMQLoss&#8217;<\/strong>&nbsp;\u2014 \u7528\u4e8e\u8bad\u7ec3\u7684\u635f\u5931\u51fd\u6570\u662f\u5177\u6709\u591a\u5206\u4f4d\u6570\u56de\u5f52\u7684 Huber \u635f\u5931\u3002\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u5f02\u5e38\u503c\u5f88\u53ef\u9760\u3002Quantiles \u901a\u8fc7\u63d0\u4f9b\u67d0\u4e9b\u7ed3\u679c\u7684\u6982\u7387\u800c\u4e0d\u4ec5\u4ec5\u662f\u70b9\u9884\u6d4b\uff0c\u63d0\u4f9b\u4e86\u66f4\u73b0\u5b9e\u7684\u89c6\u89d2\uff0c\u5c24\u5176\u662f\u5728\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u4e2d\u3002\u6b64\u5916\uff0c\u5b83\u8fd8\u5141\u8bb8\u4f7f\u7528\u5206\u4f4d\u6570\u63a8\u65ad\u6982\u7387\u3002<\/li>\n\n\n\n<li><strong>gradient_clip_val \uff1a1<\/strong>&nbsp;\u2014 \u68af\u5ea6\u526a\u88c1\u7684\u6700\u5927\u503c\uff0c\u4ee5\u9632\u6b62\u68af\u5ea6\u7206\u70b8\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 TensorBoard \u8ddf\u8e2a\u68af\u5ea6\u6765\u786e\u8ba4\u6700\u4f73\u503c\u3002<\/li>\n\n\n\n<li><strong>confidence_level \uff1a0.6<\/strong>&nbsp;\u2014 \u8fd9\u662f\u6211\u4eec\u8fdb\u5165\u4ea4\u6613\u7684\u6700\u4f4e\u6c34\u5e73\u3002\u8d1f\u56de\u62a5\u6216\u6b63\u56de\u62a5\u7684\u6982\u7387\u5fc5\u987b\u8d85\u8fc7\u6b64\u6c34\u5e73;\u5426\u5219\uff0c\u6211\u4eec\u4e0d\u4f1a\u5728\u8be5\u6570\u636e\u70b9\uff08\u6216\u65e5\u671f\uff09\u8fdb\u884c\u4ea4\u6613\u3002\u8fd9\u4e2a\u7f6e\u4fe1\u5ea6\u8bbe\u7f6e\u5f97\u5f88\u4f4e\uff0c\u56e0\u4e3a\u6a21\u578b\u5f88\u7b80\u5355\uff0c\u800c\u4e14\u60f3\u6cd5\u662f\u6784\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\u3002<\/li>\n\n\n\n<li><strong>\u5206\u4f4d\u6570 \uff1a[ 0.05\uff0c 0.4\uff0c 0.5\uff0c 0.6\uff0c 0.95 ]<\/strong>&nbsp;\u2014\u7528\u4e8e\u9884\u6d4b\u7684\u4e0d\u540c\u5206\u4f4d\u6570\u3002<\/li>\n\n\n\n<li><strong>EarlyStopping<\/strong>\uff1aPyTorch Lightning \u56de\u8c03\uff0c\u7528\u4e8e\u76d1\u63a7\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\uff0c\u5982\u679c\u635f\u5931\u5728&nbsp;<strong>20<\/strong>&nbsp;\u4e2a epoch \u5185\u6ca1\u6709\u6539\u5584\uff08<strong>\u8010\u5fc3\uff09\uff0c<\/strong>\u5c06\u505c\u6b62\u8bad\u7ec3\uff0c\u6700\u5c0f\u5316\u76d1\u63a7\u503c\uff08<strong>mode\uff1a&#8217;min&#8217;<\/strong>\uff09\u3002<\/li>\n\n\n\n<li><strong>ModelCheckPoint<\/strong>\uff1a\u5176\u4ed6&nbsp;PyTorch Lightning \u56de\u8c03\uff0c\u4f7f\u7528\u6700\u4f73\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\uff08<strong>\u6a21\u5f0f\uff1a&#8217;min&#8217;<\/strong>\uff09\u4fdd\u5b58\u6a21\u578b\uff0c\u4ec5\u4fdd\u7559\u6027\u80fd\u6700\u9ad8\u7684\u6a21\u578b \uff08<strong>save_top_k\uff1a1<\/strong>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u6700\u5c0f\u4ea4\u6613\u6570\u91cf\uff1f<\/strong><\/h3>\n\n\n\n<p>\u5728\u63a8\u7406\u8fc7\u7a0b\u4e2d\uff0c\u9884\u6d4b\u5206\u4f4d\u6570\u800c\u4e0d\u662f\u70b9\u9884\u6d4b\u4f7f\u6211\u4eec\u80fd\u591f\u8bc4\u4f30\u7279\u5b9a\u6bcf\u65e5\u56de\u62a5\u7684\u6982\u7387\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u7b2c 80 \u4e2a\u767e\u5206\u4f4d\u6570\uff0c\u5982\u679c\u56de\u62a5\u7387\u4e3a -0.1%\uff0c\u6211\u4eec\u53ef\u4ee5\u9884\u671f\u5f53\u5929\u56de\u62a5\u7387\u4e3a -0.1% \u6216\u4ee5\u4e0b\u7684\u53ef\u80fd\u6027\u4e3a 80%\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u6211\u4eec\u80fd\u591f\u6839\u636e\u7279\u5b9a\u7684\u9608\u503c\u6765\u6301\u6709\u5934\u5bf8\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u8bbe\u5b9a\u4e86 60% \u7684\u7f6e\u4fe1\u6c34\u5e73\uff0c\u800c\u7b2c 40 \u4e2a\u767e\u5206\u4f4d\u6570\u7684\u56de\u62a5\u7387\u4e3a\u6b63\uff0c\u90a3\u4e48\u6211\u4eec\u5f53\u5929\u5c31\u4f1a\u505a\u591a\uff0c\u9884\u8ba1\u6709 60% \u7684\u673a\u4f1a\u83b7\u5f97\u6b63\u56de\u62a5\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u7b2c 60 \u4e2a\u767e\u5206\u4f4d\u7684\u56de\u62a5\u7387\u4e3a\u8d1f\uff0c\u6211\u4eec\u5c06\u505a\u7a7a\uff0c\u9884\u8ba1\u6709 60% \u7684\u673a\u4f1a\u51fa\u73b0\u8d1f\u56de\u62a5\u3002\u5982\u679c\u8fd9\u4e24\u4e2a\u6761\u4ef6\u90fd\u4e0d\u6ee1\u8db3\uff0c\u907f\u514d\u5934\u5bf8\u6709\u52a9\u4e8e\u9632\u6b62\u4e0d\u6ee1\u8db3\u9884\u671f\u6982\u7387\u7684\u4e0d\u5fc5\u8981\u4ea4\u6613\u3002\u6ce8\u610f\u6211\u4eec\u53ef\u4ee5\u5728\u63a8\u7406\u8fc7\u7a0b\u4e2d\u63a8\u65ad\u6982\u7387\uff0c\u56e0\u4e3a\u6a21\u578b\u662f\u7528\u5206\u4f4d\u6570\u635f\u5931\u51fd\u6570\u8bad\u7ec3\u7684\uff1a\u591a\u5206\u4f4d\u6570 Huber \u635f\u5931\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e09\u3001\u51c6\u5907\u6570\u636e<\/strong><\/h2>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u83b7\u53d6\u6570\u636e<\/strong><\/h3>\n\n\n\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u4f7f\u7528 FRED \u7684 5 \u5e74\u76c8\u4e8f\u5e73\u8861\u901a\u8d27\u81a8\u80c0\u7387\u7684\u4f8b\u5b50\u3002\u6b64\u5904\u6982\u8ff0\u7684\u6b65\u9aa4\u9002\u7528\u4e8e\u5176\u4ed6\u53d8\u91cf\uff0c\u5305\u62ec\u5916\u751f\u53d8\u91cf\u548c\u76ee\u6807\u53d8\u91cf \uff08SPY\uff09\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u90e8\u5206\u662f\u4ece\u6e90\u83b7\u53d6\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd<br>import yaml<br><br>withopen('custom_validation_metric\/config.yaml', \"r\") as yaml_file:<br> &nbsp; &nbsp;config = yaml.load(yaml_file, Loader=yaml.FullLoader)<br><br>request = f\"https:\/\/fred.stlouisfed.org\/graph\/fredgraph.csv?id=T5YIE\"<br>request += f\"&amp;cosd={config&#91;'start_date']}\"<br>request += f\"&amp;coed={config&#91;'end_date']}\"<br><br>T5YIE_raw = pd.read_csv(request, parse_dates=&#91;'DATE'])<br><br>T5YIE_raw.rename(<br> &nbsp; &nbsp;columns={<br> &nbsp; &nbsp; &nbsp; &nbsp;'DATE': \"ds\",<br> &nbsp; &nbsp; &nbsp; &nbsp;T5YIE_raw.columns&#91;1]: \"value_T5YIE\",<br> &nbsp; &nbsp;},<br> &nbsp; &nbsp;inplace=True,<br>)<br><br>print(T5YIE_raw.head(10))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-40.png\" alt=\"\" class=\"wp-image-1576\"\/><\/figure>\n\n\n\n<p>\u6839\u636e&nbsp;NeuralForecast \u6570\u636e\u8f93\u5165\u4e2d\u7684\u89c4\u5b9a\uff0c\u5fc5\u987b\u547d\u540d\u65e5\u671f\u6233\u5217<code>ds\uff0c<\/code><code>Y_df<\/code><em>\u662f\u4e00\u4e2a\u5305\u542b\u4e09\u5217\u7684 DataFrame\uff1a\u6bcf\u4e2a\u65f6\u95f4\u5e8f\u5217\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u4e00\u4e2a\u5217\u5e26\u6709\u65e5\u671f\u6233\uff0c\u4e00\u5217\u5305\u542b\u5e8f\u5217\u7684\u503c\u3002<\/em><\/p>\n\n\n\n<p>\u6e05\u7406\u6570\u636e<\/p>\n\n\n\n<p>\u6211\u4eec\u5220\u9664\u7a7a\u6570\u636e\uff0c\u53ea\u4fdd\u7559\u5e02\u573a\u5f00\u653e\u7684\u65e5\u5b50\u3002\u5982\u4e0a\u56fe\u6240\u793a\uff0c2005 \u5e74 7 \u6708 4 \u65e5\uff08\u72ec\u7acb\u65e5\uff09\u6709\u4e00\u4e2a\u7a7a\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas_market_calendars as mcal<br>from typing importOptional<br><br>defobtain_market_dates(start_date: str, end_date: str, market : Optional&#91;str] = \"NYSE\") -&gt; pd.DataFrame:<br> &nbsp; &nbsp;nyse = mcal.get_calendar(market)<br> &nbsp; &nbsp;market_open_dates = nyse.schedule(<br> &nbsp; &nbsp; &nbsp; &nbsp;start_date=start_date,<br> &nbsp; &nbsp; &nbsp; &nbsp;end_date=end_date,<br> &nbsp; &nbsp;)<br> &nbsp; &nbsp;return market_open_dates<br><br>market_dates = obtain_market_dates(config&#91;'start_date'],config&#91;'end_date'])<br><br>T5YIE_correct_dates = T5YIE_raw.loc&#91;<br> &nbsp; &nbsp;T5YIE_raw&#91;'ds'].isin(market_dates.index)<br>]<br><br>defreplace_empty_data(df : pd.DataFrame) -&gt; pd.DataFrame:<br> &nbsp; &nbsp;mask = df.isin(&#91;\"\", \".\", None])<br> &nbsp; &nbsp;rows_to_remove = mask.any(axis=1)<br> &nbsp; &nbsp;return df.loc&#91;~rows_to_remove]<br><br>T5YIE_cleaned = replace_empty_data(T5YIE_correct_dates)<br>print(T5YIE_cleaned.head(10))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-41.png\" alt=\"\" class=\"wp-image-1577\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u5904\u7406\u4e22\u5931\u7684\u6570\u636e<\/strong><\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u4efb\u4f55\u5916\u751f\u53d8\u91cf\uff0c\u5982\u679c\u7f3a\u5931\u6570\u636e\u91cf\u8d85\u8fc7 \uff082%\uff09\uff0c\u5219\u8be5\u53d8\u91cf\u5c06\u4ece\u6a21\u578b\u8bad\u7ec3\u4e2d\u6392\u9664\u3002\u5982\u679c\u8be5\u53d8\u91cf\u6709\u8db3\u591f\u7684\u6570\u636e\uff0c\u6211\u4eec\u5c06\u7528\u524d\u4e00\u4e2a\u503c\u66ff\u6362\u4efb\u4f55\u7f3a\u5931\u7684\u6570\u636e\u3002<code>max_missing_data<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from typing importUnion, Tuple<br>import logging<br><br>defhandle_missing_data(<br> &nbsp; &nbsp; &nbsp; &nbsp;data: pd.DataFrame,<br> &nbsp; &nbsp; &nbsp; &nbsp;market_open_dates : pd.DataFrame,<br>) -&gt; Tuple&#91;Union&#91;None,pd.DataFrame], Union&#91;pd.DataFrame, None]]:<br> &nbsp; &nbsp;modified_data = data.copy()<br> &nbsp; &nbsp;market_open_dates&#91;\"count\"] = 0<br> &nbsp; &nbsp;market_open_dates.index = market_open_dates.index.strftime(\"%Y-%m-%d\")<br> &nbsp; &nbsp;date_counts = data&#91;'ds'].value_counts()<br><br> &nbsp; &nbsp;market_open_dates&#91;\"count\"] = market_open_dates.index.map(<br> &nbsp; &nbsp; &nbsp; &nbsp;date_counts<br> &nbsp; &nbsp;).fillna(0)<br><br> &nbsp; &nbsp;missing_dates = market_open_dates.loc&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp;market_open_dates&#91;\"count\"] &lt; 1<br> &nbsp; &nbsp;]<br><br> &nbsp; &nbsp;ifnot missing_dates.empty:<br> &nbsp; &nbsp; &nbsp; &nbsp;max_count = (<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;len(market_open_dates)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* config&#91;\"max_missing_data\"]<br> &nbsp; &nbsp; &nbsp; &nbsp;)<br><br> &nbsp; &nbsp; &nbsp; &nbsp;iflen(missing_dates) &gt; max_count:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;logging.warning(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f\"For current asset T5YIE there are \"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f\"{len(missing_dates)} missing data which is than the maximum threshold of \"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f\"{config&#91;'max_missing_data'] * 100}%\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;returnNone, None<br> &nbsp; &nbsp; &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for date, row in missing_dates.iterrows():<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modified_data = insert_missing_date(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modified_data, date, 'ds'<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)<br> &nbsp; &nbsp;return modified_data, missing_dates<br><br><br>definsert_missing_date(<br> &nbsp; &nbsp; &nbsp; &nbsp;data: pd.DataFrame,<br> &nbsp; &nbsp; &nbsp; &nbsp;date: str,<br> &nbsp; &nbsp; &nbsp; &nbsp;date_column: str<br>) -&gt; pd.DataFrame:<br> &nbsp; &nbsp;date = pd.to_datetime(date)<br> &nbsp; &nbsp;if date notin data&#91;date_column].values:<br> &nbsp; &nbsp; &nbsp; &nbsp;prev_date = (<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;data&#91;data&#91;date_column] &lt; date].iloc&#91;-1]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ifnot data&#91;data&#91;date_column] &lt; date].empty<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else data.iloc&#91;0]<br> &nbsp; &nbsp; &nbsp; &nbsp;)<br> &nbsp; &nbsp; &nbsp; &nbsp;new_row = prev_date.copy()<br> &nbsp; &nbsp; &nbsp; &nbsp;new_row&#91;date_column] = date<br> &nbsp; &nbsp; &nbsp; &nbsp;data = (<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pd.concat(&#91;data, new_row.to_frame().T], ignore_index=True)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.sort_values(by=date_column)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.reset_index(drop=True)<br> &nbsp; &nbsp; &nbsp; &nbsp;)<br> &nbsp; &nbsp;return data<br><br><br>T5YIE_processed, missing_dates = handle_missing_data(T5YIE_cleaned,market_dates)<br>T5YIE_processed&#91;'ds'] = pd.to_datetime(T5YIE_processed&#91;'ds'])<br><br>sample_date = pd.to_datetime(missing_dates.index&#91;0])<br>previous_day_data = T5YIE_processed&#91;T5YIE_processed&#91;'ds'] &lt; sample_date].tail(1)<br>missing_day_data = T5YIE_processed&#91;T5YIE_processed&#91;'ds'] == sample_date]<br>combined_data = pd.concat(&#91;previous_day_data, missing_day_data])<br><br>print(f'\\n{combined_data}\\n')<br>print(T5YIE_processed.head(10))<\/code><\/pre>\n\n\n\n<p>10 \u6708 10 \u65e5\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u7f3a\u5931\u503c\u3002\u5c3d\u7ba1\u54e5\u4f26\u5e03\u65e5\u662f\u7f8e\u56fd\u7684\u8054\u90a6\u5047\u65e5\uff0c\u4f46\u5e02\u573a\u4ecd\u7136\u5f00\u653e\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5c06 2005 \u5e74 10 \u6708 10 \u65e5\u7684\u7f3a\u5931\u503c\u66ff\u6362\u4e3a\u524d\u4e00\u4e2a\u4ea4\u6613\u65e5\uff082005 \u5e74 10 \u6708 7 \u65e5\uff09\u7684\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-42.png\" alt=\"\" class=\"wp-image-1578\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-43.png\" alt=\"\" class=\"wp-image-1579\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 \u62c6\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h3>\n\n\n\n<p>\u6211\u4eec\u5c06\u6bcf\u4e2a\u53d8\u91cf\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>train_proportion = config&#91;'train_test_split']<br>split_index = int(len(T5YIE_processed ) * train_proportion&#91;0])<br>train_T5YIE= T5YIE_processed.iloc&#91;:split_index]<br>test_T5YIE = T5YIE_processed.iloc&#91;split_index:]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4 \u7279\u6027\u5de5\u7a0b<\/strong><\/h3>\n\n\n\n<p>\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4ee5 Microsoft \u80a1\u7968\u4ef7\u683c\u4e3a\u4f8b\u6267\u884c\u7279\u6027\u5de5\u7a0b\u3002\u8fd9\u540c\u6837\u9002\u7528\u4e8e\u5176\u4ed6\u5916\u751f\u53d8\u91cf\u3002\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u65b0\u529f\u80fd\uff1a\u6bcf\u65e5\u56de\u62a5\u7387\u548c\u9ad8\u4f4e\u6bd4\u7387\u3002\u6211\u4eec\u5e0c\u671b\u8fd9\u4e9b\u529f\u80fd\u80fd\u591f\u63d0\u9ad8\u6a21\u578b\u6027\u80fd\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-44.png\" alt=\"\" class=\"wp-image-1580\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\ntrain_MSFT&#91;'high_low'] = train_MSFT&#91;'high'] \/ train_MSFT&#91;'low'] - 1\ntrain_MSFT&#91;'return'] = train_MSFT&#91;'close'] \/ train_MSFT&#91;'open'] - 1\n\ntest_MSFT&#91;'high_low'] = test_MSFT&#91;'high'] \/ test_MSFT&#91;'low'] - 1\ntest_MSFT&#91;'return'] = test_MSFT&#91;'close'] \/ test_MSFT&#91;'open'] - 1\n\nprint(f\"\\n{train_MSFT&#91;&#91;'high_low', 'return']].head(10)}\")\nplt.figure(figsize=(10, 5))\nplt.plot(train_MSFT&#91;'ds'], train_MSFT&#91;'high_low'], label='High-Low MSFT', color='blue')\nplt.xlabel('Date')\nplt.ylabel('High-Low MSFT')\nplt.legend()\nplt.grid(True)\nplt.show()\n\nplt.plot(train_MSFT&#91;'ds'], train_MSFT&#91;'return'], label='Return MSFT', color='green')\nplt.xlabel('Date')\nplt.ylabel('Return MSFT')\nplt.legend()\nplt.grid(True)\nplt.show()\n\n<img fetchpriority=\"high\" decoding=\"async\" height=\"312\" width=\"306\" src=\"https:\/\/mmbiz.qpic.cn\/sz_mmbiz_png\/FdYfM183hfg2eTickF8hIp3v0fG9ADVA0lLZtOYMODDsY1ADfwqqDpibBy0KLdKQWS0a1eGoJ8hJUfWdZYhDMxJg\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-45.png\" alt=\"\" class=\"wp-image-1581\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-46.png\" alt=\"\" class=\"wp-image-1582\"\/><\/figure>\n\n\n\n<p>\u6211\u4eec\u5c06\u4e3a\u8f93\u51fa\u53d8\u91cf SPY \u521b\u5efa\u76f8\u540c\u7684\u7279\u5f81\uff0c\u5305\u62ec\u5f00\u76d8\u4ef7\u3001\u6700\u9ad8\u4ef7\u3001\u6700\u4f4e\u4ef7\u3001\u6536\u76d8\u4ef7\u4ee5\u53ca\u6700\u9ad8\u4ef7\u548c\u6700\u4f4e\u4ef7\u4e4b\u95f4\u7684\u5dee\u503c\u3002\u4f46\u662f\uff0c\u76ee\u6807\u5217 SPY \u6bcf\u65e5\u8fd4\u56de\u503c\u9700\u8981\u6807\u8bc6\u7b26\u3002\u6211\u4eec\u8fd8\u9700\u8981\u4e00\u4e2a\u7cfb\u5217\u3002<code>y<\/code><code>unique_id<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SPY_processed&#91;'y'] = SPY_processed&#91;'close'] \/ SPY_processed&#91;'open'] - 1<br>SPY_processed = SPY_processed.copy()<br>SPY_processed&#91;'unique_id'] = 'SPY'<br>train_SPY= SPY_processed.iloc&#91;:split_index]<br>test_SPY = SPY_processed.iloc&#91;split_index:]<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u63d0\u53d6\u65e5\u548c\u6708\u4f5c\u4e3a\u672a\u6765\u7684\u5916\u751f\u53d8\u91cf\u3002\u6211\u4eec\u53ea\u9700\u8981\u5bf9\u4e00\u4e2a\u53d8\u91cf\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u6240\u6709\u53d8\u91cf\u90fd\u5171\u4eab\u76f8\u540c\u7684\u65e5\u671f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.preprocessing import MinMaxScaler<br><br><br>scaler = MinMaxScaler()<br>scalers = {}<br>columns_to_scale = &#91;]<br>train_FUTURE = pd.DataFrame()<br>test_FUTURE = pd.DataFrame()<br>if'day'in config&#91;'future_variables']:<br> &nbsp; &nbsp;columns_to_scale.append('day')<br> &nbsp; &nbsp;train_FUTURE&#91;'day'] = train_MSFT&#91;'ds'].dt.day<br> &nbsp; &nbsp;test_FUTURE&#91;'day'] = test_MSFT&#91;'ds'].dt.day<br> &nbsp; &nbsp;scalers&#91;'day'] = MinMaxScaler()<br><br>if'month'in config&#91;'future_variables']:<br> &nbsp; &nbsp;columns_to_scale.append('month')<br> &nbsp; &nbsp;train_FUTURE&#91;'month'] = train_MSFT&#91;'ds'].dt.month<br> &nbsp; &nbsp;test_FUTURE&#91;'month'] = test_MSFT&#91;'ds'].dt.month<br> &nbsp; &nbsp;scalers&#91;'month'] = MinMaxScaler()<br><br>if columns_to_scale:<br><br> &nbsp; &nbsp;train_FUTURE&#91;'ds'] = train_MSFT&#91;'ds']<br> &nbsp; &nbsp;test_FUTURE&#91;'ds'] = test_MSFT&#91;'ds']<br><br>for column in columns_to_scale:<br> &nbsp; &nbsp;data_reshaped_train = train_FUTURE&#91;column].values.reshape(-1, 1)<br> &nbsp; &nbsp;data_reshaped_test = test_FUTURE&#91;column].values.reshape(-1, 1)<br> &nbsp; &nbsp;train_FUTURE&#91;&#91;column]] = scalers&#91;column].fit(data_reshaped_train)<br> &nbsp; &nbsp;train_FUTURE&#91;&#91;column]] = scalers&#91;column].transform(data_reshaped_train)<br> &nbsp; &nbsp;test_FUTURE&#91;&#91;column]] = scalers&#91;column].transform(data_reshaped_test)<br><br>print(f\"\\n{train_FUTURE.head(10)}\\n\")<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-47.png\" alt=\"\" class=\"wp-image-1583\"\/><\/figure>\n\n\n\n<p>\u4f7f\u7528\u8bad\u7ec3\u6570\u636e\u5bf9 and where \u8fdb\u884c\u7f29\u653e\uff0c\u7136\u540e\u5bf9\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u5e94\u7528\u76f8\u540c\u7684\u7f29\u653e\u3002\u5bf9\u4e8e \u548c \uff0c\u503c\u5747\u4f7f\u7528 \u5728 0 \u548c 1 \u4e4b\u95f4\u7f29\u653e\u3002<code>day<\/code><code>month<\/code><code>day<\/code><code>month<\/code><code>MinMaxScaler<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.5 \u4f7f\u6570\u636e\u4fdd\u6301\u9759\u6b62<\/h2>\n\n\n\n<p>\u4e3a\u4e86\u63d0\u9ad8\u6a21\u578b\u7684\u9c81\u68d2\u6027\uff0c\u6211\u4eec\u786e\u4fdd\u6570\u636e\u662f\u9759\u6b62\u7684\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u5177\u6709 95% \u7f6e\u4fe1\u6c34\u5e73\u7684\u589e\u5f3a Dickey-Fuller \u68c0\u9a8c\u6765\u786e\u5b9a\u8fde\u7eed\u65f6\u95f4\u5e8f\u5217\u662f\u5426\u662f\u5e73\u7a33\u7684\u3002\u6211\u4eec\u5c06\u6d4b\u8bd5\u8bad\u7ec3\u6570\u636e\u7684\u5e73\u7a33\u6027\uff0c\u5e76\u5c06\u5fc5\u8981\u7684\u8f6c\u6362\u5e94\u7528\u4e8e\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002<\/p>\n\n\n\n<p>\u8ba9\u6211\u4eec\u4ee5\u4e00\u4e2a\u5916\u751f\u53d8\u91cf\u4e3a\u4f8b\uff1a\u9ec4\u91d1\u8fde\u7eed\u5408\u7ea6\u671f\u8d27\u3002\u6211\u4eec\u5c06\u53ea\u68c0\u67e5\u9ec4\u91d1\u7684\u6536\u76d8\u4ef7\uff0c\u4f46\u76f8\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u5176\u4ed6\u7279\u5f81\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>last_date = train_GOLD&#91;'ds'].iloc&#91;-1].strftime('%Y-%m-%d')<br><br>plt.figure(figsize=(10, 5))<br>plt.plot(train_GOLD&#91;'ds'], train_GOLD&#91;'close'], label='Close Price')<br>plt.title('Gold Close Price from {} to {}'.format(start_date.date(), last_date))<br>plt.xlabel('Date')<br>plt.ylabel('Close Price')<br>plt.legend()<br>plt.grid(True)<br>plt.show()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-48.png\" alt=\"\" class=\"wp-image-1584\"\/><\/figure>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230\u65f6\u95f4\u5e8f\u5217\u7684\u5e73\u5747\u503c\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\u4e0d\u662f\u6052\u5b9a\u7684\uff0c\u8fd9\u8868\u660e\u5b83\u4e0d\u662f\u5e73\u7a33\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from statsmodels.tsa.stattools import adfuller<br><br>adf_result = adfuller(train_GOLD&#91;'close'])<br>p_value = adf_result&#91;1]<br>print(\"\\nADF Test p-value:\", p_value)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-49.png\" alt=\"\" class=\"wp-image-1585\"\/><\/figure>\n\n\n\n<p>ADF \u7ed3\u679c\u786e\u8ba4\uff1ap \u503c\u5927\u4e8e 5%\uff0c\u8868\u793a\u65f6\u95f4\u5e8f\u5217\u4e3a\u975e\u5e73\u7a33\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>first_diff_GOLD = train_GOLD.copy()<br>first_diff_GOLD.loc&#91;:, 'close_diff'] = first_diff_GOLD&#91;'close'].diff()<br>first_diff_GOLD = first_diff_GOLD.dropna()<br><br>plt.figure(figsize=(10, 5))<br>plt.plot(first_diff_GOLD&#91;'ds'], first_diff_GOLD&#91;'close_diff'], label='First Difference of Close Price')<br>plt.title('First Difference of Gold Close Price')<br>plt.xlabel('Date')<br>plt.ylabel('First Difference')<br>plt.legend()<br>plt.grid(True)<br>plt.show()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-50.png\" alt=\"\" class=\"wp-image-1586\"\/><\/figure>\n\n\n\n<p>\u901a\u8fc7\u5e94\u7528\u7b2c\u4e00\u6b21\u5fae\u5206\uff0c\u6211\u4eec\u89c2\u5bdf\u5230\u65f6\u95f4\u5e8f\u5217\u7684\u5e73\u5747\u503c\u63a5\u8fd1 0\uff0c\u5e76\u4e14\u65b9\u5dee\u968f\u65f6\u95f4\u57fa\u672c\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>adf_result_diff = adfuller(first_diff_GOLD&#91;'close_diff'])<br>p_value_diff = adf_result_diff&#91;1]<br>print(\"\\nADF Test p-value after first differencing:\", p_value_diff)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-51.png\" alt=\"\" class=\"wp-image-1587\"\/><\/figure>\n\n\n\n<p>ADF p \u503c\u4e3a 0\uff0c\u8868\u793a\u65f6\u95f4\u5e8f\u5217\u5728 95% \u7684\u7f6e\u4fe1\u6c34\u5e73\u4e0a\u4fdd\u6301\u5e73\u7a33\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.6 \u5bf9\u6570\u636e\u8fdb\u884c\u89c4\u8303\u5316<\/strong><\/h3>\n\n\n\n<p>\u5728\u8bad\u7ec3\u6a21\u578b\u4e4b\u524d\uff0c\u6211\u4eec\u4f7f\u6570\u636e\u4fdd\u6301\u9759\u6b62\u3002\u6b64\u5916\uff0c\u6211\u4eec\u5728\u8bad\u7ec3\u8fed\u4ee3\u671f\u95f4\u4f7f\u7528\u4e86\u5b9a\u6807\u5668\uff0c\u53c2\u6570\u8bbe\u7f6e\u4e3a \u3002<code>scaler_type<\/code><code>robust<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.7 \u5728\u8bad\u7ec3\u6a21\u578b\u4e4b\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4<\/strong><\/h3>\n\n\n\n<p>\u6839\u636e&nbsp;NeuralForecast \u6587\u6863\u4e2d\u7684\u89c4\u5b9a\uff0c\u6211\u4eec\u9700\u8981\u5217\u51fa\u5386\u53f2\u548c\u672a\u6765\u7684\u5916\u751f\u53d8\u91cf\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8981\u5411\u6a21\u578b\u6dfb\u52a0\u5916\u751f\u53d8\u91cf\uff0c\u9996\u5148\u5728\u521d\u59cb\u5316\u671f\u95f4\u5c06\u524d\u4e00\u4e2a\u6570\u636e\u5e27\u4e2d\u7684\u6bcf\u4e2a\u53d8\u91cf\u7684\u540d\u79f0\u6307\u5b9a\u5230\u76f8\u5e94\u7684\u6a21\u578b\u8d85\u53c2\u6570\u3002<code><\/code><\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>defrename_columns(df, suffix):<br> &nbsp; &nbsp;return df.rename(columns=lambda col: f\"{col}_{suffix}\"if col notin &#91;'y','unique_id'] else col)<br><br>current_vars = locals().copy()<br><br>train_dfs = &#91;]<br>test_dfs = &#91;]<br><br>for var_name, df in current_vars.items():<br> &nbsp; &nbsp;ifisinstance(df, pd.DataFrame):<br> &nbsp; &nbsp; &nbsp; &nbsp;if var_name.startswith('train_'):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;suffix = var_name.split('_')&#91;1]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if suffix != 'FUTURE':<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;df = df.drop(columns='ds')<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;df = rename_columns(df, suffix)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;train_dfs.append(df.reset_index(drop=True))<br><br> &nbsp; &nbsp; &nbsp; &nbsp;elif var_name.startswith('test_'):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;suffix = var_name.split('_')&#91;1]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if suffix != 'FUTURE':<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;df = df.drop(columns=&#91;'ds'])<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;df = rename_columns(df, suffix)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;test_dfs.append(df.reset_index(drop=True))<br><br><br>neuralforecast_train_df = pd.concat(train_dfs, axis=1).reset_index(drop=True)<br>neuralforecast_test_df = pd.concat(test_dfs, axis=1).reset_index(drop=True)<br><br>futr_list = config&#91;'future_variables'] if config&#91;'future_variables'] elseNone<br>hist_list = &#91;col for col in neuralforecast_test_df.columns if col notin futr_list and col<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;notin &#91;\"ds\",\"time\",\"y\",\"unique_id\"]]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u56db\u3001\u4e3a\u4ea4\u6613\u51b3\u7b56\u5b9e\u65bd\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807<\/strong><\/h2>\n\n\n\n<p>NeuralForecast \u4e2d\u7684\u6240\u6709\u6a21\u578b\u90fd\u5305\u542b\u8be5\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\uff0c\u5e76\u8fdb\u884c\u4e00\u4e9b\u4fee\u6539\u3002<code>valid_loss<\/code><\/p>\n\n\n\n<p>NeuralForecast \u4e2d\u7684\u5927\u591a\u6570\u635f\u5931\u51fd\u6570\u90fd\u7ee7\u627f\u81ea\u8be5\u7c7b\uff0c\u4f8b\u5982 Huberized Multi-Quantile Loss\u3002\u8fd9\u4e9b\u51fd\u6570\u4e0e\u6784\u9020\u51fd\u6570\u548c\u53ef\u8c03\u7528\u65b9\u6cd5\u5171\u4eab\u76f8\u540c\u7684\u5b9e\u73b0\u3002\u552f\u4e00\u9700\u8981\u7684\u53c2\u6570\u662f \uff0c\u5f20\u91cf\u683c\u5f0f\u7684\u5b9e\u9645\u503c\uff0c\u4ee5\u53ca\u5f20\u91cf\u683c\u5f0f\u7684\u9884\u6d4b\u503c\u3002\u8be5\u65b9\u6cd5\u5c06\u635f\u5931\u503c\u4ee5\u5f20\u91cf\u7684\u5f62\u5f0f\u8fd4\u56de\u3002<code>BasePointLoss<\/code><code>__init__<\/code><code>__call__<\/code><code>__call__<\/code><code>y<\/code><code>y_hat<\/code><code>__call__<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-53.png\" alt=\"\" class=\"wp-image-1589\"\/><\/figure>\n\n\n\n<p>\u9a8c\u8bc1\u635f\u5931\u4f5c\u4e3a\u5b9e\u4f8b\u901a\u8fc7\u53c2\u6570\u4f20\u9012\u6216\u8bbe\u7f6e\u4e3a\u7b49\u4e8e\u53c2\u6570\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5c06\u9a8c\u8bc1\u6307\u6807\u4f5c\u4e3a\u5b9e\u4f8b\u4f20\u9012\u5230 \u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><br>#Sample code from NeuralForecast<br>classTFT(BaseWindows):<br> &nbsp; <br> &nbsp;def__init__(<br> &nbsp; &nbsp; &nbsp;self,<br> &nbsp; &nbsp; &nbsp;loss=MAE(),<br> &nbsp; &nbsp; &nbsp;valid_loss=None,<br> &nbsp; &nbsp; &nbsp;**other_parameters)<br> &nbsp; &nbsp;super(TFT, self).__init__(<br> &nbsp; &nbsp; &nbsp;loss=loss,<br> &nbsp; &nbsp; &nbsp;valid_loss=valid_loss,<br> &nbsp; &nbsp; &nbsp;**other_parameters<br> &nbsp; &nbsp; &nbsp;)<br> &nbsp; &nbsp;...<br><br>classBaseWindows(BaseModel):<br> &nbsp;def__init__(<br> &nbsp; &nbsp; &nbsp; &nbsp;self,<br> &nbsp; &nbsp; &nbsp; &nbsp;loss,<br> &nbsp; &nbsp; &nbsp; &nbsp;valid_loss,<br> &nbsp; &nbsp; &nbsp; &nbsp;**other_parameters)<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;if valid_loss isNone:<br> &nbsp; &nbsp; &nbsp;self.valid_loss = loss<br> &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp;self.valid_loss = valid_loss<br> &nbsp;...<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\uff0c\u8be5\u6307\u6807\u91c7\u7528\u76f8\u540c\u7684\u53c2\u6570\u5e76\u8fd4\u56de\u76f8\u540c\u7684\u503c\u7c7b\u578b\uff08\u5f20\u91cf\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from neuralforecast.losses.pytorch import BasePointLoss, level_to_outputs, quantiles_to_outputs<br>from typing importOptional, Union<br>import torch<br><br>classRiskReturn(BasePointLoss):<br> &nbsp; &nbsp;def__init__(<br> &nbsp; &nbsp; &nbsp; &nbsp;self, level=&#91;80, 90], quantiles=None, delta: float = 1.0, horizon_weight=None, config_manager : Optional &#91;ConfigManager] = None,<br> &nbsp; &nbsp;):<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;withopen('custom_validation_metric\/config.yaml', \"r\") as yaml_file:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self._config = yaml.load(yaml_file, Loader=yaml.FullLoader)<br><br> &nbsp; &nbsp; &nbsp; &nbsp;qs, output_names = level_to_outputs(level)<br> &nbsp; &nbsp; &nbsp; &nbsp;qs = torch.Tensor(qs)<br><br> &nbsp; &nbsp; &nbsp; &nbsp;if quantiles isnotNone:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_, output_names = quantiles_to_outputs(quantiles)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;qs = torch.Tensor(quantiles)<br> &nbsp; &nbsp; &nbsp; &nbsp;super(RiskReturn, self).__init__(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;horizon_weight=horizon_weight,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputsize_multiplier=len(qs),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;output_names=output_names,<br> &nbsp; &nbsp; &nbsp; &nbsp;)<br><br> &nbsp; &nbsp; &nbsp; &nbsp;self.quantiles = torch.nn.Parameter(qs, requires_grad=False)<br> &nbsp; &nbsp; &nbsp; &nbsp;self.delta = delta<br><br> &nbsp; &nbsp; &nbsp; &nbsp;self._lower_quantile = 1<br> &nbsp; &nbsp; &nbsp; &nbsp;self._upper_quantile = 3<br><br> &nbsp; &nbsp;def__call__(<br> &nbsp; &nbsp; &nbsp; &nbsp;self,<br> &nbsp; &nbsp; &nbsp; &nbsp;y: torch.Tensor,<br> &nbsp; &nbsp; &nbsp; &nbsp;y_hat: torch.Tensor,<br> &nbsp; &nbsp; &nbsp; &nbsp;mask: Union&#91;torch.Tensor, None] = None,<br> &nbsp; &nbsp;):<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;daily_returns = MetricCalculation.calculate_daily_returns(y, y_hat, lower_quantile=self._lower_quantile, upper_quantile=self._upper_quantile)<br> &nbsp; &nbsp; &nbsp; &nbsp;metrics = MetricCalculation.get_risk_rewards_metrics(daily_returns)<br> &nbsp; &nbsp; &nbsp; &nbsp;if metrics&#91;'nb_of_trades'] &lt; self._config&#91;'min_nb_trades']:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return torch.tensor(float('inf'))<br> &nbsp; &nbsp; &nbsp; &nbsp;return -return_on_risk<\/code><\/pre>\n\n\n\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u6570\u636e\u70b9\uff0c\u8868\u793a\u5b9e\u9645\u503c\uff0c\u800c\u8868\u793a\u4e94\u4e2a\u5206\u4f4d\u6570\u4e2d\u6bcf\u4e2a\u5206\u4f4d\u6570\u7684\u9884\u6d4b\u503c\u3002<code>y<\/code><code>y_hat<\/code><\/p>\n\n\n\n<p>\u6211\u4eec\u8fd4\u56de\u5426\u5b9a\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u76ee\u6807\u662f\u6700\u5c0f\u5316\u9a8c\u8bc1\u635f\u5931\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u6267\u884c\u7684\u4ea4\u6613\u6570\u91cf\u4f4e\u4e8e\u8981\u6c42 \uff0860\uff09\uff0c\u6211\u4eec\u5c06\u8fd4\u56de\u4e00\u4e2a\u65e0\u9650\u503c\uff0c\u4ee5\u5ffd\u7565\u5176\u5bf9\u8de8\u65f6\u671f\u8bad\u7ec3\u8fc7\u7a0b\u7684\u5f71\u54cd\u3002<code>return_on_risk<\/code><code>metrics['nb_of_trades']<\/code><code>min_nb_trades<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>classMetricCalculation:<br><br> &nbsp; &nbsp;def__int__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;self._daily_returns = torch.empty(0)<br> &nbsp; &nbsp; &nbsp; &nbsp;self._metrics = {}<br><br> &nbsp; &nbsp;defcalculate_daily_returns(self,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y : torch.Tensor,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y_hat : torch.Tensor,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lower_quantile :int,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upper_quantile : int) -&gt; torch.Tensor:<br><br> &nbsp; &nbsp; &nbsp; &nbsp;y_hat = y_hat.squeeze(1)<br> &nbsp; &nbsp; &nbsp; &nbsp;y = y.squeeze(1)<br> &nbsp; &nbsp; &nbsp; &nbsp;low_predictions = y_hat&#91;:, lower_quantile]<br> &nbsp; &nbsp; &nbsp; &nbsp;high_predictions = y_hat&#91;:, upper_quantile]<br><br> &nbsp; &nbsp; &nbsp; &nbsp;positive_condition = low_predictions &gt; 0<br> &nbsp; &nbsp; &nbsp; &nbsp;negative_condition = high_predictions &lt; 0<br> &nbsp; &nbsp; &nbsp; &nbsp;daily_returns = torch.full_like(y, float('-inf'))<br><br> &nbsp; &nbsp; &nbsp; &nbsp;daily_returns&#91;positive_condition] = y&#91;positive_condition]<br> &nbsp; &nbsp; &nbsp; &nbsp;daily_returns&#91;negative_condition] = -y&#91;negative_condition]<br> &nbsp; &nbsp; &nbsp; &nbsp;valid_returns = daily_returns&#91;daily_returns != float('-inf')]<br><br> &nbsp; &nbsp; &nbsp; &nbsp;self._daily_returns = valid_returns<br> &nbsp; &nbsp; &nbsp; &nbsp;return self._daily_returns<br>...<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u8bc4\u4f30\u6240\u6709\u9884\u6d4b\u503c\u3002\u5982\u679c\u4efb\u4f55\u8f83\u4f4e\u5206\u4f4d\u6570\uff08\u7b2c 40 \u4e2a\u767e\u5206\u4f4d\u6570\uff09\u503c\u5927\u4e8e 0\uff0c\u6211\u4eec\u9884\u8ba1\u5f53\u5929\u5c06\u83b7\u5f97\u6b63\u56de\u62a5\u5e76\u6301\u6709\u591a\u5934\u5934\u5bf8\u3002\u5982\u679c\u4efb\u4f55\u9ad8\u5206\u4f4d\u6570\uff08\u7b2c 60 \u4e2a\u767e\u5206\u4f4d\u6570\uff09\u503c\u5c0f\u4e8e 0\uff0c\u6211\u4eec\u6301\u6709\u7a7a\u5934\u5934\u5bf8\u3002<code>y_hat<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>...<br>defget_risk_rewards(self,daily_returns : torch.Tensor = None):<br> &nbsp; &nbsp;if daily_returns isNone:<br> &nbsp; &nbsp; &nbsp; &nbsp;daily_returns =self._daily_returns<br><br> &nbsp; &nbsp;self._metrics = {}<br> &nbsp; &nbsp;self._metrics&#91;\"nb_of_trades\"] = daily_returns.shape&#91;0]<br> &nbsp; &nbsp;if self._metrics&#91;\"nb_of_trades\"] &lt;= self._config&#91;'min_nb_trades']:<br> &nbsp; &nbsp; &nbsp; &nbsp;return self._set_zero_to_metrics(self._metrics&#91;\"nb_of_trades\"])<br> &nbsp; &nbsp;self._metrics&#91;'annualized_return'] = torch.prod(1 + daily_returns) ** (<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;252.0 \/ daily_returns.shape&#91;0]) - 1<br> &nbsp; &nbsp;self._metrics&#91;'annualized_risk'] = daily_returns.std() * (252 ** 0.5)<br> &nbsp; &nbsp;self._metrics&#91;'return_on_risk'] = self._metrics&#91;'annualized_return'] \/ self._metrics&#91;'annualized_risk']<br> &nbsp; &nbsp;return self._metrics<br><br>def_set_zero_to_metrics(self, nb_of_trades) -&gt; Dict:<br> &nbsp; &nbsp;self._metrics = {<br> &nbsp; &nbsp; &nbsp; &nbsp;'annualized_return': torch.tensor(0.0),<br> &nbsp; &nbsp; &nbsp; &nbsp;'annualized_risk': torch.tensor(0.0),<br> &nbsp; &nbsp; &nbsp; &nbsp;'return_on_risk': torch.tensor(0.0),<br> &nbsp; &nbsp; &nbsp; &nbsp;'nb_of_trades' : nb_of_trades<br> &nbsp; &nbsp;}<br> &nbsp; &nbsp;return self._metrics<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\u3002\u5b83\u7684\u8ba1\u7b97\u65b9\u5f0f\u662f\u9664\u4ee5\uff08\u5e74\u5316\u6807\u51c6\u5dee\uff09\uff0c\u6216\u8005\u7b80\u5355\u5730\u662f\u98ce\u9669\u56de\u62a5\u7387\u3002\u867d\u7136\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Sortino \u6216 Sharpe \u6bd4\u7387\u6765\u83b7\u5f97\u66f4\u9ad8\u7684\u7cbe\u5ea6\uff0c\u4f46\u8fd9\u4e2a\u6307\u6807\u5bf9\u4e8e\u6211\u4eec\u7684\u7528\u4f8b\u6765\u8bf4\u5df2\u7ecf\u8db3\u591f\u4e86\u3002\u8d8a\u9ad8\u8d8a\u597d\u3002<code>annualized_return<\/code><code>annualized_risk<\/code><code>return_on_risk<\/code><code>return_on_risk<\/code><\/p>\n\n\n\n<p>\u5982\u679c\u4ea4\u6613\u6570\u91cf\u5c0f\u4e8e\u6240\u9700\u7684\u6700\u4f4e\u8981\u6c42\uff0c\u6211\u4eec\u8fd4\u56de 0\u3002\u5426\u5219\uff0c\u8be5\u51fd\u6570\u5c06\u8fd4\u56de\u8ba1\u7b97\u51fa\u7684\u6307\u6807\u3002<code>min_nb_trades<\/code><code>return_on_risk<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e94\u3001\u8bad\u7ec3\u548c\u6d4b\u8bd5\u6a21\u578b<\/strong><\/h2>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\u8bad\u7ec3 TFT \u6a21\u578b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b2c 1 \u6b65\uff1a\u8bad\u7ec3\u8fc7\u7a0b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from neuralforecast.models import TFT<br>from neuralforecast import NeuralForecast<br>from neuralforecast.losses.pytorch import HuberMQLoss<br>from pandas.tseries.offsets import CustomBusinessDay<br>from pytorch_lightning.loggers.tensorboard import TensorBoardLogger<br>from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping<br>from tensorboard.backend.event_processing import event_accumulator<br>import os<br>import numpy as np<br><br>defcreate_custom_trading_days(start_date: str, end_date: str, market: str = \"NYSE\") -&gt; CustomBusinessDay:<br> &nbsp; &nbsp;market_dates = obtain_market_dates(start_date, end_date, market)<br> &nbsp; &nbsp;trading_days = pd.DatetimeIndex(market_dates.index)<br> &nbsp; &nbsp;all_dates = pd.date_range(start=start_date, end=end_date, freq='B')<br> &nbsp; &nbsp;return CustomBusinessDay(holidays=all_dates.difference(trading_days))<br><br>defcreate_callbacks():<br> &nbsp; &nbsp;callbacks_list = &#91;]<br> &nbsp; &nbsp;for callback_name, params in config&#91;'other_parameters']&#91;'callbacks'].items():<br> &nbsp; &nbsp; &nbsp; &nbsp;if callback_name == 'EarlyStopping':<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;early_stopping = EarlyStopping(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;monitor=params&#91;'monitor'],<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;patience=params&#91;'patience'],<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;verbose=params&#91;'verbose'],<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mode=params&#91;'mode']<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callbacks_list.append(early_stopping)<br> &nbsp; &nbsp; &nbsp; &nbsp;if callback_name == 'ModelCheckPoint':<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;model_checkpoint = ModelCheckpoint(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;monitor=params&#91;'monitor'],<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mode=params&#91;'mode'],<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;save_top_k=params&#91;'save_top_k'],<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;verbose=params&#91;'verbose']<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callbacks_list.append(model_checkpoint)<br> &nbsp; &nbsp;return callbacks_list<br><br>defsave_metrics_from_tensorboard(logger_dir):<br> &nbsp; &nbsp;metrics_dict = {}<br><br> &nbsp; &nbsp;os.makedirs(f'custom_validation_metric\/tensorboard', exist_ok=True)<br> &nbsp; &nbsp;for event_file in os.listdir(logger_dir):<br> &nbsp; &nbsp; &nbsp; &nbsp;ifnot event_file.startswith('events.out.tfevents'):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;continue<br> &nbsp; &nbsp; &nbsp; &nbsp;full_path = os.path.join(logger_dir, event_file)<br> &nbsp; &nbsp; &nbsp; &nbsp;ea = event_accumulator.EventAccumulator(full_path)<br> &nbsp; &nbsp; &nbsp; &nbsp;ea.Reload()<br><br> &nbsp; &nbsp; &nbsp; &nbsp;for tag in ea.Tags()&#91;'scalars']:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;metrics_dict&#91;tag] = ea.Scalars(tag)<br><br> &nbsp; &nbsp;for metric, scalars in metrics_dict.items():<br> &nbsp; &nbsp; &nbsp; &nbsp;plt.figure(figsize=(10, 5))<br><br> &nbsp; &nbsp; &nbsp; &nbsp;if metric == 'train_loss_step':<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;steps = &#91;scalar.step for scalar in scalars]<br> &nbsp; &nbsp; &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;steps = list(range(len(scalars)))<br><br> &nbsp; &nbsp; &nbsp; &nbsp;if metric == 'valid_loss'or metric == 'ptl\/val_loss':<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;values = &#91;scalar.value for scalar in scalars]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;steps, values = zip(*&#91;(step, value) for step, value inzip(steps, values) ifnot np.isinf(value)])<br> &nbsp; &nbsp; &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;values = &#91;scalar.value for scalar in scalars]<br><br> &nbsp; &nbsp; &nbsp; &nbsp;plt.plot(steps, values, label=metric)<br> &nbsp; &nbsp; &nbsp; &nbsp;plt.xlabel('Steps'if metric == 'train_loss_step'else'Epoch')<br> &nbsp; &nbsp; &nbsp; &nbsp;plt.ylabel('Value')<br> &nbsp; &nbsp; &nbsp; &nbsp;plt.title(metric)<br> &nbsp; &nbsp; &nbsp; &nbsp;plt.legend(loc='upper right')<br> &nbsp; &nbsp; &nbsp; &nbsp;plt.savefig(f\"custom_validation_metric\/tensorboard\/{metric.replace('\/', '_')}.png\")<br> &nbsp; &nbsp; &nbsp; &nbsp;plt.close()<br><br>keys_to_remove = {'loss'}<br>hyper_params = config&#91;'TFT_parameters']<br>other_param = config&#91;'other_parameters']<br>hyper_to_keep = {key: value for key, value in hyper_params.items() if key notin keys_to_remove}<br>logger = TensorBoardLogger('custom_validation_metric')<br>logger_dir = logger.log_dir<br>callbacks = create_callbacks()<br><br>nf = NeuralForecast(models=&#91;TFT(<br> &nbsp; &nbsp;loss=HuberMQLoss(quantiles=other_param&#91;'quantiles']),<br> &nbsp; &nbsp;valid_loss=RiskReturn(),<br> &nbsp; &nbsp;**hyper_to_keep,<br> &nbsp; &nbsp;futr_exog_list=futr_list,<br> &nbsp; &nbsp;hist_exog_list=hist_list,<br> &nbsp; &nbsp;logger=logger,<br> &nbsp; &nbsp;callbacks=callbacks,<br> &nbsp; &nbsp;enable_model_summary=True,<br> &nbsp; &nbsp;enable_checkpointing=True,<br> &nbsp; &nbsp;enable_progress_bar=True,<br> &nbsp; &nbsp;)], <br>freq=create_custom_trading_days( start_date=config&#91;'start_date'],<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end_date = config&#91;'end_date'])<br>)<br><br>val_size = int(len(neuralforecast_train_df)*config&#91;'val_proportion_size'])<br>nf.fit(df=neuralforecast_train_df, val_size=val_size, use_init_models=True)<br>save_metrics_from_tensorboard(logger_dir)<\/code><\/pre>\n\n\n\n<p>NeuralForecast \u5efa\u7acb\u5728 PyTorch Lightning \u4e4b\u4e0a\uff0cPyTorch Lightning \u662f\u4e00\u4e2a PyTorch \u5305\u88c5\u5668\u3002\u56e0\u6b64\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u4f20\u9012\u5176\u4ed6\u53c2\u6570\uff0c\u4f8b\u5982\u4f7f\u7528 PyTorch Lightning&nbsp;\u5b9a\u4e49\u7684 and\u3002<code>logger<\/code><code>callbacks<\/code><\/p>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528\u6307\u5b9a\u5206\u4f4d\u6570\u4f5c\u4e3a\u8bad\u7ec3\u635f\u5931\u3002\u901a\u8fc7\u53c2\u6570\u4f20\u9012\u3002<code>HuberMQLoss()<\/code><code>quantile<\/code><code>RiskReturn()<\/code><code>validation_loss<\/code><\/p>\n\n\n\n<p>\u8be5\u51fd\u6570\u786e\u4fdd\u6a21\u578b\u53ea\u8003\u8651\u4ea4\u6613\u65e5\u3002\u4ea4\u6613\u65e5\u4e0e\u718a\u732b\u7684\u8425\u4e1a\u9891\u7387\u6216\u6bcf\u65e5\u9891\u7387\u4e0d\u540c\u3002\u8be5\u51fd\u6570\u4f7f\u7528\u9002\u5f53\u7684\u53c2\u6570\u503c\u8bbe\u7f6e\u56de\u8c03\u3002\u8be5\u51fd\u6570\u4ece TensorBoard \u4e8b\u4ef6\u65e5\u5fd7\u4e2d\u63d0\u53d6\u6bcf\u4e2a\u65f6\u671f\u7684\u8bad\u7ec3\u548c\u9a8c\u8bc1\u635f\u5931\u503c\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u50cf\u3002<code>create_custom_trading_days()<\/code><code>B<\/code><code>D<\/code><code>create_callbacks()<\/code><code>save_metrics_from_tensorboard()<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>defvalidation_step(self, batch, batch_idx):<br> &nbsp;if self.val_size == 0:<br> &nbsp; &nbsp;return np.nan<br><br> &nbsp;windows = self._create_windows(batch, step=\"val\")<br> &nbsp;n_windows = len(windows&#91;\"temporal\"])<br> &nbsp;y_idx = batch&#91;\"y_idx\"]<br> &nbsp; &nbsp;<br> &nbsp; ...<br> &nbsp;for i inrange(n_batches):<br> &nbsp; &nbsp;...<br> &nbsp; &nbsp;valid_loss_batch = self._compute_valid_loss(<br> &nbsp; &nbsp; &nbsp; &nbsp;outsample_y=original_outsample_y,<br> &nbsp; &nbsp; &nbsp; &nbsp;output=output_batch,<br> &nbsp; &nbsp; &nbsp; &nbsp;outsample_mask=outsample_mask,<br> &nbsp; &nbsp; &nbsp; &nbsp;temporal_cols=batch&#91;\"temporal_cols\"],<br> &nbsp; &nbsp; &nbsp; &nbsp;y_idx=batch&#91;\"y_idx\"],<br> &nbsp; &nbsp;)<br> &nbsp; &nbsp;valid_losses.append(valid_loss_batch)<br> &nbsp; &nbsp;batch_sizes.append(len(output_batch))<br> &nbsp;<br> &nbsp;valid_loss = torch.stack(valid_losses)<br> &nbsp;batch_sizes = torch.tensor(batch_sizes).to(valid_loss.device)<br> &nbsp;valid_loss = torch.sum(valid_loss * batch_sizes) \/ torch.sum(batch_sizes)<\/code><\/pre>\n\n\n\n<p>\u5728\u9a8c\u8bc1\u8fc7\u7a0b\u4e2d\uff0cNeuralForecast \u4f7f\u7528 PyTorch Lightning \u4e2d\u7684\u65b9\u6cd5\u3002\u6b64\u65b9\u6cd5\u8ba1\u7b97\u6bcf\u4e2a\u6279\u6b21\u7684 \uff0c\u7136\u540e\u6839\u636e\u6279\u6b21\u5927\u5c0f\u8fdb\u884c\u52a0\u6743\u5e73\u5747\u3002\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u8fc7\u7a0b\u5fc5\u987b\u8003\u8651\u6574\u4e2a\u6279\u6b21\u7684\u6700\u5c0f\u4ea4\u6613\u6570\u91cf\u3002\u76ee\u524d\u7684\u5b9e\u73b0\u4e0d\u5141\u8bb8\u8fd9\u6837\u505a\u3002<code>validation_step()<\/code><code>validation_loss<\/code><code>min_nb_trades<\/code><code>validation_step()<\/code><\/p>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u6a21\u578b\uff0c\u7c7b\u4f3c\u4e8e NeuralForecast \u4e2d\u7684 TFT\uff0c\u4f46\u5177\u6709\u81ea\u5b9a\u4e49 \u3002\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u9009\u62e9\u8bbe\u7f6e\u4e00\u4e2a\u5927 \uff082000\uff09\uff0c\u4ee5\u4fbf\u5728 \u671f\u95f4\u53ea\u5904\u7406\u4e00\u4e2a\u6279\u6b21\u3002\u8fd9\u89e3\u51b3\u4e86\u6211\u4eec\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-54.png\" alt=\"\" class=\"wp-image-1590\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-55.png\" alt=\"\" class=\"wp-image-1591\"\/><\/figure>\n\n\n\n<p>\u5982\u7b2c\u4e00\u5f20\u56fe\u6240\u793a\uff0c\u8bad\u7ec3\u635f\u5931\u5728\u7b2c 5 \u7eaa\u5143\u9644\u8fd1\u8fc5\u901f\u6536\u655b\u3002\u8fd9\u8868\u660e\u6a21\u578b\u5feb\u901f\u5b66\u4e60\u4e86\u8bad\u7ec3\u6570\u636e\u4e2d\u7684\u5e95\u5c42\u6a21\u5f0f\u3002\u8be5\u6a21\u578b\u5728\u7b2c 19 \u7eaa\u5143\u8fbe\u5230\u5176\u5cf0\u503c\u6027\u80fd\uff0c\u5982\u56fe\u7b2c\u4e8c\u5f20\u56fe\u6240\u793a\u3002\u6700\u4f18\u9a8c\u8bc1\u635f\u5931\u63a5\u8fd1 -3\uff0c\u7b49\u4e8e\u5927\u7ea6 3 \u7684 a\u3002<code>return_on_risk<\/code><\/p>\n\n\n\n<p>\u9a8c\u8bc1\u635f\u5931\u8fde\u7eed 20 \u4e2a epoch \u6ca1\u6709\u6539\u5584\uff0c\u5e76\u4e14\u5728\u56de\u8c03\u4e2d\u5c06\u53c2\u6570\u8bbe\u7f6e\u4e3a 20\u3002\u56e0\u6b64\uff0c\u7531\u4e8e\u8be5\u51c6\u5219\uff0c\u8bad\u7ec3\u5faa\u73af\u5728\u7b2c 39 \u7eaa\u5143\u505c\u6b62\u3002\u8fd9\u6709\u52a9\u4e8e\u5728\u9a8c\u8bc1\u635f\u5931\u505c\u6b62\u6539\u5584\u65f6\u7ec8\u6b62\u8bad\u7ec3\u5faa\u73af\uff0c\u4ece\u800c\u9632\u6b62\u8fc7\u62df\u5408\u3002<code>patience<\/code><code>EarlyStopping<\/code><code>EarlyStopping<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b2c 2 \u6b65\uff1a\u6d4b\u8bd5\u8fc7\u7a0b<\/strong><\/h3>\n\n\n\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u6211\u4eec\u7684\u6a21\u578b\u5728\u6837\u672c\u5916\u6570\u636e\u4e0a\u7684\u8868\u73b0\u5982\u4f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch<br>import json<br><br>defpredict():<br> &nbsp; &nbsp;test_size = len(neuralforecast_test_df)<br> &nbsp; &nbsp;y_hat_test = pd.DataFrame()<br> &nbsp; &nbsp;current_train_data = neuralforecast_train_df.copy()<br> &nbsp; &nbsp;test_ftr = neuralforecast_test_df.reset_index(drop=True)<br> &nbsp; &nbsp;y_hat = nf.predict(current_train_data,futr_df=test_ftr)<br> &nbsp; &nbsp;y_hat_test = pd.concat(&#91;y_hat_test, y_hat.iloc&#91;&#91;-1]]])<br> &nbsp; &nbsp;for i inrange(test_size-1):<br> &nbsp; &nbsp; &nbsp; &nbsp;combined_data = pd.concat(&#91;current_train_data, neuralforecast_test_df.iloc&#91;&#91;i]]])<br> &nbsp; &nbsp; &nbsp; &nbsp;y_hat = nf.predict(combined_data,futr_df=test_ftr)<br> &nbsp; &nbsp; &nbsp; &nbsp;y_hat_test = pd.concat(&#91;y_hat_test, y_hat.iloc&#91;&#91;-1]]])<br> &nbsp; &nbsp; &nbsp; &nbsp;current_train_data = combined_data<br><br> &nbsp; &nbsp;y_hat_test.reset_index(drop=True, inplace=True)<br> &nbsp; &nbsp;all_columns_except_ds = &#91;col for col in y_hat_test.columns if col notin'ds']<br> &nbsp; &nbsp;median_column = &#91;col for col in y_hat_test.columns if'-median'in col]&#91;0]<br> &nbsp; &nbsp;quantile_cols = &#91;col for col in y_hat_test.columns if col notin &#91;median_column, 'ds']]<br><br> &nbsp; &nbsp;return y_hat_test, all_columns_except_ds, median_column, quantile_cols<br><br>y_hat_test, all_columns_except_ds, median_column, quantile_cols = predict()<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u6ca1\u6709\u6309\u539f\u6837\u4f7f\u7528 NeuralForecast \u4e2d\u7684\u51fd\u6570\uff0c\u56e0\u4e3a NeuralForecast \u4e0d\u652f\u6301\u4e00\u6b21\u5bf9\u6bcf\u4e2a\u6570\u636e\u70b9\u8fdb\u884c n \u6b65\u9884\u6d4b\u3002\u8fd9\u4e2a\u9650\u5236\u610f\u5473\u7740\u6211\u4eec\u5fc5\u987b\u4e00\u4e2a\u63a5\u4e00\u4e2a\u5730\u505a\u51fa\u9884\u6d4b\uff08\uff09\u3002\u5bf9\u4e8e\u60f3\u8981\u6839\u636e\u4eca\u5929\u7684\u6570\u636e\u4e86\u89e3\u7b2c\u4e8c\u5929\u56de\u62a5\u7684\u4ea4\u6613\u8005\u6765\u8bf4\uff0c\u8fd9\u662f\u5fc5\u8981\u7684\u3002<code>predict<\/code><code>y_hat_test<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>defcalculate_metric():<br> &nbsp; &nbsp;torch_target = torch.tensor(neuralforecast_test_df&#91;'y'].to_numpy(), dtype=torch.float32).unsqueeze(-1)<br> &nbsp; &nbsp;torch_predicted = torch.tensor(y_hat_test&#91;all_columns_except_ds].to_numpy(), dtype=torch.float32)<br> &nbsp; &nbsp;metric_calc = MetricCalculation()<br> &nbsp; &nbsp;metric_calc.calculate_daily_returns(y=torch_target,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y_hat=torch_predicted,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lower_quantile=1,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upper_quantile=3)<br><br> &nbsp; &nbsp;metrics = metric_calc.get_risk_rewards(is_checking_nb_trades=False)<br> &nbsp; &nbsp;return torch_target,metrics<br>torch_target, metrics = calculate_metric()<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u76f8\u540c\u7684\u524d\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u8ba1\u7b97\u6d4b\u8bd5\u671f\u95f4\u6267\u884c\u7684\u548c \u3002<br><code>get_risk_rewardsannualized_returnreturn_on_risknb_of_trades<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>defprint_metrics():<br><br> &nbsp; &nbsp;aggregate_metrics = {<br> &nbsp; &nbsp; &nbsp; &nbsp;\"annualized_return\": metrics&#91;'annualized_return'].item(),<br> &nbsp; &nbsp; &nbsp; &nbsp;\"actual_annualized_return\": actual_annualized_return,<br> &nbsp; &nbsp; &nbsp; &nbsp;\"return_on_risk\":metrics&#91;'return_on_risk'].item(),<br> &nbsp; &nbsp; &nbsp; &nbsp;\"actual_return_on_risk\": actual_return_on_risk,<br> &nbsp; &nbsp; &nbsp; &nbsp;\"nb_of_trades\": &nbsp;metrics&#91;\"nb_of_trades\"],<br> &nbsp; &nbsp; &nbsp; &nbsp;'first_last_ds' : (first_date, last_date)<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;print(f'\\n{json.dumps(aggregate_metrics, indent=4)}\\n')<br><br>first_date= (neuralforecast_test_df&#91;'ds'].iloc&#91;0]).strftime(\"%Y-%m-%d\")<br>last_date = (neuralforecast_test_df&#91;'ds'].iloc&#91;-1]).strftime(\"%Y-%m-%d\")<br><br>nb_days = len(neuralforecast_test_df)<br>spy_data = pd.read_csv('custom_validation_metric\/SPY.csv')<br>first_value = spy_data.loc&#91;spy_data&#91;'ds'] == first_date, 'open'].iloc&#91;0]<br>last_value = spy_data.loc&#91;spy_data&#91;'ds'] == last_date, 'close'].iloc&#91;0]<br>actual_annualized_return = (last_value\/first_value)** (252 \/ nb_days) - 1<br>std_daily_return = neuralforecast_test_df&#91;'y'].std()<br>actual_annualized_risk = std_daily_return * (252 ** 0.5)<br>actual_return_on_risk = actual_annualized_return\/actual_annualized_risk<br>print_metrics()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-56.png\" alt=\"\" class=\"wp-image-1592\"\/><\/figure>\n\n\n\n<p>\u4e3a\u4e86\u8fdb\u884c\u6bd4\u8f83\uff0c\u6211\u4eec\u8ba1\u7b97\u4e86\u540c\u4e00\u65f6\u671f\u7684 and\u3002\u8fd9\u662f\u901a\u8fc7\u5b9e\u65bd\u4e70\u5165\u5e76\u6301\u6709\u7b56\u7565\uff0c\u7136\u540e\u8ba1\u7b97\u5e74\u5316\u6bcf\u65e5\u6807\u51c6\u5dee\u6765\u5b8c\u6210\u7684\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u6307\u6807\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>defplot_predictions():<br> &nbsp; &nbsp;plt.figure(figsize=(10, 6))<br> &nbsp; &nbsp;plt.plot(neuralforecast_test_df&#91;'ds'], neuralforecast_test_df&#91;'y'], color='black', label='Actual')<br> &nbsp; &nbsp;plt.plot(y_hat_test&#91;'ds'], y_hat_test&#91;median_column], label='Predicted', color='blue')<br> &nbsp; &nbsp;plt.fill_between(y_hat_test&#91;'ds'], y_hat_test&#91;quantile_cols&#91;0]], y_hat_test&#91;quantile_cols&#91;-1]], color='gray', alpha=0.5)<br> &nbsp; &nbsp;plt.xlabel('Date')<br> &nbsp; &nbsp;plt.ylabel('Output')<br> &nbsp; &nbsp;plt.title('Actual vs Predicted Values over time')<br> &nbsp; &nbsp;plt.legend()<br> &nbsp; &nbsp;plt.savefig(os.path.join(f'custom_validation_metric', 'actual_vs_predicted_test.png'))<br> &nbsp; &nbsp;plt.close()<br><br>plot_predictions()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-57.png\" alt=\"\" class=\"wp-image-1593\"\/><\/figure>\n\n\n\n<p>\u6211\u4eec\u751f\u6210\u4e86\u4e00\u4e2a\u6bd4\u8f83\u5b9e\u9645\u503c\uff08\u9ed1\u7ebf\uff09\u548c\u9884\u6d4b\u4e2d\u503c\uff08\u84dd\u7ebf\uff09\u7684\u56fe\u3002\u5b83\u8fd8\u5c06 0.05 \u548c 0.95 \u5206\u4f4d\u6570\u4e4b\u95f4\u7684\u533a\u57df\u8bbe\u7f6e\u4e3a\u7070\u8272\u9634\u5f71\uff0c\u8868\u793a 90% \u7684\u9884\u6d4b\u533a\u95f4\u3002\u8fd9\u79cd\u53ef\u89c6\u5316\u6548\u679c\u4f7f\u6211\u4eec\u80fd\u591f\u770b\u5230\u6d4b\u8bd5\u96c6\u4e2d\u7684\u9884\u6d4b\u503c\u4e0e\u5b9e\u9645\u503c\u7684\u5bf9\u9f50\u7a0b\u5ea6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u89e3\u91ca\u7ed3\u679c<\/strong><\/h3>\n\n\n\n<p>\u6211\u4eec\u7684\u7b56\u7565\u7ed3\u679c\u826f\u597d\uff0c\u5e74\u5316\u56de\u62a5\u7387\u4e3a28.66%\uff0c\u98ce\u9669\u56de\u62a5\u7387\u4e3a2.97\u3002\u8fd9\u4e00\u8868\u73b0\u8d85\u8fc7\u4e86\u6807\u51c6\u666e\u5c14500\u6307\u6570\u7684\u4e70\u5165\u5e76\u6301\u6709\u7b56\u7565\uff0c\u6807\u51c6\u666e\u5c14500\u6307\u6570\u7684\u5e74\u56de\u62a5\u7387\u4e3a18.47%\uff0c\u540c\u671f\u7684\u98ce\u9669\u56de\u62a5\u7387\u4e3a1.37\u3002\u8fd9\u4e9b\u662f\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\u7684\u7b2c\u4e00\u4e2a\u826f\u597d\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\uff0c\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u6d4b\u8bd5\u671f\u95f4\uff0c\u6211\u4eec\u5728 476 \u4e2a\u4ea4\u6613\u65e5\u4e2d\u53ea\u6267\u884c\u4e86 55 \u7b14\u4ea4\u6613\u3002<\/li>\n\n\n\n<li>\u8be5\u56fe\u8868\u663e\u793a\uff0c\u8be5\u6a21\u578b\u6ca1\u6709\u5f88\u597d\u5730\u6355\u6349\u6f5c\u5728\u7684\u5e02\u573a\u6a21\u5f0f\u3002\u5b83\u7684\u884c\u4e3a\u66f4\u50cf\u662f n \u5929\u79fb\u52a8\u5e73\u5747\u7ebf\u3002<\/li>\n\n\n\n<li>0.05 \u548c 0.95 \u5206\u4f4d\u6570\u4e4b\u95f4\u7684\u8303\u56f4\u6bd4\u5b9e\u9645\u7684 90% \u7f6e\u4fe1\u533a\u95f4\u66f4\u5bbd\u3002\u9884\u6d4b\u5206\u4f4d\u6570\u5728\u7b2c\u4e00\u5e74\u8d85\u8fc7 0.03 \u548c -0.03\uff0c\u6355\u83b7\u4e86\u5b9e\u9645\u503c\u7684 90% \u4ee5\u4e0a\u3002<\/li>\n\n\n\n<li>\u8be5\u6a21\u578b\u5728\u77ed\u77ed 20 \u4e2a\u65f6\u671f\u5185\u8fc5\u901f\u6536\u655b\uff0c\u8868\u660e\u8be5\u6a21\u578b\u53ef\u80fd\u7f3a\u4e4f\u590d\u6742\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e9b\u89c2\u5bdf\u7ed3\u679c\u8868\u660e\uff0c\u8be5\u6a21\u578b\u8fc7\u4e8e\u7b80\u5355\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u7279\u5f81\u4e0d\u8db3\u6216\u8d85\u53c2\u6570\u503c\u592a\u5c0f\uff0c\u4f8b\u5982\u8f83\u5c0f\u7684 .\u4e3a\u4e86\u589e\u52a0\u4ea4\u6613\u6570\u91cf\u5e76\u63d0\u9ad8\u6027\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5f00\u53d1\u4e00\u4e2a\u591a\u53d8\u91cf\u65f6\u95f4\u5e8f\u5217\u6a21\u578b\u3002\u989d\u5916\u7684\u8bc1\u5238\u4e0d\u5e94\u4e0eSPY\u8fc7\u4e8e\u76f8\u5173\uff0c\u4f8b\u5982\u76f8\u5173\u7cfb\u6570\u5728-0.5\u81f30.5\u4e4b\u95f4\u7684\u503a\u5238\u6216\u5546\u54c1\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6355\u6349\u6f5c\u5728\u7684\u5e02\u573a\u6a21\u5f0f\u3002<code>hidden_size<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u770b\u5230 RMSE \u548c MSE \u7b49\u6807\u51c6\u9a8c\u8bc1\u6307\u6807\u5e76\u4e0d\u662f\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u4e2d\u80a1\u7968\u5e02\u573a\u9884\u6d4b\u7684\u6700\u4f73\u9009\u62e9\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5206\u6b65\u6307\u5357\uff0c\u7528\u4e8e\u5b9e\u65bd\u548c\u8bad\u7ec3\u5177\u6709\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u6307\u6807\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u4ee5\u5b9e\u73b0\u66f4\u597d\u7684\u4ea4\u6613\u51b3\u7b56\u3002\u5373\u4f7f\u8fd9\u79cd\u65b9\u6cd5\u6709\u6f5c\u529b\uff0c\u7ed3\u679c\u4e5f\u4e0d\u662f\u51b3\u5b9a\u6027\u7684\u3002\u4e3a\u4e86\u6539\u8fdb\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528\u591a\u53d8\u91cf\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u6dfb\u52a0\u66f4\u591a\u7279\u5f81\u53ef\u80fd\u4f1a\u6709\u6240\u5e2e\u52a9\u3002<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5f15\u7528\uff1a<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Philippe Ostiguy.Enhancing Deep Learning Model Evaluation for Stock Market Forecasting.medium.2024<strong><\/strong><\/h4>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><mark style=\"background-color:#ffffff\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u672c\u6587\u5185\u5bb9\u4ec5\u4ec5\u662f\u6280\u672f\u63a2\u8ba8\u548c\u5b66\u4e60\uff0c\u5e76\u4e0d\u6784\u6210\u4efb\u4f55\u6295\u8d44\u5efa\u8bae\u3002<br>\u8f6c\u53d1\u8bf7\u6ce8\u660e\u539f\u4f5c\u8005\u548c\u51fa\u5904<\/mark><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u8001\u4f59\u635e\u9c7c \u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/laoyulaoyu.com\/index.php\/2024\/08\/30\/%e5%8f%91%e8%b4%a2%e4%b8%8e%e4%ba%8f%e9%92%b1%e4%b8%80%e5%bf%b5%e9%97%b4%ef%bc%8c%e8%bf%98%e5%be%97%e7%9c%8b%e8%82%a1%e5%b8%82%e9%a2%84%e6%b5%8b%e4%b8%ad%e5%a6%82%e4%bd%95%e8%af%84%e4%bc%b0%e6%b7%b1\/\">Continue reading<span class=\"screen-reader-text\">\u53d1\u8d22\u4e0e\u4e8f\u94b1\u4e00\u5ff5\u95f4\uff0c\u8fd8\u5f97\u770b\u6df1\u5ea6\u5b66\u4e60\u5982\u4f55\u9a8c\u8bc1\u80a1\u5e02\u9884\u6d4b\u7ed3\u679c<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[6],"class_list":["post-1377","post","type-post","status-publish","format-standard","hentry","category-aiinvest","tag-6","entry"],"_links":{"self":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/comments?post=1377"}],"version-history":[{"count":3,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1377\/revisions"}],"predecessor-version":[{"id":1384,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1377\/revisions\/1384"}],"wp:attachment":[{"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}