11
$\begingroup$

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?

$\endgroup$
18
$\begingroup$

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: enter image description here

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:

enter image description here

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:

enter image description here

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])
$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – Elvis
    Jul 10 '17 at 14:59
  • $\begingroup$ 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! $\endgroup$ Jul 10 '17 at 15:03
  • 1
    $\begingroup$ $\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! ;) $\endgroup$
    – Elvis
    Jul 10 '17 at 15:05
12
$\begingroup$

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
$\endgroup$
5
$\begingroup$

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

Then your expected value is

$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
$\endgroup$
1
4
$\begingroup$

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%

$\endgroup$

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