- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试预测以下内容:
list( [
收盘价(当日)
- 开盘价(当日)
] )
使用以下内容作为输入:
list( [
开盘价(当天)
- 收盘价(昨天)
] )
但是,我的 test_prediction
结果是对错误事物的预测。
来自 sklearn
和 statsmodels
线性回归模型的预测显示输入数据 (test_data
) 和 预测之间有大约 100% 的相关性results
,而预测结果
应与test_result
相关。
我做错了什么(或遗漏了什么),如何解决? 代码将生成 4 个图,显示不同列表之间的相关性。
<小时/>###### Working usable example and code below ######
import numpy as np
from plotly.offline import plot
import plotly.graph_objs as go
from sklearn import linear_model
import statsmodels.api as sm
def xy_corr( x, y, fname ):
trace1 = go.Scatter( x = x,
y = y,
mode = 'markers',
marker = dict( size = 6,
color = 'black',
),
showlegend = False
)
layout = go.Layout( title = fname )
fig = go.Figure( data = [trace1],
layout = layout
)
plot( fig, filename = fname + '.html' )
open_p = [23215, 23659, 23770, 23659, 23659, 23993, 23987, 23935, 24380, 24271, 24314, 24018, 23928, 23240, 24193, 23708, 23525, 23640, 23494, 23333, 23451, 23395, 23395, 23925, 23936, 24036, 24008, 24248, 24249, 24599, 24683, 24708, 24510, 24483, 24570, 24946, 25008, 24880, 24478, 24421, 24630, 24540, 24823, 25090, 24610, 24866, 24578, 24686, 24465, 24225, 24526, 24645, 24780, 24538, 24895, 24921, 24743, 25163, 25163, 25316, 25320, 25158, 25375, 25430, 25466, 25231, 25103, 25138, 25138, 25496, 25502, 25610, 25625, 25810, 25789, 25533, 25785, 25698, 25373, 25558, 25594, 25026, 24630, 24509, 24535, 24205, 24465, 23847, 24165, 23840, 24216, 24355, 24158, 23203, 23285, 23423, 23786, 23729, 23944, 23637]
close_p = [23656, 23758, 23663, 23659, 23989, 23978, 24142, 24152, 24279, 24271, 24393, 23942, 23640, 24102, 23710, 23708, 23705, 23693, 23561, 23441, 23395, 23395, 23990, 23900, 24158, 24188, 24241, 24248, 24699, 24678, 24715, 24523, 24486, 24483, 24947, 24904, 24923, 24478, 24434, 24421, 24409, 24705, 25047, 24642, 24875, 24866, 24698, 24463, 24262, 24396, 24633, 24645, 24528, 24895, 24895, 24839, 25178, 25163, 25315, 25323, 25149, 25387, 25375, 25469, 25231, 25073, 25138, 25138, 25448, 25611, 25705, 25623, 25813, 25798, 25560, 25518, 25743, 25305, 25654, 25579, 25315, 24783, 24508, 24532, 24208, 24176, 24047, 24148, 24165, 24159, 24286, 24249, 23635, 23128, 23438, 23869, 23420, 23756, 23705, 24018]
open_prev_close_diff = np.array( [ open_p[i] - close_p[i-1] for i in range( 1, len( open_p ) )] )[np.newaxis].T
open_current_close_diff = np.array( [close_p[i] - open_p[i] for i in range( 1, len( open_p ) )] )
train_data = open_prev_close_diff[ :80]
test_data = open_prev_close_diff[80:]
train_result = open_current_close_diff[ :80]
test_result = open_current_close_diff[80:]
regressor = linear_model.LinearRegression()
regressor.fit( train_data, train_result )
test_prediction = np.array( [int(i) for i in regressor.predict( test_data )] )
xy_corr( [int(i) for i in test_result], test_prediction, 'known_result_and_prediction_result_sklearn')
xy_corr( [int(i) for i in test_data], test_prediction, 'input_data_and_prediction_result_sklearn' )
olsmod = sm.OLS( train_result, train_data )
olsres = olsmod.fit()
test_prediction = np.array( [int(i) for i in olsres.predict( test_data )] )
xy_corr( [int(i) for i in test_result], test_prediction, 'known_result_and_prediction_result_smOLS')
xy_corr( [int(i) for i in test_data], test_prediction, 'input_data_and_prediction_result_smOLS' )
最佳答案
With a hope no one would consider this impolite and/or harmfull,
let me cite a lovely point of view on the underlying assumption from “Correlation does not imply Causation“: that many contemporary Quantitative Finance modellers neglect or abstract from:
对于任意两个相关事件,A
和 B
,
以下关系是可能的:A
导致 B
; (直接因果关系)B
导致 A
; (反向因果关系)A
和 B
是共同原因的结果,但互不导致;A
导致 B
且 B
导致 A
(双向或循环因果关系);< br/>A
导致 C
,而 C
又导致 B
(间接因果关系);A
和 B
之间没有任何联系;相关性是 coincidence .
因此,仅从因果关系无法得出关于因果关系存在或方向的结论事实上A
和B
是相关的。
LinearRegressor()
除了一条线之外没有产生任何其他东西,这应该没什么奇怪的——也就是说,它的每个预测确实都在非常符合模特的线条。因此,对于这种预测器来说,一半的图显然是必须做的。Q.E.D.
另一半没有显示任何其他内容,只是线性模型所采用的过度简化程度尽管是可观察到的现实。
当然,所经历的真实行为不是线性的(但不要责怪预测器“无法拟合”,它有责任制定一个 MSE
- 最小化器驱动的线性模型,它无法在 DataSET
的训练部分找到任何更好的线性拟合,并且如果它是通过 进行训练的y = x^2
合成DataSET
(对抛物线形状有先验知识) ,再次它只会产生一个线性模型,在的训练部分分数上具有最小
和我们都提前很确定,因此任何行都会产生完全有缺陷的预测MSE
DataSETOoS
,但不是由于它无法很好地工作,但由于外部灌输的尝试在(故意二次)上下文中使用线性模型预测器的主要废话,它不遵循(已知的)现实)。
作为一种基本的定量观点,比严格的Kolmogorov-Smirnov
测试简单得多,对于未表达的假设,
检查 日内缺口中负差异的百分比(开盘[i] - 收盘[i-1])
[ 75% ]
仅 100 个样本的浅 DataSET
相对于
当天蜡烛主体的负差(收盘价[i] - 开盘价[i])
仅包含 100 个样本的相当浅的 DataSET
中的 [ 55% ]
无论如何,即使在设计得更好的预测模型上,训练少至 80 天的样本也很难很好地泛化,并且不仅应该关注更好的泛化能力,还应该关注避免季节性偏差等。
To have some idea where the ML goes into this field, my best preforming AI/ML-models have about
0k3
features ( many of 'em highly non-linear synthetic features ) and get intensively trained accross30k+ DataSETs
with carefully carving out their risk to overfitting and searching vast space of the learner-engines' hyperparameterStateSPACE
|
|>>> QuantFX.get_LDF_GDF_fromGivenRANGE( [ open_PRICE[i] - close_PRICE[i-1] for i in range( 1, len( close_PRICE ) ) ], nBINs_ = 31, aPrefixTEXT_ = "" )
0: ~ -432.00 LDF = 1 |____ 1.0 % _||____ 1 %
1: ~ -408.10 LDF = 1 |____ 1.0 % _||____ 2 %
2: ~ -384.19 LDF = 1 |____ 1.0 % _||____ 3 %
3: ~ -360.29 LDF = 0 |____ 0.0 % _||____ 3 %
4: ~ -336.39 LDF = 1 |____ 1.0 % _||____ 4 %
5: ~ -312.48 LDF = 1 |____ 1.0 % _||____ 5 %
6: ~ -288.58 LDF = 1 |____ 1.0 % _||____ 6 %
7: ~ -264.68 LDF = 0 |____ 0.0 % _||____ 6 %
8: ~ -240.77 LDF = 1 |____ 1.0 % _||____ 7 %
9: ~ -216.87 LDF = 3 |____ 3.0 % _||___ 10 %
10: ~ -192.97 LDF = 2 |____ 2.0 % _||___ 12 %
11: ~ -169.06 LDF = 1 |____ 1.0 % _||___ 13 %
12: ~ -145.16 LDF = 1 |____ 1.0 % _||___ 14 %
13: ~ -121.26 LDF = 2 |____ 2.0 % _||___ 16 %
14: ~ -97.35 LDF = 5 |____ 5.1 % _||___ 21 %
15: ~ -73.45 LDF = 3 |____ 3.0 % _||___ 24 %
16: ~ -49.55 LDF = 5 |____ 5.1 % _||___ 29 %
17: ~ -25.65 LDF = 18 |___ 18.2 % _||___ 47 %
18: ~ -1.74 LDF = 28 |___ 28.3 % _||___ 75 %
19: ~ 22.16 LDF = 5 |____ 5.1 % _||___ 80 %
20: ~ 46.06 LDF = 5 |____ 5.1 % _||___ 85 %
21: ~ 69.97 LDF = 2 |____ 2.0 % _||___ 87 %
22: ~ 93.87 LDF = 1 |____ 1.0 % _||___ 88 %
23: ~ 117.77 LDF = 4 |____ 4.0 % _||___ 92 %
24: ~ 141.68 LDF = 1 |____ 1.0 % _||___ 93 %
25: ~ 165.58 LDF = 1 |____ 1.0 % _||___ 94 %
26: ~ 189.48 LDF = 1 |____ 1.0 % _||___ 95 %
27: ~ 213.39 LDF = 1 |____ 1.0 % _||___ 96 %
28: ~ 237.29 LDF = 0 |____ 0.0 % _||___ 96 %
29: ~ 261.19 LDF = 1 |____ 1.0 % _||___ 97 %
30: ~ 285.10 LDF = 2 |____ 2.0 % _||__ 100 %
+0:00:06.234000
|
|
|>>> QuantFX.get_LDF_GDF_fromGivenRANGE( [ close_PRICE[i] - open_PRICE[i] for i in range( 1, len( close_PRICE ) ) ], nBINs_ = 31, aPrefixTEXT_ = "" )
0: ~ -523.00 LDF = 2 |____ 2.0 % _||____ 2 %
1: ~ -478.32 LDF = 1 |____ 1.0 % _||____ 3 %
2: ~ -433.65 LDF = 3 |____ 3.0 % _||____ 6 %
3: ~ -388.97 LDF = 1 |____ 1.0 % _||____ 7 %
4: ~ -344.29 LDF = 1 |____ 1.0 % _||____ 8 %
5: ~ -299.61 LDF = 2 |____ 2.0 % _||___ 10 %
6: ~ -254.94 LDF = 7 |____ 7.1 % _||___ 17 %
7: ~ -210.26 LDF = 3 |____ 3.0 % _||___ 20 %
8: ~ -165.58 LDF = 2 |____ 2.0 % _||___ 22 %
9: ~ -120.90 LDF = 5 |____ 5.1 % _||___ 27 %
10: ~ -76.23 LDF = 6 |____ 6.1 % _||___ 33 %
11: ~ -31.55 LDF = 22 |___ 22.2 % _||___ 55 %
12: ~ 13.13 LDF = 7 |____ 7.1 % _||___ 62 %
13: ~ 57.81 LDF = 5 |____ 5.1 % _||___ 67 %
14: ~ 102.48 LDF = 4 |____ 4.0 % _||___ 71 %
15: ~ 147.16 LDF = 8 |____ 8.1 % _||___ 79 %
16: ~ 191.84 LDF = 6 |____ 6.1 % _||___ 85 %
17: ~ 236.52 LDF = 2 |____ 2.0 % _||___ 87 %
18: ~ 281.19 LDF = 3 |____ 3.0 % _||___ 90 %
19: ~ 325.87 LDF = 2 |____ 2.0 % _||___ 92 %
20: ~ 370.55 LDF = 2 |____ 2.0 % _||___ 94 %
21: ~ 415.23 LDF = 3 |____ 3.0 % _||___ 97 %
22: ~ 459.90 LDF = 0 |____ 0.0 % _||___ 97 %
23: ~ 504.58 LDF = 0 |____ 0.0 % _||___ 97 %
24: ~ 549.26 LDF = 0 |____ 0.0 % _||___ 97 %
25: ~ 593.94 LDF = 1 |____ 1.0 % _||___ 98 %
26: ~ 638.61 LDF = 0 |____ 0.0 % _||___ 98 %
27: ~ 683.29 LDF = 0 |____ 0.0 % _||___ 98 %
28: ~ 727.97 LDF = 0 |____ 0.0 % _||___ 98 %
29: ~ 772.65 LDF = 0 |____ 0.0 % _||___ 98 %
30: ~ 817.32 LDF = 1 |____ 1.0 % _||__ 100 %
+0:01:13.172000
关于python - 线性回归预测预测输入数据(测试数据),而不是测试结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37963235/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!