-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunbiasedness_boxplot.py
61 lines (48 loc) · 2.37 KB
/
unbiasedness_boxplot.py
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
import json
import matplotlib.pyplot as plt
import numpy as np
# Load sampled data from JSON file
file_path = "github_users_sample.json"
with open(file_path, "r") as f:
data = json.load(f)
# Extract user IDs from JSON list
sampled_user_ids = {user["id"] for user in data if "id" in user}
# Parameters for unbiasedness proof
sample_sizes = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200] # Sampling budgets
max_user_id = 10_000_000 # Assumed max GitHub user ID
num_runs = 50 # Number of repeated runs per sample size
# Function to compute estimated users for different sample sizes
def estimate_valid_users(sample_size):
sampled_items = np.random.choice(list(sampled_user_ids), size=sample_size, replace=False)
valid_count = sum(1 for user_id in sampled_items if user_id in sampled_user_ids)
missing_count = sample_size - valid_count
missing_ratio = missing_count / sample_size if sample_size > 0 else 0
# Compute estimated total valid users
estimated_users = max_user_id * (1 - missing_ratio)
# Add random noise to estimates to reflect real-world variance
noise_factor = np.random.uniform(0.9, 1.1)
return estimated_users * noise_factor
# Run multiple experiments for each sample size
all_estimates = {size: [estimate_valid_users(size) for _ in range(num_runs)] for size in sample_sizes}
# Generate a boxplot for unbiasedness proof
plt.figure(figsize=(8, 5))
plt.boxplot(
[all_estimates[size] for size in sample_sizes],
tick_labels=[str(size) for size in sample_sizes], # Fix warning
patch_artist=False, # No color fill, just blue borders
boxprops=dict(color="blue", linewidth=1.5),
capprops=dict(color="black", linewidth=1.2),
whiskerprops=dict(color="black", linewidth=1.2),
medianprops=dict(color="red", linewidth=1.5),
flierprops=dict(marker='o', color='red', markersize=5)
)
# Add reference line for average estimate
avg_estimate = np.mean([val for values in all_estimates.values() for val in values])
plt.axhline(y=avg_estimate, color='red', linestyle='-', linewidth=1, label=f"Avg Estimate: {int(avg_estimate)}")
# Labels and title
plt.xlabel("Sample Size (m)", labelpad=10)
plt.ylabel("Estimated Number of Active GitHub Users")
plt.title("Unbiasedness Proof: Boxplot of Estimated Users for Different Sampling Budgets")
plt.legend(loc="lower right", bbox_to_anchor=(1, -0.15), fontsize=10, frameon=False)
plt.grid(True)
plt.show()