Take the 2-minute tour ×
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

I have a data that I want to sample such the resultant distribution of values should have specified mean and standard deviation. I can think of rejection sampling to achieve this however that seems to be overkill as rejection sampling tries to match complete distribution while I need only first two moment to be equal to specified value. There have to be easy way to do this and also multiple ways.

The current data have distribution

Min.    1st Qu.     Median     Mean     3rd Qu.    Max.   
0.0020  0.2120      0.3880     0.4298   0.6040     1.0000 

I want it to have mean of 0.2236 and standard deviation of 0.3180463.

share|improve this question
what distribution are you trying to sample from? Can you provide an example of what you are looking for / expect? –  Steve Reno Apr 22 at 16:11
@SteveReno provided example. –  avi Apr 22 at 16:23
This is a common question here on CV. Standardize (subtract sample mean, divide by sample standard deviation), giving mean 0, sd 1. Then multiply by desired sd, then add desired mean. –  Glen_b Apr 22 at 16:24
@Glen_b I cannot change the value of the data. They have biological meaning. I can only sample them to have different distribution. –  avi Apr 22 at 16:26
Are you asking, then, how to find some subset of the data that have a specified mean, SD (and size)? Or some multisubset? (In general this is not possible, but you could find such "samples" whose means and SDs are as close as possible to the specified ones.) One wonders what the purpose of such an exercise would be. –  whuber Apr 22 at 16:32
show 3 more comments

3 Answers 3

up vote 5 down vote accepted

Without looking at your data, and without needing to, I can bet 1K points of my reputation that you won't be able to pull a sample that has exactly this mean and exactly this standard deviation. This is just a zero probability event.

Without going into much asking about why you want to do this, here's an approach that will give you an approximate solution. Obtain maximum empirical likelihood weights that take your original data as input, and impose the two constraints for the two moments. Treating these weights as probabilities of selection, you will have a population with the true population mean equal to BLAH, and the true population variance equal to BLAH. You can then either pull the unequal probability samples explicitly, or simply expand your original data by a factor of say $10^4 \times$ {the empirical likelihood weight}, to get a finite population that has approximately the right mean and variance (to about four decimal points).

share|improve this answer
I kind of want to format your variable name in $\LaTeX$...I think ${\rm BLAH}$ is a much better default name than $x,\ \hat i$, etc.... –  Nick Stauner Apr 22 at 23:55
add comment

If you have $n$ sample points $(x_1,\dots,x_n)$ and want a subsample of $m$ points for which the subsample mean and subsample standard deviation approximate some given numbers $\mu^*$ and $\sigma^*$, one possibility is to think about it as an Integer Programming problem. A (possibly nonunique) solution is a vector $b\in\{0,1\}^n$, with $\sum_{i=1}^n b_i=m$, for which each coordinate indicates if the corresponding sample point is or is not included in the subsample, and $b$ minimizes something like $$ \left|\mu^* - \frac{1}{m}\sum_{i=1}^n x_ib_i\right| + \left| \sigma^* - \sqrt{\frac{1}{m-1}\sum_{i=1}^n \left(x_ib_i-\left(\frac{1}{m}\sum_{j=1}^n x_jb_j\right)\right)^2}\right| \, . $$ I don't know if this is feasible for your data set. There are freely available Integer Programming packages.

share|improve this answer
add comment

First of all as many pointed out it impossible to exactly match mean and distribution. Apologies for making not clear in the problem. All I wanted was sampled distribution as close as possible to target mean and SD.

Right now I implemented a rejection sampling which is closely linked to the solution suggested by @Stask. (Let's say) I was targeting a normal distribution $Q(x) = N( \mu, \sigma)$. The empirical distribution of the data $P(x)$ (this can be estimated using kernels or windowing function like Parzen). All need to be done is to draw sample from data with repetition uniform distribution for each sample $y$ if $Q(x)/P(x) > 1$ accept the sample otherwise accept with probability $Q(x)/P(x)$. As more sample will drawn the sample distribution will converge to $Q(x)$.

A layman version of the rejection sampling would be to divide $Q(x)$ in (large number of ) quantiles. For each quantile equal number of samples from the data which are in same range as within the quantile of $Q(x)$ . As number of quantile increases sampled distribution converges to $P(x)$.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.