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

Re: st: RE: matrix programming problem


From   Hung-Jen Wang <[email protected]>
To   [email protected]
Subject   Re: st: RE: matrix programming problem
Date   Mon, 25 Nov 2002 14:13:17 -0500

Hi,

I thank Tao Jiang and Al Feiveson for suggestions on my matrix
programming problem. Both of them suggest the same approach, that is
to figure out the algebra of the determinant(s), which is easy to do
when the dimension of the matrix is small (2x2 in the example I gave
in the previous post). Although the matrix in my "real" problem has a
higher dimension (4x4), I still find it manageable particularly with
the help of Mathematica software.

Thanks again.

HJW


Friday, November 22, 2002, 4:01:22 PM, you wrote:

> Hung-Jen -

> Since you onl y need the determinant of a 2 x 2 matrix, the fastest and
> easiest way is to calculate it by hand. First, 

> det(M) = det(A)*det(A)*det(Sigma). 

> det(Sigma) = a11*a22-a12*a12
> det(A) = x1*x3-x2*x2

> Thus, (assuming the a's are stored as scalars or constant Stata variables)

> gen d = (a11*a22-a12*a12)*(x1*x3-x2*x2)^2

> should do the trick.

> Al Feiveson



> -----Original Message-----
> From: Hung-Jen Wang [mailto:[email protected]]
> Sent: Friday, November 22, 2002 11:49 AM
> To: [email protected]
> Subject: st: matrix programming problem


> Hi,

> Suppose I have a dataset of m observations and three
> variables x1, x2, and x3. I need to create a new variable y
> in such a way that, for each of the ith observation of y,
> i=1,2,..m, the value of y[i] is the determinant of the
> following 2x2 matrix M[i]:

>    M[i] =  A[i]*Sigma*A[i]'
> where

>    A[i] is a 2x2 symmetric matrix taking values from x1 to x3:

>        A[i]=  x1[i], x2[i]
>               x2[i], x3[i]

>    and Sigma is a 2x2 symmetric matrix of constant elements:

>        Sigma = a11, a12
>                a12, a22


> My question: What is the most efficient way (fast code) to
> do the calculation. I looked at -mat accum-, -mat glsaccm-,
> and -mat vecaccum-, but do not see how they can be applied
> here.

> Currently, I loop over the observations to create the matrix
> and the determinant. It is, however, quit slow. Any
> suggestion that help speed up the computation will be
> appreciated!

> HJW
> ps. matsize or computer memory will not be a constraint for me.

> -------------- slow code begin ------

>    mat Sigma = (2, 1 \ 1, 3) /* an arbitrary example */
>    quie gen double y = .

>    forvalues k = 1/n {  /* n is the total number of observations */

>       mat Amat = (x1[`k'], x2[`k'] \ x2[`k'], x3[`k'])

>       mat Mmat = Amat*Sigma*Amat   /* a 2x2 matrix */

>       quie replace y = det(Mmat) in `k'/`k'

>    }

> -------------- slow code end -------

*
*   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/



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