Kursinhalt
Grundlagen der Wahrscheinlichkeitstheorie
Grundlagen der Wahrscheinlichkeitstheorie
Was Ist Kovarianz?
Kovarianz ist ein numerisches Maß, das die Beziehung zwischen zwei Variablen quantifiziert.
Es misst, wie Änderungen in einer Variablen mit Änderungen in einer anderen Variablen einhergehen. Genauer gesagt, misst die Kovarianz die gemeinsame Variabilität zweier Variablen und liefert Einblicke in die Richtung (positiv oder negativ) dieser Variabilität.
Berechnung der Kovarianz
- Führe das erste stochastische Experiment mehrmals durch und notiere die Ergebnisse jedes Experiments in einem Array. Dies wird ein
x
-Array sein; - Führe das zweite stochastische Experiment mehrmals durch und notiere die Ergebnisse in dem
y
-Array; - Berechne die Kovarianz mit der
numpy
-Bibliothek:covariance = np.cov(x, y)[0, 1]
.
Beispiele
import numpy as np import matplotlib.pyplot as plt # Assume that results of some stochastic experiments are stored in x array x = np.random.rand(100) * 10 # We provide another stochastic experiment by using the value of x and adding some noise y = x + np.random.randn(100) # Calculate the covariance covariance = np.cov(x, y)[0, 1] plt.scatter(x, y) # Add labels and title plt.xlabel('X') plt.ylabel('Y') plt.title('Covariance is '+ str(round(covariance, 3) )) # Show the plot plt.show()
Wir sehen, dass mit steigendem x
-Wert auch der y
-Wert zunimmt. Daher ist die Korrelation positiv. Lassen Sie uns ein weiteres Experiment durchführen:
import numpy as np import matplotlib.pyplot as plt # Assume that resylts of some stohastic experiments are stored in x array x = np.random.rand(100) * 10 # We provide another stohastic experiment by using the value of -x and adding some noise y = -x + np.random.randn(100) # Calculate the covariance covariance = np.cov(x, y)[0, 1] plt.scatter(x, y) # Add labels and title plt.xlabel('X') plt.ylabel('Y') plt.title('Covariance is '+ str(round(covariance, 3) )) # Show the plot plt.show()
Nun, während der x
-Wert zunimmt, sinkt der y
-Wert und die Kovarianz ist negativ. Schauen wir uns nun die Kovariation zwischen den Ergebnissen von zwei unabhängigen Experimenten an:
import numpy as np import matplotlib.pyplot as plt # Generate random data for two variables with zero correlation np.random.seed(0) x = np.random.rand(200) y = np.random.rand(200) # Calculate the covariance covariance = np.cov(x, y)[0, 1] plt.scatter(x, y) # Add labels and title plt.xlabel('X') plt.ylabel('Y') plt.title('Covariance is '+ str(round(covariance, 3) )) # Show the plot plt.show()
Folglich können wir die folgende Schlussfolgerung ziehen:
- Wenn die Kovarianz zwischen zwei Werten positiv ist, steigt auch der zweite Wert, wenn der erste Wert zunimmt;
- Wenn die Kovarianz zwischen zwei Werten negativ ist, sinkt der zweite Wert, wenn der erste Wert zunimmt;
- Sind die Werte unabhängig, dann ist ihre Korrelation null (sie sind unkorreliert).
Beachte den letzten Punkt: Die Korrelation ist null, wenn die Werte unabhängig sind. Aber das Umgekehrte gilt nicht: nur weil die Korrelation null ist, bedeutet das nicht, dass sie unabhängig sind. Schau dir das Beispiel an:
import numpy as np import matplotlib.pyplot as plt # Set the number of vectors/points to generate num_points = 1000 # Generate random angles uniformly distributed between 0 and 2*pi angles = np.random.uniform(0, 2*np.pi, num_points) # Convert angles to vectors in polar coordinates r = np.sqrt(np.random.uniform(0, 1, num_points)) # Square root to achieve uniform distribution within the circle x = r * np.cos(angles) y = r * np.sin(angles) # Calculate the covariance covariance = np.cov(x, y)[0, 1] plt.scatter(x, y) # Add labels and title plt.xlabel('X') plt.ylabel('Y') plt.title('Covariance is '+ str(round(covariance, 3) )) # Show the plot plt.show()
Die Punkte im obigen Beispiel liegen innerhalb des Einheitskreises und sind daher abhängig, aber unkorreliert.
Im Allgemeinen können nur lineare Zusammenhänge zwischen Werten mithilfe der Kovarianz gut identifiziert werden. Daher können wir im Fall unkorrelierter Werte schließen, dass sie keine linearen Abhängigkeiten aufweisen, jedoch möglicherweise andere, komplexere Arten von Abhängigkeiten haben.
Danke für Ihr Feedback!