MASS 库中的 fitdistr 在拟合 t 分布时发出警告
fitdistr from MASS library gives warnings when fitting t-distribution
我正在尝试重现 David Ruppert 的 "Statistics and Data Analysis for Financial Engineering" 中的以下示例,该示例符合历史无风险利率的学生 t 分布:
library(MASS)
data(Capm, package = "Ecdat")
x <- Capm$rf
fitt <- fitdistr(x,"t", start = list(m=mean(x),s=sd(x)), df=3)
as.numeric(fitt$estimate)
0.437310595161651 0.152205764779349
输出伴随以下警告消息:
警告信息:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs produced
从 R 的帮助文件中可以看出,MASS::fitdistr
使用最大似然法来寻找最佳参数。然而,当我手动优化时(同一本书),一切顺利,没有警告:
library(fGarch)
loglik_t <- function(beta) {sum( - dt((x - beta[1]) / beta[2],
beta[3], log = TRUE) + log(beta[2]) )}
start <- c(mean(x), sd(x), 5)
lower <- c(-1, 0.001, 1)
fit_t <- optim(start, loglik_t, hessian = T, method = "L-BFGS-B", lower = lower)
fit_t$par
0.44232633269102 0.163306955396773 4.12343777572566
拟合的参数在可接受的标准误差范围内,而且,除了均值和标准偏差,我还得到了 df
.
有人可以给我建议吗:
- 为什么
MASS::fitdistr
会产生警告,而通过 fGarch::optim
的优化会在没有警告的情况下成功?
- 为什么
MASS::fitdistr
输出中没有df
?
- 有没有办法在没有警告的情况下运行
MASS:fitdistr
此数据并得到df
?
免责声明:
类似的问题被问了几次都没有答案here and here
您没有将 lower
参数传递给函数 fitdistr
,这导致它在正域和负域中进行搜索。通过将 lower
参数传递给函数
fitt <- fitdistr(x,"t", start = list(m=mean(x),s=sd(x)), df=3, lower=c(-1, 0.001))
您没有得到 NaN - 正如您在手动优化中所做的那样。
编辑:
fitt <- fitdistr(x,"t", start = list(m=mean(x),s=sd(x),df=3),lower=c(-1, 0.001,1))
returns 非整数自由度结果。但是,我想,它的四舍五入值 round(fitt$estimate['df'],0)
可用于拟合自由度参数。
我正在尝试重现 David Ruppert 的 "Statistics and Data Analysis for Financial Engineering" 中的以下示例,该示例符合历史无风险利率的学生 t 分布:
library(MASS)
data(Capm, package = "Ecdat")
x <- Capm$rf
fitt <- fitdistr(x,"t", start = list(m=mean(x),s=sd(x)), df=3)
as.numeric(fitt$estimate)
0.437310595161651 0.152205764779349
输出伴随以下警告消息:
警告信息:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs produced
从 R 的帮助文件中可以看出,MASS::fitdistr
使用最大似然法来寻找最佳参数。然而,当我手动优化时(同一本书),一切顺利,没有警告:
library(fGarch)
loglik_t <- function(beta) {sum( - dt((x - beta[1]) / beta[2],
beta[3], log = TRUE) + log(beta[2]) )}
start <- c(mean(x), sd(x), 5)
lower <- c(-1, 0.001, 1)
fit_t <- optim(start, loglik_t, hessian = T, method = "L-BFGS-B", lower = lower)
fit_t$par
0.44232633269102 0.163306955396773 4.12343777572566
拟合的参数在可接受的标准误差范围内,而且,除了均值和标准偏差,我还得到了 df
.
有人可以给我建议吗:
- 为什么
MASS::fitdistr
会产生警告,而通过fGarch::optim
的优化会在没有警告的情况下成功? - 为什么
MASS::fitdistr
输出中没有df
? - 有没有办法在没有警告的情况下运行
MASS:fitdistr
此数据并得到df
?
免责声明:
类似的问题被问了几次都没有答案here and here
您没有将 lower
参数传递给函数 fitdistr
,这导致它在正域和负域中进行搜索。通过将 lower
参数传递给函数
fitt <- fitdistr(x,"t", start = list(m=mean(x),s=sd(x)), df=3, lower=c(-1, 0.001))
您没有得到 NaN - 正如您在手动优化中所做的那样。
编辑:
fitt <- fitdistr(x,"t", start = list(m=mean(x),s=sd(x),df=3),lower=c(-1, 0.001,1))
returns 非整数自由度结果。但是,我想,它的四舍五入值 round(fitt$estimate['df'],0)
可用于拟合自由度参数。