I have an expression now
expr = x + x^2
what I want to do is to replace $x$ with $k\times p$ , and $x^2$ with $[k\times p + k\times(k - 1)\times p^2]$.
The sense of this opperation is that $x$ is a binomial random variable, and I want to take expection of the expression above, so I make the substituation. However, when I try to make the first substitution
expr = ReplaceAll[expr, x -> k*p]
I obtain the following output
kp + k^2 p^2
which is not what I want. So my question is what can I do to obtain the right answer?
I am new to Mathematica, so my question may seem a little bit silly. But I am still appreciate if someone could help me with that.