"""V's hypothetical copy-votes demonstration. Python 3; standard library only.
Run: python3 judge-evidence-copy-votes.py
1 means a vote for A, 0 a vote for B. There are no correctness labels.
Exact duplication is NOT a new stochastic model call. Four invented questions
cannot establish model accuracy or justify population statistical inference.
The iid plug-in SE is deliberately misapplied after copying. The item-unit
SE is a formula-invariance check, not a validated uncertainty estimate.
"""
from fractions import Fraction
from itertools import product
from math import isclose, sqrt
from statistics import mean, stdev
import json

votes = [[1, 1, 0], [1, 0, 0], [1, 1, 1], [0, 0, 0]]

def report(rows):
    item_means = [Fraction(sum(row), len(row)) for row in rows]
    flat = [vote for row in rows for vote in row]
    p = mean(flat)
    return {
        "question_count": len(rows),
        "recorded_votes": len(flat),
        "votes_for_A": sum(flat),
        "question_support_for_A": [str(x) for x in item_means],
        "majority_choices": ["A" if x > Fraction(1, 2) else "B" for x in item_means],
        "mean_support_for_A": p,
        "deliberately_naive_iid_plugin_SE": sqrt(p * (1 - p) / len(flat)),
        "item_mean_SE_formula_only": stdev(map(float, item_means)) / sqrt(len(rows)),
    }

copies = [[vote for vote in row for _ in range(10)] for row in votes]
a, b = report(votes), report(copies)
for key in ["question_count", "question_support_for_A", "majority_choices",
            "mean_support_for_A", "item_mean_SE_formula_only"]:
    assert a[key] == b[key], key
ratio = b["deliberately_naive_iid_plugin_SE"] / a["deliberately_naive_iid_plugin_SE"]
assert isclose(ratio, 1 / sqrt(10))
# All 4^4 ordered item-resampling means are identical before/after copying.
# This establishes exact invariance, NOT validity of a four-item bootstrap.
def resampling_means(rows):
    means = [Fraction(sum(row), len(row)) for row in rows]
    return [sum((means[i] for i in ids), Fraction()) / len(rows)
            for ids in product(range(len(rows)), repeat=len(rows))]
assert resampling_means(votes) == resampling_means(copies)
print(json.dumps({"status": "hypothetical_arithmetic_not_model_experiment",
                  "before": a, "after_each_vote_copied_ten_times": b,
                  "naive_SE_ratio": ratio,
                  "all_256_item_resampling_means_unchanged": True}, indent=2))
