Пример #1
0
def ret_table(data):
    # Returns timeseries DataFrame of unique permnos
    permnos = data['PERMNO'].unique()
    dfs = threadify(lambda e: permno_to_timeseries(data, e), permnos)
    dfs = threadify(lambda df: df.loc[~df.index.duplicated()], dfs)
    table = pd.concat(dfs, axis=1, sort=False)
    table.index = table.index.values.astype(int)
    return table.fillna(0)
Пример #2
0
def portfolios_me_summary(data, wrt, n):
    # Returns DataFrame. For each date the average market equity in the quantile portfolios.
    dates = data['date'].unique()
    df = pd.DataFrame(
        threadify(lambda d: portfolios_me_summary_month(data, d, wrt, n),
                  dates)).set_index('date')
    return df.sort_index(axis=1)
Пример #3
0
def portfolios_size_summary(data, wrt):
    # Returns DataFrame. For each date the number of stocks in the quantile portfolios.
    dates = data['date'].unique()
    df = pd.DataFrame(
        threadify(lambda d: portfolios_size_summary_month(data, d, wrt),
                  dates)).set_index('date')
    return df.sort_index(axis=1)
Пример #4
0
def pos_table(assigned_data, portfolio, n):
    # Returns positions table
    dates = assigned_data['date'].unique()
    rows = threadify(
        lambda d: portfolio_positions_date(assigned_data, d, portfolio, n),
        dates)
    return pd.DataFrame(rows).set_index('date').sort_index()
Пример #5
0
def quantile_table(data, wrt, n):
    # Returns DataFrame of quantile breakpoints with respect to variable (wrt).
    # n is the number breakpoints formed, e.g. with n = 10, 10 breakpoints are formed
    # which can used to assign stock into 10 portfolios with respect to the variable.
    dates = data['date'].unique()
    qs_dates = threadify(lambda date: quantiles_for_date(data, wrt, date, n),
                         dates)
    return pd.DataFrame(qs_dates)
Пример #6
0
def form_portfolios(data, wrt, n):
    # Returns DataFrame (in CRSP form). Each stock assigned into quantile portfolio with
    # respect to variable (wrt). Total n quantile portfolios.
    start = time.time()
    print('Progress')
    breakpoints = quantile_table(data, wrt, n)
    dates = data['date'].unique()
    dfs = threadify(lambda d: portfolios_for_date(data, breakpoints, d, wrt),
                    dates)
    print('Done. Execution time: {}s'.format(round(time.time() - start, 3)))
    return pd.concat(dfs, ignore_index=True)
Пример #7
0
def lag_returns(data, n):
    # Adds lagged returns (up to n lags) of each stock to the dataset.
    # Loses n first and last observations for each stock.
    permnos = data['PERMNO'].unique()
    dfs = threadify(lambda permno: lag_return_permno(data, n, permno), permnos)
    return pd.concat(dfs, ignore_index=True)
Пример #8
0
def filter_me(data, n):
    # Removes n smallest deciles (10%) by market equity from data
    dates = data['date'].unique()
    assigned_data = form_portfolios(data, 'ME', 10)
    dfs = threadify(lambda d: filter_me_date(assigned_data, d, n), dates)
    return pd.concat(dfs, ignore_index=True).drop('PORT_ME', 1)
Пример #9
0
def portfolios_returns_mean_table(data, wrt, n):
    # Returns DataFrame. Time series returns of the quantile portfolios.
    dates = data['date'].unique()
    returns = threadify(lambda e: portfolios_returns_mean(data, e, wrt, n),
                        dates)
    return pd.DataFrame(returns).set_index('date')