Finding the best fit for a logistic function to data
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have been trying to find the best fit parameters for a logistic regression to data using the following code, however there appears to be an issue. I am not sure if the issue is with my code or with my choice of ranges - I have based the mathematics on the following paper: https://www.math.hmc.edu/~depillis/PCMI2005WEBSITE/logistic_REDWOODS.pdf. I am trying to looop through a range of parameters and define the for each, and then look for the minimum error in order to understand what is the best fit.
Here is my code:
%loading + cleaning up the necessary data
load('trial2vars.mat');
trials = 50;
volpress = sort(volpress);
P = zeros(1,length(trials)); % pre-allocating zeros
%CDF
for t = 1:trials
P(t) = length(volpress(volpress <= volpress(t)))/length(volpress);
end
plot(volpress,P,'o');
hold on
% Fitting a logistic
M= volpress';
x1=0.00004041;
x2= 0.00003957;
y1= 0.68;
y2= 0.46;
dm = (y1 -y2)/(x1 - x2);
K=1;
hold on
r = 4 * dm ;
t0=volpress(20); % guess for the point of inflection
figure(1);
y = logistic_func(volpress,K,r,t0); % defining the guessed function
plot(volpress,y) % first guess/testing
hold off
title('Probability of hearing a puretone before a specific Volume Intensity')
xlabel('Volume Intensity')
ylabel('Probability')
%defining ranges to test each parameter in
num_r=100;
r=linspace(0.000001,0.00003,num_r);
num_t0=100;
t0=linspace(0.000035,0.000045,num_t0);
e=zeros(num_t0,num_r); %pre-allocating zeros in a matrix that depends
%both the size of t0 and r
%assigning each point on the grid its attributed error
for i=1:num_t0 %loops through each t0 value
for j=1:num_r %loops through each r value
%for each t0 value, loop through each r value
e(i,j)=myerror([r(j),t0(i)],volpress,P);
end
end
%making the 3D graph
figure(2)
mesh(r,t0,e)
axis tight
xlabel('r')
ylabel('t_0')
zlabel('Error Function')
%credit: https://web.njit.edu/~goodman/Math227/matlab/algae/
%contour map
figure(3)
contour(r,t0,e)
hold on;
% finding the best spot on the contour plot and marking with a star:
[m1,p1]=min(e);
[m2,p2]=min(m1);
best_r=r(p2);
best_t0=t0(p1(p2));
plot(best_r,best_t0,'*')
hold off
mytitle=sprintf('Best guess is r=%4.2f, t_0=%5.1f',best_r,best_t0);
title(mytitle);
xlabel('r')
ylabel('t_0')
colorbar
% finding the best fit parameters
paramguess=[best_r,best_t0];
params=fminsearch(@myerror,paramguess,[],volpress,P);
r=params(1);t0=params(2);
h = logistic_func(volpress,1,r,t0); %unscaled logistic function
K=dot(h,M)./dot(h,h);
figure(4)
plot(volpress,P,'o',volpress,logistic_func(volpress,K,r,t0));
xlabel('Volume Intensity')
ylabel('Probability')
%defining the functions
function y = logistic_func(t,K,r,t0)
%creates a logistic function with the parameters inputted
y=K./(1+exp(-r*(t-t0)));
end
function z=myerror(params,t,y)
%given a set of parameters and the original data, calculates the error
%params is inputted as an array with 2 values:
r=params(1); %first r
t0=params(2); % then t0
h=1./(1+exp(-r*(t-t0))); % a non-scaled logistic function
%calculates error with formula derived using properties of vectors
numerator = -dot(h,y)^2;
denominator = dot(h,h);
z=numerator/denominator;
end
0 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu Fit Postprocessing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!