javascript chi^2 minimization