简体   繁体   English

针对R中的多变量案例(双变量正态)进行MLE

[英]Conducting MLE for multivariate case (bivariate normal) in R

The case is that I am trying to construct an MLE algortihm for a bivariate normal case. 这种情况是我试图为双变量正常情况构造一个MLE算法。 Yet, I stuck somewhere that seems there is no error, but when I run the script it ends up with a warning. 但是,我停留在似乎没有错误的地方,但是当我运行脚本时,它最终会发出警告。

I have a sample of size n (a fixed constant, trained with 100, but can be anything else) from a bivariate normal distribution with mean vector = (0,0) and covariance matrix = matrix(c(2.2,1.8,1.8,3),2,2) 我有一个均值vector = (0,0)和协方差matrix = matrix(c(2.2,1.8,1.8,3),2,2)

I've tried several optimization functions (including nlm() , mle() , spg() and optim() ) to maximize the likelihood function (,or minimize neg-likelihood), but there are warnings or errors. 我已经尝试了几种优化函数(包括nlm()mle()spg()optim() )来最大化似然函数(或最小化负似然性),但是有警告或错误。

require(MASS)
require(tmvtnorm)
require(BB)
require(matrixcalc)

I've defined the first likelihood function as follows; 我定义了第一个似然函数,如下所示;

bvrt_ll = function(mu,sigma,rho,sample)
{
  n = nrow(sample)
  mu_hat = c(mu[1],mu[2])
  p = length(mu)
  if(sigma[1]>0 && sigma[2]>0)
  {
    if(rho<=1 && rho>=-1)
    {
    sigma_hat = matrix(c(sigma[1]^2
                     ,sigma[1]*sigma[2]*rho
                     ,sigma[1]*sigma[2]*rho
                     ,sigma[2]^2),2,2)
    stopifnot(is.positive.definite(sigma_hat))


    neg_likelihood = (n*p/2)*log(2*pi) + (n/2)*log(det(sigma_hat)) + 0.5*sum(((sample-mu_hat)%*%solve(sigma_hat)%*%t(sample-mu_hat)))

    return(neg_likelihood)
    }
  }
  else NA

}

I prefered this one since I could set the constraints for sigmas and rho, but when I use mle() 我更喜欢这个,因为我可以为sigma和rho设置约束,但是当我使用mle()时

> mle(minuslogl = bvrt_ll  ,start = list(mu = mu_est,sigma=sigma_est,rho = 
rho_est)
+     ,method = "BFGS")
Error in optim(start, f, method = method, hessian = TRUE, ...) : 
  (list) object cannot be coerced to type 'double'

I also tried nlm and spg in package BB , but they did not help as well. 我还在BB包中尝试了nlmspg ,但是它们也没有帮助。 I tried the same function without defining constraints (inside the likelihood, not in optimization function), I could have some results but with warnings, like in nlm and spg both said the process was failed due to covariance matrix being not positive definite while it was, I think that was due to iteration, when iterating covariance matrix might not have been positive definite, and the fact that I did not define the constraints. 我尝试了相同的函数而未定义约束(在可能性内,而不是在优化函数中),我可能会得到一些结果,但会发出警告,例如在nlmspg ,都说由于协方差矩阵不是正定的而导致该过程失败,我认为这是由于迭代导致的,当时迭代协方差矩阵可能不是正定的,而且我没有定义约束。

Thus, as a result I need to construct an mle algorithm for bivariate normal, where do I do the mistake? 因此,结果是我需要为双变量正态构造一个mle算法,我该在哪里出错?

NOTE: I also tried the optimization functions with the following, (I am not sure I did it correct); 注意:我还尝试了以下优化功能,(我不确定是否正确);

neg_likelihood = function(mu,sigma,rho)
{
    if(rho>=-1 && rho<=1)
        {
          -sum(mvtnorm::dmvnorm(x=sample_10,mean=mu
                    ,sigma = matrix(c(sigma[1]^2
                    ,sigma[1]*sigma[2]*rho,sigma[1]*sigma[2]*rho
                    ,sigma[2]^2),2,2),log = T))
        }
  else NA
}

Any help is appreciated. 任何帮助表示赞赏。

Thanks. 谢谢。

EDIT : mu is a vector of length 2 specifying the population means, sigma is a vector of length 2 (specifying population standard deviations of the random variables), and rho is a scalar as correlation coefficient between the bivariate rv s. 编辑:mu是长度为2的向量,指定了总体平均值,sigma是长度为2的向量(指定了随机变量的总体标准偏差),rho是作为双变量rv s之间的相关系数的标量。

You can do it in closed form so there is no need for numeric optimization. 您可以以封闭形式进行操作,因此无需进行数值优化。 See wiki . 参见维基 Just use colMeans and cov and take note of the method argument in help("cov") and this comment 只需使用colMeanscov并注意help("cov")和此注释中的method参数

The denominator n - 1 is used which gives an unbiased estimator of the (co)variance for iid observations. 分母n - 1用于给出iid观测的(协)方差的无偏估计量。 These functions return NA when there is only one observation (whereas S-PLUS has been returning NaN ), and fail if x has length zero. 当只有一个观测值(而S-PLUS已返回NaN )时,这些函数将返回NA ,如果x长度为零,则这些函数将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM