Однако большие значения m например, m=7,8,9,10 никогда не дает мне результата.
Есть предложения по решению этой проблемы?
Это мой код, я нужно, чтобы это работало для больших значений m.
...
Код: Выделить всё
import sympy as sp
# Specify number of categories using m
m = 6
# Specify interval of expected number of copies o) using i variable for
minimum and maximum values
i_min = 1
i_max = 10
# Calculate expected number of essays to get at least one copy of each
category.
E_m = m * sum(1 / i for i in range(1, m + 1))
# Define auxiliary variable t for Taylor Expansion
t = sp.symbols('t')
# Definition of generative formula
def calculate_probability(m, i_min, i_max):
# Generative formula components
numerator = sp.factorial(m)
denominator = 1
# Denominator as product of (j - t) for j=2 to m
for j in range(2, m + 1):
denominator *= (j - t)
# Ratio of generative formula
generative = t - 1 + numerator / denominator
# Taylor expansion for generative formula as power series
expansion = sp.series(generative, t, 0, m) # using t=0, expansion to t^m
# Shown values for m and interval using i_min to i_max
print(f"For a collection with {m} categories and number of copies (i) from = {i_min} to {i_max}:")
# Show expansion
#print(f"Taylor expansion for generative series:")
#print(expansion)
# Show result
print(f"Expected number of essays: {E_m:.6f}")
# Iterate over interval of i (from i_min to i_max)
for i in range(i_min, i_max + 1):
# Get coefficient of t^i
coef_ti = expansion.coeff(t, i)
# Show expected number rounded to 6 decimals
probability_num = coef_ti.evalf(6)
# Expected number of elements with exactly i copies.
print(f"Expected number of elements with {i} copies: {probability_num}")
# Call function to calculate probability for referred interval
calculate_probability(m, i_min, i_max)
Подробнее здесь: https://stackoverflow.com/questions/792 ... all-values
Мобильная версия