- Learning Quantitative Finance with R
- Dr. Param Jeet Prashant Vats
- 379字
- 2021-07-09 19:06:53
Correlation
Correlation plays a very important role in quant finance. It not only determines the relation between the financial attributes but also plays a crucial role in predicting the future of financial instruments. Correlation is the measure of linear relationship between the two financial attributes. Now let us try to compute the different types of correlation in R using Sampledata
, which is used in identifying the orders of components of predictive financial models.
Correlation can be computed by the following code. Let's first subset the data and then run the function for getting correlation:
x<-Sampledata[,2:5] rcorr(x, type="pearson")
This generates the following correlation matrix, which shows the measure of linear relationship between the various daily level prices of a stock:

Autocorrelation
Autocorrelation is the correlation of the series with its past or future values. It is also known as serial correlation and lagged correlation. It plays a critical role in time series prediction modeling. The function acf
computes estimates of the autocorrelation function.
The following code when executed gives the autocorrelation of the series with its lagged values:
acf(Sampledata$Volume)
The graph is as follows:
Figure 2.10: Plot showing autocorrelation of series with its lag
This gives the plot of autocorrelations of the series with its lagged values. There are other options in functions such as lag.max
, plot
, and so on.
Partial autocorrelation
Partial autocorrelation of a time series is the correlation with its own lagged values, controlling for the values of the time series at all shorter lags. It is also used in time series modeling for identifying the orders of the components of forecasting techniques. It is computed by using the following code:
pacf(Sampledata$Volume)
It also contains other options such as how many lags you want to use and plot. The preceding code gives the following plot:
Figure 2.11: Plot showing partial autocorrelation of series with its lag
Cross-correlation
Cross-correlation is a measure of the similarity of two series as a function of the displacement of one relative to the other. Just like acf
and pacf
, it also plays a crucial role in time series forecasting. It can be computed by using the following function:
ccf(Sampledata$Volume,Sampledata$High, main = "ccf plot")
When the preceding code gets executed, it generates the following plot:
Figure 2.12: Plot showing cross-correlation of two series