Is 'bayesopt' such a poor optimizer or am I doing something wrong?
Show older comments
I am trying to fit a gauss data points with exactly the same model that produced the data. The model has just 4 integer parameters. Yet 'bayesopt' is not able to find them even if I choose InitialX very close to the parameters that generated data.
I am lost. Thank you. :-)
clc
N=300;
% - let's create 300 points with gaussian shape
x = linspace(0,10,N)';
b = [1;3;2;2]; % model parameters
mf=@modelfun
y = mf(b,x);
% - let's create search parameters
num(1) = optimizableVariable('base', [0,9],'Type','integer');
num(2) = optimizableVariable('height', [0,9],'Type','integer');
num(3) = optimizableVariable('location',[0,9],'Type','integer');
num(4) = optimizableVariable('width', [0,9],'Type','integer');
% - define loss function
bf=@(num)bayesfun(num,x,y);
% - call bayesopt with initial parameters close to model values
results = bayesopt(bf,num,...
'InitialX',table(2,2,1,3));
b0=table2array(results.bestPoint);
results.bestPoint
% let's see plot of model data and fit data
%------------------------
plot(x,y,'.b')
hold on
plot(x,mf(b0,x),'.r')
hold off
legend('y','fit')
function out=bayesfun(t,x,y)
y1=modelfun(table2array(t),x);
out=sum((y1-y).^2);
end
function out=modelfun(b,x)
out = b(1);
out= out + b(2)*exp(-((b(3)-x)/b(4)).^2);
end
Accepted Answer
More Answers (0)
Categories
Find more on Model Building and Assessment 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!