Skip to content

Instantly share code, notes, and snippets.

@Kobzol
Last active March 8, 2017 13:52
Show Gist options
  • Select an option

  • Save Kobzol/74083474afc5c2415186fb7ec3822a0e to your computer and use it in GitHub Desktop.

Select an option

Save Kobzol/74083474afc5c2415186fb7ec3822a0e to your computer and use it in GitHub Desktop.
str_hodnota = function(v, p)
{
sum(v * p);
}
rozptyl = function(v, p)
{
str_hodnota(v ** 2, p) - str_hodnota(v, p) ** 2
}
var_koeficient = function(v, p)
{
sqrt(rozptyl(v, p)) / abs(str_hodnota(v, p))
}
kovariance = function(x, y, p_x, p_y, pmat)
{
ex = str_hodnota(x, p_x)
ey = str_hodnota(y, p_y)
size = dim(pmat)
eproduct = 0
for (i in seq(size[1]))
{
eproduct = eproduct + sum(x[i] * y * pmat[i,])
}
eproduct - ex * ey
}
# 1.a
0.1
# 1.b
0.15
# 1.c
0.4
# 1.d
0.2
# 1.e
15/45
# 1.f
x = c(-1,0,1)
p_x = c(0.3,0.25,0.35)
y = c(1,2,3)
p_y = c(0.3,0.25,0.45)
# 1.g
0.2
# 1.h
ex = str_hodnota(x, p_x)
dx = rozptyl(x, p_y)
ox = sqrt(dx)
# 1.i
ey = str_hodnota(y, p_y)
dy = rozptyl(y, p_y)
oy = sqrt(dy)
# 1.j
pmat = matrix(c(
0.15,0.05,0.1,
0.1,0.1,0.15,
0.05,0.1,0.2
), nrow=3, ncol=3, byrow=TRUE)
cov = kovariance(x, y, p_x, p_y, pmat)
kor = cov / sqrt(dx * dy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment