1

I have written the following two functions to calibrate a model :

The main function is:

def function_Price(para,y,t,T,tau,N,C):  
# y= price array
# C = Auto and cross correlation array
# a= paramters need to be calibrated
    a=para[0:]
    temp=0
    for j in range(N):
        price_j = a[j]*C[j]*P[t:T-tau,j]
        temp=temp+price_j
        Price=temp
        return Price 

The objective function is :

def GError_function_Price(para,y,k,t,T,tau,N,C):
# k is the price need to be fitted
   return sum((function_Price(para,y,t,T,tau,N,C)-k[t+tau:T]) ** 2)

Now, I am calling these two functions to do the optimization of the model:

import numpy as np
from scipy.optimize import minimize
 
# Prices (example)
y = np.array([[1,2,3,4,5,4], [4,5,6,7,8,9], [6,7,8,7,8,6], [13,14,15,11,12,19]])

# Correaltion (example)
Corr= np.array([[1,2,3,4,5,4], [4,5,6,7,8,9], [6,7,8,7,8,6], [13,14,15,11,12,19],[1,2,3,4,5,4],[6,7,8,7,8,6]])

# Define 
tau=1
Size = y.shape
N = Size[1]
T = Size[0]
t=0

# initial Values
para=np.zeros(N) 

# Bounds
B = np.zeros(shape=(N,2)) 
for n in range(N):
    B[n][0]= float('-inf')
    B[n][1]= float('inf')

# Calibration 
A = np.zeros(shape=(N,N))  
for i in range (N):
    k=y[:,i] #fitted one
    C=Corr[i,:]
    parag=minimize(GError_function_Price,para,args=(y,Y,t,T,tau,N,C),method='SLSQP',bounds=B)
    A[i,:]=parag.x

Once, I run the model, It should produce an N by N array of optimized values of paramters. But, except for the first column, it keeps zeros for the rest. Something is wrong.

Can you help me fix the problem, please?

I know how to do it in Matlab.

The following is Matlab Code : main function

function Price=function_Price(para,P,t,T,tau,N,C)  
        a=para(:,:);
        temp=0;
       for j=1:N
        price_j = a(j).*C(j).*P(t:T-tau,j);
        temp=temp+price_j;
       end
      Price=temp;
end

The objective function:

function gerr=GError_function_Price(para,P,Y,t,T,tau,N,C)
gerr=sum((function_Price(para,P,t,T,tau,N,C)-Y(t+tau:T)).^2);
end

Now, I call these two functions in the following way:

P = [1,2,3,4,5,4;4,5,6,7,8,9;6,7,8,7,8,6;13,14,15,11,12,19];
AutoAndCrossCorr= [1,2,3,4,5,4;4,5,6,7,8,9;6,7,8,7,8,6;13,14,15,11,12,19;1,2,3,4,5,4;6,7,8,7,8,6];

tau=1;
Size = size(P);
N =6;
T =4;
t=1;   

for i=1:N
Y=P(:,i); % fitted one
C=AutoAndCrossCorr(i,:);
para=zeros(1,N);
lb= repmat(-inf,N,1);
ub= repmat(inf ,N,1);
parag=fminsearchbnd(@(para)abs(GError_function_Price(para,P,Y,t,T,tau,N,C)),para,lb,ub);
a(i,:)=parag;
end

1 Answer 1

2

The problem seems to be that you're passing the result of a function call to minimize, rather than the function itself. The arguments get passed by the args parameter. So instead of:

minimize(GError_function_Price(para,y,k,t,T,tau,N,C),para,method='SLSQP',bounds=B)

the following should work

minimize(GError_function_Price,para,args=(y,k,t,T,tau,N,C),method='SLSQP',bounds=B)
Sign up to request clarification or add additional context in comments.

9 Comments

Hi @eeegnu, Thank you for the suggestion. After correcting the argument as you said, now getting the following error: ValueError: Objective function must return a scalar
@MMKarim I notice that you didn't add a return statement in the objective function you posted. Are you returning gerr (if not it returns None)?
Hi @eeegnu, Thank you again. I have edited the code following your suggestion. You can see the code in the edited version of my question. It now runs. However, it should give me the optimized parameter of an (N by N) array containing the optimized values. But, except for the first column, it gives zeros to others. Something is wrong.
@MMKarim, you should probably keep the error in your submission to not confuse future readers of your question! It's a bit hard to tell what's causing your issue, but one guess is that you're passing the same x0 (para) at every step which looks like an issue.
Hi @eeegnu. I found the issue. The issue is the indention. The last line of the main function return Price position should be the same as for-loop. Now, I put it just below the Price=temp, because of that it repeating the same results instead of following the for-loop. Matlab doesn't have such issue, I was not aware of the indention problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.