halfnorm: The Half-Normal Distribution

Description Usage Arguments Details Value See Also Examples

Description

Density, distribution function, quantile function and random generation for the half-normal distribution with parameter theta.

Usage

1
2
3
4
5
6
dhalfnorm(x, theta=sqrt(pi/2), log = FALSE)
phalfnorm(q, theta=sqrt(pi/2), lower.tail = TRUE, log.p = FALSE)
qhalfnorm(p, theta=sqrt(pi/2), lower.tail = TRUE, log.p = FALSE)
rhalfnorm(n, theta=sqrt(pi/2))
sd2theta(sd)
theta2sd(theta)

Arguments

x,q

vector of quantiles.

p

vector of probabilities.

n

number of observations. If length(n) > 1, the length is taken to be the number required.

theta

parameter of half-normal distribution.

log, log.p

logical; if TRUE, probabilities p are given as log(p).

lower.tail

logical; if TRUE (default), probabilities are P[X <= x], otherwise, P[X > x].

sd

standard deviation of the zero-mean normal distribution that corresponds to the half-normal with parameter theta.

Details

x = abs(z) follows a half-normal distribution with if z is a normal variate with zero mean. The half-normal distribution has density

f(x) = 2*theta/pi e^-(x^2*theta^2/pi)

It has mean E(x) = 1/theta and variance Var(x) = (pi-2)/(2*theta^2).

The parameter theta is related to the standard deviation sigma of the corresponding zero-mean normal distribution by the equation theta = sqrt(pi/2)/sigma.

If theta is not specified in the above functions it assumes the default values of sqrt(pi/2), corresponding to sigma=1.

Value

dhalfnorm gives the density, phalfnorm gives the distribution function, qhalfnorm gives the quantile function, and rhalfnorm generates random deviates. sd2theta computes a theta parameter. theta2sd computes a sd parameter.

See Also

Normal.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# load "fdrtool" library
library("fdrtool")


## density of half-normal compared with a corresponding normal
par(mfrow=c(1,2))

sd.norm = 0.64
x  = seq(0, 5, 0.01)
x2 = seq(-5, 5, 0.01)
plot(x, dhalfnorm(x, sd2theta(sd.norm)), type="l", xlim=c(-5, 5), lwd=2,
   main="Probability Density", ylab="pdf(x)")
lines(x2, dnorm(x2, sd=sd.norm), col=8 )


plot(x, phalfnorm(x, sd2theta(sd.norm)), type="l", xlim=c(-5, 5),  lwd=2,
   main="Distribution Function", ylab="cdf(x)")
lines(x2, pnorm(x2, sd=sd.norm), col=8 )

legend("topleft", 
c("half-normal", "normal"), lwd=c(2,1),
col=c(1, 8), bty="n", cex=1.0)

par(mfrow=c(1,1))


## distribution function

integrate(dhalfnorm, 0, 1.4, theta = 1.234)
phalfnorm(1.4, theta = 1.234)

## quantile function
qhalfnorm(-1) # NaN
qhalfnorm(0)
qhalfnorm(.5)
qhalfnorm(1)
qhalfnorm(2) # NaN

## random numbers
theta = 0.72
hz = rhalfnorm(10000, theta)
hist(hz, freq=FALSE)
lines(x, dhalfnorm(x, theta))

mean(hz) 
1/theta  # theoretical mean

var(hz)
(pi-2)/(2*theta*theta) # theoretical variance


## relationship with two-sided normal p-values
z = rnorm(1000)

# two-sided p-values
pvl = 1- phalfnorm(abs(z))
pvl2 = 2 - 2*pnorm(abs(z)) 
sum(pvl-pvl2)^2 # equivalent
hist(pvl2, freq=FALSE)  # uniform distribution

# back to half-normal scores
hz = qhalfnorm(1-pvl)
hist(hz, freq=FALSE)
lines(x, dhalfnorm(x))

Example output

0.831928 with absolute error < 9.2e-15
[1] 0.831928
[1] NaN
[1] 0
[1] 0.6744898
[1] Inf
[1] NaN
Warning message:
In qnorm((p + 1)/2, mean = 0, sd = sd.norm) : NaNs produced
[1] 1.37659
[1] 1.388889
[1] 1.101962
[1] 1.101073
[1] 0

fdrtool documentation built on Nov. 14, 2021, 1:07 a.m.

Related to halfnorm in fdrtool...