Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Estimating Uncertainty and Confidence Intervals | Monte Carlo Simulation for Uncertainty and Risk
Quizzes & Challenges
Quizzes
Challenges
/
Simulation and Monte Carlo Modeling with Python

bookEstimating Uncertainty and Confidence Intervals

When you use simulation to estimate outcomes, you are not just interested in the average or expected value—you also want to understand how much those results might vary. This is called uncertainty estimation. In Monte Carlo simulation, every run produces slightly different results due to randomness. To quantify how much you can trust your simulation result, you use a confidence interval. A confidence interval gives you a range of values that likely contains the true mean (or another parameter) you are estimating, with a specified level of confidence, such as 95%. This helps you express not just the result, but also the reliability of your simulation.

123456789101112131415161718192021222324252627
import numpy as np from scipy import stats # Simulate rolling a fair six-sided die 100 times, repeated for 1000 simulations num_simulations = 1000 num_rolls = 100 means = [] for _ in range(num_simulations): rolls = np.random.randint(1, 7, size=num_rolls) means.append(np.mean(rolls)) # Calculate the mean of means sample_mean = np.mean(means) # Calculate the standard error of the mean sem = stats.sem(means) # Compute the 95% confidence interval confidence = 0.95 h = sem * stats.t.ppf((1 + confidence) / 2, num_simulations - 1) ci_lower = sample_mean - h ci_upper = sample_mean + h print(f"Estimated mean of dice rolls: {sample_mean:.3f}") print(f"95% confidence interval: ({ci_lower:.3f}, {ci_upper:.3f})")
copy

To calculate a confidence interval for your simulation results, you first collect the mean outcome from each simulation run. The standard error of the mean (SEM) measures how much these means vary from one simulation to another. Using the SEM and the t-distribution, you calculate a margin of error, which is added and subtracted from your overall sample mean to create the confidence interval. In the code above, after performing 1000 simulations of 100 dice rolls each, you find the mean of all simulation means. The 95% confidence interval provides a range that, if you repeated the entire simulation process many times, would contain the true average dice roll about 95% of the time. This interval reflects the uncertainty in your estimate due to the random nature of simulation, so you can report not just a single number but a range that quantifies your confidence in the result.

question mark

Which statement best describes the meaning of a 95% confidence interval in the context of Monte Carlo simulation?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain why the t-distribution is used instead of the normal distribution?

How would the confidence interval change if I increased the number of simulations?

What does the standard error of the mean tell us in this context?

bookEstimating Uncertainty and Confidence Intervals

Svep för att visa menyn

When you use simulation to estimate outcomes, you are not just interested in the average or expected value—you also want to understand how much those results might vary. This is called uncertainty estimation. In Monte Carlo simulation, every run produces slightly different results due to randomness. To quantify how much you can trust your simulation result, you use a confidence interval. A confidence interval gives you a range of values that likely contains the true mean (or another parameter) you are estimating, with a specified level of confidence, such as 95%. This helps you express not just the result, but also the reliability of your simulation.

123456789101112131415161718192021222324252627
import numpy as np from scipy import stats # Simulate rolling a fair six-sided die 100 times, repeated for 1000 simulations num_simulations = 1000 num_rolls = 100 means = [] for _ in range(num_simulations): rolls = np.random.randint(1, 7, size=num_rolls) means.append(np.mean(rolls)) # Calculate the mean of means sample_mean = np.mean(means) # Calculate the standard error of the mean sem = stats.sem(means) # Compute the 95% confidence interval confidence = 0.95 h = sem * stats.t.ppf((1 + confidence) / 2, num_simulations - 1) ci_lower = sample_mean - h ci_upper = sample_mean + h print(f"Estimated mean of dice rolls: {sample_mean:.3f}") print(f"95% confidence interval: ({ci_lower:.3f}, {ci_upper:.3f})")
copy

To calculate a confidence interval for your simulation results, you first collect the mean outcome from each simulation run. The standard error of the mean (SEM) measures how much these means vary from one simulation to another. Using the SEM and the t-distribution, you calculate a margin of error, which is added and subtracted from your overall sample mean to create the confidence interval. In the code above, after performing 1000 simulations of 100 dice rolls each, you find the mean of all simulation means. The 95% confidence interval provides a range that, if you repeated the entire simulation process many times, would contain the true average dice roll about 95% of the time. This interval reflects the uncertainty in your estimate due to the random nature of simulation, so you can report not just a single number but a range that quantifies your confidence in the result.

question mark

Which statement best describes the meaning of a 95% confidence interval in the context of Monte Carlo simulation?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2
some-alt