📄 _svd.ado
字号:
*! version 1.0.0 27jan2005
// _svd tol [, tol(#) name(str) ]
//
// returns in r(U), r(W), and r(V) the singular value decomposition with
// columns sorted on the singular values r(D), dropping columns associated
// with singular values smaller than tol (defaults to 1e-8)
program _svd, rclass
version 8
syntax anything(name=P id=matrix) [, tol(str) name(str) ]
confirm matrix `P'
if `"`tol'"' == "" {
local tol 1e-8
}
confirm number `tol'
if `tol' < 0 {
di as err "_svd: negative tolerance not allowed"
exit 198
}
if "`name'" == "" {
local name factor
}
tempname A B D DD dmax TA TB TD
// the internal svd command requires #rows>=#cols
if rowsof(`P') >= colsof(`P') {
matrix svd `A' `D' `B' = `P'
}
else {
tempname PP
matrix `PP' = `P''
matrix svd `B' `D' `A' = `PP'
}
local n = colsof(`D')
local nn = 0
matrix `DD' = `D'
forvalues j = 1 / `n' {
scalar `dmax' = -1
forvalues i = 1 / `n' {
if `DD'[1,`i'] > `dmax' {
local ii = `i'
scalar `dmax' = `DD'[1,`i']
}
}
if `dmax' > `tol' {
local ++nn
matrix `TA' = nullmat(`TA') , `A'[1...,`ii']
matrix `TB' = nullmat(`TB') , `B'[1...,`ii']
matrix `TD' = nullmat(`TD') , `D'[1 ,`ii']
// make DD negative, so never maximum again
matrix `DD'[1,`ii'] = -2
local factors `factors' `name'`j'
}
else {
continue, break
}
}
if `nn' == 0 {
dis as err "no singular value exceeds the tolerance `tol'"
exit 498
}
matrix colnames `TA' = `factors'
matrix colnames `TB' = `factors'
matrix colnames `TD' = `factors'
return matrix U = `TA'
return matrix V = `TB'
return matrix W = `TD'
end
exit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -