-
Notifications
You must be signed in to change notification settings - Fork 0
/
_058_geometric_realworld.R
42 lines (33 loc) · 1.2 KB
/
_058_geometric_realworld.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Filename: _058_geometric_realworld.R
# Title: Geometric Distribution Example - Number of Trials Before First Success
# Author: Raghava | GitHub: @raghavtwenty
# Date Created: June 24, 2024 | Last Updated: June 24, 2024
# Language: R | Version: 4.4.0
# PROBLEM
# We consider a real-world scenario where we are counting the number of trials
# until the first success in a series of Bernoulli trials with a probability of success of 0.2.
# Parameters for the geometric distribution
prob_success = 0.2 # Probability of success
# Number of samples to generate
num_samples = 1000
# Generate random number of trials using the geometric distribution
num_trials = rgeom(n = num_samples, prob = prob_success)
# Display the first few generated number of trials
print(head(num_trials))
# Plot the histogram of the generated number of trials
hist(num_trials,
breaks = 30,
col = "lightblue",
border = "white",
main = "Geometric Distribution of Number of Trials Before First Success",
xlab = "Number of Trials",
ylab = "Frequency"
)
# Add the theoretical density line
curve(dgeom(x,
prob = prob_success) * num_samples,
from = 0, to = max(num_trials),
add = TRUE,
col = "red",
lwd = 2
)