Hi all, I write the following code to do some grid search work. After running the code, I get errors as follows. I suspect I do not specify the arguments in the solvez() function correctly. There might be other problem in my codes as well. Could anyone have a try of my codes and tell me how to fix the problem entirely? I would very appreciate your help since I have been involved in this code for around one week...
Best regards -- Alan
solvez(): 3204 Fx[3,1] found where scalar required
solvenl__calluser_2(): - function returned error
solvenl__functioneval(): - function returned error
solvenl_solve__newton(): - function returned error
_solvenl_solve(): - function returned error
mywork(): - function returned error
<istmt>: - function returned error
Code:
set obs 20000
gen id=_n
gen e=.
gen phi=.
gen N_e=100 // number of values for e
gen N_phi=200 // number of values for phi
gen step_e=0.01 // step of grids for e
gen step_phi=1 // step of grids for phi
gen error_code=.
gen z1=.
gen z2=.
gen z3=.
gen b1=.
gen b2=.
gen b3=.
foreach seq_e of numlist 1(1)100 {
foreach seq_phi of numlist 1(1)200 {
qui replace e=0.01*`seq_e' if id==(`seq_e'-1)*(N_phi)+`seq_phi'
qui replace phi=`seq_phi' if id==(`seq_e'-1)*(N_phi)+`seq_phi'
}
}
capture program drop solveprogram
program define solveprogram
args num
mata: mywork(`num')
end
clear mata
mata:
void mywork(real num_value)
{
e_temp=st_data(num_value, "e") // import value of e and phi from Stata data to Mata
e_temp=st_data(num_value, "phi")
S = solvenl_init()
solvenl_init_argument(S, 1, e_temp)
solvenl_init_argument(S, 2, phi_temp)
solvenl_init_evaluator(S, &solvez())
solvenl_init_type(S, "zero")
solvenl_init_technique(S, "newton")
solvenl_init_startingvals(S, J(3,1,10)) // set the starting value of z1 z2 z3 as 10
solvenl_init_conv_maxiter(S, 100) // set maximum iteration number, by default it is 16000, I change to 100 for computational convenience.
solvenl_init_iter_log(S, "off")
_solvenl_solve(S)
ERR=solvenl_result_error_code(S)
M=solvenl_result_values(S)
st_matrix("M", M)
st_matrix("ERR", ERR)
}
void function solvez(real colvector z, real scalar e, real scalar phi, real colvector y)
{
y[1] = ((20000+z[1])*(1-0.2)/(1+1/e))*(1-(20000/(20000+z[1]))^(1+1/e))-phi-z[1]*(1-0.25)
y[2] = ((9000+z[2])*(1-0.2)/(1+1/e))*(1-(9000/(9000+z[2]))^(1+1/e))-phi-z[2]*(1-0.25)
y[3] = ((35000+z[3])*(1-0.25)/(1+1/e))*(1-(35000/(35000+z[3]))^(1+1/e))-phi-z[3]*(1-0.3)
}
end
solveprogram 1
Best regards -- Alan
solvez(): 3204 Fx[3,1] found where scalar required
solvenl__calluser_2(): - function returned error
solvenl__functioneval(): - function returned error
solvenl_solve__newton(): - function returned error
_solvenl_solve(): - function returned error
mywork(): - function returned error
<istmt>: - function returned error
Code:
set obs 20000
gen id=_n
gen e=.
gen phi=.
gen N_e=100 // number of values for e
gen N_phi=200 // number of values for phi
gen step_e=0.01 // step of grids for e
gen step_phi=1 // step of grids for phi
gen error_code=.
gen z1=.
gen z2=.
gen z3=.
gen b1=.
gen b2=.
gen b3=.
foreach seq_e of numlist 1(1)100 {
foreach seq_phi of numlist 1(1)200 {
qui replace e=0.01*`seq_e' if id==(`seq_e'-1)*(N_phi)+`seq_phi'
qui replace phi=`seq_phi' if id==(`seq_e'-1)*(N_phi)+`seq_phi'
}
}
capture program drop solveprogram
program define solveprogram
args num
mata: mywork(`num')
end
clear mata
mata:
void mywork(real num_value)
{
e_temp=st_data(num_value, "e") // import value of e and phi from Stata data to Mata
e_temp=st_data(num_value, "phi")
S = solvenl_init()
solvenl_init_argument(S, 1, e_temp)
solvenl_init_argument(S, 2, phi_temp)
solvenl_init_evaluator(S, &solvez())
solvenl_init_type(S, "zero")
solvenl_init_technique(S, "newton")
solvenl_init_startingvals(S, J(3,1,10)) // set the starting value of z1 z2 z3 as 10
solvenl_init_conv_maxiter(S, 100) // set maximum iteration number, by default it is 16000, I change to 100 for computational convenience.
solvenl_init_iter_log(S, "off")
_solvenl_solve(S)
ERR=solvenl_result_error_code(S)
M=solvenl_result_values(S)
st_matrix("M", M)
st_matrix("ERR", ERR)
}
void function solvez(real colvector z, real scalar e, real scalar phi, real colvector y)
{
y[1] = ((20000+z[1])*(1-0.2)/(1+1/e))*(1-(20000/(20000+z[1]))^(1+1/e))-phi-z[1]*(1-0.25)
y[2] = ((9000+z[2])*(1-0.2)/(1+1/e))*(1-(9000/(9000+z[2]))^(1+1/e))-phi-z[2]*(1-0.25)
y[3] = ((35000+z[3])*(1-0.25)/(1+1/e))*(1-(35000/(35000+z[3]))^(1+1/e))-phi-z[3]*(1-0.3)
}
end
solveprogram 1
Comment