Skip to content

Instantly share code, notes, and snippets.

@amanbthakkar
Created October 20, 2021 15:52
Show Gist options
  • Save amanbthakkar/68e9ae5c26ae83318c0000a5d9e8384d to your computer and use it in GitHub Desktop.
Save amanbthakkar/68e9ae5c26ae83318c0000a5d9e8384d to your computer and use it in GitHub Desktop.
#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()
@amanbthakkar
Copy link
Author

amanbthakkar commented Jan 13, 2022

Hi @datphan234, of course! On my medium posts I have provided the Github link with the entire code. Here it is: https://github.com/amanbthakkar/crypto_finance. The code in the posts is not meant to give a full picture anyway so do not worry about not being good enough :)

@datphan234
Copy link

Hi @amanbthakkar , sorry I was new to coding and the community in general so I missed the link. Thank you very much for sharing!!

@GripMuhCrypt
Copy link

Also appreciate the your questions and answers fellas. Good stuff here.

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