How to find the logarithmic (non-linear) model's parameters in python?

121 views Asked by At

So I need to come up with models paraments in order to be able to use the model for the prediction (coefficients and etc.). Sorry if my questions sounds noobish but I am learning :) The overall model that is used should be smth like : y=p1*np.log(x)+p2. So the questions is if p1 & p2 are represented by popt[0] & popt1? log(x) - just the number of dates (the data set is run from 2010-08-16 to 2021-04-14)? y -price should be predicted based on p1 & p2 and x.

import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

df = pd.read_csv('C:/Data_crypto/Log regression_bands/BCHAIN-MKPRU.csv')
df = df[ df['Value'] > 0]
df = df.iloc[::-1]
df['Date'] = pd.to_datetime(df['Date'])
df

enter image description here

def func(x, p1,p2):
    return p1*np.log(x)+p2
ydata = np.log(df['Value'])
xdata = np.array([x + 1 for x in range(len(df))])
popt, pcov = curve_fit(func, xdata, ydata)
print(popt)

print(pcov)

popt[0]

popt[1]

xdata

## y axis will be loged and x axis wont be

plt.semilogy(df["Date"], df['Value'])
plt.show()

## plot fitted data and expinatiate data

fittedydata = func(xdata, popt[0], popt[1])


popt[0]

plt.semilogy(df['Date'], df['Value'])
plt.plot(df['Date'], np.exp(fittedydata))
plt.ylim(bottom = 1)

plt.show()

enter image description here

0

There are 0 answers