Lab 06 - Deterministic vs Stochastic¶
Goal: compare argmax routing with probabilistic sampling.
Info: Seeding for reproducibility
In compliance workflows, determinism is mandatory. Always seed random behavior for audit trails and incident investigation.
Info: Argmax vs sampling
Argmax is deterministic and fast; sampling is stochastic and diverse. Choose per decision: argmax for routing, sampling for drafts.
In [ ]:
Copied!
import random
intents = ["account_unlock", "vpn_issue", "security_incident"]
probs = [0.58, 0.31, 0.11]
def argmax_choice(labels, p):
idx = max(range(len(p)), key=lambda i: p[i])
return labels[idx]
print("Deterministic choice:", argmax_choice(intents, probs))
import random
intents = ["account_unlock", "vpn_issue", "security_incident"]
probs = [0.58, 0.31, 0.11]
def argmax_choice(labels, p):
idx = max(range(len(p)), key=lambda i: p[i])
return labels[idx]
print("Deterministic choice:", argmax_choice(intents, probs))
In [ ]:
Copied!
def sample_choice(labels, p, seed=None):
rng = random.Random(seed)
r = rng.random()
total = 0.0
for label, pr in zip(labels, p):
total += pr
if r <= total:
return label
return labels[-1]
print("Seeded run 1:", sample_choice(intents, probs, seed=7))
print("Seeded run 2:", sample_choice(intents, probs, seed=7))
print("Unseeded run:", sample_choice(intents, probs))
def sample_choice(labels, p, seed=None):
rng = random.Random(seed)
r = rng.random()
total = 0.0
for label, pr in zip(labels, p):
total += pr
if r <= total:
return label
return labels[-1]
print("Seeded run 1:", sample_choice(intents, probs, seed=7))
print("Seeded run 2:", sample_choice(intents, probs, seed=7))
print("Unseeded run:", sample_choice(intents, probs))