# Generate numbers (mostly) from min to max So, if you set your mean to the middle of your desired minimum value and maximum value, and set your standard deviation to 1/3 of your mean, you get (mostly) values that fall within the desired interval. In a normal distribution, 99.7% of values fall within 3 standard deviations of the mean. The quick-and-dirty approach is to use the 68-95-99.7 rule. $^\dagger$ Note that the truncation alters the mean and variance of the distribution, so $\mu$ and $\sigma^2$ are not the mean and variance of the truncated distribution. It would also not be too difficult to program associated density and quantile functions for the truncated distribution. It would be easy to program functions for other truncated distributions via the same method. This is a vectorised function that will generate N IID random variables from the truncated normal distribution. U <- runif(N, pnorm(a, mean, sd), pnorm(b, mean, sd)) If you want to generate values from a truncated normal distribution, with specified lower and upper bounds $a b) stop('Error: Truncation range is empty') None of the answers here give an efficient method of generating truncated normal variables that does not involve rejection of arbitrarily large numbers of generated values. Xi'an's answer here, which has a link to his arXiv paper (along with some other worthwhile responses). Looking around, a lot of this is covered in answers on other questions (but not exactly duplicates since this question is more general than just the truncated normal). The truncnorm package in R also has functions to draw from a truncated normal. The MSM package in R has a function, rtnorm, that calculates draws from a truncated normal. The same Wikipedia link mentions two specific packages (both on CRAN) with functions for generating truncated normals: There are other approaches the same Wikipedia page mentions adapting the ziggurat method, that should work for a variety of distributions. If you have a reasonably efficient cdf and inverse cdf (such as pnorm and qnorm for the normal distribution in R) you can use the inverse-cdf method described in the first paragraph of the simulating section of the Wikipedia page on the truncated normal. but you weren't far into the tail, a uniform majorizing would work okay with the normal, for example. If the limits were reasonably narrow relative to the s.d. You could use accept-reject with some suitable majorizing function over the interval (in some cases uniform will be good enough). In R you could avoid the one-at-a-time loop by computing the area within the bounds and generate enough values that you could be almost certain that after throwing out the values outside the bounds you still had as many values as needed. If most of the distribution is within the bounds, this is pretty reasonable but it can get quite slow if you nearly always generate outside the limits. Here's one very simple method for generating one at a time (in some kind of pseudocode): I'll illustrate some approaches on your normal example. There are a variety of methods for doing so, some simple, some relatively efficient. It sounds like you want to simulate from a truncated distribution, and in your specific example, a truncated normal.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |