Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Prompt:
- # Can you write me a script to generate a line-chart? Here's the formula it should use:
- # - Generate 10000 tabulators (one data point per tabulator)
- # - The first tabulator has one recorded vote, each tabulator gets one more vote than the previous
- # - For all tabulators, each vote in the first 300 has a random outcome between two candidates (A & B)
- # - After the first 300 votes, each tabulator gets 60% of of votes to candidate A
- # - make two trendlines, the first where all votes are random, and the second as described above
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- # Set random seed for reproducibility
- np.random.seed(42)
- # Generate data for 10000 tabulators
- n_tabulators = 8000
- votes_per_tabulator = np.arange(1, n_tabulators + 1) # 1 to 10000 votes
- # Function to simulate voting with threshold
- def simulate_votes_with_threshold(n_votes, threshold=300, bias=0.6):
- if n_votes <= threshold:
- # All random votes before threshold
- return np.random.random(n_votes) < 0.5
- else:
- # Random votes up to threshold
- first_votes = np.random.random(threshold) < 0.5
- # Biased votes after threshold
- remaining_votes = np.random.random(n_votes - threshold) < bias
- return np.concatenate([first_votes, remaining_votes])
- # Function to simulate completely random voting
- def simulate_random_votes(n_votes):
- return np.random.random(n_votes) < 0.6
- # Initialize arrays to store results
- biased_results = []
- random_results = []
- # Calculate results for each tabulator
- for votes in votes_per_tabulator:
- # Simulate votes with threshold and bias
- biased_votes = simulate_votes_with_threshold(votes)
- biased_prop = np.mean(biased_votes)
- biased_results.append(biased_prop)
- # Simulate completely random votes
- random_votes = simulate_random_votes(votes)
- random_prop = np.mean(random_votes)
- random_results.append(random_prop)
- # Create the plot
- plt.figure(figsize=(12, 8))
- plt.plot(votes_per_tabulator, random_results, label='Random Voting', alpha=0.7)
- plt.plot(votes_per_tabulator, biased_results, label='Threshold Voting', alpha=0.7)
- plt.axvline(x=300, color='r', linestyle='--', alpha=0.3, label='Threshold (300 votes)')
- plt.axhline(y=0.5, color='gray', linestyle='--', alpha=0.3, label='50% Line')
- plt.xlabel('Number of Votes')
- plt.ylabel('Proportion of Votes for Candidate A')
- plt.title('Vote Distribution Analysis')
- plt.legend()
- plt.grid(True, alpha=0.3)
- # Save the plot
- plt.savefig('vote_distribution.png')
- plt.close()
- print("Analysis complete! The plot has been saved as 'vote_distribution.png'")
Advertisement
Add Comment
Please, Sign In to add comment