Risk-neutral probability in a binomial market (source code)

= Risk-neutral probability in a binomial market

If the stock factors are $1+a$ and $1+b$ and the risk-free factor is $1+r$, the discounted stock is a martingale under the probability
$$
q=\frac{r-a}{b-a}
$$
of an up move. The probability of a down move is $(b-r)/(b-a)$.