TU Wien:Statistik und Wahrscheinlichkeitstheorie UE (Levajkovic)/Übungen 2023W/HW12.4

Aus VoWi
Zur Navigation springen Zur Suche springen
Correlation 4

Let and .

(a) Generate 200 realizations of the random variable and plot all against .
(b) Compute the empirical correlation r of the realizetions and .
(c) Would you say that a lack of correlation implies independence? What does the correlation measure? Would you say that the line is meaningful? Justify your answers.
Dieses Beispiel ist als solved markiert. Ist dies falsch oder ungenau? Aktualisiere den Lösungsstatus (Details: Vorlage:Beispiel)


Lösungsvorschlag von Lessi[Bearbeiten | Quelltext bearbeiten]

--Lessi 2024-02-07T13:04:11Z

# Let X ∼ N(2, 1) and Y = (X − 2)^2

# a) Generate 200 realizations (x_i)_{i=1,...,200} of the random variable X and plot all y_i = (x_i − 2)^2 against x_i, i = 1, . . . , 200.

rm(list = ls())

n <- 200
x <- rnorm(n, 2, 1)
y <- (x - 2)^2

plot(x, y, main="Corr of X and Y")

# b) Compute the empirical correlation r of the realizetions (x_i)_i and (y_i)_i

mean_x <- mean(x)
mean_y <- mean(y)
s_x <- sd(x)
s_y <- sd(y)

r <- (1 / (n - 1)) * sum((x - mean_x) * (y - mean_y)) / (s_x * s_y)

# c) Would you say that a lack of correlation implies independence? 
# What does the correlation measure? 
# Would you say that the line is meaningful? Justify your answers.

b_1 <- r * s_y / s_x
b_0 <- mean_y - b_1 * mean_x
abline(b_0, b_1)

# Not necessarily, it just measures how "strong" the linear relationship between the RVs is
# in this case the linear approximation is not usable but the variables may be quadratically correlated