forked from Macy-Schanbacher/DS202-class-Feb6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice.Rmd
152 lines (108 loc) · 2.51 KB
/
practice.Rmd
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
143
144
145
146
147
148
149
150
151
---
title: "Feb 6 in-class practice"
author: "Yumou"
date: "2023-02-06"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Question 1
Write a function to compute the Euclidean norm of a vector (square root of sum of squared elements in a vector).
Namely, for a vector $x = (x_1, x_2, \ldots, x_p)$, the Euclidean norm of $x$ is
$$|x|_2 = (x_1^2 + x_2^2 + \ldots + x_p^2)^{1/2}.$$
```{r}
EuclideanNorm = function(x){
res = sqrt(sum(x^2))
return(res)
}
EuclideanNorm(c(1 : 5))
EuclideanNorm(-10)
```
## Question 2
Write a loop function to calculate the euclidean norm of a function
```{r}
x = c(3:16)
EuclideanNorm(x)
d = 0
for (i in 1:length(x)) {
d = d + x[i]^2
cat("Iteration: ", i, ", x = ", x[i], "d = ", d)
}
sqrt(d)
```
## Question 3
Use loop to write he function for Euclidean Norm
```{r}
EuclideanNormLoop = function(x) {
d = 0
for (i in 1 : length(x)) {
d = d + x[i]^2
}
return(sqrt(d))
}
EuclideanNormLoop(c(-10:10))
```
Write a function for finding variance of data
```{r}
Var = function(x) {
d = 0
v = sum(x)
for (i in 1:length(x)) {
d = d + (x[i] - v)^2
}
ret = 1/(length(x) - 1) * d
return(ret)
}
Var(c(-10:10))
#R built-in
var(c(-10:10))
```
## Question 4
Write a function to calculate the smapel variances of a vector $x$
```{r}
MyVar = function(x) {
n = length(x)
xbar = sum(x) / n
x.centered = x - xbar
res = sum(x.centered^2) / (n - 1)
return(res)
}
MyVar(c(-5:20))
var(c(-5:20))
```
## Question 5 (simulation example)
Suppose we have data from a Normal distribution with mean $\mu = 5$ and variance
$\sigma^2 = 1$. We Want to estimate $\thea = \mu^2$ (true value is 25)
Suppose $X_1, \ldots, X_n$ are the data. Wh have two estimators
We have 2 estimators
1. $\hat{\theta}_1 = \bar{X}^2$ where $\bar{X} = \sum_{i = 1}^{n} X_i$
2. $\hat{\theta}_2 = \frac{1}{n(n-1)}\sum_{i \neq j} X_i X_j = \frac{1}{n(n-1)} \bigg\{ \bigg(\sum_{i=1}^{n} X_i \bigg)^2 - \sum{i = 1}^{n} X_i}$
```{r}
theta1 = function(x) {
res = mean(x)^2
return(res)
}
theta2 = function(x) {
n = length(x)
res = (sum(x)^2 - sum(x^2)) / (n * (n - 1))
return(res)
}
# Simulaiton:
# SUppose the smaple size if 20 (n = 20)
# Simulation from normal distribution with mean 5 and variance 1
# SImulate 1000 times
diff1 = c()
diff2 = c()
for (rep in 1 : 1000) {
z = rnorm(20, 5, 1)
diff1[rep] = theta1(z) - 25
diff2[rep] = theta2(z) - 25
}
hist(diff1)
hist(diff2)
MSE1 = sqrt(mean(diff1^2))
MSE2 = sqrt(mean(diff2^2))
MSE1
MSE2
```