I need to fit a model with two parameters to data. The model, however, is calculated in recursive way (the current calculation is calculated based on the previous calculation) and should be >= 0.
I used several panda frames with about 180 columns and 365 rows, but to frame it right I give just simple examples here.
the code is something like this:
A = {'col1': [1, 2, 3, 4], 'col2': [3, 4, 5, 6]} #model variables
B = {'col1': [0, 50, 156, 318], 'col2': [0, 182, 427, 735]} #data for model fitting
a = {'col1': [1], 'col2': [3]} #parameter to be optimized
b = {'col1': [4], 'col2': [5]} #parameter to be optimized
X.loc[0] = 0
for index in range(1,364):
X.loc[index]=np.where(
X.loc[index-1]-(a-(b*7*(1-A.loc[index])))<0,0,
X.loc[index-1]-(a-(b*7*(1-A.loc[index])))
the columns in X should fit the columns in B by changing parameter a and b. I tried scipy.curve_fit but it does not work.
def Y(A,a,b):
return #this is where it gets tricky#
for index in range(1,364):
X.loc[index]=np.where(
X.loc[index-1]-(a-(b*7*(1-A.loc[index])))<0,0,
X.loc[index-1]-(a-(b*7*(1-A.loc[index])))
popt, pcov = curve_fit(X, A, B,p0=([1, 3],[4, 5]))
Does anyone know how to fix this equation? Or suggest the best way to optimize this equation?
Thank you.