-
-
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() |
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 :)
Hi @amanbthakkar , sorry I was new to coding and the community in general so I missed the link. Thank you very much for sharing!!
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
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