📄 mf_qrd.hlp
字号:
{it:AP} = {it:QR}{right:(2) }
{p 4 4 2}
or, if you prefer
{it:AP} = {it:Q1}*{it:R1}{right:(2') }
{p 4 4 2}
where {it:P} is a permutation matrix; see
{bf:{help m1_permutation:[M-1] permutation}}.
We can rewrite this as
{it:A} = {it:QRP}{bf:'}{right:(3) }
{p 4 4 2}
and
{it:A} = {it:Q1}*{it:R1}*{it:P}{bf:'}{right:(3') }
{p 4 4 2}
Column pivoting can improve the numerical accuracy.
The functions
{cmd:qrdp(}{it:A}{cmd:,} {it:Q}{cmd:,} {it:R}{cmd:,} {it:p}{cmd:)}
and
{cmd:hqrdp(}{it:A}{cmd:,} {it:H}{cmd:,} {it:tau}{cmd:,} {it:R1}{cmd:,} {it:p}{cmd:)}
perform pivoting and return the permutation matrix {it:P} in permutation
vector form:
: {cmd:A}
{res} {txt}1 2
{c TLC}{hline 9}{c TRC}
1 {c |} {res}7 4{txt} {c |}
2 {c |} {res}9 6{txt} {c |}
3 {c |} {res}9 6{txt} {c |}
4 {c |} {res}7 2{txt} {c |}
5 {c |} {res}3 1{txt} {c |}
{c BLC}{hline 9}{c BRC}
: {cmd:Q = R = p = .}
: {cmd:qrdp(A, Q, R, p)}
: {cmd:Ahat = (Q*R)[., invorder(p)]}{...}
{col 57}// i.e., {it:QRP}'
: {cmd:mreldif(Ahat, A)}
3.55271e-16
: {cmd:H = tau = R1 = p = .}
: {cmd:hqrdp(A, H, tau, R1, p)}
: {cmd:Ahat = (hqrdq1(H, tau)*R1)[., invorder(p)]}{...}
{col 57}// i.e., {it:Q1}*{it:R1}*{it:P}'
: {cmd:mreldif(Ahat, A)}
3.55271e-16
{p 4 4 2}
Before calling {cmd:qrdp()} or {cmd:hqrdp()}, we set {it:p} equal to
missing, specifying that all columns could be pivoted.
We could just as well have set {it:p} equal to (0, 0), which would have
stated that both columns were eligible for pivoting.
{p 4 4 2}
When pivoting is disallowed, and when {it:A} is not of full column rank, the
order in which columns appear affects the kind of generalized solution
produced; later columns are, in effect, dropped. When pivoting is allowed,
the columns are reordered based on numerical accuracy considerations.
In the rank-deficient case, you no longer know ahead of time which
columns will be dropped, because you do not know in what order the
columns will appear.
Generally, you do not care, but there are occasions when you do.
{p 4 4 2}
In such cases, you can specify which columns are eligible for pivoting and
which are not -- you specify {it:p} as a vector and if {it:p}[{it:i}]==1, the
{it:i}th column may not be pivoted. The {it:p}[{it:i}]==1
columns
are (conceptually) moved to appear first in the matrix, and the remaining
columns are ordered optimally after that. The permutation vector that is
returned in {it:p} accounts for all of this.
{title:Least-squares solutions with dropped columns}
{p 4 4 2}
Least-square solutions are one popular use of QR decomposition.
We wish to solve for {it:x}
{it:Ax} = {it:b} ({it:A}: {it:m x n}, {it:m} >= {it:n}){right:(4) }
{p 4 4 2}
The problem is that there is no solution to (4) when {it:m} > {it:n} because
we have more equations than unknowns. In that case, we want to find {it:x}
such that ({it:Ax}-{it:b}){bf:'}({it:Ax}-{it:b}) is minimized.
{p 4 4 2}
If {it:A} is of full column rank then it is well known that the least-squares
solution for {it:x} is given by
{cmd:solveupper(}{it:R1}{cmd:,} {it:Q1}{bf:'}{it:b}{cmd:)}
where {cmd:solveupper()} is an upper-triangular solver;
see {bf:{help mf_solvelower:[M-5] solvelower()}}.
{p 4 4 2}
If {it:A} is of less than full column rank and we do not care which columns
are dropped, then we can use the same solution:
{cmd:solveupper(}{it:R1}{cmd:,} {it:Q1}{bf:'}{it:b}{cmd:)}.
{p 4 4 2}
Adding pivoting to the above hardly complicates the issue, the solution
becomes
{cmd:solveupper(}{it:R1}{cmd:,} {it:Q1}{bf:'}{it:b}{cmd:)[invorder(}{it:p}{cmd:)]}.
{p 4 4 2}
For both of these cases, the full details are
: {cmd:A}
{res} {txt}1 2 3
{c TLC}{hline 13}{c TRC}
1 {c |} {res}3 9 1{txt} {c |}
2 {c |} {res}3 8 1{txt} {c |}
3 {c |} {res}3 7 1{txt} {c |}
4 {c |} {res}3 6 1{txt} {c |}
{c BLC}{hline 13}{c BRC}
: {cmd:b}
{res} {txt} 1
{c TLC}{hline 6}{c TRC}
1 {c |} {res} 7{txt} {c |}
2 {c |} {res} 3{txt} {c |}
3 {c |} {res}12{txt} {c |}
4 {c |} {res} 0{txt} {c |}
{c BLC}{hline 6}{c BRC}
: {cmd:H = tau = R1 = p = .}
: {cmd:hqrdp(A, H, tau, R1, p)}
: {cmd:q1b = hqrdmultq1t(H, tau, b)}{...}
{col 50}// i.e., {it:Q1}{bf:'}{it:b}
: {cmd:xhat = solveupper(R1, q1b)[invorder(p)]}
: {cmd:xhat}
{res} {txt} 1
{c TLC}{hline 16}{c TRC}
1 {c |} {res}-1.166666667{txt} {c |}
2 {c |} {res} 1.2{txt} {c |}
3 {c |} {res} 0{txt} {c |}
{c BLC}{hline 16}{c BRC}
{p 4 4 2}
The {it:A} matrix in the above example has less than full column rank; the
first column contains a variable with no variation and the third column
contains the data for the intercept. The solution above is correct, but we
might prefer a solution that included the intercept. To do that, we
need to specify that the third column cannot be pivoted:
: {cmd:p = (0, 0, 1)}
: {cmd:H = tau = R1 = .}
: {cmd:hqrdp(A, H, tau, R1, p)}
: {cmd:q1b = hqrdmultq1t(H, tau, b)}
: {cmd:xhat = solveupper(R1, q1b)[invorder(p)]}
: {cmd:xhat}
{res} {txt} 1
{c TLC}{hline 8}{c TRC}
1 {c |} {res} 0{txt} {c |}
2 {c |} {res} 1.2{txt} {c |}
3 {c |} {res}-3.5{txt} {c |}
{c BLC}{hline 8}{c BRC}
{title:Conformability}
{cmd:qrd(}{it:A}{cmd:,} {it:Q}{cmd:,} {it:R}{cmd:)}:
{it:input:}
{it:A}: {it:m x n}, {it:m}>={it:n}
{it:output:}
{it:Q}: {it:m x m}
{it:R}: {it:m x n}
{cmd:hqrd(}{it:A}{cmd:,} {it:H}{cmd:,} {it:tau}{cmd:,} {it:R1}{cmd:)}:
{it:input:}
{it:A}: {it:m x n}, {it:m}>={it:n}
{it:output:}
{it:H}: {it:m x n}
{it:tau}: 1 {it:x n}
{it:R1}: {it:n x n}
{cmd:_hqrd(}{it:A}{cmd:,} {it:tau}{cmd:,} {it:R1}{cmd:)}:
{it:input:}
{it:A}: {it:m x n}, {it:m}>={it:n}
{it:output:}
{it:A}: {it:m x n} (contains {it:H})
{it:tau}: 1 {it:x n}
{it:R1}: {it:n x n}
{cmd:hqrdmultq(}{it:H}{cmd:,} {it:tau}{cmd:,} {it:X}{cmd:,} {it:transpose}{cmd:)}:
{it:H}: {it:m x n}
{it:tau}: 1 {it:x n}
{it:X}: {it:m x c}
{it:result}: {it:m x c}
{cmd:hqrdmultq1t(}{it:H}{cmd:,} {it:tau}{cmd:,} {it:X}{cmd:)}:
{it:H}: {it:m x n}
{it:tau}: 1 {it:x n}
{it:X}: {it:m x c}
{it:result}: {it:n x c}
{cmd:hqrdq(}{it:H}{cmd:,} {it:tau}{cmd:)}:
{it:H}: {it:m x n}
{it:tau}: 1 {it:x n}
{it:result}: {it:m x m}
{cmd:hqrdq1(}{it:H}{cmd:,} {it:tau}{cmd:)}:
{it:H}: {it:m x n}
{it:tau}: 1 {it:x n}
{it:result}: {it:m x n}
{cmd:hqrdr(}{it:H}{cmd:,} {it:tau}{cmd:)}:
{it:H}: {it:m x n}
{it:result}: {it:m x n}
{cmd:hqrdr1(}{it:H}{cmd:,} {it:tau}{cmd:)}:
{it:H}: {it:m x n}
{it:result}: {it:n x n}
{cmd:qrdp(}{it:A}{cmd:,} {it:Q}{cmd:,} {it:R}{cmd:,} {it:p}{cmd:)}:
{it:input:}
{it:A}: {it:m x n}, {it:m}>={it:n}
{it:p}: 1 {it:x} 1 or 1 {it:x n}
{it:output:}
{it:Q}: {it:m x m}
{it:R}: {it:m x n}
{it:p}: 1 {it:x n}
{cmd:hqrdp(}{it:A}{cmd:,} {it:H}{cmd:,} {it:tau}{cmd:,} {it:R1}{cmd:,} {it:p}{cmd:)}
{it:input:}
{it:A}: {it:m x n}, {it:m}>={it:n}
{it:p}: 1 {it:x} 1 or 1 {it:x n}
{it:output:}
{it:H}: {it:m x n}
{it:tau}: 1 {it:x n}
{it:R1}: {it:n x n}
{it:p}: 1 {it:x n}
{cmd:_hqrdp(}{it:A}{cmd:,} {it:tau}{cmd:,} {it:R1}{cmd:,} {it: p}{cmd:)}:
{it:input:}
{it:A}: {it:m x n}, {it:m}>={it:n}
{it:p}: 1 {it:x} 1 or 1 {it:x n}
{it:output:}
{it:A}: {it:m x n} (contains {it:H})
{it:tau}: 1 {it:x n}
{it:R1}: {it:n x n}
{it:p}: 1 {it:x n}
{cmd:_hqrdp_la(}{it:A}{cmd:,} {it:tau}{cmd:,} {it:p}{cmd:)}:
{it:input:}
{it:A}: {it:m x n}, {it:m}>={it:n}
{it:p}: 1 {it:x} 1 or 1 {it:x n}
{it:output:}
{it:A}: {it:m x n} (contains {it:H})
{it:tau}: 1 {it:x n}
{it:p}: 1 {it:x n}
{title:Diagnostics}
{p 4 4 2}
{cmd:qrd(}{it:A}, ...{cmd:)},
{cmd:hqrd(}{it:A}, ...{cmd:)},
{cmd:_hqrd(}{it:A}, ...{cmd:)},
{cmd:qrdp(}{it:A}, ...{cmd:)},
{cmd:hqrdp(}{it:A}, ...{cmd:)},
and
{cmd:_hqrdp(}{it:A}, ...{cmd:)}
return missing results if {it:A} contains missing values.
That is, {it:Q} will contain all missing values. {it:R} will contain
missing values on and above the diagonal. {it:p} will contain the
integers 1, 2, ...
{p 4 4 2}
{cmd:_hqrd(}{it:A}, ...{cmd:)}
and
{cmd:_hqrdp(}{it:A}, ...{cmd:)}
abort with error if {it:A} is a view.
{p 4 4 2}
{cmd:hqrdmultq(}{it:H}{cmd:,} {it:tau}{cmd:,} {it:X}, {it:transpose}{cmd:)}
and
{cmd:hqrdmultq1t(}{it:H}{cmd:,} {it:tau}{cmd:,} {it:X}{cmd:)}
return missing results if {it:X} contains missing values.
{title:Files}
{p 4 4 2}
{view qrd.mata, adopath asis:qrd.mata},
{view qrdp.mata, adopath asis:qrdp.mata},
{view _hqrd.mata, adopath asis:_hqrd.mata},
{view hqrd.mata, adopath asis:hqrd.mata},
{view hqrdp.mata, adopath asis:hqrdp.mata},
{view _hqrdp.mata, adopath asis:_hqrdp.mata},
{view hqrdmultq.mata, adopath asis:hqrdmultq.mata},
{view hqrdmultq1t.mata, adopath asis:hqrdmultq1t.mata},
{view hqrdq.mata, adopath asis:hqrdq.mata},
{view hqrdq1.mata, adopath asis:hqrdq1.mata},
{view hqrdr.mata, adopath asis:hqrdr.mata},
{view hqrdr1.mata, adopath asis:hqrdr1.mata}.
{p 4 4 2}
{cmd:_hqrdp_la()} is built-in.
{title:Also see}
{p 4 13 2}
Manual: {hi:[M-5] qrd()}
{p 4 13 2}
Online: help for
{bf:{help mf_qrsolve:[M-5] qrsolve()}},
{bf:{help mf_qrinv:[M-5] qrinv()}};
{bf:{help m4_matrix:[M-4] matrix}}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -