#NEWLINE# def assortativity_indicators( user ) : #NEWLINE# #INDENT# "\n    Computes the assortativity of indicators.\n\n    This indicator measures the similarity of the current user with his\n    correspondants, for all bandicoot indicators. For each one, it calculates\n    the variance of the current user's value with the values for all his\n    correspondants:\n\n    .. math::\n\n        \\text{assortativity}(J) = \\frac{1}{n} \\sum_i^n (J_{\\text{user}} - J_{\\text{i}})^2\n\n    for the indicator :math:`J`, and all the :math:`n` correspondents.\n    " #NEWLINE# matrix = matrix_undirected_unweighted ( user ) #NEWLINE# count_indicator = defaultdict ( int ) #NEWLINE# total_indicator = defaultdict ( int ) #NEWLINE# ego_indics = all ( user , flatten = True ) #NEWLINE# ego_indics = { a : value for ( a , value ) in ego_indics . items ( ) if ( ( a != 'name' ) and ( a [ : 11 ] != 'reporting__' ) and ( a [ : 10 ] != 'attributes' ) ) } #NEWLINE# for ( i , u_name ) in enumerate ( matrix_index ( user ) ) : #NEWLINE# #INDENT# correspondent = user . network . get ( u_name , None ) #NEWLINE# if ( ( correspondent is None ) or ( u_name == user . name ) or ( matrix [ 0 ] [ i ] == 0 ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# neighbor_indics = all ( correspondent , flatten = True ) #NEWLINE# for a in ego_indics : #NEWLINE# #INDENT# if ( ( ego_indics [ a ] is not None ) and ( neighbor_indics [ a ] is not None ) ) : #NEWLINE# #INDENT# total_indicator [ a ] += 1 #NEWLINE# count_indicator [ a ] += ( ( ego_indics [ a ] - neighbor_indics [ a ] ) ** 2 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# assortativity = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for i in count_indicator : #NEWLINE# #INDENT# assortativity [ i ] = ( count_indicator [ i ] / total_indicator [ i ] ) #NEWLINE# #UNINDENT# return assortativity