Conditional Mean of K_J Given Alpha
mean_K_given_alpha.RdComputes \(\mathbb{E}[K_J \mid \alpha]\) under a Dirichlet process prior.
Details
The classical digamma closed form is: $$\mu_J(\alpha) = \alpha\{\psi(\alpha+J)-\psi(\alpha)\}$$ where \(\psi(\cdot)\) is the digamma function.
This is equivalent to the direct summation: $$\mu_J(\alpha) = \sum_{i=1}^{J} \frac{\alpha}{\alpha + i - 1}$$
Limiting behavior:
\(\alpha \to 0^+\): \(\mu_J(\alpha) \to 1\)
\(\alpha \to \infty\): \(\mu_J(\alpha) \to J\)
The implementation evaluates the equivalent finite sum. For small
alpha it adds the small increments to 1; for large alpha it
subtracts the small deficits from J. This avoids special-function
cancellation and a discontinuous small-alpha guard.
Examples
mean_K_given_alpha(50, 2.0)
#> [1] 7.037626
mean_K_given_alpha(50, c(0.5, 1, 2, 5))
#> [1] 2.937775 4.499205 7.037626 12.460485
# Limiting behavior
mean_K_given_alpha(50, 1e-10) # Returns a value just above 1
#> [1] 1
mean_K_given_alpha(50, 1e6) # Returns ~50
#> [1] 49.99878