Skip to content

Instantly share code, notes, and snippets.

@amanbthakkar
Created October 20, 2021 15:52
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 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()
@datphan234
Copy link

Hi Aman, i found you from medium when i was looking for logarithmic regression for btc and now here in github. I am very very new to programming and I have mostly self learned python the past few months. Would you mind uploading the whole code of the program (libraries used, variables defined etc) because im not good enough to work on your based codes. It would be very kind of you. Thank you in advance :D

@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