Combining several distributions into kernel-like distribution

9 views (last 30 days)
I have the following code, where I start from three different values, and the probability of them occuring (excuse me, I do not know the correct statistical term). For example, in the case below, the number "3" has a 25% chance of appearing, "5" has 50% and "7" has 25%.
The aim is to replace these 3 discrete, integer values with continuous distributions of real numbers with some desired standard deviation, and then "combine" them to a single probability distribution.
The code I've written below kind of does the job, utilizing the kernel distribution, but it feels a bit wonky and not customizable enough.
ValueAndProbability(1,:) = [3 0.25];
ValueAndProbability(2,:) = [5 0.50];
ValueAndProbability(3,:) = [7 0.25];
values =[];
n = 10000;
for i=1:size(ValueAndProbability,1)
% Add each value n times to the array
addValues = ValueAndProbability(i,1) + zeros(1, ValueAndProbability(i,2) * n );
values = single(cat(2,values,addValues));
end
pd = fitdist(values',"Kernel","Width",0.3);
minx = min(ValueAndProbability(:,1)) - 2;
maxx = max(ValueAndProbability(:,1)) + 2;
xvals = linspace(minx,maxx);
yvals = pdf(pd,xvals);
plot(xvals,yvals)
I was wondering if there's any way to make the process a bit more elegant (skipping this akward loop and the 10000-sized array perhaps), and create the final distribution as a "sum" of desired distributions of my choice (not limited to gaussian for example).

Accepted Answer

Paras Gupta
Paras Gupta on 21 Aug 2023
Hi Aristarchos,
I understand that you want to combine continuous probability distributions that have different probabilities of occurrence. The distributions can be combined using summation when they are independent and identically distributed (i.i.d.) distributions.
Please find the code below to achieve the same in MATLAB.
ValueAndProbability(1,:) = [3 0.25];
ValueAndProbability(2,:) = [5 0.5];
ValueAndProbability(3,:) = [7 0.25];
% Create a cell array to store the distributions
distributions = cell(size(ValueAndProbability, 1), 1);
desiredSTD = 0.3;
% Generate the distributions using the makedist function
for i = 1:size(ValueAndProbability, 1)
distribution = makedist('Normal', 'mu', ValueAndProbability(i, 1), 'sigma', desiredSTD);
% distribution = makedist('Exponential', 'mu', ValueAndProbability(i, 1));
distributions{i} = distribution;
end
weights = ValueAndProbability(:, 2);
% Combine i.i.d. distributions using weighted summation with dot product
combined_distribution = @(x) sum(weights .* cellfun(@(dist) pdf(dist, x), distributions));
minx = min(ValueAndProbability(:,1)) - 2;
maxx = max(ValueAndProbability(:,1)) + 2;
xvals = linspace(minx, maxx);
% Calculate the combined distribution values for the xvals array
yvals = arrayfun(combined_distribution, xvals);
plot(xvals, yvals)
The above code can be customized to account for the required distributions by changing the arguments of the ‘makedist’ function in MATLAB.
You can refer to the documentations below for more information on the code above:
Hope this helps.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!