Workbreakdown:
Problem 1: Natalya Sydor
Problem 2: Natalya Sydor, Viktoriia
Stetsyshyn, Yuliia Vistak
Problem 3: Yuliia Vistak
Problem 4: Viktoriia Stetsyshyn
In the first part, we will estimate the probability that a random digit sequence of length n contains the TLN (consider the cases n = 100, n = 200, n = 1000).
set.seed(2)
library(stringr)
library(markovchain)
## Warning: package 'markovchain' was built under R version 4.3.2
## Package: markovchain
## Version: 0.9.5
## Date: 2023-09-24 09:20:02 UTC
## BugReport: https://github.com/spedygiorgio/markovchain/issues
library(heemod)
## Warning: package 'heemod' was built under R version 4.3.2
library(diagram)
## Loading required package: shape
Estimate numerically the probability \(\hat{p}_n\) of the event that your TLN occurs in a random digit sequence \(d_1d_2d_3 . . . d_n\).
# Set TLN
tln <- "002"
n<-c(100, 200, 1000)
# Generate random digit sequences
N <- 1000 # Adjust N based on your computational resources
# Check if TLN occurs in each sequence
i <- 0
for (j in n){
sequences <- replicate(N, sample(0:9, j, replace = T))
nums <- 0
for (i in 1:N){
if (str_detect(paste(sequences[,i], collapse = ""), tln)){
nums <- nums + 1
}
}
p_hats <- nums/N
cat("For n = ", j, " ", p_hats, "\n")
i <- i + 1
}
## For n = 100 0.079
## For n = 200 0.184
## For n = 1000 0.626
Identify the the Markov chain structure with four states \(S_0\), \(S_1\), \(S_2\), \(S_3\) in this sequence with Sk denoting the number of correct last digits (eg., for the team id number 028 these states will be \(S_0\) =“*”, \(S_1\) =“0”, \(S_2\) =“02”, \(S_3\) =“028”). Determine the transition probabilities matrix P and find the limiting probability \(p_n\) for the state “028”. Compare with the result obtained in part 1.
mat <-matrix(c(9/10,1/10,0,0,9/10,0,1/10,0,8/10,0,1/10,1/10,9/10,1/10,0,0),nrow
=4,ncol=4,byrow=TRUE)
states <- c("x", "0", "00", "002")
my_chain <- new("markovchain", states = states, transitionMatrix = mat)
print(my_chain)
## x 0 00 002
## x 0.9 0.1 0.0 0.0
## 0 0.9 0.0 0.1 0.0
## 00 0.8 0.0 0.1 0.1
## 002 0.9 0.1 0.0 0.0
mat_dim <- define_transition(state_names = states,
9/10,1/10,0,0,9/10,0,1/10,0,8/10,0,1/10,1/10,9/10,1/10,0,0);
plot(mat_dim)
For this matrix we’ll need to solve a matrix of equations to find transition limiting probabilities:
\[ \begin{align*} \pi_0 &= 0.9\pi_0+0.9\pi_1+0.8\pi_2+0.9\pi_3 \\ \pi_1 &= 0.1\pi_0+0.1\pi_3\\ \pi_2 &= 0.1\pi_0+0.1\pi_1\\ \pi_3 &= 0.1\pi_2\\ \pi_0 &+ \pi_1 + \pi_2 + \pi_3 = 1 \end{align*} \]
From this system we’ll get
\[ \begin{align} \pi_0 &= \frac{899}{1000}\\ \pi_1 &= \frac{9}{100}\\ \pi_2 &= \frac{1}{100}\\ \pi_3 &= \frac{1}{1000}\\ \end{align} \]
Determine approximately the sample size \(N\) which guarantees that the absolute error \(|\hat{p}_n − p_n|\) of the estimate \(p_n\) is below \(0.03\) with confidence level of at least \(95\) percent. Rerun the experiments for \(n = 1000\) with the determined size \(N\) to illustrate the confidence interval and confidence level.
# Z-score for a 95% confidence level
Z <- 1.96
n <- 1000
epsilon <- 0.03
sigma_squared <- p_hats*(1 - p_hats)
N <- Z^2 * sigma_squared/(epsilon^2)
cat("Determined size N =", round(N, 0))
## Determined size N = 999
\[P\left\{ | \hat{p}_n - p_n| \geq \varepsilon \right\} = 0.95\\\] \[\Phi(z_{\beta}) = 0.95 \implies z_{\beta} = 1.96\\\] \[\varepsilon = \frac{z_{\beta} \cdot \sigma}{\sqrt{n}}\\\] \[\sqrt{N} = \frac{z_{\beta} \cdot \sigma}{\varepsilon}\\\] \[N = \frac{z_{\beta}^2 \cdot \sigma^2}{\varepsilon^2}\\\]
In the setting of Problem 1, assume that the random digit generation stops at the first occurrence of the TLN (i.e., that the state \(S_3\) of the Markov chain is now absorbing). In this problem, you will estimate the average length of such sequences.
Make necessary amendments to the transition probabilities matrix \(P\) above and solve the corresponding system to find the expected time \(E(T)\) till absorption
mat <-matrix(c(9/10,1/10,0,0,9/10,0,1/10,0,8/10,0,1/10,1/10,0,0,0,1),nrow
=4,ncol=4,byrow=TRUE)
states <- c("x", "0", "00", "002")
my_chain <- new("markovchain", states = states, transitionMatrix = mat)
print(my_chain)
## x 0 00 002
## x 0.9 0.1 0.0 0.0
## 0 0.9 0.0 0.1 0.0
## 00 0.8 0.0 0.1 0.1
## 002 0.0 0.0 0.0 1.0
mat_dim <- define_transition(state_names = states,
9/10,1/10,0,0,9/10,0,1/10,0,8/10,0,1/10,1/10,0,0,0,1);
plot(mat_dim)
\[ \begin{aligned} \mu_0 &= 1+0.9\mu_0+0.1\mu_1 \\ \mu_1 &= 1+0.9\mu_0+0.1\mu_2\\ \mu_2 &= 1+0.8\mu_0+0.1\mu_2+0.1\mu_3\\ \mu_3 &= 0\\ \end{aligned} \]
From this: \[ \begin{align*} \mu_0 &= 1000\\ \mu_1 &= 990\\ \mu_2 &= 890\\ \mu_3 &= 0\\ \end{align*} \]
Assume, we start from state \(S_0\)
\[ E(T) = \mu_0 = 1000\]
Estimate numerically the expected length \(E(T)\) till the first occurrence of the TLN by running a sufficiently large number \(N\) of experiments.
library(stringr)
# Set TLN
tln <- "002"
n <- 1000
# Generate random digit sequences
N <- 10000 # Adjust N based on your computational resources
nums <- numeric(N)
sequences <- replicate(N, sample(0:9, n, replace = T))
# Check if TLN occurs in each sequence
for (i in 1:N){
sequence_str <- paste(sequences[, i], collapse = "")
result <- str_locate(sequence_str, tln)
sequence <- as.numeric(strsplit(sequence_str, "")[[1]])
if (is.na(result[1,1])) {
len <- n
while (TRUE) {
new_digit <- sample(0:9, 1, replace = TRUE)
sequence <- c(sequence, new_digit)
len <- len + 1
if (sequence[len - 2] == 0 && sequence[len - 1] == 0 && sequence[len] == 2) {
break
}
nums[i] <- len
}
}
else{
nums[i] <- result[1,1]+2
}
}
sigma <- sd(nums)
theta <- round(mean(nums), 0)
cat("Expected length of sequence E(T) =", theta)
## Expected length of sequence E(T) = 989
Find the sample size \(N\) which guarantees that the absolute error \(|\hatθ − θ|\) of the estimate does not exceed \(10\) with confidence level of at least \(95\) percent.
Using Chebyshev’s inequality:
\[ \begin{align*} c &= 10\\ P(|\hat\theta - \theta| \leq c) &= 1 - P(|\hat\theta - \theta| \geq c) \geq 1 - \frac{\sigma^2}{c^2} = 1 - \frac{\sigma^2}{Nc^2}\\ 1 - \frac{\sigma^2}{Nc^2} &= 0.95\\ N &= \frac{\sigma^2}{0.05c^2} \end{align*} \]
c <- 10
N_cheb <- sigma^2/(c^2 * 0.05)
cat("Using Chebyshev's inequality, we determined size N =", round(N_cheb, 0))
## Using Chebyshev's inequality, we determined size N = 190470
\(P\left\{ |\hat{\theta} - \theta| \geq c \right\} = 0.95\\\) \(\hat{\theta} = \overline{T} = \frac{1}{n} \sum_{i=1}^{N} T_i\\\)
\(\text{For large } N, \hat{\theta} \sim \text{Normal}(\mu, \sigma^2), \text{ where } \mu = E(\hat{\theta}); \sigma^2 = \text{variance of } \overline{T\\}\)
\(\text{Var}(\overline{T}) = {\sigma}{N}\\\)
\(\text{Let } z_{\beta} = \frac{c \sqrt{N}}{\sigma}\\\)
\(P\left\{ |\hat{\theta} - \theta| \geq c \right\} = 1 - P\left\{ |\hat{\theta} - \theta| \leq c \right\} = 1 - P\left\{ \frac{\sqrt{N} |\hat{\theta} - \theta|}{\sigma} \leq \frac{c\sqrt{N}}{\sigma} \right\}\)
\(P\left\{ |\hat{\theta} - \theta| \geq c \right\} = 0.95 \Longrightarrow 2\Phi(z_{\beta}) = 0.95\\\)
So we can derive \(z_{\beta}\) from this and get \(z_{\beta} = 1.96\)
And also, we from \(z_{\beta}\) we derive \(N\)
\(N = \frac{\sigma^2 z_{\beta}^2}{c^2}\\\)
# Z-score for a 95% confidence level
Z <- 1.96
N <- Z^2 * sigma^2/(c^2)
cat("Using approximation to normal distr., we determined size N =", round(N, 0))
## Using approximation to normal distr., we determined size N = 36586
id <- 2
set.seed(id)
theta <- id/100
lambda <- 1/theta
alpha <- c( 0.1, 0.05, 0.01)
M <- 1000
N <- 100
The expected value of the exponential distribution \(E (λ)\) is \(1/λ\), so that a good point estimate of the parameter \(θ := 1/λ\) is the sample mean x. Confidence interval for \(θ\) can be formed in several different ways
# Generating matrix of samples of realization
x3 <- matrix(rexp(N*M, rate = lambda), nrow = N)
# Calculating the mean of each column in the matrix
sample_mean3 <- colMeans(x3)
# Calculating the standard deviation of each column in the matrix
sample_sd3 <- apply(x3, 2, sd)
Using the exact distribution of the statistics \(2λnX\) (show it is \(χ^2_2n\) and then use quantiles of the latter to get the interval endpoints)
\[X_1 + \ldots + X_n \sim \Gamma(n, \lambda) \\\] \[n \cdot \frac{X_1 + \ldots + X_n}{n} \sim \Gamma(n, \lambda) \\\] \[n \cdot \bar{X} \sim \Gamma(n, \lambda) \\\] \[2 \lambda \bar{X} \sim \Gamma\left(n, \frac{1}{2}\right) \\\] \[X_n^2 \sim \Gamma\left(\frac{n}{2}, \frac{1}{2}\right) \\\] \[X^{2}_{2n} \sim \Gamma(n, \frac{1}{2}) \\\] \[\text{Confidence level} = 1 - \lambda \\\] \[1 - \lambda = P\left\{\chi^2_{\lambda/2, 2n} \leq 2 \lambda n \bar{X} \leq \chi^2_{1-\lambda/2, 2n}\right\} \\\] \[1 - \lambda = P\left\{\frac{\chi^2_{\lambda/2, 2n}}{2n\bar{X}} \leq \lambda \leq \frac{\chi^2_{1-\lambda/2, 2n}}{2n\bar{X}}\right\} \\\]
\[1 - \lambda = P\left\{\frac{2n\bar{X}}{\chi^2_{1-\lambda/2, 2n}} \leq \theta \leq \frac{2n\bar{X}}{\chi^2_{\lambda/2, 2n}}\right\}\]
# For chi-squared distribution
for (alpha in c(.01, .05, .1)){
cat("For confidence level", 1-alpha, "\n", sep=" ")
# Calculate the first quantile of the chi-squared distribution
first_quantile3 <- qchisq(p = alpha/2, df = 2 * N)
# Calculate the second quantile of the chi-squared distribution
second_quantile3 <- qchisq(p = 1 - alpha/2, df = 2 * N)
# Calculate the lower bound of the confidence interval
lower_bound3_1 <- 2*N*sample_mean3/second_quantile3
# Calculate the upper bound of the confidence interval
upper_bound3_1 <- 2*N*sample_mean3/first_quantile3
cat(" the fraction of CI's containing the parameter is",
mean((lower_bound3_1 <= theta) & (theta <= upper_bound3_1)), "\n", sep=" ")
cat(" the CI length is", mean(upper_bound3_1 - lower_bound3_1), "\n", sep=" ")
cat("\n")
}
## For confidence level 0.99
## the fraction of CI's containing the parameter is 0.993
## the CI length is 0.01058324
##
## For confidence level 0.95
## the fraction of CI's containing the parameter is 0.953
## the CI length is 0.007971667
##
## For confidence level 0.9
## the fraction of CI's containing the parameter is 0.906
## the CI length is 0.006662548
Using the normal approximation \(N (µ, σ2)\) for \(X\); the parameters are \(µ = θ\) and \(σ^2 = s^2/n\), where \(s^2 = θ^2\) is the population variance (i.e., variance of the original distribution \(E (λ)\)). In other words, we form the \(Z\)-statistics \(Z := \sqrt{n}(X − θ)/θ\) and use the fact that it is approximately standard normal \(N (0, 1)\) to find that \(P(|θ − X| ≤ z_β*θ/√n) = P(|Z| ≤ z_β) = 2β − 1\). In other words, \(θ\) is with probability \(2β − 1\) within \(X ± z_β *θ/√n\).
\[\bar{X} \sim \mathcal{N}(\mu, \sigma^2),\ \mu = \theta,\ \sigma^2 = \frac{\theta^2}{n} \\\] \[Z = \frac{\sqrt{n}(\bar{X} - \theta)}{\theta} \\\] \[Z \sim \mathcal{N}(0, 1) \\\] \[P\left\{|\theta - \bar{X}| \leq \frac{z_{\beta}\theta}{\sqrt{n}}\right\} = P\left\{|Z| \leq z_{\beta}\right\} \\\] \[P\left\{\bar{X} - \frac{z_{\beta}\theta}{\sqrt{n}} \leq \theta \leq \bar{X} + \frac{z_{\beta}\theta}{\sqrt{n}}\right\} = 2\beta - 1 \\\] \[P\left\{-z_{\beta} \leq Z \leq z_{\beta}\right\} = \Phi(z_{\beta}) - \Phi(-z_{\beta}) = \Phi(z_{\beta}) - (1 - \Phi(z_{\beta})) = \beta - (1 - \beta) = 2\beta - 1 \\\] \[2\beta - 1 = 1 - \alpha \\\] \[\beta = \frac{2 - \alpha}{2} \\\] \[P\left\{\bar{X} - \frac{z_{\beta}\theta}{\sqrt{n}} \leq \theta \leq \bar{X} + \frac{z_{\beta}\theta}{\sqrt{n}}\right\}\]
for (alpha in c(.01, .05, .1)){
cat("For confidence level", 1-alpha, "\n", sep=" ")
# Calculate beta
beta <- (2 - alpha)/2
# Determine the z-score for the beta quantile from the normal distribution
z_beta <- qnorm(beta)
# Calculate the lower bound of the confidence interval
lower_bound3_2 <- sample_mean3 - z_beta*theta/sqrt(N)
# Calculate the upper bound of the confidence interval
upper_bound3_2 <- sample_mean3 + z_beta*theta/sqrt(N)
cat(" the fraction of CI's containing the parameter is",
mean((lower_bound3_2 <= theta) & (theta <= upper_bound3_2)), "\n", sep=" ")
cat(" the CI length is", mean(upper_bound3_2 - lower_bound3_2), "\n", sep=" ")
cat("\n")
}
## For confidence level 0.99
## the fraction of CI's containing the parameter is 0.99
## the CI length is 0.01030332
##
## For confidence level 0.95
## the fraction of CI's containing the parameter is 0.961
## the CI length is 0.007839856
##
## For confidence level 0.9
## the fraction of CI's containing the parameter is 0.909
## the CI length is 0.006579415
The confidence interval constructed above uses the unknown variance \(s^2 = θ^2\) and is of little use in practice. Instead, we can solve the double inequality \(|θ − X| ≤ zβθ/√n\) for \(θ\) and get another confidence interval of confidence level \(2β − 1\) that is independent of the unknown parameter.
\[s^2 = \theta^2\]
\[\left| \theta - \overline{X} \right| \leq \frac{z_{\beta} \theta}{\sqrt{n}}\]
\[\theta - \overline{X} \leq \frac{z_{\beta} \theta}{\sqrt{n}} \\ \theta - \overline{X} \geq -\frac{z_{\beta} \theta}{\sqrt{n}}\]
\[\overline{X} \geq \theta \left( \frac{\sqrt{n} - z_{\beta}}{\sqrt{n}} \right) \\ \overline{X} \leq \theta \left( \frac{\sqrt{n} + z_{\beta}}{\sqrt{n}} \right)\]
\[\overline{X} \geq \theta \left(1 - \frac{z_{\beta}}{\sqrt{n}}\right) \\ \overline{X} \leq \theta \left(1 + \frac{z_{\beta}}{\sqrt{n}}\right)\]
\[\theta \leq \frac{\overline{X} \sqrt{n}}{\sqrt{n} - z_{\beta}} \\ \theta \geq \frac{\overline{X} \sqrt{n}}{\sqrt{n} + z_{\beta}}\] \[P\left\{ \left| \theta - \overline{X} \right| \leq \frac{z_{\beta} \theta}{\sqrt{n}} \right\} = 2\beta - 1\]
\[P\left\{ \frac{\overline{X} \sqrt{n}}{\sqrt{n} + z_{\beta}} \leq \theta \leq \frac{\overline{X} \sqrt{n}}{\sqrt{n} - z_{\beta}} \right\} = 2\beta - 1\] \[2\beta - 1 = 1 - \alpha \\\] \[\beta = \frac{2 - \alpha}{2}\]
for (alpha in c(.01, .05, .1)){
cat("For confidence level", 1-alpha, "\n", sep=" ")
# Calculate beta
beta <- (2 - alpha)/2
# Determine the z-score for the beta quantile
z_beta <- qnorm(beta)
# Calculate the lower bound of the confidence interval
lower_bound3_3 <- sample_mean3 * sqrt(N)/(sqrt(N) + z_beta)
# Calculate the upper bound of the confidence interval
upper_bound3_3 <- sample_mean3 * sqrt(N)/(sqrt(N) - z_beta)
cat(" the fraction of CI's containing the parameter is",
mean((lower_bound3_3 <= theta) & (theta <= upper_bound3_3)), "\n", sep=" ")
cat(" the CI length is", mean(upper_bound3_3 - lower_bound3_3), "\n", sep=" ")
cat("\n")
}
## For confidence level 0.99
## the fraction of CI's containing the parameter is 0.99
## the CI length is 0.01101381
##
## For confidence level 0.95
## the fraction of CI's containing the parameter is 0.961
## the CI length is 0.008137019
##
## For confidence level 0.9
## the fraction of CI's containing the parameter is 0.909
## the CI length is 0.006749076
\(S\) - sample standard error
\(S^2 = \frac{\sum(X_k - \overline X)^2}{n-1} \Longrightarrow S =\sqrt\frac{\sum(X_k - \overline X)^2}{n-1}\)
But in our case we calculate \(S\) from realization.
\(X \sim N(\theta, \frac{\theta^2}{n})\) for large \(n\)
Let \(T = \frac {\sqrt {n}(\overline X - \theta)}{S}\)
\(T\) has Student distribution with \(n - 1\) degrees of freedom
\[P\{|T| \leq t^{(n-1)}_{1-\frac{\alpha}{2}}\} = P\{t^{(n-1)}_{\frac{\alpha}{2}} \leq \frac{\sqrt n(\overline X - \theta)}{S} \leq t^{(n-1)}_{1-\frac{\alpha}{2}}\} = 1 - \alpha \]
Thus, \(P\{\overline X - \frac{S}{\sqrt n}t^{(n-1)}_{1- \frac{\alpha}{2}} \leq \theta \leq X - \frac{S}{\sqrt n}t^{(n-1)}_{\frac{\alpha}{2}}\} = 1 - \alpha\)
for (alpha in c(.01, .05, .1)){
cat("For confidence level", 1-alpha, "\n", sep=" ")
# Calculate beta
beta <- (2 - alpha)/2
# Determine the z-score corresponding to beta
z_beta <- qnorm(beta)
# Calculate the t-score from the t-distribution for (1 - alpha/2) quantile with (N-1) degrees of freedom
t <- qt(1-alpha/2, N-1)
# Calculate the lower bound of the confidence interval
lower_bound3_4 <- sample_mean3 - sample_sd3/ sqrt(N) * t
# Calculate the upper bound of the confidence interval
upper_bound3_4 <- sample_mean3 + sample_sd3/ sqrt(N) * t
cat(" the fraction of CI's containing the parameter is",
mean((lower_bound3_4 <= theta) & (theta <= upper_bound3_4)), "\n", sep=" ")
cat(" the CI length is", mean(upper_bound3_4 - lower_bound3_4), "\n", sep=" ")
cat("\n")
}
## For confidence level 0.99
## the fraction of CI's containing the parameter is 0.987
## the CI length is 0.0104112
##
## For confidence level 0.95
## the fraction of CI's containing the parameter is 0.932
## the CI length is 0.007865533
##
## For confidence level 0.9
## the fraction of CI's containing the parameter is 0.893
## the CI length is 0.006581872
Repeat parts (2)–(4) of Problem 3 (with corresponding amendments) for a Poisson distribution \(P(θ)\)
\(X_1, X_2, \ldots \sim \text{Poisson}(\theta)\\\) \(X_1, X_2, \ldots \text{ - i.i.d. r.v.}\)
# Generate a matrix of random samples from a Poisson distribution
x4 <- matrix(rpois(N*M, theta), nrow = N)
# Calculate the mean of each column in the matrix
sample_mean4 <- colMeans(x4)
# Calculate the standard deviation of each column in the matrix
sample_sd4 <- apply(x4, 2, sd)
\[\text{Let's normalize } \overline{X}\] \[P\left\{ |\theta - \overline{X}| \leq \varepsilon \right\} = P\left\{ \frac{|\theta - \overline{X}| \sqrt{n}}{\sqrt{\theta}} \leq \frac{\varepsilon \sqrt{n}}{\sqrt{\theta}} \right\} = P\left\{ |Z| \leq \frac{\varepsilon \sqrt{n}}{\sqrt{\theta}} \right\}\] \[\text{Let } z_{\beta} = \frac{\varepsilon \sqrt{n}}{\sqrt{\theta}}\] \[\varepsilon = z_{\beta} \frac{\sqrt{\theta}}{\sqrt{n}}\] \[P\left\{ |\theta - \overline{X}| \leq z_{\beta} \frac{\sqrt{\theta}}{\sqrt{n}} \right\} = P\left\{ \overline{X} - z_{\beta} \frac{\sqrt{\theta}}{\sqrt{n}} \leq \theta \leq \overline{X} + z_{\beta} \frac{\sqrt{\theta}}{\sqrt{n}} \right\}\] \[\text{Hence, } P\left\{ |Z| \leq z_{\beta} \right\} = 2 \beta - 1 = 0.95\] \[z_{\beta} = 1.96\]
for (alpha in c(.01, .05, .1)){
cat("For confidence level", 1-alpha, "\n", sep=" ")
# Calculate beta
beta <- (2 - alpha)/2
# Determine the z-score corresponding to beta
z_beta <- qnorm(beta)
# Calculate the lower bound of the confidence interval
lower_bound4_1 <- sample_mean4 - z_beta*sqrt(theta)/sqrt(N)
# Calculate the upper bound of the confidence interval
upper_bound4_1 <- sample_mean4 + z_beta*sqrt(theta)/sqrt(N)
cat(" the fraction of CI's containing the parameter is",
mean((lower_bound4_1 <= theta) & (theta <= upper_bound4_1)), "\n", sep=" ")
cat(" the CI length is", mean(upper_bound4_1 - lower_bound4_1), "\n", sep=" ")
cat("\n")
}
## For confidence level 0.99
## the fraction of CI's containing the parameter is 0.985
## the CI length is 0.07285545
##
## For confidence level 0.95
## the fraction of CI's containing the parameter is 0.941
## the CI length is 0.05543615
##
## For confidence level 0.9
## the fraction of CI's containing the parameter is 0.941
## the CI length is 0.04652349
\(|\theta - \bar{X}| \leq \frac{z_{\beta} \sqrt{\theta}}{\sqrt{n}}\\\) \(\theta - \bar{X} \leq \frac{z_{\beta} \sqrt{\theta}}{\sqrt{n}}\\\) \(\theta - \bar{X} \geq -\frac{z_{\beta} \sqrt{\theta}}{\sqrt{n}\\}\) \(\sqrt{n}\theta - z_{\beta} \sqrt{\theta} - \sqrt{n}\bar{X} \leq 0\\\) \(\sqrt{n}\theta + z_{\beta} \sqrt{\theta} - \sqrt{n}\bar{X} \geq 0\\\) \(\frac{\left(-z_{\beta} + \sqrt{z_{\beta}^2 + 4n\bar{X}}\right)^2}{4n} \leq \theta \leq \frac{\left(z_{\beta} + \sqrt{z_{\beta}^2 + 4n\bar{X}}\right)^2}{4n}\\\)
for (alpha in c(.01, .05, .1)){
cat("For confidence level", 1-alpha, "\n", sep=" ")
# Calculate beta
beta <- (2 - alpha)/2
# Determine the z-score corresponding to beta
z_beta <- qnorm(beta)
# Calculate the discriminant of the quadratic equation used to find the CI bounds
discr <- z_beta^2 + 4*N*sample_mean4
# Calculate the lower bound of the confidence interval
lower_bound4_2 <- (sqrt(discr) - z_beta)^2/(4*N)
# Calculate the upper bound of the confidence interval
upper_bound4_2 <- (sqrt(discr) + z_beta)^2/(4*N)
cat(" the fraction of CI's containing the parameter is",
mean((lower_bound4_2 <= theta) & (theta <= upper_bound4_2)), "\n", sep=" ")
cat(" the CI length is", mean(upper_bound4_2 - lower_bound4_2), "\n", sep=" ")
cat("\n")
}
## For confidence level 0.99
## the fraction of CI's containing the parameter is 0.985
## the CI length is 0.09666112
##
## For confidence level 0.95
## the fraction of CI's containing the parameter is 0.941
## the CI length is 0.06543007
##
## For confidence level 0.9
## the fraction of CI's containing the parameter is 0.941
## the CI length is 0.05179963
\(S\) - sample standard error
\(S^2 = \frac{\sum(X_k - \overline X)^2}{n-1} \Longrightarrow S =\sqrt\frac{\sum(X_k - \overline X)^2}{n-1}\)
But in our case we calculate \(S\) from realization.
\(X \sim N(\theta, \frac{\theta^2}{n})\) for large \(n\)
Let \(T = \frac {\sqrt {n}(\overline X - \theta)}{S}\)
\(T\) has Student distribution with \(n - 1\) degrees of freedom
\[P\{|T| \leq t^{(n-1)}_{1-\frac{\alpha}{2}}\} = P\{t^{(n-1)}_{\frac{\alpha}{2}} \leq \frac{\sqrt n(\overline X - \theta)}{S} \leq t^{(n-1)}_{1-\frac{\alpha}{2}}\} = 1 - \alpha \]
Thus, \(P\{\overline X - \frac{S}{\sqrt n}t^{(n-1)}_{1- \frac{\alpha}{2}} \leq \theta \leq X - \frac{S}{\sqrt n}t^{(n-1)}_{\frac{\alpha}{2}}\} = 1 - \alpha\)
for (alpha in c(.01, .05, .1)){
cat("For confidence level", 1-alpha, "\n", sep=" ")
# Calculate beta
beta <- (2 - alpha)/2
# Determine the z-score corresponding to beta
z_beta <- qnorm(beta)
# Calculate the t-score from the t-distribution for (1 - alpha/2) quantile with (N-1) degrees of freedom
t <- qt(1-alpha/2, N-1)
# Calculate the lower bound of the confidence interval
lower_bound4_3 <- sample_mean4 - sample_sd4/ sqrt(N) * t
# Calculate the upper bound of the confidence interval
upper_bound4_3 <- sample_mean4 + sample_sd4/ sqrt(N) * t
cat(" the fraction of CI's containing the parameter is",
mean((lower_bound4_3 < theta) & (theta < upper_bound4_3)), "\n", sep=" ")
}
## For confidence level 0.99
## the fraction of CI's containing the parameter is 0.873
## For confidence level 0.95
## the fraction of CI's containing the parameter is 0.873
## For confidence level 0.9
## the fraction of CI's containing the parameter is 0.86
In Problems 3 and 4 we constructed confidence intervals by different methods:
Chi-Squared Distribution Method (only in Problem 3)
Normal Approximation Method
Adjusted Normal Approximation Method
Student’s t-Distribution Method