0% found this document useful (0 votes)
4 views

bayesian-assignment-5

The bayesian analysis of discrete random variables

Uploaded by

prashanthi.rs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

bayesian-assignment-5

The bayesian analysis of discrete random variables

Uploaded by

prashanthi.rs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Bayesian Analysis of an Urn Problem and Binomial Trials with

Poisson Observations: A Comprehensive R Implementation


Prashanthi

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.

• Apply Bayesian Methods: Use Bayesian techniques to solve real-world problems,


like estimating proportions in an urn, predicting success in binomial trials, and
updating rate estimates in Poisson distributions.

• Leverage R for Analysis: Learn to perform Bayesian calculations in R, using


functions like binodp() and poisdp() to compute posterior probabilities for binomial
and Poisson models.

• Interpret Results: Analyze how posterior distributions reflect changes in belief after
incorporating new data, demonstrating the evolving nature of probability.
CODE:
library(Bolstad)

## Warning: package 'Bolstad' was built under R version 4.3.3

##
## Attaching package: 'Bolstad'

## The following objects are masked from 'package:stats':


##
## IQR, sd, var
The Bayesian universe consists of all possible values for the number of red balls ‘X’ in the
urn. Since there are 9 balls total,X can take on any integer value from 0 to 9.
# Define Bayesian universe
bayesian_universe <- 0:9
print(bayesian_universe)

## [1] 0 1 2 3 4 5 6 7 8 9

Interpretation: The output 0 1 2 3 4 5 6 7 8 9 represents all possible counts of red balls X in


the urn, from 0 (no red balls) to 9 (all balls are red). Each value is an equally likely
hypothesis in the Bayesian framework before observing any draws from the urn.
The prior distribution is taken to be with uniform probability as 1/10
b) Let 𝑌=1 if the first ball drawn is red, and 𝑌=0 if it is green. We need to fill in a table
with the probabilities of drawing a red or green ball based on the number of red
balls X.
X_values <- 0:9

# Prior probability P(X)


P_X <- rep(1/10, length(X_values))

# Calculate joint probability for Y1 = 1 and Y1 = 0


P_Y1_1_given_X <- X_values / 9 # P(Y1 = 1 | X)
P_Y1_0_given_X <- (9 - X_values) / 9 # P(Y1 = 0 | X)

# Joint probability P(X, Y1 = 1) and P(X, Y1 = 0)


P_X_Y1_1 <- P_Y1_1_given_X * P_X
P_X_Y1_0 <- P_Y1_0_given_X * P_X

# Create a data frame to store the joint probabilities


joint_prob_table <- data.frame(
X = X_values,
P_X = P_X,
P_Y1_1_given_X = P_Y1_1_given_X,
P_X_Y1_1 = P_X_Y1_1,
P_Y1_0_given_X = P_Y1_0_given_X,
P_X_Y1_0 = P_X_Y1_0
)

print(joint_prob_table)

## X P_X P_Y1_1_given_X P_X_Y1_1 P_Y1_0_given_X P_X_Y1_0


## 1 0 0.1 0.0000000 0.00000000 1.0000000 0.10000000
## 2 1 0.1 0.1111111 0.01111111 0.8888889 0.08888889
## 3 2 0.1 0.2222222 0.02222222 0.7777778 0.07777778
## 4 3 0.1 0.3333333 0.03333333 0.6666667 0.06666667
## 5 4 0.1 0.4444444 0.04444444 0.5555556 0.05555556
## 6 5 0.1 0.5555556 0.05555556 0.4444444 0.04444444
## 7 6 0.1 0.6666667 0.06666667 0.3333333 0.03333333
## 8 7 0.1 0.7777778 0.07777778 0.2222222 0.02222222
## 9 8 0.1 0.8888889 0.08888889 0.1111111 0.01111111
## 10 9 0.1 1.0000000 0.10000000 0.0000000 0.00000000

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)

# Output the marginal distribution of Y1


marginal_Y1 <- data.frame(Y1 = c(1, 0), Probability = c(P_Y1_1, P_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

# Create a data frame for the reduced Bayesian universe


reduced_bayesian_universe <- data.frame(X = X_values, P_X_given_Y1_1 =
P_X_given_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

Posterior Calculation: The posterior probabilityP(X∣Y 1 =1) is calculated by normalizing


the joint probability 𝑃 ( 𝑋 , 𝑌 1 = 1 ) with the marginal probability 𝑃 ( 𝑌 1 = 1 ) = 0.5
Increasing Likelihood: As 𝑋 increases, the posterior probability increases, showing that
larger values of 𝑋 are more likely given 𝑌 1 = 1 .
Key Observation:
𝑃 ( 𝑋 = 0 ∣ 𝑌 1 = 1 ) = 0 (very unlikely for 𝑋 = 0 when 𝑌 1 = 1). The probability grows
steadily, reaching 𝑃 ( 𝑋 = 9 ∣ 𝑌 1 = 1 ) = 0.2, indicating 𝑋 = 9 is the most likely outcome.
Result :
Given 𝑌 1 = 1, larger values of X become increasingly probable.

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

# Create a data frame to display the posterior distribution


posterior_distribution <- data.frame(X = X_values, P_X_given_Y1_1 =
P_X_given_Y1_1)

# Print the posterior distribution


print(posterior_distribution)

## 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

Posterior Distribution: The values represent the updated probability distribution of 𝑋 X


after observing 𝑌 1 = 1 (a red ball).
Key Observation:
The probability starts at 0 for 𝑋 = 0, and increases as 𝑋 increases. The highest probability is
0.2 for 𝑋 = 9, indicating 𝑋 = 9 is the most likely value after observing 𝑌 1 = 1. Trend: As 𝑋 X
increases, the probability steadily increases, showing that larger values of 𝑋 become more
probable when 𝑌 1 = 1.
Conclusion:
Given 𝑌 1 = 1, the posterior distribution suggests that larger values of 𝑋 are increasingly
more likely.
f)This step consolidates the posterior distribution obtained in the previous step.
# Create a bar plot using ggplot2
library(ggplot2)

## Warning: package 'ggplot2' was built under R version 4.3.3

ggplot(posterior_distribution, aes(x = X, y = P_X_given_Y1_1)) +


geom_bar(stat = "identity", fill = "lightblue") +
labs(title = "Posterior Distribution P(X | Y1 = 1)",
x = "Values of X",
y = "Posterior Probability") +
theme_minimal() +
scale_x_continuous(breaks = X_values) + # Show all X values on x-axis
ylim(0, max(P_X_given_Y1_1) * 1.1) # Add some space above bars

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)

# Define the possible values of π and the initial uniform prior


pi <- c(0, 0.2, 0.4, 0.6, 0.8, 1.0)
pi_prior <- rep(1/6, 6) # Uniform prior probabilities for π

# First observation: 8 trials with 3 successes


x1 <- 3 # Number of successes in the first set of trials
n1 <- 8 # Number of trials in the first set

# Calculate the posterior distribution after 8 trials and 3 successes


posterior_1 <- binodp(x = x1, n = n1, pi = pi, pi.prior = pi_prior)
## Conditional distribution of x given pi and n:
##
## 0 1 2 3 4 5 6 7 8
## 0 1.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## 0.2 0.1678 0.3355 0.2936 0.1468 0.0459 0.0092 0.0011 0.0001 0.0000
## 0.4 0.0168 0.0896 0.2090 0.2787 0.2322 0.1239 0.0413 0.0079 0.0007
## 0.6 0.0007 0.0079 0.0413 0.1239 0.2322 0.2787 0.2090 0.0896 0.0168
## 0.8 0.0000 0.0001 0.0011 0.0092 0.0459 0.1468 0.2936 0.3355 0.1678
## 1 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.0000
##
## Joint distribution:
##
## 0 1 2 3 4 5 6 7 8
## [1,] 0.1667 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [2,] 0.0280 0.0559 0.0489 0.0245 0.0076 0.0015 0.0002 0.0000 0.0000
## [3,] 0.0028 0.0149 0.0348 0.0464 0.0387 0.0206 0.0069 0.0013 0.0001
## [4,] 0.0001 0.0013 0.0069 0.0206 0.0387 0.0464 0.0348 0.0149 0.0028
## [5,] 0.0000 0.0000 0.0002 0.0015 0.0076 0.0245 0.0489 0.0559 0.0280
## [6,] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1667
##
## Marginal distribution of x:
##
## 0 1 2 3 4 5 6 7 8
## [1,] 0.1975 0.0722 0.0908 0.0931 0.0927 0.0931 0.0908 0.0722 0.1975
##
##
## Prior Likelihood Posterior
## 0 0.1666667 0.00000000 0.0000000
## 0.2 0.1666667 0.14680064 0.2628337
## 0.4 0.1666667 0.27869184 0.4989733
## 0.6 0.1666667 0.12386304 0.2217659
## 0.8 0.1666667 0.00917504 0.0164271
## 1 0.1666667 0.00000000 0.0000000

# Extract the posterior probabilities from the list for the next step
pi_prior_updated <- posterior_1$posterior

# Second observation: Additional 7 trials with 2 successes


x2 <- 2 # Number of additional successes
n2 <- 7 # Number of additional trials

# Calculate the updated posterior after additional 7 trials and 2 successes


posterior_2 <- binodp(x = x2, n = n2, pi = pi, pi.prior = pi_prior_updated)
## Conditional distribution of x given pi and n:
##
## 0 1 2 3 4 5 6 7
## 0 1.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## 0.2 0.2097 0.3670 0.2753 0.1147 0.0287 0.0043 0.0004 0.0000
## 0.4 0.0280 0.1306 0.2613 0.2903 0.1935 0.0774 0.0172 0.0016
## 0.6 0.0016 0.0172 0.0774 0.1935 0.2903 0.2613 0.1306 0.0280
## 0.8 0.0000 0.0004 0.0043 0.0287 0.1147 0.2753 0.3670 0.2097
## 1 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.0000
##
## Joint distribution:
##
## 0 1 2 3 4 5 6 7
## [1,] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [2,] 0.0551 0.0965 0.0723 0.0301 0.0075 0.0011 0.0001 0.0000
## [3,] 0.0140 0.0652 0.1304 0.1449 0.0966 0.0386 0.0086 0.0008
## [4,] 0.0004 0.0038 0.0172 0.0429 0.0644 0.0579 0.0290 0.0062
## [5,] 0.0000 0.0000 0.0001 0.0005 0.0019 0.0045 0.0060 0.0034
## [6,] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
##
## Marginal distribution of x:
##
## 0 1 2 3 4 5 6 7
## [1,] 0.0695 0.1655 0.22 0.2184 0.1704 0.1022 0.0437 0.0105
##
##
## Prior Likelihood Posterior
## 0 0.0000000 0.0000000 0.0000000000
## 0.2 0.2628337 0.2752512 0.3289134253
## 0.4 0.4989733 0.2612736 0.5927126725
## 0.6 0.2217659 0.0774144 0.0780526976
## 0.8 0.0164271 0.0043008 0.0003212045
## 1 0.0000000 0.0000000 0.0000000000

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.

(b) In total, we have taken 15 trials and achieved 5 successes. Go back


to the original prior and then find the posterior after the 15 trials and
5 successes.
# Combining all trials: 15 trials with 5 successes

x_total <- 5 # Total successes in all trials


n_total <- 15 # Total number of trials

# Calculating the posterior distribution after 15 trials and 5 successes,


using the original prior
posterior_total <- binodp(x = x_total, n = n_total, pi = pi, pi.prior =
pi_prior)
## Conditional distribution of x given pi and n:
##
## 0 1 2 3 4 5 6 7 8 9
## 0 1.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## 0.2 0.0352 0.1319 0.2309 0.2501 0.1876 0.1032 0.0430 0.0138 0.0035 0.0007
## 0.4 0.0005 0.0047 0.0219 0.0634 0.1268 0.1859 0.2066 0.1771 0.1181 0.0612
## 0.6 0.0000 0.0000 0.0003 0.0016 0.0074 0.0245 0.0612 0.1181 0.1771 0.2066
## 0.8 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0007 0.0035 0.0138 0.0430
## 1 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## 10 11 12 13 14 15
## 0 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## 0.2 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000
## 0.4 0.0245 0.0074 0.0016 0.0003 0.0000 0.0000
## 0.6 0.1859 0.1268 0.0634 0.0219 0.0047 0.0005
## 0.8 0.1032 0.1876 0.2501 0.2309 0.1319 0.0352
## 1 0.0000 0.0000 0.0000 0.0000 0.0000 1.0000
##
## Joint distribution:
##
## 0 1 2 3 4 5 6 7 8 9
## [1,] 0.1667 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [2,] 0.0059 0.0220 0.0385 0.0417 0.0313 0.0172 0.0072 0.0023 0.0006 0.0001
## [3,] 0.0001 0.0008 0.0037 0.0106 0.0211 0.0310 0.0344 0.0295 0.0197 0.0102
## [4,] 0.0000 0.0000 0.0000 0.0003 0.0012 0.0041 0.0102 0.0197 0.0295 0.0344
## [5,] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0006 0.0023 0.0072
## [6,] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## 10 11 12 13 14 15
## [1,] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [2,] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [3,] 0.0041 0.0012 0.0003 0.0000 0.0000 0.0000
## [4,] 0.0310 0.0211 0.0106 0.0037 0.0008 0.0001
## [5,] 0.0172 0.0313 0.0417 0.0385 0.0220 0.0059
## [6,] 0.0000 0.0000 0.0000 0.0000 0.0000 0.1667
##
## Marginal distribution of x:
##
## 0 1 2 3 4 5 6 7 8 9
## [1,] 0.1726 0.0228 0.0422 0.0525 0.0536 0.0523 0.0519 0.0521 0.0521 0.0519
## 10 11 12 13 14 15
## [1,] 0.0523 0.0536 0.0525 0.0422 0.0228 0.1726
##
##
## Prior Likelihood Posterior
## 0 0.1666667 0.000000000 0.0000000000
## 0.2 0.1666667 0.103182294 0.3289134253
## 0.4 0.1666667 0.185937845 0.5927126725
## 0.6 0.1666667 0.024485642 0.0780526976
## 0.8 0.1666667 0.000100764 0.0003212045
## 1 0.1666667 0.000000000 0.0000000000

print("Posterior after 15 trials and 5 successes (using original prior):")

## [1] "Posterior after 15 trials and 5 successes (using original prior):"

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

# Step 2: Define observed data


y_observed <- 2

# Step 3: Calculate the likelihood for each μ given Y = 2 using Poisson(μ)


likelihood <- dpois(y_observed, lambda = mu_values)

# Step 4: Calculate the unnormalized posterior by multiplying prior and


likelihood
unnormalized_posterior <- likelihood * prior_prob

# Step 5: Normalize to get the posterior distribution


posterior <- unnormalized_posterior / sum(unnormalized_posterior)

# 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):")

## [1] "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

# Step 6: Plot the posterior distribution


library(ggplot2)

ggplot(posterior_distribution, aes(x = factor(mu), y = Posterior)) +


geom_bar(stat = "identity", fill = "skyblue") +
labs(title = "Posterior Distribution of μ given Y = 2",
x = "Values of μ",
y = "Posterior Probability") +
theme_minimal()

For each value of μ


(1 through 6), we get a posterior probability:
μ = 1: 0.109 (about 11%)
μ = 2: 0.241 (about 24%)
μ = 3: 0.332 (about 33%) - Highest probability
μ = 4: 0.217 (about 22%)
μ = 5: 0.075 (about 7.5%)
μ = 6: 0.026 (about 2.6%)
The interpretation is: after observing Y = 2, μ = 3 is most likely (33% probability), followed
by μ = 2 (24%), while extreme values like μ = 6 are least likely (2.6%). This aligns with
what we’d expect since the observed value was Y = 2.
This histogram shows the posterior probability distribution for a parameter μ (mu) after
observing that Y = 2. The distribution tells us how likely different values of μ are, given this
observation. The highest probability (around 0.3) occurs at μ = 3, suggesting this is the
most likely value. The probability decreases on both sides, with lower probabilities for very
small (μ = 1) or very large (μ = 6) values. This bell-shaped pattern is typical of posterior
distributions in Bayesian statistics. This graph represents our updated beliefs about what μ
could be after seeing the data point Y = 2.
CONCLUSION:
The code demonstrates Bayesian updating, where prior knowledge about a parameter (e.g.,
𝜇 in a Poisson distribution or 𝜋 in a Binomial distribution) is combined with observed data
to update our beliefs through the posterior distribution. By multiplying the prior by the
likelihood of the observed data and normalizing, we obtain the posterior distribution,
which reflects the updated probability of the parameter given the evidence. For instance,
after observing 2 successes in a Poisson model, the posterior for 𝜇 = 3 becomes most
probable, while in the Binomial case, after 3 successes out of 8 trials, values like 𝜋 = 0.4
gain higher posterior probabilities. This process allows us to refine estimates and make
more informed predictions based on both prior knowledge and new data.

You might also like