sangha 1.0.0

Sangha — sociology engine for social networks, game theory, and group dynamics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
//! Multi-agent coordination — public goods games, auctions, mechanism design.

use serde::{Deserialize, Serialize};

use crate::error::{
    Result, SanghaError, validate_finite, validate_non_negative, validate_positive,
};

/// Configuration for an N-player public goods game.
///
/// Each player has an endowment they may contribute to a public pool.
/// The pool is multiplied by `multiplier` and split evenly among all players.
///
/// Deserialization validates invariants automatically.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct PublicGoodsGame {
    /// Number of players.
    pub player_count: usize,
    /// Multiplication factor for the public pool (must be > 1).
    pub multiplier: f64,
    /// Each player's endowment (uniform).
    pub endowment: f64,
}

impl<'de> Deserialize<'de> for PublicGoodsGame {
    fn deserialize<D: serde::Deserializer<'de>>(
        deserializer: D,
    ) -> core::result::Result<Self, D::Error> {
        #[derive(Deserialize)]
        struct Raw {
            player_count: usize,
            multiplier: f64,
            endowment: f64,
        }
        let raw = Raw::deserialize(deserializer)?;
        PublicGoodsGame::new(raw.player_count, raw.multiplier, raw.endowment)
            .map_err(serde::de::Error::custom)
    }
}

impl PublicGoodsGame {
    /// Create a new public goods game.
    ///
    /// # Errors
    ///
    /// Returns error if `player_count` is 0, `multiplier <= 1`, or `endowment <= 0`.
    pub fn new(player_count: usize, multiplier: f64, endowment: f64) -> Result<Self> {
        if player_count == 0 {
            return Err(SanghaError::ComputationError(
                "player_count must be > 0".into(),
            ));
        }
        validate_finite(multiplier, "multiplier")?;
        if multiplier <= 1.0 {
            return Err(SanghaError::ComputationError(
                "multiplier must be > 1.0 for a social dilemma".into(),
            ));
        }
        validate_positive(endowment, "endowment")?;
        Ok(Self {
            player_count,
            multiplier,
            endowment,
        })
    }

    /// Validate that this game is well-formed.
    ///
    /// Call this after deserialization to ensure invariants hold.
    ///
    /// # Errors
    ///
    /// Returns error if `player_count` is 0, `multiplier <= 1`, or `endowment <= 0`.
    pub fn validate(&self) -> Result<()> {
        if self.player_count == 0 {
            return Err(SanghaError::ComputationError(
                "player_count must be > 0".into(),
            ));
        }
        validate_finite(self.multiplier, "multiplier")?;
        if self.multiplier <= 1.0 {
            return Err(SanghaError::ComputationError(
                "multiplier must be > 1.0 for a social dilemma".into(),
            ));
        }
        validate_positive(self.endowment, "endowment")
    }
}

/// Outcome of a public goods game round.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PublicGoodsOutcome {
    /// Payoff for each player.
    pub payoffs: Vec<f64>,
    /// Total contributed to the public pool.
    pub total_contribution: f64,
}

impl PublicGoodsOutcome {
    /// Create a new public goods outcome.
    #[inline]
    #[must_use]
    pub fn new(payoffs: Vec<f64>, total_contribution: f64) -> Self {
        Self {
            payoffs,
            total_contribution,
        }
    }
}

/// Sealed-bid auction type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum AuctionType {
    /// Winner pays their own bid.
    FirstPrice,
    /// Winner pays the second-highest bid (Vickrey auction).
    SecondPrice,
}

/// Result of an auction.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AuctionResult {
    /// Index of the winning bidder.
    pub winner: usize,
    /// Price paid by the winner.
    pub price: f64,
}

impl AuctionResult {
    /// Create a new auction result.
    #[inline]
    #[must_use]
    pub fn new(winner: usize, price: f64) -> Self {
        Self { winner, price }
    }
}

/// Play one round of a public goods game.
///
/// Each player contributes `contributions[i]` (between 0 and endowment).
/// The total contribution is multiplied and split evenly.
///
/// `payoff_i = (endowment - contribution_i) + multiplier * total / player_count`
///
/// # Errors
///
/// Returns error if contributions length mismatches, or any contribution is
/// negative or exceeds the endowment.
#[must_use = "returns the game outcome without side effects"]
pub fn public_goods_round(
    game: &PublicGoodsGame,
    contributions: &[f64],
) -> Result<PublicGoodsOutcome> {
    if contributions.len() != game.player_count {
        return Err(SanghaError::ComputationError(format!(
            "contributions length {} != player_count {}",
            contributions.len(),
            game.player_count
        )));
    }

    let n = game.player_count as f64;
    let mut total = 0.0;

    for (i, &c) in contributions.iter().enumerate() {
        validate_non_negative(c, &format!("contributions[{i}]"))?;
        // Use a scaled tolerance: 1e-9 relative to endowment
        if c > game.endowment * (1.0 + 1e-9) + 1e-15 {
            return Err(SanghaError::ComputationError(format!(
                "contributions[{i}] = {c} exceeds endowment {}",
                game.endowment
            )));
        }
        total += c;
    }

    let public_share = game.multiplier * total / n;
    let payoffs: Vec<f64> = contributions
        .iter()
        .map(|&c| (game.endowment - c) + public_share)
        .collect();

    Ok(PublicGoodsOutcome::new(payoffs, total))
}

/// Nash equilibrium contributions (free-rider equilibrium).
///
/// When `multiplier / player_count < 1`, the dominant strategy is to contribute
/// nothing — every dollar contributed returns less than a dollar to the contributor.
///
/// # Errors
///
/// Returns error if the game is invalid.
#[inline]
#[must_use = "returns the equilibrium contributions without side effects"]
pub fn free_rider_equilibrium(game: &PublicGoodsGame) -> Result<Vec<f64>> {
    let mpcr = game.multiplier / game.player_count as f64;
    if mpcr >= 1.0 {
        // When MPCR >= 1, contributing is individually rational
        Ok(vec![game.endowment; game.player_count])
    } else {
        Ok(vec![0.0; game.player_count])
    }
}

/// Socially optimal contributions.
///
/// When `multiplier > 1`, the social optimum is for everyone to contribute
/// their full endowment, since each dollar contributed returns `multiplier`
/// dollars total (though only `multiplier/n` to the contributor).
///
/// # Errors
///
/// Returns error if the game is invalid.
#[inline]
#[must_use = "returns the optimal contributions without side effects"]
pub fn social_optimum(game: &PublicGoodsGame) -> Result<Vec<f64>> {
    // multiplier > 1 is guaranteed by PublicGoodsGame::new
    Ok(vec![game.endowment; game.player_count])
}

/// Run a sealed-bid auction.
///
/// - `FirstPrice`: winner pays their own bid.
/// - `SecondPrice` (Vickrey): winner pays the second-highest bid.
///
/// In case of tied highest bids, the first bidder (lowest index) wins.
///
/// # Errors
///
/// Returns error if `bids` is empty or any bid is negative or non-finite.
#[must_use = "returns the auction result without side effects"]
pub fn sealed_bid_auction(bids: &[f64], auction_type: AuctionType) -> Result<AuctionResult> {
    if bids.is_empty() {
        return Err(SanghaError::ComputationError("no bids provided".into()));
    }
    for (i, &b) in bids.iter().enumerate() {
        validate_non_negative(b, &format!("bids[{i}]"))?;
    }

    // Find winner (highest bidder, first index on tie)
    let mut winner = 0;
    let mut highest = bids[0];
    let mut second_highest = 0.0_f64;

    for (i, &b) in bids.iter().enumerate().skip(1) {
        if b > highest {
            second_highest = highest;
            highest = b;
            winner = i;
        } else if b > second_highest {
            second_highest = b;
        }
    }

    let price = match auction_type {
        AuctionType::FirstPrice => highest,
        AuctionType::SecondPrice => second_highest,
    };

    Ok(AuctionResult::new(winner, price))
}

/// Mechanism efficiency: ratio of actual to optimal social welfare.
///
/// Returns a value in `[0, 1]` when `optimal >= actual >= 0`.
///
/// # Errors
///
/// Returns error if `optimal` is non-positive or values are non-finite.
#[inline]
#[must_use = "returns the efficiency ratio without side effects"]
pub fn mechanism_efficiency(actual_welfare: f64, optimal_welfare: f64) -> Result<f64> {
    validate_finite(actual_welfare, "actual_welfare")?;
    validate_positive(optimal_welfare, "optimal_welfare")?;
    Ok((actual_welfare / optimal_welfare).clamp(0.0, 1.0))
}

/// Configuration for a tragedy of the commons game.
///
/// Each player extracts from a shared resource. Overextraction depletes the resource.
///
/// `π_i = e_i * (1 - E/K) - c * e_i`
///
/// where `e_i` is player i's extraction, `E = Σ e_i`, `K` is capacity, `c` is cost.
///
/// Reference: Hardin (1968), *Science* 162.
///
/// Deserialization validates invariants automatically.
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct TragedyOfCommons {
    /// Number of players.
    pub player_count: usize,
    /// Total resource capacity (K > 0).
    pub resource_capacity: f64,
    /// Cost per unit of extraction (c >= 0, c < K).
    pub extraction_cost: f64,
}

impl TragedyOfCommons {
    /// Create a new tragedy of the commons game.
    ///
    /// # Errors
    ///
    /// Returns error if `player_count` is 0, `resource_capacity` is not positive,
    /// `extraction_cost` is negative, or `extraction_cost >= resource_capacity`.
    pub fn new(player_count: usize, resource_capacity: f64, extraction_cost: f64) -> Result<Self> {
        if player_count == 0 {
            return Err(SanghaError::ComputationError(
                "player_count must be > 0".into(),
            ));
        }
        validate_positive(resource_capacity, "resource_capacity")?;
        validate_non_negative(extraction_cost, "extraction_cost")?;
        if extraction_cost >= resource_capacity {
            return Err(SanghaError::ComputationError(
                "extraction_cost must be < resource_capacity".into(),
            ));
        }
        Ok(Self {
            player_count,
            resource_capacity,
            extraction_cost,
        })
    }

    /// Validate after deserialization.
    ///
    /// # Errors
    ///
    /// Returns error if the game parameters are invalid.
    pub fn validate(&self) -> Result<()> {
        if self.player_count == 0 {
            return Err(SanghaError::ComputationError(
                "player_count must be > 0".into(),
            ));
        }
        validate_positive(self.resource_capacity, "resource_capacity")?;
        validate_non_negative(self.extraction_cost, "extraction_cost")?;
        if self.extraction_cost >= self.resource_capacity {
            return Err(SanghaError::ComputationError(
                "extraction_cost must be < resource_capacity".into(),
            ));
        }
        Ok(())
    }
}

impl<'de> Deserialize<'de> for TragedyOfCommons {
    fn deserialize<D: serde::Deserializer<'de>>(
        deserializer: D,
    ) -> core::result::Result<Self, D::Error> {
        #[derive(Deserialize)]
        struct Raw {
            player_count: usize,
            resource_capacity: f64,
            extraction_cost: f64,
        }
        let raw = Raw::deserialize(deserializer)?;
        TragedyOfCommons::new(raw.player_count, raw.resource_capacity, raw.extraction_cost)
            .map_err(serde::de::Error::custom)
    }
}

/// Compute payoffs for one round of the tragedy of the commons.
///
/// `π_i = e_i * (1 - E/K) - c * e_i`
///
/// # Errors
///
/// Returns error if `extractions` length != `player_count`, or any extraction
/// is negative or non-finite.
#[must_use = "returns the payoffs without side effects"]
pub fn tragedy_of_commons_round(game: &TragedyOfCommons, extractions: &[f64]) -> Result<Vec<f64>> {
    if extractions.len() != game.player_count {
        return Err(SanghaError::ComputationError(format!(
            "extractions length {} != player_count {}",
            extractions.len(),
            game.player_count
        )));
    }

    let mut total = 0.0;
    for (i, &e) in extractions.iter().enumerate() {
        validate_non_negative(e, &format!("extractions[{i}]"))?;
        total += e;
    }

    let k = game.resource_capacity;
    let c = game.extraction_cost;
    let payoffs = extractions
        .iter()
        .map(|&e| e * (1.0 - total / k) - c * e)
        .collect();

    Ok(payoffs)
}

/// Cournot-Nash equilibrium extraction levels for the tragedy of the commons.
///
/// Each player extracts: `e* = (K - c) / (n + 1)`
///
/// Total extraction: `E* = n * (K - c) / (n + 1)`, which exceeds the social optimum.
#[inline]
#[must_use = "returns the equilibrium extractions without side effects"]
pub fn commons_nash_equilibrium(game: &TragedyOfCommons) -> Result<Vec<f64>> {
    let e_star = (game.resource_capacity - game.extraction_cost) / (game.player_count as f64 + 1.0);
    Ok(vec![e_star; game.player_count])
}

/// Socially optimal extraction levels for the tragedy of the commons.
///
/// Total optimal extraction: `E_opt = (K - c) / 2`, split equally.
/// Per player: `e_opt = (K - c) / (2n)`.
#[inline]
#[must_use = "returns the optimal extractions without side effects"]
pub fn commons_social_optimum(game: &TragedyOfCommons) -> Result<Vec<f64>> {
    let e_opt = (game.resource_capacity - game.extraction_cost) / (2.0 * game.player_count as f64);
    Ok(vec![e_opt; game.player_count])
}

/// Discounted sum of a constant payoff over a finite number of rounds.
///
/// `V = Σ_{t=0}^{rounds-1} payoff * δ^t = payoff * (1 - δ^rounds) / (1 - δ)`
///
/// For `δ = 1.0`, returns `payoff * rounds`.
///
/// # Errors
///
/// Returns error if `discount_factor` not in \[0, 1\], `payoff` not finite, or `rounds` is 0.
#[inline]
#[must_use = "returns the discounted sum without side effects"]
pub fn repeated_game_discount(payoff: f64, rounds: usize, discount_factor: f64) -> Result<f64> {
    validate_finite(payoff, "payoff")?;
    validate_finite(discount_factor, "discount_factor")?;
    if !(0.0..=1.0).contains(&discount_factor) {
        return Err(SanghaError::ComputationError(format!(
            "discount_factor must be in [0, 1], got {discount_factor}"
        )));
    }
    if rounds == 0 {
        return Err(SanghaError::ComputationError("rounds must be > 0".into()));
    }

    if (discount_factor - 1.0).abs() < f64::EPSILON {
        Ok(payoff * rounds as f64)
    } else if discount_factor.abs() < f64::EPSILON {
        Ok(payoff) // only the first round counts
    } else {
        Ok(payoff * (1.0 - discount_factor.powi(rounds as i32)) / (1.0 - discount_factor))
    }
}

/// Check if cooperation is sustainable under the folk theorem.
///
/// Cooperation is sustainable in an infinitely repeated game if:
/// `δ >= (T - R) / (T - P)`
///
/// where `T` = temptation, `R` = reward (mutual cooperation),
/// `P` = punishment (mutual defection), `δ` = discount factor.
///
/// Requires `T > R > P` (prisoner's dilemma ordering).
///
/// # Errors
///
/// Returns error if `T <= R`, `R <= P`, `discount` not in \[0, 1\],
/// or any value is non-finite.
///
/// Reference: Friedman (1971), *Review of Economic Studies* 38(1).
#[inline]
#[must_use = "returns whether cooperation is sustainable without side effects"]
pub fn folk_theorem_threshold(
    temptation: f64,
    reward: f64,
    punishment: f64,
    discount: f64,
) -> Result<bool> {
    validate_finite(temptation, "temptation")?;
    validate_finite(reward, "reward")?;
    validate_finite(punishment, "punishment")?;
    validate_finite(discount, "discount")?;
    if temptation <= reward {
        return Err(SanghaError::ComputationError(
            "temptation must be > reward".into(),
        ));
    }
    if reward <= punishment {
        return Err(SanghaError::ComputationError(
            "reward must be > punishment".into(),
        ));
    }
    if !(0.0..=1.0).contains(&discount) {
        return Err(SanghaError::ComputationError(format!(
            "discount must be in [0, 1], got {discount}"
        )));
    }

    let threshold = (temptation - reward) / (temptation - punishment);
    Ok(discount >= threshold)
}

#[cfg(test)]
mod tests {
    use super::*;

    // --- PublicGoodsGame ---

    #[test]
    fn test_public_goods_all_contribute() {
        let game = PublicGoodsGame::new(4, 2.0, 10.0).unwrap();
        let contributions = vec![10.0, 10.0, 10.0, 10.0];
        let outcome = public_goods_round(&game, &contributions).unwrap();
        // Total = 40, pool = 2*40 = 80, share = 80/4 = 20
        // Payoff = (10-10) + 20 = 20
        for &p in &outcome.payoffs {
            assert!((p - 20.0).abs() < 1e-10);
        }
        assert!((outcome.total_contribution - 40.0).abs() < 1e-10);
    }

    #[test]
    fn test_public_goods_free_rider() {
        let game = PublicGoodsGame::new(4, 2.0, 10.0).unwrap();
        // Player 0 free-rides, others contribute fully
        let contributions = vec![0.0, 10.0, 10.0, 10.0];
        let outcome = public_goods_round(&game, &contributions).unwrap();
        // Total = 30, pool = 60, share = 15
        // Player 0: (10-0) + 15 = 25 (highest!)
        // Others: (10-10) + 15 = 15
        assert!((outcome.payoffs[0] - 25.0).abs() < 1e-10);
        assert!((outcome.payoffs[1] - 15.0).abs() < 1e-10);
    }

    #[test]
    fn test_public_goods_none_contribute() {
        let game = PublicGoodsGame::new(3, 2.0, 10.0).unwrap();
        let contributions = vec![0.0, 0.0, 0.0];
        let outcome = public_goods_round(&game, &contributions).unwrap();
        // Everyone keeps endowment
        for &p in &outcome.payoffs {
            assert!((p - 10.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_public_goods_wrong_length_error() {
        let game = PublicGoodsGame::new(3, 2.0, 10.0).unwrap();
        assert!(public_goods_round(&game, &[5.0, 5.0]).is_err());
    }

    #[test]
    fn test_public_goods_negative_contribution_error() {
        let game = PublicGoodsGame::new(3, 2.0, 10.0).unwrap();
        assert!(public_goods_round(&game, &[-1.0, 5.0, 5.0]).is_err());
    }

    #[test]
    fn test_public_goods_excess_contribution_error() {
        let game = PublicGoodsGame::new(3, 2.0, 10.0).unwrap();
        assert!(public_goods_round(&game, &[11.0, 5.0, 5.0]).is_err());
    }

    #[test]
    fn test_game_invalid_multiplier() {
        assert!(PublicGoodsGame::new(3, 0.5, 10.0).is_err());
        assert!(PublicGoodsGame::new(3, 1.0, 10.0).is_err());
    }

    #[test]
    fn test_game_zero_players() {
        assert!(PublicGoodsGame::new(0, 2.0, 10.0).is_err());
    }

    // --- free_rider_equilibrium / social_optimum ---

    #[test]
    fn test_free_rider_equilibrium_low_mpcr() {
        // MPCR = 2/4 = 0.5 < 1 → contribute nothing
        let game = PublicGoodsGame::new(4, 2.0, 10.0).unwrap();
        let eq = free_rider_equilibrium(&game).unwrap();
        for &c in &eq {
            assert!((c - 0.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_free_rider_equilibrium_high_mpcr() {
        // MPCR = 5/3 ≈ 1.67 >= 1 → contribute fully
        let game = PublicGoodsGame::new(3, 5.0, 10.0).unwrap();
        let eq = free_rider_equilibrium(&game).unwrap();
        for &c in &eq {
            assert!((c - 10.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_social_optimum_full_contribution() {
        let game = PublicGoodsGame::new(4, 2.0, 10.0).unwrap();
        let opt = social_optimum(&game).unwrap();
        for &c in &opt {
            assert!((c - 10.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_social_dilemma_gap() {
        // The gap between Nash and social optimum demonstrates the social dilemma
        let game = PublicGoodsGame::new(4, 2.0, 10.0).unwrap();
        let nash = free_rider_equilibrium(&game).unwrap();
        let opt = social_optimum(&game).unwrap();
        let nash_outcome = public_goods_round(&game, &nash).unwrap();
        let opt_outcome = public_goods_round(&game, &opt).unwrap();
        let nash_welfare: f64 = nash_outcome.payoffs.iter().sum();
        let opt_welfare: f64 = opt_outcome.payoffs.iter().sum();
        assert!(nash_welfare < opt_welfare); // social dilemma!
    }

    // --- sealed_bid_auction ---

    #[test]
    fn test_first_price_auction() {
        let result = sealed_bid_auction(&[10.0, 30.0, 20.0], AuctionType::FirstPrice).unwrap();
        assert_eq!(result.winner, 1);
        assert!((result.price - 30.0).abs() < 1e-10);
    }

    #[test]
    fn test_second_price_auction() {
        let result = sealed_bid_auction(&[10.0, 30.0, 20.0], AuctionType::SecondPrice).unwrap();
        assert_eq!(result.winner, 1);
        assert!((result.price - 20.0).abs() < 1e-10);
    }

    #[test]
    fn test_auction_single_bidder() {
        let result = sealed_bid_auction(&[100.0], AuctionType::SecondPrice).unwrap();
        assert_eq!(result.winner, 0);
        assert!((result.price - 0.0).abs() < 1e-10); // no second bid
    }

    #[test]
    fn test_auction_tied_bids() {
        // First bidder wins on tie
        let result = sealed_bid_auction(&[50.0, 50.0], AuctionType::FirstPrice).unwrap();
        assert_eq!(result.winner, 0);
    }

    #[test]
    fn test_auction_empty_error() {
        assert!(sealed_bid_auction(&[], AuctionType::FirstPrice).is_err());
    }

    #[test]
    fn test_auction_negative_bid_error() {
        assert!(sealed_bid_auction(&[10.0, -5.0], AuctionType::FirstPrice).is_err());
    }

    // --- mechanism_efficiency ---

    #[test]
    fn test_mechanism_efficiency_perfect() {
        let e = mechanism_efficiency(100.0, 100.0).unwrap();
        assert!((e - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_mechanism_efficiency_half() {
        let e = mechanism_efficiency(50.0, 100.0).unwrap();
        assert!((e - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_mechanism_efficiency_zero_optimal_error() {
        assert!(mechanism_efficiency(50.0, 0.0).is_err());
    }

    // --- serde roundtrips ---

    #[test]
    fn test_public_goods_game_serde_roundtrip() {
        let game = PublicGoodsGame::new(3, 2.0, 10.0).unwrap();
        let json = serde_json::to_string(&game).unwrap();
        let back: PublicGoodsGame = serde_json::from_str(&json).unwrap();
        assert_eq!(game.player_count, back.player_count);
    }

    #[test]
    fn test_public_goods_outcome_serde_roundtrip() {
        let outcome = PublicGoodsOutcome::new(vec![15.0, 15.0], 20.0);
        let json = serde_json::to_string(&outcome).unwrap();
        let back: PublicGoodsOutcome = serde_json::from_str(&json).unwrap();
        assert_eq!(outcome.payoffs, back.payoffs);
    }

    #[test]
    fn test_auction_type_serde_roundtrip() {
        let at = AuctionType::SecondPrice;
        let json = serde_json::to_string(&at).unwrap();
        let back: AuctionType = serde_json::from_str(&json).unwrap();
        assert_eq!(at, back);
    }

    #[test]
    fn test_auction_result_serde_roundtrip() {
        let ar = AuctionResult::new(2, 50.0);
        let json = serde_json::to_string(&ar).unwrap();
        let back: AuctionResult = serde_json::from_str(&json).unwrap();
        assert_eq!(ar.winner, back.winner);
    }

    // --- audit tests ---

    #[test]
    fn test_mechanism_efficiency_clamp_negative() {
        let e = mechanism_efficiency(-10.0, 100.0).unwrap();
        assert!((e - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_mechanism_efficiency_clamp_above_one() {
        let e = mechanism_efficiency(200.0, 100.0).unwrap();
        assert!((e - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_free_rider_equilibrium_mpcr_exactly_one() {
        // MPCR = 3/3 = 1.0 exactly → should contribute fully
        let game = PublicGoodsGame::new(3, 3.0, 10.0).unwrap();
        let eq = free_rider_equilibrium(&game).unwrap();
        for &c in &eq {
            assert!((c - 10.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_social_optimum_direct() {
        let game = PublicGoodsGame::new(5, 1.5, 20.0).unwrap();
        let opt = social_optimum(&game).unwrap();
        assert_eq!(opt.len(), 5);
        for &c in &opt {
            assert!((c - 20.0).abs() < 1e-10);
        }
    }

    // --- TragedyOfCommons ---

    #[test]
    fn test_tragedy_new_valid() {
        let g = TragedyOfCommons::new(5, 1000.0, 10.0);
        assert!(g.is_ok());
    }

    #[test]
    fn test_tragedy_new_invalid() {
        assert!(TragedyOfCommons::new(0, 1000.0, 10.0).is_err()); // no players
        assert!(TragedyOfCommons::new(5, -1.0, 10.0).is_err()); // negative capacity
        assert!(TragedyOfCommons::new(5, 1000.0, 1000.0).is_err()); // cost >= capacity
    }

    #[test]
    fn test_tragedy_round_basic() {
        let game = TragedyOfCommons::new(2, 100.0, 5.0).unwrap();
        // Player 0 extracts 10, player 1 extracts 20. Total = 30.
        // π_0 = 10*(1-30/100) - 5*10 = 10*0.7 - 50 = 7 - 50 = -43
        // π_1 = 20*(1-30/100) - 5*20 = 20*0.7 - 100 = 14 - 100 = -86
        let payoffs = tragedy_of_commons_round(&game, &[10.0, 20.0]).unwrap();
        assert!((payoffs[0] - (-43.0)).abs() < 1e-10);
        assert!((payoffs[1] - (-86.0)).abs() < 1e-10);
    }

    #[test]
    fn test_tragedy_round_zero_extraction() {
        let game = TragedyOfCommons::new(3, 100.0, 5.0).unwrap();
        let payoffs = tragedy_of_commons_round(&game, &[0.0, 0.0, 0.0]).unwrap();
        for &p in &payoffs {
            assert!((p - 0.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_tragedy_round_overextraction() {
        let game = TragedyOfCommons::new(2, 100.0, 0.0).unwrap();
        let payoffs = tragedy_of_commons_round(&game, &[80.0, 80.0]).unwrap();
        // Total=160 > K=100, so (1-E/K) = -0.6, payoffs are negative
        for &p in &payoffs {
            assert!(p < 0.0);
        }
    }

    #[test]
    fn test_tragedy_round_wrong_length() {
        let game = TragedyOfCommons::new(3, 100.0, 5.0).unwrap();
        assert!(tragedy_of_commons_round(&game, &[10.0, 20.0]).is_err());
    }

    #[test]
    fn test_commons_nash_formula() {
        // e* = (K - c) / (n + 1) = (1000 - 10) / 6 = 165.0
        let game = TragedyOfCommons::new(5, 1000.0, 10.0).unwrap();
        let nash = commons_nash_equilibrium(&game).unwrap();
        assert_eq!(nash.len(), 5);
        for &e in &nash {
            assert!((e - 165.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_commons_social_optimum_formula() {
        // e_opt = (K - c) / (2n) = (1000 - 10) / 10 = 99.0
        let game = TragedyOfCommons::new(5, 1000.0, 10.0).unwrap();
        let opt = commons_social_optimum(&game).unwrap();
        for &e in &opt {
            assert!((e - 99.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_commons_nash_exceeds_optimum() {
        let game = TragedyOfCommons::new(5, 1000.0, 10.0).unwrap();
        let nash = commons_nash_equilibrium(&game).unwrap();
        let opt = commons_social_optimum(&game).unwrap();
        let nash_total: f64 = nash.iter().sum();
        let opt_total: f64 = opt.iter().sum();
        assert!(nash_total > opt_total); // the tragedy
    }

    #[test]
    fn test_commons_nash_vs_optimum_payoffs() {
        let game = TragedyOfCommons::new(5, 1000.0, 10.0).unwrap();
        let nash = commons_nash_equilibrium(&game).unwrap();
        let opt = commons_social_optimum(&game).unwrap();
        let nash_payoffs = tragedy_of_commons_round(&game, &nash).unwrap();
        let opt_payoffs = tragedy_of_commons_round(&game, &opt).unwrap();
        let nash_welfare: f64 = nash_payoffs.iter().sum();
        let opt_welfare: f64 = opt_payoffs.iter().sum();
        assert!(nash_welfare < opt_welfare); // Nash is worse for society
    }

    // --- repeated_game_discount ---

    #[test]
    fn test_repeated_discount_single_round() {
        let v = repeated_game_discount(10.0, 1, 0.9).unwrap();
        assert!((v - 10.0).abs() < 1e-10);
    }

    #[test]
    fn test_repeated_discount_geometric() {
        // V = 10 * (1 - 0.9^5) / (1 - 0.9) = 10 * (1 - 0.59049) / 0.1 = 40.951
        let v = repeated_game_discount(10.0, 5, 0.9).unwrap();
        let expected = 10.0 * (1.0 - 0.9_f64.powi(5)) / 0.1;
        assert!((v - expected).abs() < 1e-8);
    }

    #[test]
    fn test_repeated_discount_delta_one() {
        let v = repeated_game_discount(10.0, 100, 1.0).unwrap();
        assert!((v - 1000.0).abs() < 1e-10);
    }

    #[test]
    fn test_repeated_discount_delta_zero() {
        let v = repeated_game_discount(10.0, 100, 0.0).unwrap();
        assert!((v - 10.0).abs() < 1e-10);
    }

    #[test]
    fn test_repeated_discount_invalid() {
        assert!(repeated_game_discount(10.0, 0, 0.9).is_err()); // rounds=0
        assert!(repeated_game_discount(10.0, 5, 1.5).is_err()); // delta>1
        assert!(repeated_game_discount(10.0, 5, -0.1).is_err()); // delta<0
    }

    // --- folk_theorem_threshold ---

    #[test]
    fn test_folk_theorem_cooperate() {
        // PD: T=5, R=3, P=1. Threshold = (5-3)/(5-1) = 0.5
        assert!(folk_theorem_threshold(5.0, 3.0, 1.0, 0.9).unwrap());
    }

    #[test]
    fn test_folk_theorem_defect() {
        assert!(!folk_theorem_threshold(5.0, 3.0, 1.0, 0.3).unwrap());
    }

    #[test]
    fn test_folk_theorem_boundary() {
        // At exactly the threshold: δ = 0.5 >= 0.5 → true
        assert!(folk_theorem_threshold(5.0, 3.0, 1.0, 0.5).unwrap());
    }

    #[test]
    fn test_folk_theorem_invalid_ordering() {
        assert!(folk_theorem_threshold(3.0, 5.0, 1.0, 0.9).is_err()); // T <= R
        assert!(folk_theorem_threshold(5.0, 1.0, 3.0, 0.9).is_err()); // R <= P
    }

    #[test]
    fn test_folk_theorem_pd_payoffs() {
        // Standard PD: T=5, R=3, P=1. Threshold = 2/4 = 0.5
        let threshold_met = folk_theorem_threshold(5.0, 3.0, 1.0, 0.5).unwrap();
        assert!(threshold_met);
        let below = folk_theorem_threshold(5.0, 3.0, 1.0, 0.49).unwrap();
        assert!(!below);
    }

    // --- serde roundtrips ---

    #[test]
    fn test_tragedy_serde_roundtrip() {
        let game = TragedyOfCommons::new(5, 1000.0, 10.0).unwrap();
        let json = serde_json::to_string(&game).unwrap();
        let back: TragedyOfCommons = serde_json::from_str(&json).unwrap();
        assert_eq!(game.player_count, back.player_count);
    }

    #[test]
    fn test_public_goods_deserialize_rejects_invalid() {
        // multiplier <= 1 is invalid
        let json = r#"{"player_count":3,"multiplier":0.5,"endowment":10.0}"#;
        let result: core::result::Result<PublicGoodsGame, _> = serde_json::from_str(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_tragedy_deserialize_rejects_invalid() {
        // extraction_cost >= resource_capacity is invalid
        let json = r#"{"player_count":3,"resource_capacity":100.0,"extraction_cost":200.0}"#;
        let result: core::result::Result<TragedyOfCommons, _> = serde_json::from_str(json);
        assert!(result.is_err());
    }
}