bayesian-assignment-5
bayesian-assignment-5
2024-11-05
INTRODUCTION:
This report explores Bayesian statistics as a powerful method for updating beliefs based on
new data. Through problems involving urn experiments, binomial trials, and Poisson
distributions, we demonstrate how Bayesian inference is applied using R. In the first
problem, we calculate conditional and joint probabilities to analyze an urn with red and
green balls. The second problem uses binomial distribution to assess the probability of
success in multiple trials, updating our understanding of the success rate. The third
problem applies Bayesian inference to a Poisson distribution, updating estimates of the
occurrence rate. Using R functions like binodp() and poisdp(), we show the effectiveness
of Bayesian methods in solving diverse problems.
OBJECTIVES:
• Grasp Bayesian Inference: Understand how prior, likelihood, and posterior
distributions work together to update the probability of hypotheses with new data.
• Interpret Results: Analyze how posterior distributions reflect changes in belief after
incorporating new data, demonstrating the evolving nature of probability.
CODE:
library(Bolstad)
##
## Attaching package: 'Bolstad'
## [1] 0 1 2 3 4 5 6 7 8 9
print(joint_prob_table)
The table shows the joint probability distribution of two variables, 𝑌1 and 𝑋, where 𝑌 1 can
take values from 0 to 9, and 𝑋 takes binary values (0 and 1). The entries represent the
probability of the specific combination of values for 𝑌 1and 𝑋. Here’s a more detailed
interpretation:
For 𝑋 = 0: The probability distribution for 𝑌1 decreases as 𝑌1 increases.
Specifically:
At Y 1 =0, the probability is 0.1, meaning there is a 10% chance that 𝑌 1 = 0 and 𝑋 = 0.
The probability decreases gradually, with 𝑌 1 = 1 having a probability of 0.0889, and so on
until 𝑌 1 = 9, where the probability becomes 0.
This suggests that the value of 𝑌 1 is more likely to be smaller when 𝑋 = 0.
For 𝑋 =1: The distribution of probabilities is the reverse of 𝑋 = 0. The probability of 𝑌1
starts at 0 at 𝑌 1 = 0 ,and increases as 𝑌 1 increases.
At 𝑌 1 = 9 , the probability is 0.1, implying that 𝑌 1 = 9and 𝑋 = 1 has a 10% chance. For
smaller values of 𝑌 1 , the probability decreases, reaching 0 at 𝑌 1 = 0.
Key Insights:
Inverse Relationship Between 𝑋 and 𝑌 1 : The probability distribution suggests an inverse
relationship between 𝑋 and 𝑌 1 . When 𝑋 = 0, smaller values of 𝑌 1 are more probable,
while when 𝑋 = 1, larger values of 𝑌 1 are more probable. This indicates that as the value
of 𝑌 1 increases, the likelihood of 𝑋 = 1 increases, and the likelihood of 𝑋 = 0 decreases.
Symmetry: The distribution is symmetric in that the probability mass for 𝑋 = 0 is mirrored
by the probability mass for 𝑋 = 1 but in reverse order. For example, the probability of 𝑌1 =
0 and 𝑋 = 0 (0.1) is the same as that of 𝑌 1 = 9 and 𝑋 = 1 (0.1).
c) The marginal distribution of 𝑌 is calculated by summing the joint probabilities
across all possible values of x.
# Marginal distribution of Y1
P_Y1_1 <- sum(P_X_Y1_1)
P_Y1_0 <- sum(P_X_Y1_0)
print(marginal_Y1)
## Y1 Probability
## 1 1 0.5
## 2 0 0.5
Marginal Probability for 𝑌 1 = 0 : The marginal probability for 𝑌 1 = 0 is 0.5. This means
there is a 50% chance that 𝑌 1 = 0 , regardless of the value of 𝑋 . In other words, when
summing the joint probabilities for 𝑋 = 0 and 𝑋 = 1 when 𝑌 1 = 0 , the total probability is
0.5.
Marginal Probability for 𝑌 1 = 1: The marginal probability for 𝑌 1 = 1 is also 0.5. This
indicates that there is a 50% chance that 𝑌 1 = 1 , regardless of 𝑋 ’s value. Similarly,
summing the joint probabilities for 𝑋 = 0 and 𝑋 = 1 when 𝑌 1 = 1 gives the total probability
of 0.5.
Marginal Probabilities: If we sum the rows, we get the marginal probabilities for 𝑋 = 0 and
𝑋 = 1:
For 𝑋 = 0, the total probability is 0.1 + 0.0889 + 0.0778 + 0.0667 + 0.0556 + 0.0444 +
0.0333 + 0.0222 + 0.0111 + 0 = 0.5
For 𝑋 = 1, the total probability is 0 + 0.0111 + 0.0222 + 0.0333 + 0.0444 + 0.0556 + 0.0667
+ 0.0778 + 0.0889 + 0.1 = 0.5
The marginal distribution of 𝑌 1 shows that 𝑌 1 has an equal chance of being 0 or 1, each
with a probability of 0.5. This indicates that 𝑌 1 is equally likely to take either value,
irrespective of the value of 𝑋 . Therefore, based on the marginal distribution, 𝑌 1 follows a
uniform distribution with equal probabilities for 0 and 1.
d)If a red ball was drawn, the reduced universe reflects the updated probabilities of X given
this new evidence.
# Calculate posterior distribution P(X | Y1 = 1)
P_X_given_Y1_1 <- (P_Y1_1_given_X * P_X) / P_Y1_1
print(reduced_bayesian_universe)
## X P_X_given_Y1_1
## 1 0 0.00000000
## 2 1 0.02222222
## 3 2 0.04444444
## 4 3 0.06666667
## 5 4 0.08888889
## 6 5 0.11111111
## 7 6 0.13333333
## 8 7 0.15555556
## 9 8 0.17777778
## 10 9 0.20000000
e) The posterior distribution P(X∣Y =1) is calculated as shown in the previous step,
providing updated probabilities for the number of red balls.
# Posterior distribution P(X | Y1 = 1) using Bayes' Theorem
P_X_given_Y1_1 <- (P_Y1_1_given_X * P_X) / P_Y1_1
## X P_X_given_Y1_1
## 1 0 0.00000000
## 2 1 0.02222222
## 3 2 0.04444444
## 4 3 0.06666667
## 5 4 0.08888889
## 6 5 0.11111111
## 7 6 0.13333333
## 8 7 0.15555556
## 9 8 0.17777778
## 10 9 0.20000000
This bar graph shows the posterior distribution of the number of red balls (denoted as X)
after observing a certain result. The x-axis represents the possible values for X, while the y-
axis shows the posterior probability associated with each possible value.
In this distribution, the posterior probability increases as X goes up, peaking at the higher
values, specifically around 8 or 9 red balls. This suggests that, based on the observed data,
it is more likely that the actual number of red balls in the population is closer to these
higher values. The pattern indicates that observing more red balls makes higher counts
more probable, reflecting a Bayesian update of our beliefs about X.
2. Define the possible values of π as given. Assign a uniform prior to each value of π.
Calculate the posterior after observing 3 successes in 8 trials.
(a) Let the posterior after the 8 trials and 3 successes in the previous problem be
the prior, find the new posterior distribution for π.
# Load the Bolstad library
library(Bolstad)
# Extract the posterior probabilities from the list for the next step
pi_prior_updated <- posterior_1$posterior
INTERPRETATION:
Based on 15 trials and 5 successes, the posterior distribution indicates that the most likely
probability of success is around 0.4. Lower values like 0.2 are also possible, but higher
values such as 0.6 or above are unlikely. Probabilities at 0.0 and 1.0 are ruled out by the
data.
This plot shows a comparison between the prior and posterior probability distributions of
𝜋 π, the probability of success in each trial. The x-axis represents different possible values
of π, and the y-axis shows their associated probabilities.
The red points indicate the prior probabilities, which were initially set equal for each
possible π value, reflecting no prior preference. The blue points show the updated
(posterior) probabilities after observing the data. The posterior distribution now assigns
higher probabilities to certain values of π, particularly around 0.4 and 0.6, indicating that
these values are more likely given the observed data.
print(posterior_total)
## $name
## [1] "pi"
##
## $param.x
## [1] 0.0 0.2 0.4 0.6 0.8 1.0
##
## $prior
## [1] 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667
##
## $likelihood
## [1] 0.000000000 0.103182294 0.185937845 0.024485642 0.000100764
0.000000000
##
## $posterior
## [1] 0.0000000000 0.3289134253 0.5927126725 0.0780526976 0.0003212045
## [6] 0.0000000000
##
## $mean
## [1] 0.3499563
##
## $var
## [1] 0.01382567
##
## $cdf
## function (X, ...)
## {
## cumDistFun(X, pi, posterior)
## }
## <bytecode: 0x000001ee2d62fa50>
## <environment: 0x000001ee2b7a0ff0>
##
## $quantileFun
## function (probs, ...)
## {
## qFun(probs, pi, posterior)
## }
## <bytecode: 0x000001ee2d62f698>
## <environment: 0x000001ee2b7a0ff0>
##
## $pi
## [1] 0.0 0.2 0.4 0.6 0.8 1.0
##
## $pi.prior
## [1] 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667
##
## $f.cond
## 0 1 2 3 4
## 0 1.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 0.2 3.518437e-02 1.319414e-01 2.308974e-01 2.501389e-01 1.876042e-01
## 0.4 4.701850e-04 4.701850e-03 2.194197e-02 6.338790e-02 1.267758e-01
## 0.6 1.073742e-06 2.415919e-05 2.536715e-04 1.648865e-03 7.419892e-03
## 0.8 3.276800e-11 1.966080e-09 5.505024e-08 9.542042e-07 1.145045e-05
## 1 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 5 6 7 8 9
10
## 0 0.000000000 0.0000000000 0.000000000 0.000000000 0.0000000000
0.000000000
## 0.2 0.103182294 0.0429926226 0.013819057 0.003454764 0.0006717597
0.000100764
## 0.4 0.185937845 0.2065976053 0.177083662 0.118055774 0.0612141053
0.024485642
## 0.6 0.024485642 0.0612141053 0.118055774 0.177083662 0.2065976053
0.185937845
## 0.8 0.000100764 0.0006717597 0.003454764 0.013819057 0.0429926226
0.103182294
## 1 0.000000000 0.0000000000 0.000000000 0.000000000 0.0000000000
0.000000000
## 11 12 13 14 15
## 0 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 0.2 1.145045e-05 9.542042e-07 5.505024e-08 1.966080e-09 3.276800e-11
## 0.4 7.419892e-03 1.648865e-03 2.536715e-04 2.415919e-05 1.073742e-06
## 0.6 1.267758e-01 6.338790e-02 2.194197e-02 4.701850e-03 4.701850e-04
## 0.8 1.876042e-01 2.501389e-01 2.308974e-01 1.319414e-01 3.518437e-02
## 1 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00
##
## $f.joint
## 0 1 2 3 4
## [1,] 1.666667e-01 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## [2,] 5.864062e-03 2.199023e-02 3.848291e-02 4.168982e-02 3.126736e-02
## [3,] 7.836416e-05 7.836416e-04 3.656994e-03 1.056465e-02 2.112930e-02
## [4,] 1.789570e-07 4.026532e-06 4.227858e-05 2.748108e-04 1.236649e-03
## [5,] 5.461333e-12 3.276800e-10 9.175040e-09 1.590340e-07 1.908408e-06
## [6,] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 5 6 7 8 9
## [1,] 0.000000e+00 0.000000000 0.0000000000 0.0000000000 0.000000000
## [2,] 1.719705e-02 0.007165437 0.0023031762 0.0005757941 0.000111960
## [3,] 3.098964e-02 0.034432934 0.0295139436 0.0196759624 0.010202351
## [4,] 4.080940e-03 0.010202351 0.0196759624 0.0295139436 0.034432934
## [5,] 1.679399e-05 0.000111960 0.0005757941 0.0023031762 0.007165437
## [6,] 0.000000e+00 0.000000000 0.0000000000 0.0000000000 0.000000000
## 10 11 12 13 14
## [1,] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## [2,] 1.679399e-05 1.908408e-06 1.590340e-07 9.175040e-09 3.276800e-10
## [3,] 4.080940e-03 1.236649e-03 2.748108e-04 4.227858e-05 4.026532e-06
## [4,] 3.098964e-02 2.112930e-02 1.056465e-02 3.656994e-03 7.836416e-04
## [5,] 1.719705e-02 3.126736e-02 4.168982e-02 3.848291e-02 2.199023e-02
## [6,] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
## 15
## [1,] 0.000000e+00
## [2,] 5.461333e-12
## [3,] 1.789570e-07
## [4,] 7.836416e-05
## [5,] 5.864062e-03
## [6,] 1.666667e-01
##
## $f.marg
## 0 1 2 3 4 5
6
## [1,] 0.1726093 0.0227779 0.04218219 0.05252944 0.05363522 0.05228442
0.05191268
## 7 8 9 10 11 12
## [1,] 0.05206888 0.05206888 0.05191268 0.05228442 0.05363522 0.05252944
## 13 14 15
## [1,] 0.04218219 0.0227779 0.1726093
##
## attr(,"class")
## [1] "Bolstad"
INTERPRETATION:
The posterior distribution shows that after 15 trials and 5 successes, the most likely
probability of success is around 0.4, followed by 0.2 as a possible value. Higher
probabilities (𝜋 ≥ 0.6) are unlikely, and 𝜋 = 0.0 or 𝜋 = 1.0 are ruled out.
In this plot, we see a comparison of prior and posterior probabilities for the parameter 𝜋
(the probability of success). The prior distribution (red) initially assigns equal probability
to all values of 𝜋, indicating no prior preference. After observing the data, the posterior
distribution (blue) adjusts, giving more weight to values around 0.4. This suggests that,
based on the observed data, a 𝜋 value close to 0.4 is more likely to be the true probability of
success in each trial.
3. The R function poisdp is used to find the posterior distribution when the
observation distribution of Y |µ is Poisson(µ) and we have a discrete prior
distribution for µ. Suppose there are six possible values µ = 1,…,6 and the prior
probabilities are given by
µ , g(µ) 1 .10 2 .15 3 .25 4 .25 5 .15 6 .10
Suppose the first observation is Y 1 = 2. Use the R function poisdp to find the posterior
distribution g(µ|y).
# Step 1: Define possible values of μ and prior distribution
mu_values <- 1:6 # Possible values of μ
prior_prob <- c(0.10, 0.15, 0.25, 0.25, 0.15, 0.10) # Prior probabilities
# Create a data frame to hold values for μ and their corresponding posterior
probabilities
posterior_distribution <- data.frame(mu = mu_values, Posterior = posterior)
print("Posterior Distribution g(μ | Y = 2):")
print(posterior_distribution)
## mu Posterior
## 1 1 0.10901314
## 2 2 0.24062217
## 3 3 0.33194980
## 4 4 0.21709779
## 5 5 0.07487420
## 6 6 0.02644289