# How to generate a $\pm 1$ sequence with mean $0.05$?

I know how to generate a $\pm 1$ sequence with mean $0$. For example, in Matlab, if I want to generate a $\pm 1$ sequence of length $10000$, it is:

2*(rand(1, 10000, 1)<=.5)-1


However, how to generate a $\pm 1$ sequence with mean $0.05$, i.e., with $1$ being slightly preferred?

Your desired mean is given by equation:

$\frac{N\cdot p - N \cdot (1-p)}{N} = .05$

from which follows that the probability of the 1s should be .525

In Python:

x = np.random.choice([-1,1], size=int(1e6), replace = True, p = [.475, .525])


Proof:

x.mean()
0.050742000000000002


1'000 experiments with 1'000'000 samples of 1s and -1s:

For the sake of completeness (hat tip to @Elvis ):

import scipy.stats as st
x = 2*st.binom(1, .525).rvs(1000000) - 1
x.mean()
0.053859999999999998


1'000 experiments with 1'000'000 samples of 1s and -1s:

And finally drawing from uniform distribution, as suggested by @Łukasz Deryło (also, in Python):

u = st.uniform(0,1).rvs(1000000)
x = 2*(u<.525) -1
x.mean()
0.049585999999999998


1'000 experiments with 1'000'000 samples of 1s and -1s:

All the three look virtually identical!

EDIT

Couple of lines on Central limit theorem and the spread of resulting distributions.

First of all, the draws of means indeed follow Normal Distribution.

Second, @Elvis in his comment to this answer did some nice calculations on the exact spread of the means drawn over 1'000 experiments (circa (0.048;0.052)), 95% confidence interval.

And these are results of the simulations, to confirm his results:

mn = []
for _ in range(1000):
mn.append((2*st.binom(1, .525).rvs(1000000) - 1).mean())
np.percentile(mn, [2.5,97.5])
array([ 0.0480773,  0.0518703])

• Nice job. My point with the Bernoulli was to reduce the question to a well known probability distribution ; from an 'implementation' point of view, your answer and Łukasz' were perfect. Jul 10 '17 at 14:59
• No kidding, yours is most scientific and the best! ;) I was thinking about Binomial distribution for half a second but that was not enough to turn it into -1 and 1's, so I borrowed your solution "as is", thanks! Jul 10 '17 at 15:03
• $\def\var{\text{var}}$ So with my notations, $\var(Y)= 4\var(X) = 4p(1-p) = 0.9975$, and the standard deviation of $Y$ is $\simeq 0.999$. When you take the mean on $10^6$ samples, the standard deviation is $0.999 \times 10^{-3}$ and 95% of the computed means should be in the interval $0.05 \pm 1.96 \times 0.999 \times 10^{-3}$, that is $(0.048; 0.052)$. Math check out! ;) Jul 10 '17 at 15:05

A variable with values $-1$ and $1$ is of the form $Y = 2X - 1$ with $X$ a Bernoulli with parameter $p$. Its expected value is $E(Y) = 2 E(X) - 1 = 2p - 1$, so you know how to obtain $p$ (here $p = 0.525$).

In R you can generate Bernoulli variables with rbinom(n, size = 1, prob = p), so for example

x <- rbinom(100, 1, 0.525)
y <- 2*x-1


Generate $N$ samples uniformly from $[0,1]$, recode numbers lower than 0.525 to 1 and rest to -1.

$1 \cdot 0.525 + (-1)\cdot (1-0.525) = 0.525 - 0.475 = 0.05$

I'm not a Matlab user, but I guess it sholud be

2*(rand(1, 10000, 1)<=.525)-1


You need to generate more 1s than -1s. Precisely, 5% more 1s because you want your mean to be 0.05. So, you increase the probability of 1s by 2.5% and decrease -1s by 2.5%. In your code it's equivalent to changing 0.5 to 0.525, i.e. from 50% to 52.5%

Just in case you want an EXACT 0.05 mean you can do the equivalent to the following R code in MATLAB:

sample(c(rep(-1, 95*50), rep(1, 105*50)))

• -1 this answer is wrong! The only thing that this code does is it randomly permutes the static vector of values. The output is not random!
– Tim
Jul 11 '17 at 8:43
• @Tim Why does it not work? It returns a list of -1 and 1 in a random order with counts designed to ensure an exact mean of 0.05. Jul 11 '17 at 13:15
• @Tim This solution is random. Have you tried running it repeatedly?
– whuber
Jul 11 '17 at 13:37
• @whuber this is the same as solution suggested by Amos Coats, the only difference is permuting of the values. Statistical properties of such sample will be deterministic and constant.
– Tim
Jul 11 '17 at 13:41
• @Tim I think you might be reading some unwarranted assumptions into this question that are not explicitly made. Although the frequencies--and therefore all moments--of the unordered sample itself will be constant, a great variety of "statistical properties" of the series that is generated will vary randomly. Since the example in the question generates an array, and arrays are not sets--order matters in an array--I think this interpretation is a fair one (and it illuminates the question). The "solution" posted by Coats, on the other hand, is a good joke--but SE doesn't like joking.
– whuber
Jul 11 '17 at 13:51