Last active
February 1, 2021 19:04
-
-
Save tolleiv/d8e2dc0c587e63d78b1ee1efc16054be to your computer and use it in GitHub Desktop.
Logarithmic regression with historical data - as seen on https://www.youtube.com/watch?v=gYm6OgeJAuE&ab_channel=sentdex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
from scipy.optimize import curve_fit | |
import matplotlib.pyplot as plt | |
import matplotlib.cm as mcm | |
# data prep | |
df = pd.read_csv('input.csv') | |
df = df.iloc[::-1] | |
df = df[df['Value'] > 0] | |
df['Date'] = pd.to_datetime(df['Date']) | |
df = df[['Date', 'Value']] | |
#print(df.head()) | |
#print(df.tail()) | |
# regression | |
def funct(x, p1, p2): | |
return p1 * np.log(x) + p2 | |
xdata = np.array([x+1 for x in range(len(df))]) | |
ydata = np.log(df['Value']) | |
popt, pcov = curve_fit(funct, xdata, ydata, p0=(3.0,-10)) | |
#print(popt) | |
fitteddata = funct(xdata,popt[0], popt[1]) | |
# plot | |
plt.style.use('dark_background') | |
colors = mcm.rainbow(np.linspace(0, 1, 6)) | |
plt.semilogy(df['Date'], df['Value']) | |
plt.plot(df['Date'], np.exp(fitteddata)) | |
for i in range(-2,4): | |
plt.fill_between(df['Date'], np.exp(fitteddata+i-1), np.exp(fitteddata+i), alpha=0.4, color=colors[i+2]) | |
plt.ylim(bottom=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment