[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
Kit Baum <BAUM@BC.EDU> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
st: precision problems with Mata? naaaahh... |

Date |
Wed, 17 Aug 2005 11:05:10 -0400 |

Here's a Mata solution (no doubt hideously inefficient, but I can't find all the fns. I'm looking for) that appears to be just as precise as a -generate, sum- at doing a running sum. It could readily operate on a column vector instead of a Stata variable if that is desired.

Note that a running sum can be calculated by premultiplying by a square matrix with 1's in the lower triangle, 0's above...

clear

set obs 1000

g double x = (uniform()-0.5)*10^(int(_n/10))

g sort = uniform()

sort sort

drop sort

g double sum = sum(x)

mata:

void function runsum(string scalar vname)

{

real matrix L

real matrix runsum

real scalar resindex

real scalar vnew

vnew = vname+"_sum"

st_view(A,.,vname)

// if(cols(A)>1) A=A'

L = J(rows(A),rows(A),0)

for (i=1; i<=rows(A); i++) {

for(j=1; j<=i; j++) {

L[i,j] = 1

}

}

runsum = L*A

resindex = st_addvar("double",vnew)

st_store((1,rows(runsum)),resindex,runsum)

}

end

mata runsum("x")

g double discrep = sum - x_sum

su

l, sep(0)

Kit Baum, Boston College Economics

http://ideas.repec.org/e/pba1.html

*

* For searches and help try:

* http://www.stata.com/support/faqs/res/findit.html

* http://www.stata.com/support/statalist/faq

* http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: precision problems with Mata? naaaahh...***From:*Nicola Orsini <nicola.orsini@imm.ki.se>

- Prev by Date:
**Re: st: Problems with program** - Next by Date:
**st: optimal hedge variance ratio** - Previous by thread:
**st: Mata vs C** - Next by thread:
**Re: st: precision problems with Mata? naaaahh...** - Index(es):

© Copyright 1996–2016 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |