Geometric Annual Return In SQL

Finance, SQL Add comments

Here is some quick-and-dirty SQL to calculate an geometric annual return (as a percent) from a column of monthly returns (in percents).

  1. /* Convert the annualized number back to a percent */
  2. SELECT (T3.AnnHPR - 1) * 100 AS GeomAnnRet
  3. FROM
  4.   (
  5.   /* Annualize the holding period return */
  6.   SELECT POWER(T2.HPR, 12.0 / T2.NumReturns) AS AnnHPR
  7.   FROM
  8.     (
  9.     /* Calculate the holding period return over the time
  10.         period.
  11.    
  12.        POWER(10, SUM(LOG10(n))) is a simulated PRODUCT(n)
  13.        aggregate function.
  14.    
  15.        The precision of POWER is determined by the precision
  16.        of the first argument, so use a lot of decimals. */
  17.     SELECT POWER(10.0000000000000000,
  18.                  SUM(LOG10(T.MonthReturn))) AS HPR,
  19.            COUNT(*) AS NumReturns
  20.     FROM
  21.       (
  22.       /* Convert all percent returns to multipliers (1% ->
  23.          1.01) */
  24.       SELECT 1 + MonthPctReturn / 100 AS MonthReturn
  25.       FROM
  26.       ) AS T
  27.     ) AS T2
  28.   ) AS T3</code>

Update 2008-01-30 10:52PM: Here’s the equivalent “one-liner”:

  1. SELECT 100 * (POWER(POWER(10.000000000000000,
  2.                           SUM(LOG10(1 + MonthPctReturn / 100))),
  3.                     12.0 / COUNT(*)) - 1)
  4. FROM

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in