英文:
how do I plot a standard deviation error line?
问题
使用scipy,我可以绘制标准差线。如何在该线的上方复制一个95%的线,以及在该线的下方复制另一个95%的线。
请查看下面的两张图片,以了解我的意思:
我的代码:
import datetime as date
import matplotlib.pyplot as plt
%matplotlib inline
import datetime
from scipy import stats
Ins_Name = "EURUSD=X"
# Ins_Name = "AAPL"
df = yf.download(Ins_Name,'2019-05-01','2020-01-03')
df.reset_index(inplace=True)
df['date_ordinal'] = pd.to_datetime(df['Date']).apply(lambda date: date.toordinal())
DateVariance = [datetime.date(2019, 5, 1), datetime.date(2020, 1, 3)]
x_reg = df.date_ordinal
y_reg = df.Close
slope, intercept, r_value, p_value, std_err = stats.linregress(x_reg, y_reg)
print("slope: %f intercept: %f STD Error: %f" % (slope, intercept, std_err))
sns.set()
# plt.figure(figsize=(26, 10))
fig, ax = plt.subplots(figsize=(15,7))
ax = plt.plot(x_reg,intercept + slope*x_reg, color='b')
# ax = sns.lmplot('date_ordinal', 'Close', data=df, fit_reg=True, aspect=2, ) #Scatter PLot
ax = sns.regplot(data=df,x='date_ordinal',y=df.Close,ci=1,fit_reg=False,scatter_kws={"color": "red"}, line_kws={"color": "black"}, marker='x') #scatterplot
# sns.jointplot('date_ordinal', df.Close, data=df, kind="reg",ylim=[1.089,1.15],xlim=DateVariance, height=12,color='red',scatter_kws={"color": "red"}, line_kws={"color": "black"})
ax.set_xlabel('Interval', fontsize=25)
ax.set_xlim(DateVariance)
ax.set_ylabel('Price', fontsize=25)
ax.set_title('Mean reversion of ' + Ins_Name + ' Close Prices',fontsize= 45,color='black')
new_labels = [datetime.date.fromordinal(int(item)) for item in ax.get_xticks()]
ax.set_xticklabels(new_labels)
希望这能帮助你。
英文:
Using scipy I can plot the standard deviation line. How do I duplicate that same line 95% above and another one 95% below.
See the two images below of what I mean:
My code:
import datetime as date
import matplotlib.pyplot as plt
%matplotlib inline
import datetime
from scipy import stats
Ins_Name = "EURUSD=X"
#Ins_Name = "AAPL"
df = yf.download(Ins_Name,'2019-05-01','2020-01-03')
df.reset_index(inplace=True)
df['date_ordinal'] = pd.to_datetime(df['Date']).apply(lambda date: date.toordinal())
DateVariance = [datetime.date(2019, 5, 1), datetime.date(2020, 1, 3)]
x_reg = df.date_ordinal
y_reg = df.Close
slope, intercept, r_value, p_value, std_err = stats.linregress(x_reg, y_reg)
print("slope: %f intercept: %f STD Error: %f" % (slope, intercept, std_err))
sns.set()
#plt.figure(figsize=(26, 10))
fig, ax = plt.subplots(figsize=(15,7))
ax = plt.plot(x_reg,intercept + slope*x_reg, color='b')
#ax = sns.lmplot('date_ordinal', 'Close', data=df, fit_reg=True, aspect=2, ) #Scatter PLot
ax = sns.regplot(data=df,x='date_ordinal',y=df.Close,ci=1,fit_reg=False,scatter_kws={"color": "red"}, line_kws={"color": "black"}, marker='x') #scatterplot
#sns.jointplot('date_ordinal', df.Close, data=df, kind="reg",ylim=[1.089,1.15],xlim=DateVariance, height=12,color='red',scatter_kws={"color": "red"}, line_kws={"color": "black"})
ax.set_xlabel('Interval', fontsize=25)
ax.set_xlim(DateVariance)
ax.set_ylabel('Price', fontsize=25)
ax.set_title('Mean reversion of ' + Ins_Name + ' Close Prices',fontsize= 45,color='black')
new_labels = [datetime.date.fromordinal(int(item)) for item in ax.get_xticks()]
ax.set_xticklabels(new_labels)
答案1
得分: 0
我认为函数 stats.linregress()
并不是你想要的。它只返回斜率的不确定性,而不是 y 截距,如果我理解你的问题正确的话,你想要的是 y 截距。
我建议使用 scipy.optimize.curve_fit()
,像下面这样:
import datetime as date
import matplotlib.pyplot as plt
import datetime
from scipy.optimize import curve_fit
import yfinance as yf
import pandas as pd
import numpy as np
import seaborn as sns
def fline(x, *par):
return par[0] + par[1]*x
Ins_Name = "EURUSD=X"
#Ins_Name = "AAPL"
df = yf.download(Ins_Name,'2019-05-01','2020-01-03')
df.reset_index(inplace=True)
df['date_ordinal'] = pd.to_datetime(df['Date']).apply(lambda date: date.toordinal())
DateVariance = [datetime.date(2019, 5, 1), datetime.date(2020, 1, 3)]
x_reg = df.date_ordinal
x_trans = x_reg - x_reg[0]
y_reg = df.Close
pi = [1.1, -0.0001] # 初始猜测值,可以使用 linregress 获取这些值
# 在这里执行曲线拟合
popt, cov = curve_fit(fline, x_trans, y_reg, p0=pi) # 返回值和协方差(误差)矩阵
yInterceptSigma = np.sqrt(cov[0,0])
print(x_reg)
print(x_reg - x_reg[0])
print(popt)
print(cov)
sns.set()
#plt.figure(figsize=(26, 10))
fig, ax = plt.subplots(figsize=(15,7))
ax = plt.plot(x_reg, fline(x_trans, *popt), color='b')
# +/- 2sigma gives ~95% CI
plt.plot(x_reg, fline(x_trans, *[popt[0] + 2 * yInterceptSigma, popt[1]]), '--b') # +2sigma on the yintercept
plt.plot(x_reg, fline(x_trans, *[popt[0] - 2 * yInterceptSigma, popt[1]]), '--b') # -2sigma on teh yintercept
#ax = sns.lmplot('date_ordinal', 'Close', data=df, fit_reg=True, aspect=2, ) #Scatter PLot
ax = sns.regplot(data=df,x='date_ordinal',y=df.Close,ci=1,fit_reg=False,scatter_kws={"color": "red"}, line_kws={"color": "black"}, marker='x') #scatterplot
#sns.jointplot('date_ordinal', df.Close, data=df, kind="reg",ylim=[1.089,1.15],xlim=DateVariance, height=12,color='red',scatter_kws={"color": "red"}, line_kws={"color": "black"})
ax.set_xlabel('Interval', fontsize=25)
ax.set_xlim(DateVariance)
ax.set_ylabel('Price', fontsize=25)
ax.set_title('Mean reversion of ' + Ins_Name + ' Close Prices',fontsize= 45,color='black')
new_labels = [datetime.date.fromordinal(int(item)) for item in ax.get_xticks()]
ax.set_xticklabels(new_labels)
plt.show()
英文:
I think the function stats.linregress()
is not what you want here. It only returns the uncertainty on the slope and not the y-intercept, which, if I understand your question, is what you want.
I would use scipy.optimize.curve_fit()
like below:
import datetime as date
import matplotlib.pyplot as plt
import datetime
from scipy.optimize import curve_fit
import yfinance as yf
import pandas as pd
import numpy as np
import seaborn as sns
def fline(x, *par):
return par[0] + par[1]*x
Ins_Name = "EURUSD=X"
#Ins_Name = "AAPL"
df = yf.download(Ins_Name,'2019-05-01','2020-01-03')
df.reset_index(inplace=True)
df['date_ordinal'] = pd.to_datetime(df['Date']).apply(lambda date: date.toordinal())
DateVariance = [datetime.date(2019, 5, 1), datetime.date(2020, 1, 3)]
x_reg = df.date_ordinal
x_trans = x_reg - x_reg[0]
y_reg = df.Close
pi = [1.1, -0.0001] # initial guess. Could use linregress to get these values
# Perform curve fitting here
popt, cov = curve_fit(fline, x_trans, y_reg, p0=pi) # returns the values and the covariance (error) matrix
yInterceptSigma = np.sqrt(cov[0,0])
print(x_reg)
print(x_reg - x_reg[0])
print(popt)
print(cov)
sns.set()
#plt.figure(figsize=(26, 10))
fig, ax = plt.subplots(figsize=(15,7))
ax = plt.plot(x_reg, fline(x_trans, *popt), color='b')
# +/- 2sigma gives ~95% CI
plt.plot(x_reg, fline(x_trans, *[popt[0] + 2 * yInterceptSigma, popt[1]]), '--b') # +2sigma on the yintercept
plt.plot(x_reg, fline(x_trans, *[popt[0] - 2 * yInterceptSigma, popt[1]]), '--b') # -2sigma on teh yintercept
#ax = sns.lmplot('date_ordinal', 'Close', data=df, fit_reg=True, aspect=2, ) #Scatter PLot
ax = sns.regplot(data=df,x='date_ordinal',y=df.Close,ci=1,fit_reg=False,scatter_kws={"color": "red"}, line_kws={"color": "black"}, marker='x') #scatterplot
#sns.jointplot('date_ordinal', df.Close, data=df, kind="reg",ylim=[1.089,1.15],xlim=DateVariance, height=12,color='red',scatter_kws={"color": "red"}, line_kws={"color": "black"})
ax.set_xlabel('Interval', fontsize=25)
ax.set_xlim(DateVariance)
ax.set_ylabel('Price', fontsize=25)
ax.set_title('Mean reversion of ' + Ins_Name + ' Close Prices',fontsize= 45,color='black')
new_labels = [datetime.date.fromordinal(int(item)) for item in ax.get_xticks()]
ax.set_xticklabels(new_labels)
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论