Vous êtes sur la page 1sur 3

classdef Moreno_CurveFit < handle

% This class uses the Markov Chain Monte Carlo Method to fit curves to
% noisy data by finding minima using variable jump sizes.

properties
Tvector
Sigma
JumpSigma
Data
FuncH
Model
GuessConstant
NumberOfJumps
ModelFound
FigGUI
res
end

methods
function obj = MCMC(obj)

obj.Data = obj.FuncH(obj.Tvector); % Generation of data based on function input.


NData = obj.Data + obj.Sigma*randn(length(obj.Tvector),1); % Generation of noisy
data. Likelihood=@(Theta,Y,T)prod(
1/sqrt(2*pi*obj.Sigma^2)*exp(-(Y-obj.Model(Theta,T)).^2/(2*obj.Sigma^2) )); % Likelihood formula.
subplot(2,1,1); % Graph in second quadrant.
plot(obj.Tvector,NData,'ro') % Plots the noisy data points.
xlabel('T') % Label for the x axis for the first figure.
ylabel('Y') % Label for the y axis for the firts figure.
title('Curve Fit and Initial Guess')% Title for the first figure.
hold on

Theta0=[NData(1) (NData(2)-NData(1))/(obj.Tvector(2)-obj.Tvector(1)) obj.GuessConstant ]; %


Initial guess vector.
obj.ModelFound=obj.Model(Theta0,obj.Tvector); % Points for our fitted curve (plotted later).

hold on
subplot(2,1,1); % Plot for the second quadrant.
plot(obj.Tvector,obj.ModelFound); % Plots the fitted curve.

obj.JumpSigma=abs(Theta0/10); % Default jump size.


Chain=zeros(obj.NumberOfJumps,length(Theta0)); % Initial array for the values to be tested.
Chain(1,:)=Theta0; % Initial value; starting point.

for nn=2:obj.NumberOfJumps % Iterates through the number of jumps for test points.

Theta = Chain(nn-1,:); % Intializing accepted parameters array.


Theta_Test=Theta+obj.JumpSigma.*randn(1,length(Theta0)); % Jump for test
value.
alpha=Likelihood(Theta_Test,NData,obj.Tvector)/Likelihood(Theta,NData,obj.Tvector);
% Acceptance rate for test values.
if alpha>rand() % If statement for acceptance rate.
Chain(nn,:)=Theta_Test; % Filling array with accepted values.
else
Chain(nn,:)=Theta; % Resets to previous value.
end

end
FoundTheta=mean(Chain); % Parameters found = average of accepted values.
obj.ModelFound=obj.Model(FoundTheta,obj.Tvector); % Fills resulting values into object property.
plot(obj.Tvector,obj.ModelFound) % Plots the fitted curve.
legend('Data','Initial Guesss','Fit') % Legend for the plots.

obj.res = obj.ModelFound - NData; % Residual data (ModelFound-NoisyData).


subplot(2,1,2) % Plot for second quadrant.
plot(obj.Tvector,obj.res,'ro') % Plots the residual plot.
xlabel('T') % Labels the x axis for the second plot.
ylabel('Y') % Labels the y axis for the second plot.
title('Residual') % Title for the second plot.

end
function delete(obj)

delete(obj.FigGUI); % Closing figure.

end
function gui(obj)
% Graphical User Interface function for the class to have
% modularity for jump size, the number of jumps, and the
% initial guess.
Z=findall(0,'Tag','MCMCGUI') % Checks for an empty figure.
if ~isempty(Z);return;end

F = figure('Position',[100 100 300 400]); % Creates figure for object.


F.Tag = 'MCMCGUI' % Tag for figure.

obj.FigGUI=F; % Saves figure to object property.

% Setup UI CONTROLS----------------------------------------------
TextBoxA = uicontrol('Style','edit','String','Number of Jumps',... % First Text Box (editable).
'Position',[100 300 50 50]);
ALabel = uicontrol('Style','text','String','Number of Jumps',... % Label for the first text box.
'Position',[0 300 50 50]);
TextBoxB = uicontrol('Style','edit','String','Jump Size',... % Second text box for jump size (editable).
'Position',[100 250 50 50]);
BLabel = uicontrol('Style','text','String','Jump Size',... % Label for the second text box.
'Position',[0 250 50 50]);

TextBoxC = uicontrol('Style','edit','String','Initial Guess',... % Third Text Box (editable.


'Position',[100 200 50 50]);
CLabel = uicontrol('Style','text','String','Initial Guess',... % Label for the third text box.
'Position',[0 200 50 50]);
TextBoxD = uicontrol('Style','edit','String','Sigma',... % Fourth Text Box (editable).
'Position',[100 150 50 50]);
DLabel = uicontrol('Style','text','String','Sigma ("Noisiness")',... % Label for the fourth text box.
'Position',[0 150 50 50]);

Button = uicontrol('Style', 'pushbutton', 'String', 'MCMC',... % Button to run MCMC function.


'Position', [0 50 50 50],...
'Callback', @runMCMC);

property2gui() % Initialize UI controls.

function runMCMC(~,~)
gui2property() % Callback for button.
obj.MCMC() % Runs MCMC function.
property2gui()
end

function property2gui()
TextBoxA.String=num2str(obj.NumberOfJumps); % Writes object property for the number of jumps to
the first uicontrol.
TextBoxB.String=num2str(obj.JumpSigma); % Writes object property for the jump size to the second
uicontrol.
TextBoxC.String=num2str(obj.GuessConstant); % Writes object property for the initial guess to the third
uicontrol.
TextBoxD.String=num2str(obj.Sigma); % Writes object property for the noisiness of the data to the
fourth uicontrol.
end

function gui2property()
obj.NumberOfJumps=str2double(TextBoxA.String); % Value for first object property.
obj.JumpSigma=str2double(TextBoxB.String); % Value for second object property.
obj.GuessConstant=str2double(TextBoxC.String); % Value for third object property.
obj.Sigma=str2double(TextBoxD.String); % Value for fourth object property.
end

end

end
end

Vous aimerez peut-être aussi