Bird
Raised Fist0
MATLABdata~10 mins

Probability distributions in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Probability distributions
Define distribution type
↓
Set parameters (mean, std, etc.)
↓
Generate random samples or compute PDF/CDF
↓
Use samples or values for analysis or plotting
↓
End
Start by choosing a distribution and its parameters, then generate samples or calculate probabilities, and finally use these results.
Execution Sample
MATLAB
pd = makedist('Normal','mu',0,'sigma',1);
samples = random(pd,5,1);
pdf_vals = pdf(pd,samples);
This code creates a normal distribution with mean 0 and std 1, generates 5 random samples, and computes their probability density values.
Execution Table
StepActionVariable/FunctionInputOutput/Result
1Create normal distributionmakedist'Normal', 'mu'=0, 'sigma'=1pd (Normal distribution object)
2Generate random samplesrandompd, 5x1[x1, x2, x3, x4, x5] (5 random values)
3Calculate PDF valuespdfpd, samples[p1, p2, p3, p4, p5] (probability densities)
4End---
💡 All steps completed; samples and PDF values generated for analysis.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
pdundefinedNormal distribution object (mu=0, sigma=1)samesamesame
samplesundefinedundefined5x1 array of random valuessamesame
pdf_valsundefinedundefinedundefined5x1 array of PDF valuessame
Key Moments - 3 Insights
Why do we need to create a distribution object before generating samples?
The distribution object (pd) stores the type and parameters (mean, std) needed to generate samples correctly, as shown in step 1 of the execution_table.
Are the random samples the same every time we run the code?
No, random samples change each run unless you set a random seed. Step 2 shows samples generated freshly each time.
What does the pdf function output represent?
The pdf function outputs the probability density values for each sample, indicating how likely those values are under the distribution (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is stored in variable 'pd' after step 1?
AAn array of random samples
BProbability density values
CA normal distribution object with mean 0 and std 1
DUndefined
💡 Hint
Check the output column for step 1 in the execution_table.
At which step are the random samples generated?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Action and Variable/Function columns in the execution_table.
If we change the mean parameter in makedist, how does it affect the samples?
ASamples shift around the new mean
BSamples become zero
CSamples do not change
DSamples become negative only
💡 Hint
Refer to the variable_tracker and understand how parameters affect sample values.
Concept Snapshot
Probability Distributions in MATLAB:
- Use makedist('Name',params) to create distribution object
- Use random(pd,n,m) to generate samples
- Use pdf(pd,x) or cdf(pd,x) to get probabilities
- Parameters (mean, std) define shape and location
- Samples vary each run unless seed fixed
Full Transcript
This visual execution traces how to work with probability distributions in MATLAB. First, we create a distribution object using makedist with specified parameters like mean and standard deviation. Then, we generate random samples from this distribution using the random function. Next, we calculate the probability density function (pdf) values for these samples to understand their likelihood. Variables change step-by-step: the distribution object is created first, then samples are generated, and finally pdf values are computed. Common confusions include why the distribution object is needed before sampling, the randomness of samples, and the meaning of pdf outputs. The quizzes test understanding of these steps and effects of parameter changes. This process helps analyze data and model uncertainty in MATLAB.

Practice

(1/5)
1. Which MATLAB function is used to create a probability distribution object?
easy
A. rand
B. makedist
C. histogram
D. plot

Solution

  1. Step 1: Understand the purpose of each function

    makedist creates a probability distribution object, while rand generates random numbers, histogram plots data distribution, and plot creates graphs.
  2. Step 2: Identify the correct function for creating distributions

    Only makedist is designed to create probability distribution objects in MATLAB.
  3. Final Answer:

    makedist -> Option B
  4. Quick Check:

    Function to create distribution = makedist [OK]
Hint: Use makedist to create distributions in MATLAB [OK]
Common Mistakes:
  • Confusing makedist with rand
  • Using histogram to create distributions
  • Trying to use plot for distributions
2. Which of the following is the correct syntax to create a normal distribution with mean 0 and standard deviation 1 in MATLAB?
easy
A. pd = makedist('normal', 0, 1);
B. pd = makedist('Normal', 'mean', 0, 'std', 1);
C. pd = makedist('Normal', 0, 1);
D. pd = makedist('Normal', 'mu', 0, 'sigma', 1);

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    pd = makedist('Normal', 'mu', 0, 'sigma', 1); -> Option D
  4. Quick Check:

    Normal dist params = 'mu' and 'sigma' [OK]
Hint: Use 'mu' and 'sigma' as parameter names for normal dist [OK]
Common Mistakes:
  • Using lowercase 'normal' instead of 'Normal'
  • Using 'mean' and 'std' instead of 'mu' and 'sigma'
  • Passing parameters without name-value pairs
3. What is the output of the following MATLAB code?
pd = makedist('Exponential', 'mu', 2);
p = pdf(pd, 1);
disp(round(p, 3));
medium
A. 0.303
B. 0.368
C. 0.500
D. 0.607

Solution

  1. 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.
  2. Step 2: Compute and round the value

    p ≈ 0.3033, round(p, 3) = 0.303.
  3. Final Answer:

    0.303 -> Option A
  4. Quick Check:

    (1/mu)*exp(-x/mu) at x=1, mu=2 ≈ 0.303 [OK]
Hint: Recall pdf = (1/mu)*exp(-x/mu) for exponential [OK]
Common Mistakes:
  • Ignoring the (1/mu) factor
  • Confusing PDF with CDF
  • Rounding errors causing mismatch
4. Identify the error in this MATLAB code snippet:
pd = makedist('Binomial', 'n', 10, 'p', 0.5);
prob = cdf(pd, 11);
medium
A. The probability p must be an integer
B. makedist does not support 'Binomial' distribution
C. The cdf input value 11 exceeds the number of trials n
D. The variable name 'prob' is reserved

Solution

  1. Step 1: Understand binomial distribution parameters

    Binomial distribution with n=10 means possible outcomes are from 0 to 10.
  2. 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).
  3. 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.
  4. Final Answer:

    The cdf input value 11 exceeds the number of trials n -> Option C
  5. Quick Check:

    cdf input must be ≤ n for binomial [OK]
Hint: cdf input must be within distribution support range [OK]
Common Mistakes:
  • Using cdf input outside valid range
  • Thinking p must be integer
  • Assuming makedist lacks binomial support
5. You want to calculate the probability that a normally distributed variable with mean 5 and standard deviation 2 is less than 7 in MATLAB using a probability distribution object. Which code correctly computes this?
hard
A. pd = makedist('Normal', 'mu', 5, 'sigma', 2); p = cdf(pd, 7);
B. p = normcdf(7, 5, 2);
C. pd = makedist('Normal', 5, 2); p = pdf(pd, 7);
D. p = cdf('Normal', 7, 5, 2);

Solution

  1. Step 1: Understand how to create a normal distribution object

    Use makedist('Normal', 'mu', 5, 'sigma', 2) to create the distribution with mean 5 and std 2.
  2. Step 2: Use cdf to find probability less than 7

    Calling cdf(pd, 7) returns the cumulative probability up to 7.
  3. Step 3: Check other options

    p = normcdf(7, 5, 2); uses normcdf which is valid but not using makedist. 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.
  4. Final Answer:

    pd = makedist('Normal', 'mu', 5, 'sigma', 2); p = cdf(pd, 7); -> Option A
  5. Quick Check:

    Use makedist + cdf for cumulative probability [OK]
Hint: Use cdf with makedist object for cumulative probability [OK]
Common Mistakes:
  • Using pdf instead of cdf
  • Wrong syntax for cdf
  • Ignoring distribution parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard