Plug4
Plug4

Reputation: 3938

geometric mean in Matlab for a specific time interval

say that I have a matrix (NX2) where the first column's elements are the number of seconds (since midnight) and the second column is stock returns associated to associated to the seconds. How can I use the function geomean and compute the geometric returns for a specific time interval? Say the geomean returns at every minute interval? The problem is that I might have say 4 stock returns in some specific minute interval and sometime 10 returns in another interval. If I extract all the trades by minutes, the numbers of returns registered in each minute interval will not be the same.
Thank you!

Upvotes: 0

Views: 1320

Answers (2)

tmpearce
tmpearce

Reputation: 12693

You can do this with a simple arrayfun:

>> n = [15 1;25 2;67 3; 99 4;182 5] #% example data

n =
 #% t(s)   val  
    15     1
    25     2
    67     3
    99     4
   182     5

>> t1=0:60:240;
>> t2=t1+60;
>> arrayfun(@(t1,t2) geomean(n( n(:,1)>t1 & n(:,1)<=t2, 2)), t1,t2)

ans =

    1.4142    3.4641       NaN    5.0000       NaN

t1 and t2 are just vectors of the start and end times of your windows of interest. Here, I've made them be contiguous 60-second intervals; however they could be whatever you want. Non-contiguous, overlapping, you name it.

The anonymous function captures the matrix n as it exists when arrayfun is called, and creates a logical index into the rows of the matrix for each t1,t2 pair.

Upvotes: 1

user85109
user85109

Reputation:

As long as you have the time in minutes (since midnight) as an integer, you can use my consolidator function and geomean to compute what you need, and it is vectorized. Just download it from the file exchange.

Consolidator will return a vector of the times where it found any data in a given minute, and a geometric mean of returns over each of those times.

Upvotes: 1

Related Questions