Guest User

Untitled

a guest
Mar 18th, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | Cybersecurity | 0 0
  1. # Prompt:
  2. # Can you write me a script to generate a line-chart?  Here's the formula it should use:
  3. # - Generate 10000 tabulators (one data point per tabulator)
  4. # - The first tabulator has one recorded vote, each tabulator gets one more vote than the previous
  5. # - For all tabulators, each vote in the first 300 has a random outcome between two candidates (A & B)
  6. # - After the first 300 votes, each tabulator gets 60% of of votes to candidate A
  7. # - make two trendlines, the first where all votes are random, and the second as described above
  8.  
  9. import numpy as np
  10. import pandas as pd
  11. import matplotlib.pyplot as plt
  12.  
  13. # Set random seed for reproducibility
  14. np.random.seed(42)
  15.  
  16. # Generate data for 10000 tabulators
  17. n_tabulators = 8000
  18. votes_per_tabulator = np.arange(1, n_tabulators + 1)  # 1 to 10000 votes
  19.  
  20. # Function to simulate voting with threshold
  21. def simulate_votes_with_threshold(n_votes, threshold=300, bias=0.6):
  22.     if n_votes <= threshold:
  23.         # All random votes before threshold
  24.         return np.random.random(n_votes) < 0.5
  25.     else:
  26.         # Random votes up to threshold
  27.         first_votes = np.random.random(threshold) < 0.5
  28.         # Biased votes after threshold
  29.         remaining_votes = np.random.random(n_votes - threshold) < bias
  30.         return np.concatenate([first_votes, remaining_votes])
  31.  
  32. # Function to simulate completely random voting
  33. def simulate_random_votes(n_votes):
  34.     return np.random.random(n_votes) < 0.6
  35.  
  36. # Initialize arrays to store results
  37. biased_results = []
  38. random_results = []
  39.  
  40. # Calculate results for each tabulator
  41. for votes in votes_per_tabulator:
  42.     # Simulate votes with threshold and bias
  43.     biased_votes = simulate_votes_with_threshold(votes)
  44.     biased_prop = np.mean(biased_votes)
  45.     biased_results.append(biased_prop)
  46.    
  47.     # Simulate completely random votes
  48.     random_votes = simulate_random_votes(votes)
  49.     random_prop = np.mean(random_votes)
  50.     random_results.append(random_prop)
  51.  
  52. # Create the plot
  53. plt.figure(figsize=(12, 8))
  54. plt.plot(votes_per_tabulator, random_results, label='Random Voting', alpha=0.7)
  55. plt.plot(votes_per_tabulator, biased_results, label='Threshold Voting', alpha=0.7)
  56.  
  57. plt.axvline(x=300, color='r', linestyle='--', alpha=0.3, label='Threshold (300 votes)')
  58. plt.axhline(y=0.5, color='gray', linestyle='--', alpha=0.3, label='50% Line')
  59.  
  60. plt.xlabel('Number of Votes')
  61. plt.ylabel('Proportion of Votes for Candidate A')
  62. plt.title('Vote Distribution Analysis')
  63. plt.legend()
  64. plt.grid(True, alpha=0.3)
  65.  
  66. # Save the plot
  67. plt.savefig('vote_distribution.png')
  68. plt.close()
  69.  
  70. print("Analysis complete! The plot has been saved as 'vote_distribution.png'")
Advertisement
Add Comment
Please, Sign In to add comment