Skip to content

Instantly share code, notes, and snippets.

@tolleiv
Last active February 1, 2021 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolleiv/d8e2dc0c587e63d78b1ee1efc16054be to your computer and use it in GitHub Desktop.
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
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