Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Statistics / hw2.r
diff options
context:
space:
mode:
1 files changed, 21 insertions, 38 deletions
-rw-r--r--Year 3/Statistics/hw2.r59
diff --git a/Year 3/Statistics/hw2.r b/Year 3/Statistics/hw2.r
index bc207bf..03daba8 100644
--- a/Year 3/Statistics/hw2.r
+++ b/Year 3/Statistics/hw2.r
@@ -1,57 +1,40 @@
-alpha <- 0.9 # TODO, каква стойност трябва да е
-
-ex1 <- function() {
+ex1 <- function(ns = 1:6 * 10) {
check_null_hyp <- function(n, iters = 10000) {
- accepts <- 0
- for (i in 1:iters) {
+ accepts <- sum(sapply(1:iters, function(i) {
W1 <- rexp(n, 1)
W2 <- rexp(n, 1)
- X <- rowSums(expand.grid(W1, W2))
- if (shapiro.test(X)$statistic >= alpha) { # TODO, провери дали е >
- accepts <- accepts + 1
- }
- }
- cat("Заключението е вярно през", (accepts / ex1_iters) * 100, "процента от времето\n")
- return (rep(n, each=((accepts / ex1_iters) * 100)))
+ X <- W1 + W2
+ return (shapiro.test(X)$p.value > 0.05)
+ }))
+ return (accepts / iters)
}
+
+ ys <- sapply(ns, check_null_hyp)
- all <- c()
- for (n in 1:6 * 10) {
- all <- append(all, check_null_hyp(n))
- }
- hist(all, breaks=(0:6 * 10))
+ plot(ns, ys, type="l", xlab="n", ylab="Съотношение на вярност на Шапиро-Уилк")
+ text(ns + 1, ys + 0.01, ys)
}
ex1()
-ex2 <- function(greens = 2, whites = 3, yellows = 5) {
+ex2 <- function(greens = 2, whites = 3, yellows = 5, ns = c(50, 100, 200, 500)) {
balls <- append(append(rep("green", greens), rep("white", whites)), rep("yellow", yellows))
check_hyp <- function(n, iters=10000) {
- true_hyp <- 0
- for (i in 1:iters) {
- sampled_greens <- 0
+ accepts <- sum(sapply(1:iters, function(i) {
+ sampled_greens <- sum(sapply(1:n, function(s) {
+ return (sample(balls, 1, replace=TRUE) == "green")
+ }))
- for (s in 1:n) {
- if (sample(balls, 1, replace=TRUE) == "green") {
- sampled_greens <- sampled_greens + 1
- }
- }
-
- if (sampled_greens / n == 1/5) {
- true_hyp <- true_hyp + 1
- }
- }
+ return (prop.test(sampled_greens, n, 1/5)$p.value > 0.05)
+ }))
- return (rep(n, each=(true_hyp / iters * 100)))
+ return (accepts / iters)
}
- all <- c()
- for (n in c(50, 100, 200, 500)) {
- all <- append(all, check_hyp(n))
- }
- print(all)
- hist(all, breaks=c(0, 50, 100, 200, 500))
+ ys <- sapply(ns, check_hyp)
+ plot(ns, ys, type="l", xlab="n", ylab="Съотношение на вярност на заключението")
+ text(ns, ys, ys)
}
ex2() \ No newline at end of file