# Evaluation of RandomChoice in a summation

Of course the familiar summation

Sum[i, {i,n}]


yields $n (1 + n)/2$.

I'd like to add integers with an arbitrary randomly chosen sign (+ or -) before each term in the summation, as in

Sum[RandomChoice[{1,-1}] i, {i,n}]


or

Sum[RandomChoice[{i, -i}], {i, n}]


I don't expect there to be a nice closed-form solution, as it will depend upon the particular random signs that are assigned in any evaluation. In fact, there is no natural answer to the summation, since the limit is a variable $n$.

Nevertheless, both these functions yield either $n (1+n)/2$ or $-n (1+n)/2$, showing that the RandomChoice is fixed within the summation. Even if the limit of summation is specified with an integer value (e.g., $n = 9$), the RandomChoice is held fixed, giving either a positive or a negative value for the summation.

I've tried various forms of Evaluate, but that doesn't change the summation.

How can I ensure that each calculation invokes a random sign for each term in the summation? Or... how does Mathematica know that there cannot be a closed form solution and thus does not re-evaluate the RandomChoice for each term?

• The "details and options" in the help page for Sum says "If a sum cannot be carried out explicitly by adding up a finite number of terms, Sum will attempt to find a symbolic result. In this case, f is first evaluated symbolically." I suspect that means in your examples that means your RandomChoice either becomes 1 or -1 and the evaluation proceeds from there.
– Bill
Sep 27, 2017 at 1:31
• @Bill: Yes... I suppose that is correct. But I just checked and the "fixed" RandomChoice even holds with a specified summation limit, e.g., $n = 9$, giving a fixed summation (up to sign). Weird! Sep 27, 2017 at 1:33
• Trace[Sum[RandomChoice[{-1, 1}], {i, 9}]] doesn't seem to show "fixed" RandomChoice for me. I'm also unsure what "natural" result you might be able to suggest should be the result of Sum[RandomChoice[{-1, 1}], {i, n}] where n has not value assigned. Perhaps just Sum[RandomChoice[{-1, 1}], {i, n}] ?
– Bill
Sep 27, 2017 at 1:37
• Is what you want n = 6; sum=Total[Table[RandomChoice[{1, -1}] i, {i, n}]] ? If so, then sum is a random variable. Do you want the distribution of sum for a general n?
– JimB
Sep 27, 2017 at 2:02
• Agree with @Bill, if n is unassigned, the expression should be returned unevaluated. If n is assigned an integer, it works as expected.
– Alan
Sep 27, 2017 at 4:03

Sum, like several other Mathematica functions, has two distinct purposes:

1. Compute numbers in a loop and add them up. This is a "programming use" of Sum. It functions comparably to Table or the for loop of other languages.

2. Compute symbolic sums. This is a "mathematical use" of Sum.

Personally, I find it a bit ugly that these two very different things are mixed together in the same function name ("Sum").

These two uses require very different evaluation behaviour. When used as a programming construct, Sum does not (must not) evaluate its argument before substituting in the value of the iterator. When used as a symbolic math function, there is no iteration, and therefore there is no true iterator. The argument is evaluated immediately, and analytical methods are used to arrive to a result.

With symbolic limits, Sum will always do a symbolic calculation, and will not iterate. This is mentioned in the documentation under Details.

You need to decide which use you want. If you want it to iterate, n must be replaced by an explicit number, to force Sum to run a loop (and to not evaluate its argument immediately):

sum[n_Integer] := Sum[RandomChoice[{1,-1}] i, {i,n}]


A symbolic math use does not make sense here.

Personal note: I strongly dislike that these two very distinct functionalities are accessible through the same Mathematica function. I do use Sum for symbolic calculations without worries, but I tend to avoid it for programming uses because I am never entirely sure when it will switch to using symbolic/analytic methods. The documentation does say:

If the range of a sum is finite, i is typically assigned a sequence of values, with f being evaluated for each one.

But what does "typically" mean? Is that a guarantee?

From the timings, it is pretty clear that Sum[i, {i, 1, 1000000000}] is evaluated symbolically. If we change the body to something that can't be handled symbolically, like Sum[Boole@OddQ[i], {i, 1, 1000000000}], then it will loop. The problem is that the system decides automatically, and in a way that is not at all transparent (or controllable) to the user.

If I am going to use Sum as a programming function (i.e. I want looping/iteration) then I must be assured that it will actually do that. Otherwise using programming (non-math) functions in the body of Sum, like RandomChoice, will mess things up.

Note: One specific case where I am comfortable using Sum as a programming function is inside Compile.

Update:

We can force Sum to do an explicit iteration using Method -> "Procedural". If the limits are symbolic, it simply won't evaluate the sum, but it will still evaluate its argument prematurely.

Sum[RandomChoice[{1, -1}] i, {i, n}, Method -> "Procedural"]
(* Sum[-i, {i, n}, Method -> "Procedural"] *)

• Your Update works fine. Thanks. +1 and accept. Sep 27, 2017 at 16:36