Random Variables · Linearity of Mathematical Expectation
Lesson 7
It can be calculated directly from the definition: for each of the \(n!\) permutations, write down how many fixed points it has, then take their average. As \(n\) increases, this becomes more difficult both on paper and on a computer.
from itertools import permutations
from statistics import mean
n = 8
totals = []
for p in permutations(range(n)):
fixed = [i for i in range(n) if p[i] == i]
totals.append(len(fixed))
print(mean(totals))1