-
-
Save amanbthakkar/68e9ae5c26ae83318c0000a5d9e8384d to your computer and use it in GitHub Desktop.
This file contains 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
#this is the function we want to fit over our data: a.log(x)+b | |
#we need to find appropriate coefficients | |
def func(x, p1, p2): | |
return p1*np.log(x) + p2 | |
#we are fitting log of price of BTC against the function, not actual price | |
ydata = np.log(df["Value"]) | |
xdata = [x+1 for x in range(len(df))] #just use numbers for dates | |
extended_dates = pd.date_range(df["Date"].iloc[0], "2023-01-01") | |
#extract optimal coefficients using curve fit | |
popt, pcov = curve_fit(func, xdata, ydata, p0=(3.0, -10)) | |
#try to get ydata from xdata and function | |
#popt has coefficients, pcov has covariances between them | |
print(popt) | |
#generate fitted Y data | |
fittedYdata = func(np.array([x+1 for x in range(len(df))]), popt[0], popt[1])#pass values to function | |
plt.style.use("dark_background") | |
fig, ax = plt.subplots() | |
ax.semilogy(df["Date"], df["Value"]) | |
plt.yscale('log', subsy=[1]) | |
ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter()) | |
ax.yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter()) | |
plt.plot(df["Date"], np.exp(fittedYdata)) #exponentiate the data | |
plt.title("BTC logarithmic regression") | |
plt.ylabel("Price in USD") | |
plt.ylim(bottom=0.1) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also appreciate the your questions and answers fellas. Good stuff here.