calculate cdf from discrete values in matlab

To calculate the cumulative distribution function (CDF) from discrete values in MATLAB, you can use the ecdf function, which computes the empirical cumulative distribution function (ECDF) for a set of sample points. Here's an example:

main.m
% Define discrete distribution
x = [-2 -1 0 1 2];
p = [0.1 0.2 0.4 0.2 0.1];

% Compute ECDF
[f, x_values] = ecdf(x, 'frequency', p);

% Plot CDF
stairs(x_values, f);
title('Cumulative Distribution Function');
xlabel('x');
ylabel('F(x)');
239 chars
13 lines

In this example, x and p define a discrete distribution with five possible values and corresponding probabilities. The ecdf function is called with two arguments: x and the 'frequency' option, indicating that the probabilities should be used as weights for each sample point. The outputs f and x_values contain the values of the ECDF and the corresponding x-points, respectively. Finally, the CDF is plotted using the stairs function.

Note that the CDF is a step function for discrete distributions, which means that its value remains constant between each x-value.

gistlibby LogSnag