P =
Hey Kashif,
I understand that you are trying to determine the best-fit parameters ( q ) and ( m ) for your probability equation using MATLAB. This involves fitting the model to subsets of your data iteratively and ensuring convergence of the optimization process.
Here’s a structured approach to achieve this:
To fit the parameters ( q ) and ( m ) for each group of data, we can use "fmincon" function, which is suitable for constrained optimization problems. We will iterate over the data in specified group sizes, perform the optimization for each group, and store the results.
% Load the data from Excel
data = readtable('Parameter_estimation.xlsx');
x_data = data{:, 1};
P_data = data{:, 2};
% Define the model function
model_function = @(params, x) (1 - (1 - params(1)) * x * params(2)) .^ (1 / (1 - params(1)));
% Define the objective function for optimization
objective_function = @(params, x, P) sum((model_function(params, x) - P).^2);
% Define initial guess and bounds
initial_guess = [1.5, 100]; % Example initial guess [q, m]
lb = [1, 10]; % Lower bounds for [q, m]
ub = [3, 1000]; % Upper bounds for [q, m]
% Options for fmincon
options = optimoptions('fmincon', 'Display', 'off', 'MaxFunctionEvaluations', 10000);
% Define the group size
group_size = 2000; % You can adjust this size
% Initialize results storage
results = [];
% Iterate over the data
for start = 1:500:(length(x_data) - group_size + 1)
x_group = x_data(start:start + group_size - 1);
P_group = P_data(start:start + group_size - 1);
% Define a local objective function for the current group
local_objective = @(params) objective_function(params, x_group, P_group);
% Perform the optimization
[params, fval, exitflag] = fmincon(local_objective, initial_guess, [], [], [], [], lb, ub, [], options);
% Check if the optimization converged
if exitflag > 0
results = [results; start, params];
fprintf('Group starting at %d: q = %.4f, m = %.4f, Error = %.4f\n', start, params(1), params(2), fval);
else
fprintf('Fit did not converge for group starting at %d\n', start);
end
end
% Display all results at the end
fprintf('\nSummary of results:\n');
for i = 1:size(results, 1)
fprintf('Group starting at %d: q = %.4f, m = %.4f\n', results(i, 1), results(i, 2), results(i, 3));
end
Note: To improve the efficiency of your parameter fitting with a large dataset, we will increase the step size in our iteration. This means we will skip some data points between each group, allowing the script to run faster while still obtaining a representative fit for each data segment.
For more information on "fmincon" function in MATLAB, refer to the following MathWorks documentation:
Hope this helps !!
Regards