0% found this document useful (0 votes)
55 views26 pages

Matlab

The document contains MATLAB code snippets for calculating statistical measures such as mean, median, mode, and variance for both raw and grouped data, as well as for discrete and continuous random variables. It also includes implementations for various probability distributions including binomial, Poisson, and geometric distributions. Each section provides code examples and explanations for the calculations performed.

Uploaded by

devansh dewan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views26 pages

Matlab

The document contains MATLAB code snippets for calculating statistical measures such as mean, median, mode, and variance for both raw and grouped data, as well as for discrete and continuous random variables. It also includes implementations for various probability distributions including binomial, Poisson, and geometric distributions. Each section provides code examples and explanations for the calculations performed.

Uploaded by

devansh dewan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

MATLAB RECORD

NAME : DEVANSH DEWAN

ROLL NO : CB.SC.U4CSE23017

COURSE CODE : 23MAT216

Course name : Probability and Random Processes


Output
1. Mean for raw data

Code

clc
clear all
totalSum = 0;
arr=[70,120,110,101,88,83,95,107,100]
for i = 1:length(arr)
totalSum = totalSum + arr(i);
end
meanValue = totalSum / length(arr);
fprintf("Mean is %.4f\n",meanValue)
Output
2. Mean for frequency data

Code

clc;
clear all;
arr=[1,2,3,4,5,6,7];
f=[5,9,12,17,14,10,6];
sum=0;
sumf=0;
for i = 1:length(f)
sum = sum + arr(i) * f(i)
sumf = sumf + f(i)
end
mean_value = sum / sumf;
disp(['Mean: ',num2str(mean_value)])
Output
3. Median for raw data

Code

% Sample raw data


raw_data = [12,45,67,23,89,34,56,78,90,11];
% Calculate the median value
median_value = median(raw_data);
% Display the median value
disp(['The median value is: ',
num2str(median_value)])
Output
4. Median for grouped data

Code

clc
clear all;
class_intervals = [10 20;20 30; 30 40; 40 50; 50
60];
frequencies = [5,8,12,6,4];
cum_freq = cumsum(frequencies);
total_freq = sum(frequencies);
half_total = total_freq / 2;
median_class_index = find(cum_freq >=
half_total,1);
L = class_intervals(median_class_index,1);
f = frequencies(median_class_index);
cf = (median_class_index >
1)*cum_freq(median_class_index -1);
h =class_intervals(median_class_index,2) -
class_intervals(median_class_index,1);
median = L + ((half_total - cf)/f)*h;
disp(['Median of the grouped data is :
',num2str(median)]);
Output
5. Mode for raw data

Code

clc;
clear all;
data = [1,2,2,3,4,4,5,6,6,6];
mode_value = mode(data);
disp(['The node of the data is:
',num2str(mode_value)]);
Output
6. Multi modes in raw data

Code

clc;
clear all;
function modes = findModes(data)
% Ensure the data is a column vector
data = data(:);
% Find the unique values and their counts
[uniqueValues, ~,idx] = unique(data);
counts = accumarray(idx,1);
% Find the maximum count
maxCount = max(counts);
% Find all values that have the maximum count
modes = uniqueValues(counts == maxCount);
end
% Example usage:
rawData = [1,2,2,3,3,4,4,4,5,5,5];
modes = findModes(rawData);
disp('Modes of the dataset are: ');
disp(modes);
Output
7. Modes in grouped data

Code

clc;
clear all;
format short
syms x;
total = 0;
mean1 = 0;
moment1 = 0;
% Define number of subintervals and their
respective functions and limits
n = 2; % Number of subintervals
% Define probability density functions (PDFs) and
limits manually
fun_list = {@(x) 0.5*x, @(x) 0.3*x^2}; % Cell
array of function handles
limits = [0 2; 2 4]; % Corresponding limits for
each function
for i = 1:n
fun_1_a = fun_list{i};
a = limits(i,1);
b = limits(i,2);
% Compute total probability
result = int(fun_1_a, x, a, b);
total = total + result;

% Compute mean
mean_res1 = @(x) x * fun_1_a(x);
mean_result1 = int(mean_res1, x, a, b);
mean1 = mean1 + mean_result1;

% Compute second moment


moment_1 = @(x) x^2 * fun_1_a(x);
moment_result1 = int(moment_1, x, a, b);
moment1 = moment1 + moment_result1;
end
% Display results
disp("The total probability is");
disp(double(total));
disp("The mean of the distribution is");
disp(double(mean1));
Variance = moment1 - mean1^2;
disp("The variance of the distribution is");
disp(double(Variance));
Output
8. Binomial distribution

Code
clc; clear; close all;
n = input('Enter number of trials (n): ');
p = input('Enter probability of success (p): ');
% Generate probability mass function (PMF)
x = 0:n; % Possible values
binomial_pmf = binopdf(x, n, p);
binomial_cdf = binocdf(x, n, p);
disp('Binomial Distribution PMF and CDF:');
disp(table(x', binomial_pmf', binomial_cdf',
'VariableNames', {'x', 'PMF', 'CDF'}));
% Plot PMF
figure;
subplot(1,2,1);
stem(x, binomial_pmf, 'b', 'LineWidth', 1.5);
xlabel('Number of Successes');
ylabel('Probability');
title('Binomial Distribution PMF');
grid on;
% Plot CDF
subplot(1,2,2);
stairs(x, binomial_cdf, 'r', 'LineWidth', 1.5);
xlabel('Number of Successes');
ylabel('Cumulative Probability');
title('Binomial Distribution CDF');
grid on;
Output
9. Poisson distribution

Code
clc; clear; close all;
lambda = input('Enter mean (λ) of Poisson
distribution: ');
% Generate PMF and CDF
x = 0:20; % Define range
poisson_pmf = poisspdf(x, lambda);
poisson_cdf = poisscdf(x, lambda);
disp('Poisson Distribution PMF and CDF:');
disp(table(x', poisson_pmf', poisson_cdf',
'VariableNames', {'x', 'PMF', 'CDF'}));
% Plot PMF
figure;
subplot(1,2,1);
stem(x, poisson_pmf, 'b', 'LineWidth', 1.5);
xlabel('Number of Occurrences');
ylabel('Probability');
title('Poisson Distribution PMF');
grid on;
% Plot CDF
subplot(1,2,2);
stairs(x, poisson_cdf, 'r', 'LineWidth', 1.5);
xlabel('Number of Occurrences');
ylabel('Cumulative Probability');
title('Poisson Distribution CDF');
grid on;
Output
10. Geometric distribution

Code
clc; clear; close all;
p = input('Enter probability of success (p): ');
% Generate PMF and CDF
x = 0:10; % Number of failures before first
success
geometric_pmf = geopdf(x, p);
geometric_cdf = geocdf(x, p);
disp('Geometric Distribution PMF and CDF:');
disp(table(x', geometric_pmf', geometric_cdf',
'VariableNames', {'x', 'PMF', 'CDF'}));
% Plot PMF
figure;
subplot(1,2,1);
stem(x, geometric_pmf, 'b', 'LineWidth', 1.5);
xlabel('Number of Failures Before First Success');
ylabel('Probability');
title('Geometric Distribution PMF');
grid on;
% Plot CDF
subplot(1,2,2);
stairs(x, geometric_cdf, 'r', 'LineWidth', 1.5);
xlabel('Number of Failures Before First Success');
ylabel('Cumulative Probability');
title('Geometric Distribution CDF');
grid on;
Output
11. Mean & Variance of Discrete Random Variable

Code
clc;
clear all;
format long;
% Input values
x = input('Enter the values of the discrete random
variable X (as a row vector): ');
p = input('Enter the corresponding probabilities
P(X) (as a row vector): ');
% Checking if probabilities sum to 1
if abs(sum(p) - 1) > 1e-6
error('The sum of probabilities must be 1.');
end
% Mean (Expected Value) E(X)
mean_X = sum(x .* p);
% Variance Var(X) = E(X^2) - (E(X))^2
E_X2 = sum((x.^2) .* p);
variance_X = E_X2 - mean_X^2;
% Display Results
disp(['Mean (E[X]): ', num2str(mean_X)]);
disp(['Variance (Var[X]): ',
num2str(variance_X)]);
Output
12. Mean , Variance of Continuous Random Variable

Code
clc;
clear all;
syms x;
% User input: Probability Density Function (PDF)
pdf_str = input('Enter the probability density
function (as a function of x): ', 's');
pdf = str2func(['@(x)', pdf_str]);
% User input: Limits of integration
a = input('Enter the lower limit: ');
b = input('Enter the upper limit: ');
% Calculate Mean (Expected Value)
mean_X = integral(@(x) x .* pdf(x), a, b);
% Calculate Variance: Var(X) = E[X^2] - (E[X])^2
E_X2 = integral(@(x) x.^2 .* pdf(x), a, b);
variance_X = E_X2 - mean_X^2;
% Display Results
disp(['Mean (E[X]): ', num2str(mean_X)]);
disp(['Variance (Var[X]): ',
num2str(variance_X)]);

You might also like