代码拉取完成,页面将自动刷新
同步操作将从 连享会/Stata-Event_Study 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
*! Date : 2016-07-22
*! version : 0.1
*! Author : Richard Herron
*! Email : richard.c.herron@gmail.com
*! fast rolling correlations
/*
2016-07-22 v0.1 fork of rolling_beta
*/
program rolling_rho
version 11.2
syntax varlist(min=2 max=2 numeric) [ , short(integer 36) long(integer 36) faster ]
// dependent and indpendent vars from varlist
tempvar x y
tokenize `varlist'
generate `y' = `1'
generate `x' = `2'
// faster version uses constant window and lag operators
if ("`faster'" == "") {
local l = `long'
local s = `short'
// check logical windows
if (`s' > `l') display as error "long must be >= short"
// tsfill to use simple subscripting, flag will me missing for fill observations
tempvar flag
generate `flag' = 1
tsfill
}
else {
display "faster option: long window = short window = `long'"
local w = `long'
}
// first round of temporary variables (more below)
tempvar xs ys xys x2s y2s
// cumulative sums
generate `xs' = sum(`x')
generate `ys' = sum(`y')
generate `xys' = sum(`x' * `y')
generate `x2s' = sum(`x' * `x')
generate `y2s' = sum(`y' * `y')
if ("`faster'" == "") {
// SLOW -- allows growing window
// index lags
/*
ida is absolute id (row number)
idf is first absolute id for each panel
idr is relative id (or ida - idf)
idl is absolute id of correct lag
*/
tempvar ida idf idr idl
generate `ida' = _n
xtset
bysort `r(panelvar)' (`r(timevar)') : generate `idf' = `ida'[1]
generate `idr' = `ida' - `idf' + 1
generate `idl' = cond(`idr' > `l', `ida' - `l', `idf') if (`idr' > `s')
generate len = `ida' - `idl'
// observations used
tempvar ms
generate `ms' = sum(missing(`y', `x'))
generate obs = len - (`ms' - `ms'[`idl'])
// variances and covariances
tempvar covxy varx vary
generate `covxy' = (`xys' - `xys'[`idl'])/(obs - 1) - ((`xs' - `xs'[`idl'])/obs)*((`ys' - `ys'[`idl'])/obs)*obs/(obs - 1)
generate `varx' = (`x2s' - `x2s'[`idl'])/(obs - 1) - ((`xs' - `xs'[`idl'])/obs)*((`xs' - `xs'[`idl'])/obs)*obs/(obs - 1)
generate `vary' = (`y2s' - `y2s'[`idl'])/(obs - 1) - ((`ys' - `ys'[`idl'])/obs)*((`ys' - `ys'[`idl'])/obs)*obs/(obs - 1)
// rho
generate rho = `covxy' / sqrt(`varx' * `vary')
// labels
label variable rho "Rolling rho"
label variable obs "Observations used"
label variable len "Window length"
// return to original state
drop if missing(`flag')
}
else {
// FAST -- requires constant window
// observations used
tempvar ms
generate `ms' = sum(missing(`y', `x'))
generate obs = `w' - s`w'.`ms'
generate len = `w'
// generate variances and covariances
tempvar covxy varx vary
generate `covxy' = s`w'.`xys'/(obs - 1) - (s`w'.`xs'/obs)*(s`w'.`ys'/obs)*obs/(obs - 1)
generate `varx' = s`w'.`x2s'/(obs - 1) - (s`w'.`xs'/obs)*(s`w'.`xs'/obs)*obs/(obs - 1)
generate `vary' = s`w'.`y2s'/(obs - 1) - (s`w'.`ys'/obs)*(s`w'.`ys'/obs)*obs/(obs - 1)
// rho
generate rho = `covxy' / sqrt(`varx' * `vary')
// labels
label variable rho "Rolling rho"
label variable obs "Observations used"
label variable len "Window length"
}
end
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。