""" Movie night polls, simulated: one vote each (plurality) vs approval voting. Feeds /guides/movie-night-poll-simulation. Served at https://nocturna.mov/research/poll-simulation.py. Re-run with `python3 apps/web/public/research/poll-simulation.py`; the seed is fixed, so the numbers in the post come out exactly the same. The model (stated in the post, keep them in sync): - A group of 4 to 10 friends picks from 3 to 6 films. Each size is equally likely. - Every film has one main genre out of 6 (thriller, comedy, horror, drama, sci-fi, animation), and a quality between 0 and 1. - Every person has a taste for each genre between 0 and 1, and a taste for the film itself (their personal reaction to it, between 0 and 1). - A person's liking for a film = 0.5 * genre taste + 0.3 * film quality + 0.2 * personal reaction. - Each person is "happy to watch" the films they like at least 0.55, and always the one they like most. - One vote each: everyone votes for the film they like most. Approval: everyone ticks every film they are happy to watch. Ties are broken at random in both. - A "good pick" is the film the most people are happy to watch (ties: any of them counts). """ import random import statistics import sys SEED = 20261029 RUNS = 100_000 GENRES = 6 # The bar for "happy to watch". Pass another as the first argument to test how much it matters. THRESHOLD = float(sys.argv[1]) if len(sys.argv) > 1 else 0.55 rng = random.Random(SEED) def run(): people = rng.randint(4, 10) films = rng.randint(3, 6) genre = [rng.randrange(GENRES) for _ in range(films)] quality = [rng.random() for _ in range(films)] taste = [[rng.random() for _ in range(GENRES)] for _ in range(people)] like = [[0.5 * taste[p][genre[f]] + 0.3 * quality[f] + 0.2 * rng.random() for f in range(films)] for p in range(people)] happy = [] for p in range(people): best = max(range(films), key=lambda f: like[p][f]) happy.append({f for f in range(films) if like[p][f] >= THRESHOLD} | {best}) happy_count = [sum(f in h for h in happy) for f in range(films)] top_votes = [0] * films for p in range(people): top_votes[max(range(films), key=lambda f: like[p][f])] += 1 def winner(score): top = max(score) return rng.choice([f for f in range(films) if score[f] == top]) plural = winner(top_votes) approval = winner(happy_count) best = max(happy_count) # Two films from the same genre in the poll: the case where fans split. shared_genre = len(set(genre)) < films return { 'people': people, 'films': films, 'plural_happy': happy_count[plural] / people, 'approval_happy': happy_count[approval] / people, 'plural_best': happy_count[plural] == best, 'approval_best': happy_count[approval] == best, 'plural_minority': happy_count[plural] * 2 < people, 'approval_minority': happy_count[approval] * 2 < people, 'differ': plural != approval, 'plural_majority_vote': top_votes[plural] * 2 > people, 'shared_genre': shared_genre, } rows = [run() for _ in range(RUNS)] print(f'threshold {THRESHOLD}, seed {SEED}, {RUNS:,} polls') def pct(rows, key): return 100 * sum(r[key] for r in rows) / len(rows) def report(label, rs): print(f'\n{label} (n={len(rs):,})') print(f" winner is a film most-people-happy pick: plurality {pct(rs, 'plural_best'):.1f}% approval {pct(rs, 'approval_best'):.1f}%") print(f" avg share of the group happy with winner: plurality {100 * statistics.mean(r['plural_happy'] for r in rs):.1f}% approval {100 * statistics.mean(r['approval_happy'] for r in rs):.1f}%") print(f" winner leaves most of the group unhappy: plurality {pct(rs, 'plural_minority'):.1f}% approval {pct(rs, 'approval_minority'):.1f}%") print(f" the two methods pick different films: {pct(rs, 'differ'):.1f}%") print(f" plurality winner had a majority of votes: {pct(rs, 'plural_majority_vote'):.1f}%") report('All polls', rows) report('Two or more films share a genre', [r for r in rows if r['shared_genre']]) report('Every film a different genre', [r for r in rows if not r['shared_genre']]) for films in range(3, 7): report(f'{films} films in the poll', [r for r in rows if r['films'] == films]) for lo, hi in ((4, 5), (6, 7), (8, 10)): report(f'{lo} to {hi} people', [r for r in rows if lo <= r['people'] <= hi])