Let's say I have a value 100 and I want to perturb this value randomly by 1% based on a normal distribution. How could I do this in MATLAB? I seem to have some confusion with randn etc.
I have used randn so far but I do not seem to be inside this 1% threshold all the time.
The output of the normal distribution
randnis unbounded, it does not give values in the range[0,1]likeranddoes.This paper offers a good explanation of the "tool" you'll need, which is the truncated normal distribution: https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf
This explicitly sets the function output to 0 outside your bounded range, and scales it by the difference of the cumulative distribution evaluated at
banda. Note that the standard deviation of this new distribution will not be the same as the original standard deviation, although if we choose symmetricalaandbaround the mean then that should remain unchanged.I'm going to skip the maths and implement one suggested sampling method from the linked literature which simply "imposes the truncation interval by rejection" with the following condition:
Where
Ф⁻¹(µ, σ; p)is the inverse CDF of your normal distribution with meanµand standard deviationσ, andrand()generates a uniform random number. MATLAB has an in-built function for sampling the inverse CDF if you have the Statistics toolbox:icdf('normal',p,mu,sigma)So we can write a function like
Testing this for different values of sigma, and a symmetrical distribution around mean
0and interval[-1,1]. For your example, you could add these values to a base value of100:You can see that for sigma values where the truncation interval lies several standard deviations away from the mean, there is hardly any change to the distribution. However as the standard deviation gets closer to the boundary we see the distribution become less and less "normal" looking.