<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def ess(sim, n):
 """Calculate effective sample size Parameters sim : chains n : int Parameter index starting from 0 """

    try:
        ess = _chains.effective_sample_size(sim, n)
    except (ValueError, ZeroDivisionError):
        ess = nan
    return ess