⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m1_permutation.hlp

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 HLP
📖 第 1 页 / 共 2 页
字号:

{pstd}
That would be the solution except that the LU decomposition function does
not decompose {it:A} into {it:L} and {it:U}; it decomposes {it:P}'{it:A},
although the function does tell us {it:P}.  Thus we can write,

		    {it:P}'{it:A}  =  {it:L}*{it:U}
		      {it:A}  =  {it:P}*{it:L}*{it:U}                   (remember {it:P}'^(-1) = {it:P})
		 {it:A}^(-1)  =  {it:U}^(-1)*{it:L}^(-1)*{it:P}'

{pstd}
Thus the solution to our problem is to use the {it:U} and {it:L} just as we
planned -- calculate {it:U}^(-1)*{it:L}^(-1) -- and then make a column
permutation of that, which we can do by postmultiplying by {it:P}'.

{pstd}
There is, however, a detail that we have yet to reveal to you:  Mata's
LU decomposition function does not return {it:P}, the permutation matrix.
It instead returns {it:p}, a permutation vector equivalent to {it:P}, 
and so the last step -- forming the column permutation by postmultiplying by
{it:P}' -- is done differently.  That is the subject of the next section.

{pstd}
Permutation vectors are more efficient that permutation matrices, but 
you are going to discover that they are not as mathematically transparent.
Thus when working with a function that returns a permutation vector -- when
working with a function that permutes the rows or columns -- think in terms of
permutation matrices and then translate back into permutation vectors.


    {title:3.  Permutation vectors}

{pstd}
Permutation vectors are used with Mata's subscripting operator, so before
explaining permutation vectors, let's understand subscripting.

{pstd}
Not surprisingly, Mata allows subscripting.  Given matrix 

			 {c TLC}{c -}       {c -}{c TRC}
		         {c |} 1  2  3 {c |}
		   {it:A}  =  {c |} 4  5  6 {c |}
		         {c |} 7  8  9 {c |}
			 {c BLC}{c -}       {c -}{c BRC}

{pstd}
Mata understands that 

	      {it:A}{cmd:[2,3]}  =  6

{pstd}
Mata also understands that if one or the other subscript is specified as
{cmd:.} (missing value), the entire column or row is to be selected:

			 {c TLC}{c -} {c -}{c TRC}
		         {c |} 3 {c |}
	      {it:A}{cmd:[.,3]}  =  {c |} 6 {c |}
		         {c |} 9 {c |}
			 {c BLC}{c -} {c -}{c BRC}

			 {c TLC}{c -}       {c -}{c TRC}
	      {it:A}{cmd:[2,.]}  =  {c |} 4  5  6 {c |}
			 {c BLC}{c -}       {c -}{c BRC}

{pstd}
Mata also understands that if a vector is specified for either subscript

	   {c TLC}{c -} {c -}{c TRC}         {c TLC}{c -}       {c -}{c TRC}
           {c |} 2 {c |}         {c |} 4  5  6 {c |}
	{it:A}{cmd:[} {c |} 3 {c |} {cmd:, .]} =  {c |} 7  8  9 {c |}
           {c |} 2 {c |}         {c |} 4  5  6 {c |}
	   {c BLC}{c -} {c -}{c BRC}         {c BLC}{c -}       {c -}{c BRC}

{pstd}
In Mata, we would actually write the above as {it:A}{cmd:[(2\3\2),.]}, and Mata 
would understand that we want the matrix made up of rows 2, 3, and 2
of {it:A}, all columns.  Similarly, we can request all rows, columns 2, 3 and 2:

{pstd}
and 

			 {c TLC}{c -}       {c -}{c TRC}
                         {c |} 2  3  2 {c |}
	 {it:A}{cmd:[.,(2,3,2)]} =  {c |} 5  6  5 {c |}
                         {c |} 8  9  8 {c |}
			 {c BLC}{c -}       {c -}{c BRC}


{pstd}
In the above, we wrote (2,3,2) as a row vector because it seems 
more logical that way, but we could just as well have written 
{it:A}{cmd:[.,(2\3\2)]}.  In subscripting, Mata does not care whether the
vectors are rows or columns.

{pstd}
In any case, we can use a vector of indices inside Mata's subscripts to select
rows and columns of a matrix, and that means we can permute them.  All that is
required is that the vector we specify contain a permutation of the integers
1 through {it:n} because, otherwise, we would repeat some rows or columns and
omit others.

{pstd}
A permutation vector is a {it:n} {it:x} 1 or 1 {it:x} {it:n} vector containing
a permutation of the integers 1 through {it:n}.  For example, the permutation
vector equivalent to the permutation matrix

			 {c TLC}{c -}       {c -}{c TRC}
                         {c |} 0  1  0 {c |}
	           {it:P}  =  {c |} 0  0  1 {c |}
                         {c |} 1  0  0 {c |}
			 {c BLC}{c -}       {c -}{c BRC}

{pstd}
is

			 {c TLC}{c -} {c -}{c TRC}
                         {c |} 2 {c |}
		  {it:p}   =  {c |} 3 {c |}
			 {c |} 1 {c |}
			 {c BLC}{c -} {c -}{c BRC}

{pstd}
{it:p} can be used with subscripting to permute the rows of {it:A}

			 {c TLC}{c -}       {c -}{c TRC}
                         {c |} 4  5  6 {c |}
	      {it:A}{cmd:[}{it:p}{cmd:,.]}  =  {c |} 7  8  9 {c |}
                         {c |} 1  2  3 {c |}
			 {c BLC}{c -}       {c -}{c BRC}


{pstd}
and similarly, {it:A}{cmd:[.,}{it:p}{cmd:]} would permute the columns.  

{pstd}
In addition, subscripting can be used on the left-hand side of the 
equal-sign assignment operator.  So far, we have assumed that the subscripts
are on the right-hand side of the assignment operator, such as

	{it:B}{cmd: =} {it:A}{cmd:[}{it:p}{cmd:,.]}

{pstd}
We have learned that if {it:p}=(2\3\1) (or {it:p}=(2,3,1)), the result is to
copy the 2nd row of {it:A} to the first row of {it:B}, the 3rd row of {it:A}
to the second row of {it:B}, and the 1st row of {it:A} to the third row of
{it:B}.  Coding

	{it:B}{cmd:[}{it:p}{cmd:,.] =} {it:A}

{pstd}
does the inverse:  it copies the first row of {it:A} to the 2nd row of {it:B},
the second row of {it:A} to the 3rd row of {it:B}, and the third row of {it:A}
to the 1st row of {it:B}.  {it:B}{cmd:[}{it:p}{cmd:,.]=}{it:A} really is the
inverse of {it:C}{cmd:=}{it:A}{cmd:[}{it:p}{cmd:,.]} in that, if we code

	{it:C} {cmd:=} {it:A}{cmd:[}{it:p}{cmd:,.]}
	{it:B}{cmd:[}{it:p}{cmd:,.] =} {it:C}

{pstd}
{it:B} will be equal to {it:C}, and if we code 

	{it:C}{cmd:[}{it:p}{cmd:,.] =} {it:A}
	{it:B} {cmd:=} {it:C}{cmd:[}{it:p}{cmd:,.]}

{pstd}
{it:B} will also be equal to {it:C}.

{pstd}
There is, however, one pitfall that you must watch for when using 
subscripts on the left-hand side:  the matrix on the left-hand side
must already exist and it must be of the appropriate (in this case, same)
dimension.  Thus when performing the inverse copy, it is common to code

	{it:B} {cmd:=} {it:C}
	{it:B}{cmd:[}{it:p}{cmd:,.] =} {it:C}

{pstd}
The first line is not unnecessary; it is what ensures that {it:B} exists and 
is of the proper dimension, although we could just as well code

	{it:B} {cmd:= J(rows(}{it:C}{cmd:), cols(}{it:C}{cmd:), .)}
	{it:B}{cmd:[}{it:p}{cmd:,.] =} {it:C}

{pstd}
The first construction is preferred because it assures that {it:B} is of the 
same type as {it:C}.  If you really like the second form, you should code

	{it:B} {cmd:= J(rows(}{it:C}{cmd:), cols(}{it:C}{cmd:), missingof(}{it:C}{cmd:))}
	{it:B}{cmd:[}{it:p}{cmd:,.] =} {it:C}

{pstd}
Going back to the preferred code

	{it:B} {cmd:=} {it:C}
	{it:B}{cmd:[}{it:p}{cmd:,.] =} {it:C}

{pstd}
some programmers combine it into one statement:
	
	{cmd:(}{it:B}{cmd:=}{it:C}{cmd:)[}{cmd:p,.] =} {it:C}

{pstd}
In addition, Mata provides an {cmd:invorder(}{it:p}{cmd:)} 
(see {bf:{help mf_invorder:[M-5] invorder()}})
that will return an
inverted {it:p} appropriate for use on the right-hand side, so you can
also code

	{it:B} {cmd:=} {it:C}{cmd:[invorder(}{it:p}{cmd:),.]}


{title:Also see}

{psee}
Manual:  {hi:[M-1] permutation}

{psee}
Online:  
{bf:{help mata:[M-0] intro}},
{bf:{help m1_intro:[M-1] intro}}
{p_end}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -