import math


def user_based_sim(mean_data, user_data, u1, u2):
    """Returns a similarity using Pearson correlation.

        Parameters
        ----------
        mean_data : dict
            Dictionary of the users mean of ratings

        user_data : dict
            Dictionary of the data set.

        u1 : str
            Id of user to be used for similarity measure.

        u2 : str
            Id of user to be used for similarity measure.

        Returns
        -------
        similarity value : float
            Returns Pearson correlation of u1 and u2.
    """
    u1_movies = set([m1 for m1 in user_data[u1]])
    u2_movies = set([m2 for m2 in user_data[u2]])

    joint = u1_movies & u2_movies

    if len(joint) == 0:
        return 0

    mean1 = mean_data[u1]
    mean2 = mean_data[u2]

    numer = 0
    denom1 = 0
    denom2 = 0

    for m in joint:
        ra = user_data[u1][m]
        rb = user_data[u2][m]

        numer += (ra - mean1) * (rb - mean2)
        denom1 += math.pow(ra - mean1, 2)
        denom2 += math.pow(rb - mean2, 2)

    if denom1 == 0 or denom2 == 0:
        return 0

    return round(numer / (math.sqrt(denom1) * math.sqrt(denom2)), 15)