I'm trying to compute the Associated Stirling numbers of the second kind from the recurrence relation http://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind#Associated_Stirling_numbers_of_the_second_kind but I'm not sure what the initial condition are for K <= 1 and N <= 1. I thought if either K <= 1 or N <= 1 the number of ways is just 1 but that doesn't give me the right answers.
My code is just:
import scipy.misc
def stirling_helper(R, N, K):
if N <= 1 or K <= 1:
return 1
Z = K * stirling_helper(R, N-1, K) + scipy.misc.comb(N-1, R-1) * stirling_helper (R, N-1-R+1, K-1)
return Z
def stirling(R, N, K): # Added because the recurrence is for S_r(N+1, K)
return stirling_helper(R, N-1, K)
R = 2
N = 5
K = 2
print stirling(R, N, K) # prints 19, but should be 20?
In general you should choose these initial conditions so that they give the right answers for $n$ and $k$ positive. That is, you force them to work.
If $k>0$ and $n=0$ then $S_r(n,k) = 0$, since there are zero partitions of the empty set into $k$ blocks or size at least $r>1$.
If $k=0$ and $n>0$ then $S_r(n,k)=0$, since there will be no way to partition a non-empty set into zero blocks.
Now define $S_r(0,0)$ to be $1$. Why? We will want $$ 1= S_r(r,1)= 1 \cdot S_r(r-1,1) + \binom{r-1}{r-1} S_r(0,0) $$ to hold.