def interaction_features_query(table_name='%(table_name)s', c_coord={
    'latitude': 52.52437, 'longitude': 13.41053}, n_home_antennas=1):
    """
    Create interaction features which are part of the final feature set.
    For both event types (call, sms) three features are being created.
    X_dist_mean: The average distance in km of an event where the antenna
                 was part of. This feature tries to capture an indication
                 of reach per antenna.
    X_isolation: Simply the percentage of how many unique antennas an
                 antenna has had interactions with. This feature is bound
                 between 0 and 1.
    X_entropy: Entropy means the informative content of an antenna based
               on the interactions of all antennas. It's being calculated
               with the formula for the Shannon Entropy,
               -sum(probability*log(2, probability)), where probability is
               based on the percentage of events happening between each
               pair of antennas over all events.
    Additionally the feature dist2c tracks the distance of an antenna to
    the GPS coordinates of a capital as defined in the attributes class.

    Inputs
    ------
    table_name : name of the table the query is supposed to run on,
                 defaulting to '%(table_name)s', i.e. no substitution

    Output
    ------
    Partial final features dataframe which holds features
    calculated over interactions of antenna pairs.
    """
    query = """
        SELECT
          antenna_id1 as antenna_id,
          -- distance
          SUM(call_count*dist_km)/SUM(call_count) as calls_dist_mean,
          SUM(sms_count*dist_km)/SUM(sms_count) as sms_dist_mean,
          -- isolation
          COUNT(DISTINCT IF(NOT ISNULL(call_count), antenna_id2, NULL))/
           %(n_home_antennas)s as calls_isolation,
          COUNT(DISTINCT IF(NOT ISNULL(sms_count), antenna_id2, NULL))/
           %(n_home_antennas)s as sms_isolation,
          -- entropy
          -SUM(calls_proportion*LOG(2, calls_proportion)) as calls_entropy,
          -SUM(sms_proportion*LOG(2, sms_proportion)) as sms_entropy,
          dist2c
        FROM
        (
          SELECT
            antenna_id1,
            antenna_id2,
            sms_count,
            call_count,
            sms_count/all_sms as sms_proportion,
            call_count/all_calls as calls_proportion,
            -- haversine distance formula
            2 * asin(
                 sqrt(
                   cos(radians(lat1)) *
                   cos(radians(lat2)) *
                   pow(sin(radians((lon1 - lon2)/2)), 2)
                       +
                   pow(sin(radians((lat1 - lat2)/2)), 2)

                 )
               ) * 6371 dist_km,
             2 * asin(
                  sqrt(
                    cos(radians(lat1)) *
                    cos(radians(%(lat_c)s)) *
                    pow(sin(radians((lon1 - %(lon_c)s)/2)), 2)
                        +
                    pow(sin(radians((lat1 - %(lat_c)s)/2)), 2)

                  )
                ) * 6371 dist2c
          FROM
          (
            SELECT
              antenna_id1,
              al1.latitude as lat1,
              al1.longitude as lon1,
              antenna_id2,
              al2.latitude as lat2,
              al2.longitude as lon2,
              sms_count,
              call_count,
              SUM(sms_count) OVER() AS all_sms,
              SUM(call_count) OVER() AS all_calls
            FROM %(table_name)s ai
            JOIN table_antennas_locations al1
            ON ai.antenna_id1 = al1.antenna_id
            JOIN table_antennas_locations al2
            ON ai.antenna_id2 = al2.antenna_id
          )
        )
        GROUP BY antenna_id, dist2c
    """
    return query % {'table_name': table_name, 'lat_c': c_coord['latitude'],
        'lon_c': c_coord['longitude'], 'n_home_antennas': n_home_antennas}
