# Method of moments using genetic optimization

조회 수: 22(최근 30일)
Prerna Mishra 2023년 1월 20일
댓글: Walter Roberson 2023년 1월 20일
I am trying to calibrate a model using simulated method of moments. I have a function f_SMM that calculates the model moments and sets up the objective function as follows:
function out = f_SMM(x)
global datamomvec datacovmat
....
momvec = zeros(length(datamomvec),1);
momvec(1) = 1.01;
momvec(2) = 1.0436;
momdiffvec = momvec - datamomvec;
SMMobj = (momdiffvec')*(datacovmat\momdiffvec); %criteria t be minimized
out = momvec
end
In my main.m I have the follows:
global datamomvec datacovmat
datamomvec = zeros(2,1);
datamomvec(1) = 1.287198;
datamomvec(2) = 14.17697;
datacovmat = eye(length(datamomvec));
xval(1) = .2; %the parameter to be claibrated
Nmom = length(datamomvec);
Nparam = length(xval);
%%SMM Optimization
lb = zeros(Nparam,1);
ub = lb;
lb(1) = .05; ub(1) = 1;%theta
lb_ub = [lb ub];
gaopts = gaoptimset('PopulationSize',50);
xval = ga(@(x)f_SMM(x),length(xval),[],[],[],[],lb,ub,[],gaopts);
I keep getting the following error:
Error using makeState (line 61)
Your fitness function must return a scalar value.
Error in galincon (line 22)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 402)
[x,fval,exitFlag,output,population,scores] =
galincon(FitnessFcn,nvars, ...
Error in wrapper_capstr (line 57)
xval = ga(@(x)f_SMM(x),length(xval),[],[],[],[],lb,ub,[],gaopts);
I tired to set my out to SMMobj. But that does nothing in terms of optimization. I am not sure how to best set my optimization to calibrate the model

댓글을 달려면 로그인하십시오.

### 답변(1개)

Walter Roberson 2023년 1월 20일
You have multiple model moments, and you are returning the vector of modeled values from your function f_SMM. However, ga() can only deal with scalar functions.
If you have multiple values being independently tuned, that is a "multi-objective optimization", such as is handled by gamultiobj() . gamultiobj() would look for pareto fronts -- places where no value can be made better without making another one worse (which is to say, local minima in the curvature)
If you want to do some kind of overall "best fit" then you need to change your f_SMM to return a scalar. For example you could have it return the sum of squares of differences between the modeled values and the known values.
##### 댓글 수: 2표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
Walter Roberson 2023년 1월 20일
I would suggest testing f_SMM on a few different inputs, to be sure that it returns different numbers (and not, for example, inf or nan). Be sure to initialize the global variables before you do that however.
Better yet would be not to use global variables.

댓글을 달려면 로그인하십시오.

### 범주

Find more on Genetic Algorithm in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!