Skip to content

Instantly share code, notes, and snippets.

@ChadThackray
Last active June 22, 2024 09:16
Show Gist options
  • Save ChadThackray/bed7d1ac28567317afd223d66e230764 to your computer and use it in GitHub Desktop.
Save ChadThackray/bed7d1ac28567317afd223d66e230764 to your computer and use it in GitHub Desktop.
# 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")
@ChadThackray
Copy link
Author

ChadThackray commented Jun 20, 2024

Here is some sample data you can use. Save it as data.csv in the same folder as the code

Date,Value
2020-12-26,23715.53
2020-12-25,23253.37
2020-12-24,23824.99
2020-12-23,22745.48
2020-12-22,23490.58
2020-12-21,23869.92
2020-12-20,23150.79
2020-12-19,22847.46
2020-12-18,21379.48
2020-12-17,19439.75
2020-12-16,19276.59
2020-12-15,19164.48
2020-12-14,18803.44
2020-12-13,18029.36
2020-12-12,18247.76
2020-12-11,18554.15
2020-12-10,18318.87
2020-12-09,19181.41
2020-12-08,19377.66
2020-12-07,19155.06
2020-12-06,18670.49
2020-12-05,19454.54
2020-12-04,19226.97
2020-12-03,18792.52
2020-12-02,19709.73
2020-12-01,18191.6
2020-11-30,17732.42
2020-11-29,17138.87
2020-11-28,17151.44
2020-11-27,18739.8
2020-11-26,19172.52
2020-11-25,18398.91
2020-11-24,18422.28
2020-11-23,18699.75
2020-11-22,18687.45
2020-11-21,17820.57
2020-11-20,17798.45
2020-11-19,17679.72
2020-11-18,16725.15
2020-11-17,15968.16
2020-11-16,16091.07
2020-11-15,16339.33
2020-11-14,16295.57
2020-11-13,15708.65
2020-11-12,15317.04
2020-11-11,15328.53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment