Home > Enterprise >  Plotting a gaussian sample against theoretical gaussian curve in Matlab
Plotting a gaussian sample against theoretical gaussian curve in Matlab

Time:09-27

I am running some basic examples about gaussian processes.

I want to manually generate a vector with samples of a gaussian process and then plot them.

First I create a Gaussian distribution and a sample vector s:

% CLEAR ALL
clear all
close all

% CREATE GAUSSIAN PROCESS
mu = 2
sigma = 1
d = makedist('Normal','mu',mu,'sigma',sigma);

% CREATE SAMPLE OF ABOVE GAUSSIAN PROCESS
s = random(d, [1000, 1]);

Then I plot the samples in a histogram:

% DISPLAY HOW DISTRIBUTION LOOKS LIKE
figure('Name', 'PDF')
title('GAUSSIAN DISTRIBUTION \mu=2 and \sigma=1')
histogram(s,100); hold on;

% DISPLAY HOW DISTRIBUTION SHALL LIKE
gauss_x = -5:0.01:5
gauss_y = ...
    (1/(sigma*sqrt(2*pi))) ...
    *exp( ...
    (-1/2)*((gauss_x-mu)/sigma).^2 ...
    )
plot(gauss_x, gauss_y); hold off;

Because the area is not normalised to 1, the theoretical gauss plot for mu=2 and sigma=1 appears at a much lower scale.

plot1

I try to normalise the area using Normalization parameter:

% NORMALIZE SAMPLE
figure('Name', 'PDF NORMALISED')
title('GAUSSIAN DISTRIBUTION \mu=2 and \sigma=1 NORM.')
histogram(s,100, 'Normalization','probability'); hold on;
plot(gauss_x, gauss_y); hold off;

But I still do not get the expected result, which would be the histogram being scaled with the theoretical curve.

plot2

What am I missing? Is it related to not understanding the Normalization parameter correctly or am I missing something related to the mathematical gaussian probability itself?

CodePudding user response:

The issue is indeed the setting for Normalization. Try:

histogram(s,100, 'Normalization','pdf')

The normalization 'pdf' will take the width of the bins into account and ensure that the area underneath the histogram is equal to 1 (thus allowing a comparison with the pdf on the same scale). The option 'probability' does not yet take the area into account but only makes the heights of the bars of the histogram add to 1. Further details can be found in Matlab's help.

  • Related