-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcercise_03_week_03.R
125 lines (117 loc) · 2.29 KB
/
Excercise_03_week_03.R
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
# exercise 1.1
n <- 10000
v <- rnorm(n,0,1) ; v
# exercise 1.2
sum(v[i] < -3 | v[i] > 3)
# using for loop
v
n <- length(v)
r <- 0
for(i in 1:n){
if ((v[i] < -3) | (v[i] > 3)){
r <- r + 1
}
}
print(r)
# exercise 1.3
res_per <- r/length(v)
res_per # yes it does conform to expectations , alternatively
########################
m <- mean(v) ; s <- sd(v) ; m ; s
m + 3*s ; m - 3*s
plot(density(v))
sum(v > (m + 3*s))/10000
sum(v < (m - 3*s))/10000
########################
# exercise 1.4
# using the match function
match((v[i] < -3) | (v[i] > 3),v)
which((v[i] < -3) | (v[i] > 3))
# using for loop
# let us return the indices of elements corresponding to x[i] > 3
# without
p1 <- which((v[i] < -3) | (v[i] > 3)) ; p1
# with for loop
p2 <- c() # this is of unknown size but empty
j <- 0
for (i in 1:length(v)){
if ((v[i] < -3) | (v[i] > 3)){
j <- j+1
p2[j] <- i
}
}
print(v[p2]) ; print(p2)
min(v[p2]) ; max(v[p2])
# exercise 1.6
n <- 10000
v <- rnorm(n,0,1) ; v
b <- 1
while (max(abs(v)) > 5){
v <- rnorm(n,0,1)
b <- b + 1
}
b
# exercise 2.1
n <- 1 # it says a number u hence n <- 1
u <- runif(n,0,1) ; u # it is more than 0.1
# exercise 2.2
n <- 50
U <- runif(n,0,1) ; U ####### this is the same code used for the standard uniform
U[3]
# exercise 2.3
n <- 50
U <- runif(n,0,1);U
k <- 1
for (i in seq_along(U)){
if ( U[i] < 0.1 ){
k <- k + 1
}
}
k
U[k]
# exercise 2.4
n <- 50
U <- runif(n,0,1)
for (i in seq_along(U)){
if ( U[i] < 0.1 ){
break
}
}
print(U[i]);print(i) # hence it's position or round is 14 and value = 0.035586
# exercise 2.6
j <- 1
while (U[j] < 0.1){
if (U[j] < 0.1){
break
}
}
print(U[j]);print(j) # now they provide the same results
# exercise 2.5.a
U <- runif(1) # I need only one copy of such R.V.
j <- 1
while (U >= 0.1){
U <- runif(1)
j <- j + 1
}
# exercise 2.5.b
j <- 1
U <- runif(1)
while (U[j] < 0.1){
j <- j + 1
print(j)
}
j
# exercise 2.7 contrary to situation of exercise 2.6
n <- 50
U <- runif(n,0,1)
j <- 1 # serves as an index to access the elements
i <- 0 # serves as a counter of number of iterations
while (U[j] >= 10^-5){
i <- i+1
j <- j+1
if (i == 50) {
cat("I'm out")
break
}
}
i;j