#!/usr/bin/env python3
# Source: t3-probability-information.html
import numpy as np

rng = np.random.default_rng(0)
logits = np.array([2.0, 1.0, 0.0, -1.0])
probs = np.exp(logits - logits.max())
probs /= probs.sum()

# Gumbel-max: argmax(logits + Gumbel noise) is an exact draw from softmax(logits).
n = 200_000
g = -np.log(-np.log(rng.uniform(size=(n, logits.size))))
draws = np.argmax(logits + g, axis=1)
freq = np.bincount(draws, minlength=logits.size) / n

print("softmax  :", np.round(probs, 4))
print("gumbel   :", np.round(freq, 4))
# softmax  : [0.6439 0.2369 0.0871 0.0321]
# gumbel   : [0.6436 0.2374 0.0872 0.0318]
