<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>计量经济学 on 格物堂</title><link>https://gewutang.cn/tags/%E8%AE%A1%E9%87%8F%E7%BB%8F%E6%B5%8E%E5%AD%A6/</link><description>Recent content in 计量经济学 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Tue, 13 Sep 2011 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/%E8%AE%A1%E9%87%8F%E7%BB%8F%E6%B5%8E%E5%AD%A6/index.xml" rel="self" type="application/rss+xml"/><item><title>非线性模型极大似然估计方法</title><link>https://gewutang.cn/2011/09/13/max-likelyho/</link><pubDate>Tue, 13 Sep 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/09/13/max-likelyho/</guid><description>&lt;h2 id="一模型设定与概率框架"&gt;&lt;a href="#%e4%b8%80%e6%a8%a1%e5%9e%8b%e8%ae%be%e5%ae%9a%e4%b8%8e%e6%a6%82%e7%8e%87%e6%a1%86%e6%9e%b6" class="header-anchor"&gt;&lt;/a&gt;一、模型设定与概率框架&#10;&lt;/h2&gt;&lt;p&gt;设非线性生产函数形式为：&#10;$$&#10;y_i = aL_i^bK_i^c + \epsilon_i \quad (i=1,&amp;hellip;,n)&#10;$$&#10;其中误差项 &lt;code&gt;\(\epsilon_i \sim N(0,\sigma^2)\)&lt;/code&gt;，则观测值 &lt;code&gt;\(y_i\)&lt;/code&gt; 服从均值为 &lt;code&gt;\(aL_i^bK_i^c\)&lt;/code&gt;、方差为 &lt;code&gt;\(\sigma^2\)&lt;/code&gt; 的正态分布。&lt;/p&gt;&#10;&lt;h2 id="二似然函数构建"&gt;&lt;a href="#%e4%ba%8c%e4%bc%bc%e7%84%b6%e5%87%bd%e6%95%b0%e6%9e%84%e5%bb%ba" class="header-anchor"&gt;&lt;/a&gt;二、似然函数构建&#10;&lt;/h2&gt;&lt;h3 id="1-联合概率密度函数"&gt;&lt;a href="#1-%e8%81%94%e5%90%88%e6%a6%82%e7%8e%87%e5%af%86%e5%ba%a6%e5%87%bd%e6%95%b0" class="header-anchor"&gt;&lt;/a&gt;1. 联合概率密度函数&#10;&lt;/h3&gt;&lt;p&gt;$$&#10;L(a,b,c,\sigma) = \prod_{i=1}^n \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left[ -\frac{(y_i-aL_i^bK_i^c)^2}{2\sigma^2} \right]&#10;$$&lt;/p&gt;&#10;&lt;h3 id="2-负对数似然函数目标函数"&gt;&lt;a href="#2-%e8%b4%9f%e5%af%b9%e6%95%b0%e4%bc%bc%e7%84%b6%e5%87%bd%e6%95%b0%e7%9b%ae%e6%a0%87%e5%87%bd%e6%95%b0" class="header-anchor"&gt;&lt;/a&gt;2. 负对数似然函数（目标函数）&#10;&lt;/h3&gt;&lt;p&gt;$$&#10;\ell(a,b,c,\sigma) = -\ln L = \frac{n}{2}\ln(2\pi) + n\ln\sigma + \frac{1}{2\sigma^2}\sum_{i=1}^n (y_i - aL_i^bK_i^c)^2&#10;$$&lt;/p&gt;&#10;&lt;h2 id="三参数估计流程"&gt;&lt;a href="#%e4%b8%89%e5%8f%82%e6%95%b0%e4%bc%b0%e8%ae%a1%e6%b5%81%e7%a8%8b" class="header-anchor"&gt;&lt;/a&gt;三、参数估计流程&#10;&lt;/h2&gt;&lt;h3 id="1-数据处理与准备"&gt;&lt;a href="#1-%e6%95%b0%e6%8d%ae%e5%a4%84%e7%90%86%e4%b8%8e%e5%87%86%e5%a4%87" class="header-anchor"&gt;&lt;/a&gt;1. 数据处理与准备&#10;&lt;/h3&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 导入数据&#10;data &amp;lt;- data.frame(&#10; y = c(76,47,97,107,123,139,159,152,191,201,207,200),&#10; L = c(0.02,0.02,0.06,0.06,0.11,0.11,0.22,0.22,0.56,0.56,1.10,1.10),&#10; K = c(1.1,0.9,2.3,2.1,3.4,3.6,4.5,4.3,5.8,6.0,7.2,7.0)&#10;)&#10;&#10;# 定义负对数似然函数&#10;mloglik &amp;lt;- function(theta) {&#10; a &amp;lt;- theta[1]&#10; b &amp;lt;- theta[2]&#10; c &amp;lt;- theta[3]&#10; sigma &amp;lt;- theta[4]&#10; &#10; y_pred &amp;lt;- a * data$L^b * data$K^c&#10; residuals &amp;lt;- data$y - y_pred&#10; n &amp;lt;- length(data$y)&#10; &#10; # 避免\sigma 趋近于零&#10; if(sigma &amp;lt;= 0) return(1e10)&#10; &#10; (n/2)*log(2*pi) + n*log(sigma) + sum(residuals^2)/(2*sigma^2)&#10;}&#10;&#10;# 采用线性化方法获取初始值&#10;log_model &amp;lt;- lm(log(y) ~ log(L) + log(K), data = data)&#10;theta_init &amp;lt;- c(&#10; exp(coef(log_model)[1]),&#10; coef(log_model)[2],&#10; coef(log_model)[3],&#10; sd(resid(log_model))&#10;)&#10;&#10;# 优化求解&#10;fit &amp;lt;- nlm(mloglik, theta_init, hessian = TRUE)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning in nlm(mloglik, theta_init, hessian = TRUE): NA/NaN replaced by maximum&#10;## positive value&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning in nlm(mloglik, theta_init, hessian = TRUE): Inf replaced by maximum&#10;## positive value&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 提取估计结果&#10;params &amp;lt;- fit$estimate&#10;names(params) &amp;lt;- c(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;sigma&amp;quot;)&#10;params&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## a b c sigma &#10;## 55.45620820 -0.02273718 0.67704166 7.67493891&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item><item><title>R与时间序列的平稳性</title><link>https://gewutang.cn/2011/01/09/r-and-the-stationary-of-time-series/</link><pubDate>Sun, 09 Jan 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/01/09/r-and-the-stationary-of-time-series/</guid><description>&lt;p&gt;“协整”算是计量经济学里面的明星概念，不了解计量经济学的人很容易被它笼罩的计量经济学光环给吓到。但事实上，协整并不神秘，是平易近人。&lt;/p&gt;&#10;&lt;p&gt;要了解“协整”，首先得提到一个名词——平稳。&lt;/p&gt;&#10;&lt;h2 id="什么是平稳时间序列"&gt;&lt;a href="#%e4%bb%80%e4%b9%88%e6%98%af%e5%b9%b3%e7%a8%b3%e6%97%b6%e9%97%b4%e5%ba%8f%e5%88%97" class="header-anchor"&gt;&lt;/a&gt;什么是平稳时间序列&#10;&lt;/h2&gt;&lt;p&gt;金融领域有很多类型的时间序列，学者们为了研究的方便通常会对这些时间序列进行分类。比如，按照时间序列数据产生时间间隔的长短分为高频时间序列和非高频时间序列。按照时间序列的平稳性与否，便可以将时间序列分为平稳的时间序列和非平稳的时间序列。&lt;/p&gt;&#10;&lt;p&gt;判断某个时间序列平稳与否主要看一下三点：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;时间序列（随机过程）的均值是否是常数?或者说时间序列（随机过程）的均值是不是跟时间无关？如果不是常数则该时间序列是非平稳的；如果是常数，则该时间序列可能是平稳的，但不肯定，须进入第二步。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;时间序列（随机过程）的方差是否是常数？或者说时间序列（随机过程）的方差是不是跟时间无关？如果不是常数则该时间序列是非平稳的；如果是常数，则该时间序列可能是平稳的，但不肯定，须进入第三步。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;任意两个时期的时间序列之间的协方差是否仅仅依赖于两个时间序列的时间间隔。&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;如果一个时间序列同时满足上述三个条件，那么，该时间序列就是平稳的。&lt;/p&gt;&#10;&lt;p&gt;由于均值、方差和协方差可以被统一的成为统计特性，因此也可以说如果时间序列的统计特性不随着时间的推移而发生变化，则说明时间序列是平稳的。&lt;/p&gt;&#10;&lt;p&gt;平稳时间序列还可以进一步细分。根据平稳时间序列进行差分的次数，可以将平稳时间序列分为不同阶的平稳时间序列。如果一个时间序列本身是平稳的，那么该时间序列可被记作I(0),括号里面的0表示该时间序列没有进行差分。如果一个时间序列一阶差分之后平稳，那么可被记作I(1),以此类推。&lt;/p&gt;&#10;&lt;h2 id="平稳性的检验"&gt;&lt;a href="#%e5%b9%b3%e7%a8%b3%e6%80%a7%e7%9a%84%e6%a3%80%e9%aa%8c" class="header-anchor"&gt;&lt;/a&gt;平稳性的检验&#10;&lt;/h2&gt;&lt;p&gt;在应用过程中，时间序列的平稳性的判断要简单的多。常用的判断时间序列平稳性的方法有两个：图示法和单位根检验法。&lt;/p&gt;&#10;&lt;p&gt;图示法，顾名思义，就是画出时间序列的时序图，来目测时间序列是否平稳。如果画出的时间序列不存在明显的趋势，那么时间序列可能是平稳的。这个方法比较随意和主观，因此，只能作为辅助判断的手段。&lt;/p&gt;&#10;&lt;p&gt;单位根检验法是一个理论基础扎实的判断手段，单位根检验的方法非常多，一般常用的有DF检验(Dickey-Fuller Test)，ADF检验(Augmented Dickey-Fuller Test)和PP检验(Phillips-Perron Test)。这三个检验的理论基础在这里不讲,因为除却数理统计的专业人员，大部分人都不必深究这些理论，现实中只要懂得这些检验用软件的实现方法就可以了。&lt;/p&gt;&#10;&lt;p&gt;下面以R为例：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;#生成两个时间序列：&#10;&#10;x=rnorm(500); #没有单位根&#10;&#10;y=cumsum(x); #有单位根&#10;&#10;# 绘制时序图&#10;&#10;plot.ts(x);&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2011/01/09/r-and-the-stationary-of-time-series/index_files/figure-html/unnamed-chunk-1-1.png" width="672" /&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;plot.ts(y);&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2011/01/09/r-and-the-stationary-of-time-series/index_files/figure-html/unnamed-chunk-1-2.png" width="672" /&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# DF.test检验&#10;&#10;library(tseries) #载入tseries包&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by 'quantmod':&#10;## method from&#10;## as.zoo.data.frame zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;adf.test(x)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning in adf.test(x): p-value smaller than printed p-value&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &#9;Augmented Dickey-Fuller Test&#10;## &#10;## data: x&#10;## Dickey-Fuller = -7.863, Lag order = 7, p-value = 0.01&#10;## alternative hypothesis: stationary&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;P值等于0.01，拒绝x是原假设（原假设为非平稳），即可以认为x是平稳的。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;adf.test(y)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &#9;Augmented Dickey-Fuller Test&#10;## &#10;## data: y&#10;## Dickey-Fuller = -2.6979, Lag order = 7, p-value = 0.2829&#10;## alternative hypothesis: stationary&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;P值等于0.303，因此y是非平稳的。&lt;/p&gt;&#10;&lt;p&gt;还可以用urca包中的ur.df()函数来完成上述过程：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# install.packages('urca') #安装urca包&#10;&#10;library(urca)#载入urca包&#10;&#10;?ur.df#查看ur.df()函数的帮助文档&#10;&#10;#用uroot包中的ADF.test()函数来完成上述过程&#10;&#10;# install.packages('uroot') #没有安装的话&#10;&#10;library(uroot) #载入uroot包&#10;?ADF.test#查看ADF.test()函数的帮助文档&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 在指定的程序包和图书馆里没有与'ADF.test'有关的文档:&#10;## 你可以用用'??ADF.test'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="pp检验"&gt;&lt;a href="#pp%e6%a3%80%e9%aa%8c" class="header-anchor"&gt;&lt;/a&gt;PP检验&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code class="language-r"&gt;library(tseries)&#10;&#10;pp.test(x)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Warning in pp.test(x): p-value smaller than printed p-value&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &#9;Phillips-Perron Unit Root Test&#10;## &#10;## data: x&#10;## Dickey-Fuller Z(alpha) = -513.04, Truncation lag parameter = 5, p-value&#10;## = 0.01&#10;## alternative hypothesis: stationary&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;P值等于0.01，拒绝x为非平稳序列的假设。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;pp.test(y)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## &#9;Phillips-Perron Unit Root Test&#10;## &#10;## data: y&#10;## Dickey-Fuller Z(alpha) = -15.795, Truncation lag parameter = 5, p-value&#10;## = 0.2286&#10;## alternative hypothesis: stationary&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;P值等于0.2056，不能拒绝x是非平稳序列的假设，即x非平稳。&lt;/p&gt;&#10;&lt;p&gt;也可以用stats包中的PP.test()函数来完成PP检验。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;?PP.test#查看PP.test()函数的帮助文档&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item></channel></rss>