Get Started

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”

View Course

Exercise instructions

  • Use cov() to calculate the sample covariance between DAX_logreturns and FTSE_logreturns.
  • Use another call to cov() to calculate the sample covariance matrix for logreturns.
  • Use cor() to calculate the sample correlation between DAX_logreturns and FTSE_logreturns.
  • Use another call to cor() to calculate the sample correlation matrix for logreturns.

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

Edit and Run Code

This exercise is part of the course

Time Series Analysis in R

IntermediateSkill Level
4.5+
8 reviews

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 correlations
Exercise 7: Guess the correlation coefficientExercise 8: AutocorrelationExercise 9: Calculating autocorrelationsExercise 10: The autocorrelation functionExercise 11: Visualizing the autocorrelation function

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free