-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compare_two_method_for_CI_in_Uniform_distribution.qmd
142 lines (109 loc) · 3.89 KB
/
Compare_two_method_for_CI_in_Uniform_distribution.qmd
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---
title: "Foruzan JamaliNasab"
format: pdf
editor: visual
---
## First Method
$$
\begin{aligned}
& \text{if}~~ X_1, X_2, \dots, X_n \overset{iid}{\sim} \text{Unif}(0, \theta), \\
& \implies Y_i = \frac{X_i}{\theta} \sim \text{Unif}(0, 1) \implies -2\ln(Y_i) \sim \chi^2_{(2)}, \\
& -2\sum_{i = 1}^n \ln(Y_i) \sim \chi^2_{(2n)} \implies \\
& \mathcal{P}\left(\chi^2_{(2n,~\frac{\alpha}{2})} \leq -2\sum_{i = 1}^n \ln(Y_i)\leq \chi^2_{(2n, ~1-\frac{\alpha}{2})} \right) = 1 - \alpha \\
& \implies
\mathcal{P}\left(\chi^2_{(2n,~\frac{\alpha}{2})} \leq -2\sum_{i = 1}^n \ln\left(\frac{X_i}{\theta}\right)\leq \chi^2_{(2n, ~1-\frac{\alpha}{2})} \right) = 1 - \alpha \\
& \implies \mathcal{P}\left(\frac{\chi^2_{(2n,~\frac{\alpha}{2})} + 2\sum_{i = 1}^n \ln(X_i)}{2n} \leq \ln(\theta) \leq
\frac{\chi^2_{(2n,~1-\frac{\alpha}{2})} + 2\sum_{i = 1}^n \ln(X_i)}{2n} \right) = 1 - \alpha \\
& \implies \mathcal{P}\left[\exp\left( \frac{\chi^2_{(2n,~\frac{\alpha}{2})} + 2\sum_{i = 1}^n \ln(X_i)}{2n}\right)\leq \theta \leq \exp\left( \frac{\chi^2_{(2n,~1-\frac{\alpha}{2})} + 2\sum_{i = 1}^n \ln(X_i)}{2n} \right)\right] = 1 - \alpha
\end{aligned}
$$
## code in R for method I with theta = 1
```{r}
set.seed(1234)
get_conf_sim_uniform <- function(theta = 1, alpha = 0.05, nsamp = 25, R = 10000) {
Lower <- c()
Upper <- c()
temp1 <- qchisq(alpha/2, df = 2*nsamp)
temp2 <- qchisq(1 - alpha/2, df = 2 * nsamp)
count <- 0
repeat{
count = count + 1
U <- runif(n = nsamp, min = 0, max = theta)
sumlogx_2 <- 2 * sum(log(U))
a <- (temp1 + sumlogx_2) / (2 * nsamp)
b <- (temp2 + sumlogx_2) / (2 * nsamp)
res1 <- exp(a);
res2 <- exp(b)
Lower <- c(Lower, res1)
Upper <- c(Upper, res2)
if (count == R) break
}
Average_Length <- (Upper - Lower) |> mean()
mat_result <- cbind(Lower = Lower, Upper = Upper)
conf_res <- apply(mat_result, 1, function(x){
a <- x[1]; b <- x[2]
cond <- (a < theta & b > theta)
return(1 * cond)
})
simulate_conf <- mean(conf_res)
final_result <- c(theta = theta, Average_CI = Average_Length, Simulated_Conf = simulate_conf)
attr(final_result, "Method" ) <- "Method I"
return(final_result)
}
get_conf_sim_uniform()
```
***
***
$$
\begin{aligned}
& \text{if}~~ X_1, X_2, \dots, X_n \overset{iid}{\sim} \text{Unif}(0, \theta)\implies \\
& Y = \frac{X_{(n)}}{\theta} \sim \text{Beta}(n, 1) \implies \\
& \mathcal{P}(\text{Beta}(n, 1, \alpha/2) \leq Y \leq \text{Beta}(n, 1, 1-\alpha/2)) = 1-\alpha \\
& \mathcal{P}\left(\frac{X_{(n)}}{\text{Beta}(n, 1, 1-\alpha/2)} \leq \theta \leq \frac{X_{(n)}}{\text{Beta}(n, 1, \alpha/2)}\right) = 1-\alpha
\end{aligned}
$$
## Method II with theta = 1
```{r}
set.seed(1234)
get_conf_sim_uniform2 <- function(theta = 1, alpha = 0.05, nsamp = 25, R = 10000) {
Lower <- c()
Upper <- c()
temp1 <- qbeta(alpha/2, nsamp, 1)
temp2 <- qbeta(1 - alpha/2, nsamp, 1)
count <- 0
repeat{
count = count + 1
U <- runif(n = nsamp, min = 0, max = theta)
yy <- max(U)
a <- yy / temp2
b <- yy / temp1
Lower <- c(Lower, a)
Upper <- c(Upper, b)
if (count == R) break
}
Average_Length <- (Upper - Lower) |> mean()
mat_result <- cbind(Lower = Lower, Upper = Upper)
conf_res <- apply(mat_result, 1, function(x){
a <- x[1]; b <- x[2]
cond <- (a < theta & b > theta)
return(1 * cond)
})
simulate_conf <- mean(conf_res)
final_result <- c(theta = theta, Average_CI = Average_Length, Simulated_Conf = simulate_conf)
attr(final_result, "Method" ) <- "Method II"
return(final_result)
}
get_conf_sim_uniform2()
```
***
***
***
***
## Method I with theta = 3, nsample = 35
```{r}
get_conf_sim_uniform(theta = 3, nsamp = 35)
```
## Method II with theta = 3, nsample = 35
```{r}
get_conf_sim_uniform2(theta = 3, nsamp = 35)
```