Last active
February 28, 2020 10:36
-
-
Save MaxHalford/12eb3dd9f3d6eddb5eb007a722392dac to your computer and use it in GitHub Desktop.
Find count(s) from percentage(s)
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 | |
goal_fracs = np.array([.1, .9]) | |
goal_fracs = np.array([.04, .04, .11, .14, .67]) | |
counts = np.zeros_like(goal_fracs) | |
n = 1 | |
tol = .001 | |
a = 0 | |
while True: | |
fracs = counts / n | |
if np.allclose(fracs, goal_fracs, atol=tol): | |
break | |
if sum(fracs) < 1: | |
i = np.argmin(fracs - goal_fracs) | |
counts[i] += 1 | |
else: | |
n += 1 | |
counts, n |
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
goal_frac = .2 | |
k = 0 | |
n = 1 | |
tol = .01 | |
while True: | |
frac = k / n | |
if math.isclose(frac, goal_frac, abs_tol=tol): | |
break | |
if frac < goal_frac: | |
k += 1 | |
else: | |
n += 1 | |
k, n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment