【发布时间】:2016-09-15 11:43:41
【问题描述】:
基于Mathematica UUPDE database 中给出的公式 我已经在 R 中绘制了标准正态分布的风险函数。
在一定范围内似乎是正确的;数值较大时会出现数值问题,见附图。以下是完整的 R 代码。
任何 cmets 将不胜感激。
PDF = function(x) { 1/(sqrt(2*pi))*exp(-x^2/2) }
erf <- function(x) 2 * pnorm(x * sqrt(2)) - 1
erfc <- function(x) 2 * pnorm(x * sqrt(2), lower = FALSE)
CDF = function(x) { 1/2 * (1 + erf(x/(sqrt(2)))) }
HF = function(x) { sqrt(2/pi)/(exp(x^2/2)*(2-erfc(-x/sqrt(2)))) }
SF = function(x) { 1 - 1/2 *erfc(-x/sqrt(2)) }
par(mar=c(3,3,1.5,0.5), oma=c(0,0,0,0), mgp=c(2,1,0))
par(mfrow = c(2, 2))
x = seq(from = -4,to = 10,by = .001)
##### PDF
a = PDF(x)
plot(x,a,'l',main='',ylab="PDF",xlab="x")
grid(nx = NULL,ny = NULL,col = "grey",lty = "dotted",lwd = par("lwd"),equilogs = TRUE)
##### CDF
a = CDF(x)
plot(x,a,'l',main='',ylab="CDF",xlab="x")
grid(nx = NULL,ny = NULL,col = "grey",lty = "dotted",lwd = par("lwd"),equilogs = TRUE)
##### HF
a = HF(x)
plot(x,a,'l',main='',ylab="HF",xlab="x")
grid(nx = NULL,ny = NULL,col = "grey",lty = "dotted",lwd = par("lwd"),equilogs = TRUE)
##### SF
a = SF(x)
plot(x,a,'l',main='',ylab="SF",xlab="x")
grid(nx = NULL,ny = NULL,col = "grey",lty = "dotted",lwd = par("lwd"),equilogs = TRUE)
【问题讨论】:
-
您的问题是什么?您需要任意精度还是只对特定范围感兴趣?
-
旁注:有趣的是您选择使用内置的普通 cdf 函数来获取 erf 函数,然后使用它来烘焙您自己的 cdf 函数。
-
@Roland 我只是对正确的风险函数图感兴趣,想知道哪里出了问题
-
@Dason,不可否认,在我这边使用 R 是一种非常低效的方式。
标签: r normal-distribution hazard