How to use Gamma distribution as the kernel of Naive Bayes in MATLAB code?

Illustration
Xiwei She - 2022-05-17T14:45:21+00:00
Question: How to use Gamma distribution as the kernel of Naive Bayes in MATLAB code?

Here I have a group of data which following the Gamma distribution and now I want to use Naive Bayes method to fit this data. I tried the original function named 'fitcnb' and knowing that it providing 4 types of distribution: 'box', 'epanechnikov', 'normal' and 'triangle'. Now I want to revise this fitcnb function through adding Gamma distribution kernel into its original code. But I'm not sure how to implement that, can anyone give me some hint or example code? Many thanks for that.

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

If your features follow a Gamma distribution you should be fine just using the 'normal' smoothing Kernel. To provide some intuition the Kernel prior is using a Kernel smoothing function to approximate the distribution of the features from the discrete data. You can think of it as trying to drop a table-cloth over the histogram to smooth out the jumps the histogram creates.
 
To illustrate this, let's sample from a Gamma distribution
 
 
dataSamp = gamrnd(9,0.5,1e5,1); % Gamma Samples.
histogram(dataSamp,'Normalization','pdf') % Visualize pdf

Fit a Kernel distribution (tablecloth) with the restriction that the support is positive (as is the case with the Gamma distribution).

dist = fitdist(dataSamp,'kernel','Kernel','normal','Support','positive');

Evaluate the pdf of the distribution and plot on the histogram

xVals = linspace(0,15,1000); % x Values to evaluate the pdf
yFit = pdf(dist,xVals); % pdf values at each xVals
hold on
plot(xVals,yFit,'LineWidth',2) % Plot the fitted pdf.
hold off

This is exactly what is happening in fitcnb when you use the same calls

Mdl = fitcnb(___,'DistributionNames','kernel','Kernel','normal','Support','positive')

There is no need to modify the existing code to support directly the Gamma distribution, which I image would be akin to coding it from scratch (if not more difficult).


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!