Created
September 16, 2024 02:35
-
-
Save cmrfrd/8d243b4abed9f1f25bbf7a811989a818 to your computer and use it in GitHub Desktop.
jitter calc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from matplotlib import pyplot as plt | |
from tqdm import tqdm | |
data = [] | |
num_refreshed_data = [] | |
ITERATIONS = 5_000 | |
N = 10_000 | |
T = 100 | |
window_size = T | |
frac_T_values = np.arange(0.5, 5, 0.5) # noqa: N816 | |
for frac_T in frac_T_values: | |
dT = lambda: np.random.randint(0, int(T * frac_T) + 1, size=N) # noqa: E731, N816 | |
num_refreshed_over_time = np.zeros(ITERATIONS, dtype=float) | |
nodes = (5 * dT()) + T | |
for i in tqdm(range(ITERATIONS), desc=f"Simulating frac_T={frac_T:.2f}", unit="iteration"): | |
nodes -= 1 | |
refresh_mask = nodes <= 0 | |
num_refreshed = np.sum(refresh_mask) | |
new_nodes = dT() + T | |
nodes[refresh_mask] = new_nodes[refresh_mask] | |
num_refreshed_over_time[i] = num_refreshed | |
# Calculate the sum of the nodes refreshed over a window of time | |
sliding_windows = np.lib.stride_tricks.sliding_window_view(num_refreshed_over_time, window_size) | |
num_refreshed_over_time_windowed = np.sum(sliding_windows, axis=1) / N | |
x = np.arange(0, num_refreshed_over_time_windowed.shape[0]) | |
data.append((frac_T, num_refreshed_over_time_windowed.mean())) | |
num_refreshed_data.append((frac_T, x, num_refreshed_over_time_windowed)) | |
# Create a single figure with two subplots | |
fig, axs = plt.subplots(2, 1, figsize=(12, 14)) | |
# Plot num_refreshed_over_time per frac_T in the first subplot | |
for frac_T, x, num_refreshed in num_refreshed_data: | |
axs[0].plot(x, num_refreshed, label=f"frac_T={frac_T:.2f}") | |
axs[0].set_xlabel("Time (Iterations)") | |
axs[0].set_ylabel(f"Fraction of Refreshed Nodes (Window Size: {window_size})") | |
axs[0].set_title("Fraction of Refreshed Nodes Over Time for Different frac_T") | |
axs[0].legend() | |
axs[0].grid(True) | |
# Plot the data variable in the second subplot | |
fracs = [frac_T for frac_T, _ in data] | |
means = [mean_refreshed for _, mean_refreshed in data] | |
rat_func = [(2 / (2 + frac_T)) for frac_T in fracs] | |
axs[1].plot(fracs, means, "bo-", label="Mean Fraction of Refreshed Nodes") | |
axs[1].plot(fracs, rat_func, "r--", label="2 / (2 + frac_T)") | |
axs[1].set_xlabel("frac_T") | |
axs[1].set_ylabel("Mean Fraction of Refreshed Nodes") | |
axs[1].set_title("Mean Fraction of Refreshed Nodes vs. frac_T") | |
axs[1].legend() | |
axs[1].grid(True) | |
plt.tight_layout() | |
plt.savefig("data/combined_plot.png") | |
plt.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment