Probability distributions in MATLAB - Time & Space Complexity
When working with probability distributions in MATLAB, it's important to understand how the time needed to compute values grows as the input size increases.
We want to know how the execution time changes when we calculate probabilities for many data points.
Analyze the time complexity of the following code snippet.
% Calculate normal distribution PDF for each value in data array
data = linspace(-5, 5, n);
pdf_values = zeros(1, n);
for i = 1:n
pdf_values(i) = (1 / sqrt(2 * pi)) * exp(-0.5 * data(i)^2);
end
This code computes the probability density function (PDF) of a normal distribution for n points stored in the array data.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that calculates the PDF value for each element in the
dataarray. - How many times: The loop runs exactly n times, once for each data point.
As the number of points n increases, the total calculations increase in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calculations |
| 100 | 100 calculations |
| 1000 | 1000 calculations |
Pattern observation: Doubling the input size roughly doubles the work done.
Time Complexity: O(n)
This means the time to compute grows directly in proportion to the number of data points.
[X] Wrong: "Calculating the PDF for many points is constant time because each calculation is simple."
[OK] Correct: Even though one calculation is quick, doing it many times adds up, so total time grows with the number of points.
Understanding how time grows with input size helps you write efficient code when working with data and probability, a common task in many projects.
"What if we used MATLAB's vectorized operations instead of a for-loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of each function
makedistcreates a probability distribution object, whilerandgenerates random numbers,histogramplots data distribution, andplotcreates graphs.Step 2: Identify the correct function for creating distributions
Onlymakedistis designed to create probability distribution objects in MATLAB.Final Answer:
makedist -> Option BQuick Check:
Function to create distribution = makedist [OK]
- Confusing makedist with rand
- Using histogram to create distributions
- Trying to use plot for distributions
Solution
Step 1: Recall the correct parameter names for normal distribution
In MATLAB, the normal distribution parameters are specified as 'mu' for mean and 'sigma' for standard deviation.Step 2: Check the syntax for makedist
The correct syntax uses name-value pairs:makedist('Normal', 'mu', 0, 'sigma', 1). Other options use incorrect parameter names or formats.Final Answer:
pd = makedist('Normal', 'mu', 0, 'sigma', 1); -> Option DQuick Check:
Normal dist params = 'mu' and 'sigma' [OK]
- Using lowercase 'normal' instead of 'Normal'
- Using 'mean' and 'std' instead of 'mu' and 'sigma'
- Passing parameters without name-value pairs
pd = makedist('Exponential', 'mu', 2);
p = pdf(pd, 1);
disp(round(p, 3));Solution
Step 1: Understand the exponential distribution PDF formula
The PDF for exponential with mean mu=2 is f(x) = (1/2)*exp(-x/2). For x=1, f(1) = 0.5 * exp(-0.5) ≈ 0.5 * 0.6065 = 0.3033.Step 2: Compute and round the value
p ≈ 0.3033, round(p, 3) = 0.303.Final Answer:
0.303 -> Option AQuick Check:
(1/mu)*exp(-x/mu) at x=1, mu=2 ≈ 0.303 [OK]
- Ignoring the (1/mu) factor
- Confusing PDF with CDF
- Rounding errors causing mismatch
pd = makedist('Binomial', 'n', 10, 'p', 0.5);
prob = cdf(pd, 11);Solution
Step 1: Understand binomial distribution parameters
Binomial distribution with n=10 means possible outcomes are from 0 to 10.Step 2: Check the cdf input value
Calling cdf(pd, 11) asks for probability of outcomes ≤ 11, but 11 is outside the valid range (max 10).Step 3: Identify the error
Input to cdf must be within 0 to n. Using 11 is invalid and causes an error or unexpected result.Final Answer:
The cdf input value 11 exceeds the number of trials n -> Option CQuick Check:
cdf input must be ≤ n for binomial [OK]
- Using cdf input outside valid range
- Thinking p must be integer
- Assuming makedist lacks binomial support
Solution
Step 1: Understand how to create a normal distribution object
Usemakedist('Normal', 'mu', 5, 'sigma', 2)to create the distribution with mean 5 and std 2.Step 2: Use cdf to find probability less than 7
Callingcdf(pd, 7)returns the cumulative probability up to 7.Step 3: Check other options
p = normcdf(7, 5, 2); usesnormcdfwhich is valid but not usingmakedist. pd = makedist('Normal', 5, 2); p = pdf(pd, 7); incorrectly uses pdf instead of cdf. p = cdf('Normal', 7, 5, 2); has wrong syntax for cdf.Final Answer:
pd = makedist('Normal', 'mu', 5, 'sigma', 2); p = cdf(pd, 7); -> Option AQuick Check:
Use makedist + cdf for cumulative probability [OK]
- Using pdf instead of cdf
- Wrong syntax for cdf
- Ignoring distribution parameters
Ready to prove it?
Pass the test and this pattern moves unknown
