0

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!

Amro
  • 123,847
  • 25
  • 243
  • 454
Plug4
  • 3,838
  • 9
  • 51
  • 79
  • possible duplicate of [MATLAB: compute mean of each 1-minute interval of a time-series](http://stackoverflow.com/questions/2323031/matlab-compute-mean-of-each-1-minute-interval-of-a-time-series) – Amro Jun 01 '12 at 08:54

2 Answers2

1

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.

  • Ya I have to take a closer look to this consolidator function. Otherwise I can do the following:data = [(1:130)', randi(100,130,1)]; out = accumarray(ceil(data(:,1)/60),data(:,2),[],@geomean,NaN); – Plug4 Jun 01 '12 at 19:20
1

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.

tmpearce
  • 12,523
  • 4
  • 42
  • 60
  • I should have been more clear though in my question: I have multiple days stack on over the other. For intance: all stock returns per second for Sept.02 and then Sept.03. I have to find out how not to accumulate geomean returns combining multiple days at the same time. – Plug4 Jun 01 '12 at 19:47
  • The most straightforward thing to do in that case is recalculate the time of the transactions so they all are referenced from a single start date. If you want to start the clock at 12:00AM Sept. 02, add 24*60*60 seconds to all the trades on Sept. 03, and so on. – tmpearce Jun 01 '12 at 20:16
  • that's what I already have for the seconds. Good! Would you know if there is a way for me to store beside the geomean vector answers from which minute and date the geomean was computed? THANKS – Plug4 Jun 01 '12 at 21:56
  • In the solution I've suggested above, the start time and end time for each of the elements in the `geomean` vector are given by `t1` and `t2`. So you don't actually have to do anything more... the information is there already. – tmpearce Jun 01 '12 at 23:28
  • thanks a lot! now I am also trying to learn it how to do it in SAS! – Plug4 Jun 02 '12 at 02:18