# Timeseries Analysis. Calculating Returns, Correlations between timeseries data and overlapping data

I have a set of sample time-series data below of monthly prices for two companies.

Q1. I want to calculate monthly and quarterly log returns.what is the most expedient way to do this? TimeSeriesAggregate[] only has the standard Mean, etc.

Q2. With the returns from Q1, what is the most expedient method to calculate the correlation of the monthly returns between the two companies?

Q3. How would it be possible to calculate six-monthly log returns and then create a series of overlapping $6m \log$ returns so I can derive $7\times 6M$ outcomes from the limited dataset below; i.e. [1m-6m, 2m-7m, 3m-8m, ...] (and then calculate a correlation between these)?

(data1 = {{Date, CompanyA, CompanyB}, {"16/01/2007", 3655,
1000}, {"16/02/2007", 3655, 1000}, {"16/03/2007", 3655,
1000}, {"16/04/2007", 3655, 1000}, {"16/05/2007", 3655,
1000}, {"16/06/2007", 3435, 1011}, {"16/07/2007", 3528,
1012}, {"16/08/2007", 3348, 1013}, {"16/09/2007", 3648,
1022}, {"16/10/2007", 3648, 1022}, {"16/11/2007", 3648,
1022}, {"16/12/2007", 3648, 1022}});
(data2 = MapAt[DateList[{#, {"Day", "Month", "Year"}}] &,
data1, {2 ;;, 1}]) // Grid

Thanks

• I suggest that show the equations that you would like to apply to the data. Jul 24 '16 at 15:02
• Please, define "log returns". Also, it would be better if you provide larger data. Jul 24 '16 at 15:48

The code below was written with the assumption that you want to keep your data within TimeSeries objects. (Since you mention "time-series data" and TimeSeriesAggregate.) That is not necessary with the regular grid of dates in the data you provided; you could use simple lists of numbers instead.

## Q1

You can redefine LogReturn accordingly.

Clear[LogReturn]
LogReturn[{oldValue_, newValue_}] := LogReturn[oldValue, newValue];
LogReturn[oldValue_, newValue_] := Log[newValue/oldValue]

{rA, rB} =
Table[
TimeSeries[
Map[LogReturn, Partition[ts["Values"], 2, 1]],
{Mean /@ Partition[ts["Times"], 2, 1]}
], {ts, {tsA, tsB}}]

DateListPlot[{rA, rB}, PlotRange -> All]

## Q2

N@Correlation[rA["Values"], rB["Values"]]
(* 0.0152059 *)

(Of course, other correlation functions can be applied.)

## Q3

The following code is the same as in Q1 and Q2, just the partitioning is different using 6 consecutive records (i.e. months.)

{rA, rB} =
Table[
TimeSeries[
Map[LogReturn[#[[1]], #[[-1]]] &, Partition[ts["Values"], 6, 1]],
{Mean /@ Partition[ts["Times"], 6, 1]}
], {ts, {tsA, tsB}}]

N@Correlation[rA["Values"], rB["Values"]]
(* 0.0152059 *)

DateListPlot[{rA, rB}, PlotRange -> All]

Q1. TimeSeriesAggregate works with any function, it maps over non-overlaping windows.

TimeSeriesAggregate[ts,dt,YourFunction]

Q3. Use MovingMap to map over overlapping windows.