STATS_200AP_r_practice_hanpu
.rmd
keyboard_arrow_up
School
University of California, San Diego *
*We aren’t endorsed by this school
Course
200
Subject
Industrial Engineering
Date
Dec 6, 2023
Type
rmd
Pages
2
Uploaded by CoachMantisPerson322
---
title: "Stats200Dis5"
output: html_document
date: "2023-10-31"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
# Learn to use help function to see documents.
help(pnorm)
```
Q1. Plot the PMF of a binomial distribution for 10 coin flips (n = 10) with a
probability of heads (success) being 0.5.
```{r}
# Number of trials
n <- 10
# Probability of success
# Need to complete
p <-
# Range of possible number of successes (from 0 to n)
# Need to complete
x <-
# Calculate the probabilities using the binomial distribution
probabilities <- dbinom(x, size = n, prob = p)
# Plot the PMF as a barplot
barplot(probabilities, names.arg = x, xlab = "Number of Heads", ylab =
"Probability", main = "PMF of Binomial Distribution (n=10, p=0.5)")
```
Question 2: Continuous Probability - Normal Distribution
Given a normal distribution with a mean of 100 and a standard deviation of 15,
calculate the probability of drawing a value between 90 and 110.
```{r}
# Using the pnorm function to find cumulative probabilities
# Need to complete
probability_less_than_110 <- pnorm( , mean = 100, sd = 15)
probability_less_than_90 <- pnorm( , mean = 100, sd = 15)
# Probability of drawing a value between 90 and 110
probability_between_90_and_110 <- probability_less_than_110 -
probability_less_than_90
print(probability_between_90_and_110)
```
Simulation of Q.7
```{r}
# Function to simulate one game
simulate_game <- function(p1, p2) {
total_steps <- 0
# Repeat until one player succeeds
while (TRUE) {
# Player A's turn
total_steps <- total_steps + 1
if (runif(1) < p1) {
break
}
# Player B's turn
total_steps <- total_steps + 1
if (runif(1) < p2) {
break
}
}
return(total_steps)
}
# Parameters
p1 <- 0.3
# Probability of success for player A
p2 <- 0.5
# Probability of success for player B
n_simulations <- 10000
# Number of simulations
# Run simulations
set.seed(1234)
# For reproducibility
simulation_results <- replicate(n_simulations, simulate_game(p1, p2))
# Plot histogram
hist(simulation_results, breaks = 50, col = "lightblue",
main = "Histogram of Total Steps Taken Until Success",
xlab = "Total Steps", ylab = "Frequency",xlim = c(1, 15))
# Calculate the probability of stop at step 5
P_sim = sum(simulation_results==5)/length(simulation_results)
P_oracle = p1*(1-p1)^2*(1-p2)^2
cat("Simulation reuslts:",P_sim,"True probability:", P_oracle)
```
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help