MATLAB: Fit Kernel Distribution to Data with Boundary Condition

boundary conditiondistributionepanechnikovfitfitdistfittingkernelkernel distributionMATLABmirrormirroringprbabilityprobability distributionStatistics and Machine Learning Toolbox

I need to fit a distribution to a data set I created and decided on a kernel density distribution. For this I use
fitdist(data,'Kernel','Kernel','epanechnikov');
My data set has only values greater than zero. Unfortunately, my current implementation of the kernel fit ignores that boundary condition. From literature and also this great youtube video I know that it is possible to create a kernel distribution that respects such a boundary condition by using a mirroring method.
Is there a possibility within the fitdist function or by some other method to easily implement this boundary condition?

Best Answer

I would do this by using the ksdensity function. Specifically, see this section about using the reflection boundary condition.
Here is an example, distilled from that documentation:
rng('default') % For reproducibility
% Create some data
pd = makedist('HalfNormal','mu',0,'sigma',1);
x = random(pd,50,1);
pts = linspace(0,5,1000); % points to evaluate the estimator
[f,xi] = ksdensity(x,pts,'Support','positive','BoundaryCorrection','reflection');
figure
hold on
plot(x,zeros(numel(x),1),'.')
plot(xi,f)