Last active
June 22, 2024 09:16
-
-
Save ChadThackray/bed7d1ac28567317afd223d66e230764 to your computer and use it in GitHub Desktop.
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
# Licensed under the MIT License. See comment below for full licence information. | |
import pandas as pd | |
import numpy as np | |
from scipy.optimize import curve_fit | |
import matplotlib.pyplot as plt | |
## data processing | |
df = pd.read_csv("data.csv") | |
df = df[ df['Value'] > 0] | |
df = df.iloc[::-1] | |
dates = pd.to_datetime(df['Date']) | |
## logarithmic function | |
def func(x, p1,p2): | |
return p1*np.log(x)+p2 | |
## fitting data | |
ydata = np.log(df['Value']) | |
xdata = [x + 1 for x in range(len(df))] | |
popt, pcov = curve_fit(func, xdata, ydata,p0=(3.0,-10)) | |
fittedYdata = func(np.array([x for x in range(len(df))]),popt[0],popt[1]) | |
plt.style.use('dark_background') | |
for i in range(-3,5): | |
#plt.plot(dates,np.exp(fittedYdata+i)) | |
plt.fill_between(dates, np.exp(fittedYdata + i - 1), np.exp(fittedYdata +i), alpha = 0.4) | |
plt.semilogy(dates,df['Value']) | |
plt.ylim(bottom = 1) | |
#plt.show() | |
plt.savefig("log-regression.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is some sample data you can use. Save it as
data.csv
in the same folder as the code