Expected Value

import numpy as np
import bplot as bp
x = np.arange(4) + 1
fx = np.asarray([.2, .1, .4, .3])
EX = np.sum(x * fx)
(x * fx).sum()
2.8
bp.point(x, fx)
bp.point(EX, 0, color = '#2ca02c', style="+")
bp.labels(x = 'x', y = 'f(x)', size = 16)
bp.title("Arbitrary random variable", size=16);
np.random.choice(x, p = fx, size = 10000).mean()
2.7967

Uniform

x = np.arange(6) + 1
fx = np.repeat((1/6), 6)
EX = np.sum(x * fx)
VX = np.sum((x - EX) ** 2 * fx)

print(f"E(X) = {EX}")
print(f"V(X) = {VX}")
E(X) = 3.5
V(X) = 2.9166666666666665
np.random.choice(x, p = fx, size = 10000).mean()
3.4983
bp.point(x, fx)
bp.point(EX, 0, color = '#2ca02c', style="+")
bp.labels(x = 'x', y = 'f(x)', size = 16)
bp.title("Uniform", size=16);

Tail Heavy

fx = np.asarray([.4, .05, .05, .05, .05, .4])
EX = np.sum(x * fx)
VX = np.sum((x - EX) ** 2 * fx)

print(f"E(X) = {EX}")
print(f"V(X) = {VX}")
E(X) = 3.5000000000000004
V(X) = 5.250000000000002
bp.point(x, fx)
bp.point(EX, 0, color = '#2ca02c', style="+")
bp.labels(x = 'x', y = 'f(x)', size = 16)
bp.title("Tail Heavy", size=16);

Mean Heavy

fx = np.asarray([.05, .15, .3, .3, .15, .05])
EX = np.sum(x * fx)
VX = np.sum((x - EX) ** 2 * fx)

print(f"E(X) = {EX}")
print(f"V(X) = {VX}")
E(X) = 3.5
V(X) = 1.4499999999999997
bp.point(x, fx)
bp.point(EX, 0, color = '#2ca02c')
bp.labels(x = 'x', y = 'f(x)', size = 16)
bp.title("Mean heavy", size=16);

Another, for experimenting

x = np.arange(6) + 1
fx = np.asarray([.2, .05, .25, .25, .05, .2])
EX = np.sum(x * fx)
np.sum((x - EX) ** 2 * fx)
2.85
bp.point(x, fx)
bp.point(EX, 0, color = '#2ca02c', style="+")
bp.labels(x = 'x', y = 'f(x)', size = 16);