TNorm: Truncated normal distribution

Description Usage Arguments Value Author(s) References See Also Examples

Description

Random generation for the truncated normal distribution. The mean and standard deviation of the original normal distribution are mean and sd. Truncation limits are given by a, b, type of truncation is given by trunc.

Usage

1
rTNorm(n, mean=0, sd=1, a, b, trunc)

Arguments

mean

mean (if common for all observations) or a vector of length n of means.

sd

standard deviation (if common for all observations) or a vector of length n of standard deviations.

Note that mean and sd must have the same length, either 1 or n.

a

truncation limit 1 (if common for all observations) or a vector of length n of truncation limits 1.

b

truncation limit 2 (if common for all observations) or a vector of length n of truncation limits 2.

trunc

type of truncation (if common for all observations) or a vector of length n of types of truncation

trunc=0

normal distribution is truncated on the interval (a, Infty). Value of b is ignored.

trunc=1

degenerated normal distribution, all values are with probability 1 equal to a, b is ignored.

trunc=2

normal distribution is truncated on the interval (-Infty, a). Value of b is ignored.

trunc=3

normal distribution is truncated on the interval (a,\,b).

trunc=4

there is no truncation, values of a and b are ignored.

If trunc is not given, it is assumed that it is equal to 4. Note that a, b and trunc must have the same length, either 1 or n with exception that b does not have to be supplied if trunc is 0, 1, 2 or 4.

n

number of observations to be sampled.

Value

A numeric vector with sampled values.

Author(s)

Arnošt Komárek arnost.komarek[AT]mff.cuni.cz

References

Geweke, J. (1991). Efficient simulation from the multivariate normal and Student-t distributions subject to linear constraints and the evaluation of constraint probabilities. Computer Sciences and Statistics, 23, 571–578.

See Also

rnorm, rTMVN.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
set.seed(1977)

### Not truncated normal distribution
x1 <- rTNorm(1000, mean=10, sd=3)
c(mean(x1), sd(x1), range(x1))

### Truncation from left only
x2 <- rTNorm(1000, mean=10, sd=3, a=7, trunc=0)
c(mean(x2), sd(x2), range(x2))

### Degenerated normal distribution
x6 <- rTNorm(1000, mean=10, sd=3, a=13, trunc=1)
c(mean(x6), sd(x6), range(x6))

### Truncation from right only
x3 <- rTNorm(1000, mean=10, sd=3, a=13, trunc=2)
c(mean(x3), sd(x3), range(x3))

### Truncation from both sides
x4 <- rTNorm(1000, mean=10, sd=3, a=7, b=13, trunc=3)
c(mean(x4), sd(x4), range(x4))

x5 <- rTNorm(1000, mean=10, sd=3, a=5.5, b=14.5, trunc=3)
c(mean(x5), sd(x5), range(x5))

oldPar <- par(mfrow=c(2, 3))
hist(x1, main="N(10, 3^2)")
hist(x2, main="TN(10, 3^2, 7, Infty)")
hist(x6, main="TN(10, 3^2, 13, 13)")
hist(x3, main="TN(10, 3^2, -Infty, 13)")
hist(x4, main="TN(10, 3^2, 7, 13)")
hist(x5, main="TN(10, 3^2, 5.5, 14.5)")
par(oldPar)

### Different truncation limits
n <- 1000
a <- rnorm(n, -2, 1)
b <- a + rgamma(n, 1, 1)
trunc <- rep(c(0, 1, 2, 3, 4), each=n/5)
x7 <- rTNorm(n, mean=1, sd=2, a=a, b=b, trunc=trunc)
cbind(trunc, a, x7)[1:10,]
sum(x7[1:(n/5)] > a[1:(n/5)])      ## must be equal to n/5

cbind(trunc, a, x7)[201:210,]
sum(x7[(n/5+1):(2*n/5)] == a[(n/5+1):(2*n/5)])         ## must be equal to n/5

cbind(trunc, x7, a)[401:410,]
sum(x7[(2*n/5+1):(3*n/5)] < a[(2*n/5+1):(3*n/5)])      ## must be equal to n/5

cbind(trunc, a, x7, b)[601:610,]
sum(x7[(3*n/5+1):(4*n/5)] > a[(3*n/5+1):(4*n/5)])      ## must be equal to n/5
sum(x7[(3*n/5+1):(4*n/5)] < b[(3*n/5+1):(4*n/5)])      ## must be equal to n/5

cbind(trunc, x7)[801:810,]

### Different moments and truncation limits
n <- 1000
mu <- rnorm(n, 1, 0.2)
sigma <- 0.5 + rgamma(n, 1, 1)
a <- rnorm(n, -2, 1)
b <- a + rgamma(n, 1, 1)
trunc <- rep(c(0, 1, 2, 3, 4), each=n/5)
x8 <- rTNorm(n, mean=1, sd=2, a=a, b=b, trunc=trunc)

### Truncation from left only
### (extreme cases when we truncate to the area
###  where the original normal distribution has
###  almost zero probability)
x2b <- rTNorm(1000, mean=0, sd=1, a=7.9, trunc=0)
c(mean(x2b), sd(x2b), range(x2b))

x2c <- rTNorm(1000, mean=1, sd=2, a=16, trunc=0)
c(mean(x2c), sd(x2c), range(x2c))

### Truncation from right only (extreme cases)
x3b <- rTNorm(1000, mean=0, sd=1, a=-7.9, trunc=2)
c(mean(x3b), sd(x3b), range(x3b))

x3c <- rTNorm(1000, mean=1, sd=2, a=-13, trunc=2)
c(mean(x3c), sd(x3c), range(x3c))

### Truncation from both sides (extreme cases)
x4b <- rTNorm(1000, mean=0, sd=1, a=-9, b=-7.9, trunc=3)
c(mean(x4b), sd(x4b), range(x4b))

x4c <- rTNorm(1000, mean=0, sd=1, a=7.9, b=9, trunc=3)
c(mean(x4c), sd(x4c), range(x4c))

Example output

Loading required package: colorspace
Loading required package: lme4
Loading required package: Matrix

### Mixture of methods including mixtures
### Arnost Komarek

### See citation("mixAK") or toBibtex(citation("mixAK")) for the best way to cite
### the package if you find it useful.


[1]  9.9338613  3.0900475  0.1537227 18.8622153
[1] 10.893230  2.395869  7.024839 19.463740
[1] 13  0 13 13
[1]  9.246320  2.284959  2.081407 12.985907
[1]  9.932905  1.621369  7.003948 12.997329
[1] 10.066537  2.272369  5.502337 14.411148
      trunc         a         x7
 [1,]     0 -1.414811  0.1648144
 [2,]     0 -2.263320 -1.8038055
 [3,]     0 -1.388657  0.3352667
 [4,]     0 -1.508865  1.7570021
 [5,]     0 -1.307260  1.7462429
 [6,]     0 -2.824149 -0.1162081
 [7,]     0 -1.292072  2.1303457
 [8,]     0 -1.443852  2.5093005
 [9,]     0 -2.803870  3.3863111
[10,]     0 -1.974448  3.6966346
[1] 200
      trunc          a         x7
 [1,]     1 -4.9938680 -4.9938680
 [2,]     1 -1.8048689 -1.8048689
 [3,]     1 -4.6117579 -4.6117579
 [4,]     1 -0.7367241 -0.7367241
 [5,]     1 -2.9264663 -2.9264663
 [6,]     1 -2.1862644 -2.1862644
 [7,]     1 -1.4273745 -1.4273745
 [8,]     1 -2.1457919 -2.1457919
 [9,]     1 -1.4757866 -1.4757866
[10,]     1 -3.0037826 -3.0037826
[1] 200
      trunc        x7          a
 [1,]     2 -1.142592 -1.1134026
 [2,]     2 -2.893612 -2.5788893
 [3,]     2 -3.206292 -2.5080784
 [4,]     2 -3.755503 -2.8552056
 [5,]     2 -1.041637 -0.9942981
 [6,]     2 -2.387568 -1.9478067
 [7,]     2 -1.946592 -1.5396170
 [8,]     2 -2.415195 -1.0026723
 [9,]     2 -2.373687 -1.9731415
[10,]     2 -1.753092 -1.4343813
[1] 200
      trunc          a         x7           b
 [1,]     3 -1.7917156 -1.5338094 -1.45999565
 [2,]     3 -1.4620674 -1.1737261 -0.54094272
 [3,]     3 -1.7472294 -0.7491825 -0.57492355
 [4,]     3 -0.5787103 -0.4850831  0.20521225
 [5,]     3 -2.2608909 -1.1786224 -0.89426676
 [6,]     3 -2.6073149 -2.5840902 -2.38897193
 [7,]     3 -2.7554894 -2.6976244 -2.46933978
 [8,]     3 -1.0363808 -0.1000197 -0.04711515
 [9,]     3 -0.8024094 -0.4271664 -0.34967472
[10,]     3 -2.5008871 -1.8890855 -1.73012489
[1] 200
[1] 200
      trunc         x7
 [1,]     4 -1.5194528
 [2,]     4 -0.4708495
 [3,]     4 -0.5921021
 [4,]     4  0.7671957
 [5,]     4  4.2544396
 [6,]     4  0.8510144
 [7,]     4  0.4205640
 [8,]     4 -1.6740098
 [9,]     4  3.7020944
[10,]     4  2.1375473
[1] 7.97541018 0.03811854 7.89571397 8.00000000
[1] 16.2415683  0.2354292 16.0003742 17.0000000
[1] -7.97632752  0.03729069 -8.00000000 -7.90010724
[1] -13.2737344   0.2774024 -15.0000000 -13.0002948
[1] -7.97730712  0.03683842 -8.00000000 -7.90021017
[1] 7.97363019 0.03892174 7.89571397 8.00000000

mixAK documentation built on July 2, 2020, 2:48 a.m.

Related to TNorm in mixAK...