Я думаю, что функция stats.linregress()
не то, что вы хотите здесь. Это только возвращает неопределенность на склоне, а не 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] # 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()
, что дает следующий график: ![enter image description here](https://i.stack.imgur.com/s7hQi.png)