-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBivariate 2D with Vertical Gauss
More file actions
67 lines (56 loc) · 1.73 KB
/
Bivariate 2D with Vertical Gauss
File metadata and controls
67 lines (56 loc) · 1.73 KB
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
# Reference https://onlinecourses.science.psu.edu/stat414/node/118
require(MASS)
set.seed(0)
mu_x = 0
mu_y = 0
sigma_2_x = 0.25
sigma_2_y = 0.25
cov = 0.20 # covariance
rho = cov/sqrt(sigma_2_x * sigma_2_y)
m = mvrnorm(1e4, c(mu_x,mu_y), matrix(c(sigma_2_x, cov, cov, sigma_2_y), 2,2))
plot(m[,1], m[,2], pch=19, cex = .2, col=rgb(.9,.3,.2, .2),
xlab = "X", ylab="Y", main="Bivariate normal")
x = seq(min(m[,1]), max(m[,2]), 0.001)
mean_Y_given_x = mu_y + rho *
sqrt(sigma_2_y/sigma_2_x) * (x - mu_x)
lines(x, mean_Y_given_x)
abline(h=0)
abline(v=0)
sigma_2_Y_given_x = sigma_2_y * (1 - rho^2)
modl = lm(m[,2] ~ m[,1])
x = 1
mean_Y_given_x = mu_y + rho *
sqrt(sigma_2_y/sigma_2_x) * (x - mu_x)
c = .5
df1 <- data.frame(yval = seq(
from = -1.5 + mean_Y_given_x,
to = 1.5 + mean_Y_given_x,
by = 0.1),
xval = (dnorm(seq(from = -1.5, to = 1.5, by = 0.1), 0, sqrt(sigma_2_Y_given_x))*c))
with(df1, lines(xval + x, yval, lwd = 1, col="darkblue"))
abline(v = x)
abline(h = mean_Y_given_x)
x = 0
mean_Y_given_x = mu_y + rho *
sqrt(sigma_2_y/sigma_2_x) * (x - mu_x)
c = .5
df1 <- data.frame(yval = seq(
from = -1.5 + mean_Y_given_x,
to = 1.5 + mean_Y_given_x,
by = 0.1),
xval = (dnorm(seq(from = -1.5, to = 1.5, by = 0.1), 0, sqrt(sigma_2_Y_given_x))*c))
with(df1, lines(xval + x, yval, lwd = 1, col="darkblue"))
abline(v = x)
abline(h = mean_Y_given_x)
x = -1
mean_Y_given_x = mu_y + rho *
sqrt(sigma_2_y/sigma_2_x) * (x - mu_x)
c = .5
df1 <- data.frame(yval = seq(
from = -1.5 + mean_Y_given_x,
to = 1.5 + mean_Y_given_x,
by = 0.1),
xval = (dnorm(seq(from = -1.5, to = 1.5, by = 0.1), 0, sqrt(sigma_2_Y_given_x))*c))
with(df1, lines(xval + x, yval, lwd = 1, col="darkblue"))
abline(v = x)
abline(h = mean_Y_given_x)