Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: precision problems with Mata? naaaahh...

From   Kit Baum <BAUM@BC.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...

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)

void function runsum(string scalar vname)
real matrix L
real matrix runsum
real scalar resindex
real scalar vnew
vnew = vname+"_sum"
// 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)

mata runsum("x")
g double discrep = sum - x_sum
l, sep(0)

Kit Baum, Boston College Economics

* For searches and help try:

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