Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Statistics / hw2.r
blob: bc207bf34c51e66b9bc879216a23c39be60c3625 (plain) (blame)
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
alpha <- 0.9 # TODO, каква стойност трябва да е

ex1 <- function() {
  check_null_hyp <- function(n, iters = 10000) {
    accepts <- 0
    for (i in 1:iters) {
      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)))
  }
  
  all <- c()
  for (n in 1:6 * 10) {
    all <- append(all, check_null_hyp(n))
  }
  hist(all, breaks=(0:6 * 10))
}

ex1()

ex2 <- function(greens = 2, whites = 3, yellows = 5) {
  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
      
      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 (rep(n, each=(true_hyp / iters * 100)))
  }
  
  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))
}

ex2()