completed implementation of p_similarity