Advertisement
Guest User

Untitled

a guest
Feb 5th, 2024
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.07 KB | None | 0 0
  1. # %%
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4.  
  5.  
  6. def p_win(p_ov):
  7.     return np.exp(1 / (1 - p_ov) * np.log(p_ov)) + 1 - np.exp(p_ov / (1 - p_ov) * np.log(p_ov))
  8.  
  9.  
  10. p_ovs = np.logspace(-5, 0, 1000)
  11. plt.figure(figsize=(4, 3), dpi=300)
  12. plt.plot(p_ovs, p_win(p_ovs), label="indistinguished")
  13. plt.plot(p_ovs, p_ovs, label="distinguished", c="k")
  14. plt.xscale("log")
  15. plt.xlabel("p_ov")
  16. plt.ylabel("P(humanity wins)")
  17. plt.title("P(humanity wins) if e_ov = 1")
  18. plt.legend()
  19. plt.show()
  20. # %%
  21. plt.figure(figsize=(4, 3), dpi=300)
  22. p_ovs = np.logspace(-5, 0, 1000)
  23. colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
  24. for s, c in zip([1, 0.99, 0.9, 0.5, 0.1, 0.01], colors):
  25.     p_ovs_p = p_ovs / (p_ovs + s * (1 - p_ovs))
  26.     plt.plot(p_ovs, p_win(p_ovs_p), label=f"s={s}", color=c)
  27.     if s != 1:
  28.         plt.plot(p_ovs, 1 - (1 - p_win(p_ovs)) * s, label=f"s_loud={s}", color=c, linestyle="--")
  29. plt.xlabel("p_ov")
  30. plt.ylabel("P(humanity wins)")
  31. plt.xscale("log")
  32. plt.title(f"P(humanity wins) if e_ov = 1\nGiven silent or loud failures")
  33. plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
  34. # %%
  35. plt.figure(figsize=(3, 3), dpi=300)
  36. p_ovs = np.logspace(-5, 0, 1000)
  37. colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
  38. for s, c in zip([1, 0.99, 0.9, 0.5, 0.1, 0.01], colors):
  39.     p_ovs_p = p_ovs / (p_ovs + s * (1 - p_ovs))
  40.     lamb = np.log(1/p_ovs_p)/(1 - p_ovs_p) / (p_ovs + s * (1 - p_ovs))
  41.     plt.plot(p_ovs, lamb, label=f"s={s}", color=c)
  42. plt.axhline(1, color="k", linestyle="--", label="1 attempt\non average")
  43. plt.xlabel("p_ov")
  44. plt.ylabel("λ T")
  45. plt.xscale("log")
  46. plt.yscale("log")
  47. plt.title(f"Average number of take-over attempts\n(λ T)")
  48. plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
  49. # %%
  50. # combination of the two plots above
  51. fig, axs = plt.subplots(1, 2, figsize=(7, 3), dpi=300)
  52. p_ovs = np.logspace(-5, 0, 1000)
  53. colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
  54. ax = axs[0]
  55. for s, c in zip([1, 0.99, 0.9, 0.5, 0.1, 0.01], colors):
  56.     p_ovs_p = p_ovs / (p_ovs + s * (1 - p_ovs))
  57.     ax.plot(p_ovs, p_win(p_ovs_p), color=c)
  58. ax.set_xlabel("p_ov")
  59. ax.set_ylabel("P(humanity wins)")
  60. ax.set_xscale("log")
  61. ax.set_title(f"P(humanity wins)")
  62. ax = axs[1]
  63. for s, c in zip([1, 0.99, 0.9, 0.5, 0.1, 0.01], colors):
  64.     p_ovs_p = p_ovs / (p_ovs + s * (1 - p_ovs))
  65.     lamb = np.log(1/p_ovs_p)/(1 - p_ovs_p) / (p_ovs + s * (1 - p_ovs))
  66.     ax.plot(p_ovs, lamb, label=f"s={s}", color=c)
  67. ax.axhline(1, color="k", linestyle="--")
  68. ax.set_xlabel("p_ov")
  69. ax.set_ylabel("λ T")
  70. ax.set_xscale("log")
  71. ax.set_yscale("log")
  72. ax.set_title(f"Avg nb of take-over attempts")
  73. ax.legend(loc="center left", bbox_to_anchor=(1, 0.5))
  74. plt.tight_layout()
  75.  
  76. # %%
  77. from scipy.optimize import bisect
  78.  
  79.  
  80. def p_win_mult(Tis, sis, cis, B):
  81.     n_tasks = len(Tis)
  82.     assert n_tasks == len(sis) == len(cis) == len(Tis)
  83.  
  84.     def over_budget(P):
  85.         return B - sum([Ti * ci * P / (P + (1 - P) / si) for Ti, si, ci in zip(Tis, sis, cis)])
  86.  
  87.     P = bisect(over_budget, 0, 1, maxiter=1000)
  88.     Lambda = 1 / n_tasks * np.log(1 / P) / (1 - P)
  89.     return np.exp(-Lambda * n_tasks) + 1 - np.exp(-Lambda * n_tasks * P)
  90.  
  91.  
  92. def close_to(a, b):
  93.     return np.abs(a - b) < 1e-5
  94.  
  95.  
  96. # assert close_to(p_win_mult([2], [0.5], [4], 3), 0.5)
  97. Tis = [1, 1]
  98. sis = [0.1, 0.1]
  99. cis = [4, 4]
  100. B = 3
  101. p_ov = B / (cis[0] * Tis[0]) / 2
  102. p_ov_ = p_ov / (p_ov + sis[0] * (1 - p_ov))
  103. print(p_ov, p_ov_)
  104. old = p_win(p_ov_)
  105. new = p_win_mult(Tis, sis, cis, B)
  106. assert close_to(old, new), f"{old} != {new}"
  107. # %%
  108. plt.figure(figsize=(5, 3), dpi=300)
  109. Ttot = 1
  110. s0 = 1
  111. n_tasks = 2
  112. cis = [1, 1]
  113. kept_colors = [colors[0]] + colors[3:]
  114. for s1, c in zip([1, 0.5, 0.1, 0.01], kept_colors):
  115.     for T1, ls in zip([0.9, 0.99, 1], ["dotted", "--", "-"]):
  116.         if s1 == 1 and T1 != 1:
  117.             continue
  118.  
  119.         p_ovs = np.logspace(-5, 0, 1000)
  120.         Bs = Ttot * cis[0] * p_ovs
  121.         p_wins = [p_win_mult([Ttot - T1, T1], [s0, s1], cis, B) for B in Bs]
  122.         plt.plot(p_ovs, p_wins, label=f"s={s1} r={T1}", color=c, linestyle=ls)
  123. plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
  124. plt.xlabel("p_ov")
  125. plt.ylabel("P(humanity wins)")
  126. plt.xscale("log")
  127. plt.title(
  128.     f"P(humanity wins)\nif a fraction r of the episodes are 'low danger'\nand have a takeover success rate s.\nThe other episodes have a success rate of 1"
  129. )
  130. # %%
  131. plt.figure(figsize=(6, 3), dpi=300)
  132. p_ovs = np.logspace(-5, 0, 1000)
  133. colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
  134. for s, c in zip([1, 0.99, 0.9, 0.5, 0.1, 0.01], colors):
  135.     p_wins = 1 - 1 / (1 + p_ovs / (s * (1 - p_ovs)))
  136.     plt.plot(p_ovs, p_wins, label=f"s={s}", color=c)
  137. plt.xlabel("p_ov")
  138. plt.ylabel("P(humanity wins)")
  139. plt.xscale("log")
  140. plt.title(f"P(humanity wins) if e_ov = 1\nIn an infinite game")
  141. plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
  142. # %%
  143. from scipy.optimize import fsolve
  144. import numpy as np
  145. import sympy as sp
  146.  
  147.  
  148. def p_win_given_lambda(lamb, k, p, T):
  149.     return 1 - (1 - (1 - np.power(lamb, k)) ** (T * (1 - p))) * np.power(1 - lamb, T * p)
  150.  
  151.  
  152. # lambda_val_, k_, T_, p_ = sp.symbols("lambda k T p")
  153. # der_p_win_given_lambda = sp.diff(p_win_given_lambda(lambda_val_, k_, p_, T_), lambda_val_)
  154.  
  155.  
  156. def der_p_win_given_lambda(lamb, k, p, T):
  157.     return -T * k * lamb**k * (1 - lamb) ** (T * p) * (1 - lamb**k) ** (T * (1 - p)) * (1 - p) / (
  158.         lamb * (1 - lamb**k)
  159.     ) + T * p * (1 - lamb) ** (T * p) * (1 - (1 - lamb**k) ** (T * (1 - p))) / (1 - lamb)
  160.  
  161.  
  162. def other_zero_expression(lamb, k, p, T):
  163.     # return -T * k * lamb**k * (1 - lamb) ** (T * p) * (1 - lamb**k) ** (T * (1 - p)) * (1 - p) * (
  164.     #     1 - lamb
  165.     # ) + T * p * (1 - lamb) ** (T * p) * (1 - (1 - lamb**k) ** (T * (1 - p))) * (lamb * (1 - lamb**k))
  166.     t1 = np.log(k) + k*np.log(lamb) + T*p*np.log(1 - lamb) + T*(1 - p)*np.log(1 - lamb**k) + np.log(1 - p) + np.log(1 - lamb)
  167.     t2 = np.log(p) + T*p*np.log(1 - lamb) + np.log(1 - (1 - lamb**k) ** (T * (1 - p))) + np.log(lamb) + np.log(1 - lamb**k)
  168.     return t2 - t1
  169.  
  170. # lambdas = np.linspace(0, 1, 1000)
  171. # T = 100
  172. # p = 0.001
  173. # k = 2
  174. # plt.plot(lambdas, p_win_given_lambda(lambdas, k, p, T))
  175. # plt.plot(lambdas, der_p_win_given_lambda(lambdas, k, p, T) > 0)
  176. # plt.plot(lambdas, other_zero_expression(lambdas, k, p, T) > 0)
  177. # %%
  178.  
  179. def coordination_p_win(k, p, T):
  180.     def func(lamb):
  181.         return other_zero_expression(lamb, k, p, T)
  182.     lambd = bisect(func, 1e-6, 1-1e-6, maxiter=1000)
  183.     return p_win_given_lambda(lambd, k, p, T)
  184.  
  185.  
  186. coordination_p_win(3, 0.9, 1000)
  187.  
  188. plt.figure(figsize=(6, 4), dpi=300)
  189. for T, c in zip([10, 100, 1000, 10000], colors):
  190.     for k, ls in zip([1, 2, 3, 6], ["-", "--", "-.", "dotted"]):
  191.         if k == 1 and T != 10000:
  192.             continue
  193.         N = 1
  194.         p_ovs = np.logspace(-5, 0, 100)
  195.         p_wins = [coordination_p_win(k, p, T) for p in p_ovs]
  196.         plt.plot(p_ovs, p_wins, label=f"k={k} T/N={T}", color=c, linestyle=ls)
  197. plt.xlabel("p_ov")
  198. plt.ylabel("P(humanity wins)")
  199. plt.xscale("log")
  200. plt.title(f"P(humanity wins)\nin a cooperative take-over scenario\nwith k AIs")
  201. plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
  202.  
  203. # %%
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement