import math


def check_convergence(values, limit):
    #We want the final values for each particle to be very very similar/close. I check convergence by standard deviation.
    """
    :param values: list of values of obj. function corresponding to a set of particles
    :param sd: standard deviation
    :return: boolean whether the criterion is met or not, criterion : stander var. <= a set limit
    """
    flag = False
    n = len(values)
    mean = sum(values) / n
    diff = [i - mean for i in values]
    diff_2 = [x ** 2 for x in diff]
    my_sum = sum(diff_2)
    variance = my_sum / n
    sd = math.sqrt(variance)
    if (sd <= limit):
        flag = True
    return flag