Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2986

How to group data then apply functions to the grouped data?

From Tyler <hayes.tyler@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject How to group data then apply functions to the grouped data?
Date 2011-06-07 10:45 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iskvh6$5o7$1@smc.vnet.net> (permalink)

Show all headers | View raw


Hi Everyone:

Below is a snippet of code to calculate the annualized returns for a
ten year set of data. Ultimately, I want to find the "ten-year
annualized returns." I know, cryptic. But, as I understand it, it is
to find the average annualized return over the last ten years. In
other words, find the annualized return for each year, then average
(if you think that's wrong, let me know!). So in my mind the challenge
before me is to work on the data in one-year chunks (one year defined
as March 31 to March 31) much the same way a SQL query would group the
data. So, the big question, how would any of you suggest I group
efficiently?

Any suggestions are greatly appreciated.

Cheers,

t.


(* TSX Composite Index *)
data = FinancialData["^GSPTSE", {{2001, 3, 31}, {2011, 3, 31}}];
px = data[[;; , 2]];
dates = data[[;; , 1]];
period = 252; (* number of trading days *)
(* Arithmetic Returns *)
aret = Differences[px]/Rest[px];
(* Log Returns *)
lnret = Differences[Log[px]];
(* Find Annualized Arithmetic Returns - must be a better way to code
this *)
annualizeAret =  ( Product[(1 + aret[[i]]), {i,
Length[aret]}])^(period/Length[aret]) - 1
(* Find Annualized Logarithmic Returns *)
annualizeLNret =  Mean[lnret]*(period)

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

How to group data then apply functions to the grouped data? Tyler <hayes.tyler@gmail.com> - 2011-06-07 10:45 +0000

csiph-web