Calculating sample covariances and correlations
Sample covariances measure the strength of the linear relationship between matched pairs of variables. The cov()
function can be used to calculate covariances for a pair of variables, or a covariance matrix when a matrix containing several variables is given as input. For the latter case, the matrix is symmetric with covariances between variables on the off-diagonal and variances of the variables along the diagonal. On the right you can see the scatterplot matrix of your logreturns
data.
Covariances are very important throughout finance, but they are not scale free and they can be difficult to directly interpret. Correlation is the standardized version of covariance that ranges in value from -1 to 1, where values close to 1 in magnitude indicate a strong linear relationship between pairs of variables. The cor()
function can be applied to both pairs of variables as well as a matrix containing several variables, and the output is interpreted analogously.
In this exercise, you'll use cov()
and cor()
to explore your logreturns
data.
This is a part of the course
“Time Series Analysis in R”
Exercise instructions
- Use
cov()
to calculate the sample covariance betweenDAX_logreturns
andFTSE_logreturns
. - Use another call to
cov()
to calculate the sample covariance matrix forlogreturns
. - Use
cor()
to calculate the sample correlation betweenDAX_logreturns
andFTSE_logreturns
. - Use another call to
cor()
to calculate the sample correlation matrix forlogreturns
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Use cov() with DAX_logreturns and FTSE_logreturns
cov(___, ___)
# Use cov() with logreturns
# Use cor() with DAX_logreturns and FTSE_logreturns
cor(___, ___)
# Use cor() with logreturns
This exercise is part of the course
Time Series Analysis in R
Learn the core techniques necessary to extract meaningful insights from time series data.
In this chapter, you will review the correlation coefficient, use it to compare two time series, and also apply it to compare a time series with its past, as an autocorrelation. You will discover the autocorrelation function (ACF) and practice estimating and visualizing autocorrelations for time series data.
Exercise 1: ScatterplotsExercise 2: Asset prices vs. asset returnsExercise 3: Characteristics of financial time seriesExercise 4: Plotting pairs of dataExercise 5: Covariance and correlationExercise 6: Calculating sample covariances and correlationsExercise 7: Guess the correlation coefficientExercise 8: AutocorrelationExercise 9: Calculating autocorrelationsExercise 10: The autocorrelation functionExercise 11: Visualizing the autocorrelation functionWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.