Created
March 1, 2023 06:41
-
-
Save marethyu/d322d5dea98aea55e9d3c477bdb9995a to your computer and use it in GitHub Desktop.
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 matplotlib.pyplot as plt | |
import numpy as np | |
from math import comb, log | |
def annotate_max(x, y, ax=None): | |
xmax = x[np.argmax(y)] | |
ymax = y.max() | |
text= "U_A={:.3f}, S={:.3f}".format(xmax, ymax) | |
if not ax: | |
ax=plt.gca() | |
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72) | |
arrowprops=dict(arrowstyle="->",connectionstyle="angle,angleA=0,angleB=60") | |
kw = dict(xycoords='data',textcoords="axes fraction", | |
arrowprops=arrowprops, bbox=bbox_props, ha="right", va="top") | |
ax.annotate(text, xy=(xmax, ymax), xytext=(0.94,0.96), **kw) | |
U_Amax = 100 | |
U_Bmax = 60 | |
U_t = 30 | |
U = np.arange(0, U_t + 1) | |
S_A = np.zeros(U_t + 1) | |
S_B = np.zeros(U_t + 1) | |
for U_A in range(0, U_t + 1): | |
S_A[U_A] = log(comb(U_Amax, U_A)) | |
S_B[U_A] = log(comb(U_Bmax, U_t - U_A)) | |
S = S_A + S_B | |
plt.plot(U, S_A) | |
plt.plot(U, S_B) | |
plt.plot(U, S) | |
annotate_max(U, S) | |
plt.legend(['S_A', 'S_B', 'S']) | |
plt.title('S vs. U_A') | |
plt.xlabel('U_A') | |
plt.ylabel('S') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment