def do_one_robustness(df):
    [betas_soleshared, mgr_keys,
     permno_keys] = matlab_sparse(df.mgrno,
                                  df.permno,
                                  df.beta_soleshared,
                                  compress=False)
    [betas_sole, mgr_keys, permno_keys] = matlab_sparse(df.mgrno,
                                                        df.permno,
                                                        df.beta_sole,
                                                        compress=False)

    [betas, mgr_keys, permno_keys] = matlab_sparse(df.mgrno,
                                                   df.permno,
                                                   df.beta,
                                                   compress=False)

    kappa_sole = raw_kappa(betas, betas_sole)
    kappa_soleshared = raw_kappa(betas, betas_soleshared)
    kappa_all = raw_kappa(betas, betas)
    #kappa_drop=raw_kappa(betas_drop,betas_drop)

    idx = kappa_all.nonzero()
    out_df = pd.DataFrame({
        'from': permno_keys[idx[0]],
        'to': permno_keys[idx[1]],
        'kappa_all': kappa_all[idx].flatten(),
        'kappa_sole': kappa_sole[idx].flatten(),
        'kappa_soleshared': kappa_soleshared[idx].flatten()
    })
    out_df['quarter'] = df.quarter.iloc[0]
    return out_df
def do_one_merger_breakup(df2):
    # breakup in three blocks
    blockA = df2.loc[~df2['InvestorName'].isnull(),
                     ['mgrno', 'permno', 'beta']]
    blockB = df2.loc[df2['InvestorName'].isnull(), ['mgrno', 'permno', 'beta']]
    blockA.beta = 0.5 * blockA.beta
    blockC = blockA.copy()
    blockC.mgrno = -blockC.mgrno
    df3 = pd.concat([blockA, blockB, blockC], axis=0, ignore_index=True)

    # first do the regular case
    [betas, mgr_keys, permno_keys] = matlab_sparse(df2.mgrno, df2.permno,
                                                   df2.beta)
    k1 = calc_kappa(betas)

    # now do the breakup case using the augmented data
    [betas_b, mgr_keys_b,
     permno_keys_b] = matlab_sparse(df3.mgrno, df3.permno, df3.beta)
    k2 = calc_kappa(betas_b)

    df4 = df2.groupby(['mgrno_merger', 'permno']).sum().reset_index()
    # finally do the merger using the merger mgrno's instead of the real ones
    [betas_m, mgr_keys_m,
     permno_keys_m] = matlab_sparse(df4.mgrno_merger, df4.permno, df4.beta)
    k3 = calc_kappa(betas_m)

    # Ignore BlackRock+Vanguard
    df4 = df2[~(df2['InvestorName'].isin(['BlackRock', 'Vanguard']))]
    [betas_drop, mgr_keys_drop,
     permno_keys_drop] = matlab_sparse(df4.mgrno,
                                       df4.permno,
                                       df4.beta,
                                       compress=False)
    k4 = calc_kappa(betas_drop)

    # put it all together and return
    idx = k1.nonzero()
    out_df = pd.DataFrame({
        'from': permno_keys[idx[0]],
        'to': permno_keys[idx[1]],
        'kappa': k1[idx].flatten(),
        'kappa_breakup': k2[idx].flatten(),
        'kappa_merger': k3[idx].flatten(),
        'kappa_drop': k4[idx].flatten()
    })
    out_df['quarter'] = df2.quarter.iloc[0]
    return out_df
def do_one_l1(df):
    [betas, mgr_keys, permno_keys] = matlab_sparse(df.mgrno, df.permno,
                                                   df.beta)
    l1_measure = calc_l1_measure(betas)

    idx = l1_measure.nonzero()
    out_df = pd.DataFrame({
        'from': permno_keys[idx[0]],
        'to': permno_keys[idx[1]],
        'l1_measure': l1_measure[idx].flatten()
    })
    out_df['quarter'] = df.quarter.iloc[0]
    return out_df
def do_one_investor_similarity(df):
    [betas, mgr_keys, permno_keys] = matlab_sparse(df.mgrno, df.permno,
                                                   df.beta)
    # Market portfolio weights
    (aum, l2, l1) = investor_helper(betas)
    out_df = pd.DataFrame({
        'mgrno': mgr_keys.astype(int),
        'aum_weight': aum,
        'l2_similarity': l2,
        'l1_similarity': l1,
        'cov_aum_l1': np.cov(l1, aum)[1][0]
    })
    out_df['quarter'] = df.quarter.iloc[0]
    return out_df
def do_one_firm_similarity(df):
    [betas, mgr_keys, permno_keys] = matlab_sparse(df.mgrno, df.permno,
                                                   df.beta)
    (aum, l2, l1) = investor_helper(betas)

    norm_l2 = y = (l2 @ (betas / betas.sum(0)))
    norm_l1 = y = (l1 @ (betas / betas.sum(0)))
    nonnorm_l2 = y = (l2 @ betas)
    nonnorm_l1 = y = (l1 @ betas)

    out_df = pd.DataFrame({
        'permno': permno_keys.astype(int),
        'normalized_l1': norm_l1,
        'nonnormalized_l1': nonnorm_l1,
        'normalized_l2': norm_l2,
        'nonnormalized_l2': nonnorm_l2
    })
    out_df['quarter'] = df.quarter.iloc[0]
    return out_df
def do_one_period(df):
    [betas, mgr_keys, permno_keys] = matlab_sparse(df.mgrno, df.permno,
                                                   df.beta)
    kappa = calc_kappa(betas)
    kappa2 = calc_kappa(betas, 2)
    kappa3 = calc_kappa(betas, 3)
    kappa4 = calc_kappa(betas, 0.5)
    kappa5 = calc_kappa(betas, 'CLWY')
    cosine = cosine_similarity(betas.transpose())

    idx = kappa.nonzero()
    out_df = pd.DataFrame({
        'from': permno_keys[idx[0]],
        'to': permno_keys[idx[1]],
        'kappa': kappa[idx].flatten(),
        'kappa_pow2': kappa2[idx].flatten(),
        'kappa_pow3': kappa3[idx].flatten(),
        'kappa_sqrt': kappa4[idx].flatten(),
        'kappa_CLWY': kappa5[idx].flatten(),
        'cosine': cosine[idx].flatten()
    })
    out_df['quarter'] = df.quarter.iloc[0]
    return out_df
def do_one_period(df):
    [betas, mgr_keys, permno_keys] = matlab_sparse(df.mgrno, df.permno,
                                                   df.beta)
    kappa = calc_kappa(betas)
    kappa2 = calc_kappa(betas, 2)
    kappa3 = calc_kappa(betas, 3)
    kappa4 = calc_kappa(betas, 0.5)
    kappa5 = calc_kappa(betas, 'CLWY')
    cosine = cosine_similarity(betas.transpose())
    # this is a bit slow
    l1_measure = calc_l1_measure(betas)

    idx = kappa.nonzero()
    return pd.DataFrame({
        'from': permno_keys[idx[0]],
        'to': permno_keys[idx[1]],
        'kappa': kappa[idx].flatten(),
        'kappa_pow2': kappa2[idx].flatten(),
        'kappa_pow3': kappa3[idx].flatten(),
        'kappa_sqrt': kappa4[idx].flatten(),
        'kappa_CLWY': kappa5[idx].flatten(),
        'cosine': cosine[idx].flatten(),
        'l1_measure': l1_measure[idx].flatten()
    })