how to calculate the probability of a bernoulli distribution in matlab

The probability mass function (PMF) of a Bernoulli distribution can be calculated in MATLAB using the bernpdf function. The function takes two input arguments: the value to evaluate the PMF at (x), and the probability of success (p). The output is the probability of observing the value x, given the probability p.

main.m
% Define the probability of success
p = 0.3;

% Evaluate the PMF for X = 0 and X = 1
prob_X0 = bernpdf(0, p);
prob_X1 = bernpdf(1, p);

% Display the results
fprintf('The probability of X = 0 is %.2f\n', prob_X0);
fprintf('The probability of X = 1 is %.2f\n', prob_X1);
270 chars
11 lines

This code calculates and displays the probabilities of observing X = 0 and X = 1 for a Bernoulli distribution with p = 0.3. The output will be:

main.m
The probability of X = 0 is 0.70
The probability of X = 1 is 0.30
66 chars
3 lines

gistlibby LogSnag