Skip to content

Instantly share code, notes, and snippets.

@cdaven
Created May 24, 2022 19:18
Show Gist options
  • Save cdaven/896733a105df0486320b704741e172dc to your computer and use it in GitHub Desktop.
Save cdaven/896733a105df0486320b704741e172dc to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import random
import numpy as np
from scipy.optimize import curve_fit
INITIAL_BUGS = 50
LINES_OF_CODE = 1000
TESTERS = 5
RUNS = 10
def random_bugs_found(remaining_bugs):
probability = remaining_bugs / LINES_OF_CODE
if random.random() < probability:
return 1
else:
return 0
def run_simulation():
remaining_bugs = INITIAL_BUGS
remaining_bugs_each_t = [remaining_bugs]
t = 0
t_array = [0]
while remaining_bugs > 0:
t += 1
for _ in range(0, TESTERS):
if remaining_bugs > 0:
remaining_bugs -= random_bugs_found(remaining_bugs)
remaining_bugs_each_t.append(remaining_bugs)
t_array.append(t)
return t_array, remaining_bugs_each_t
def b_of_t(t, a):
return INITIAL_BUGS * np.power(a, np.divide(np.multiply(-TESTERS, t), LINES_OF_CODE))
a_values = []
fig, ax = plt.subplots()
for _ in range(0, RUNS):
x, y = run_simulation()
ax.plot(x, y)
popt, pcov = curve_fit(b_of_t, x, y)
a_values.append(popt[0])
#plt.plot(x, b_of_t(x, *popt), 'r-', label='fit: a=%5.3f' % tuple(popt))
# Print average fitted value of parameter "a" in b_of_t()
print(sum(a_values) / len(a_values))
plt.title('Remaining unknown bugs')
plt.xlabel('time')
plt.ylabel('bugs')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment