Exemple #1
0
def test_capm_lower_rf(series):
    manager, index, rf = statistics.extract_returns_bmark_rf_partial(
        series, 0, 7, 9, True, 0, True)
    alpha, beta, r2 = statistics.capm(manager, index, rf)
    assert alpha == pytest.approx(-0.0119, abs=MINE)
    assert beta == pytest.approx(0.3991, abs=MINE)
    assert r2 == pytest.approx(0.6079, abs=MINE)
Exemple #2
0
def test_capm_nas(series):
    manager, index, rf = statistics.extract_returns_bmark_rf(
        series, 1, 7, None, True)
    alpha, beta, r2 = statistics.capm(manager, index)
    assert alpha == pytest.approx(0.0111, abs=MINE)
    assert beta == pytest.approx(0.3431, abs=MINE)
    assert r2 == pytest.approx(0.1704, abs=MINE)
Exemple #3
0
def test_capm_lower(series):
    manager, index, rf = statistics.extract_returns_bmark_rf_partial(
        series, 0, 7, None, True, 0, True)
    alpha, beta, r2 = statistics.capm(manager, index)
    assert alpha == pytest.approx(-0.0100, abs=MINE)
    assert beta == pytest.approx(0.4000, abs=MINE)
    assert r2 == pytest.approx(0.6103, abs=MINE)
Exemple #4
0
def test_capm_upper_rf(series):
    manager, index, rf = statistics.extract_returns_bmark_rf_partial(
        series, 0, 7, 9, True, 0, False)
    alpha, beta, r2 = statistics.capm(manager, index, rf)
    assert alpha == pytest.approx(0.0151, abs=MINE)
    assert beta == pytest.approx(0.1992, abs=MINE)
    assert r2 == pytest.approx(0.1985, abs=MINE)
Exemple #5
0
def test_capm_rf(series):
    manager, index, rf = statistics.extract_returns_bmark_rf(
        series, 0, 7, 9, True)
    alpha, beta, r2 = statistics.capm(manager, index, rf)
    assert alpha == pytest.approx(0.0057, abs=MINE)
    assert beta == pytest.approx(0.3900, abs=MINE)
    assert r2 == pytest.approx(0.4338, abs=MINE)
Exemple #6
0
def capm_table(data_series, manager_cols, index_col, rf_col):
    # check to make sure nothing is missing
    if index_col is None:
        raise ValueError("Index Column cannot be blank")
    if isinstance(index_col, collections.Iterable):
        raise ValueError("Index Column must be a single value")
    if manager_cols is None:
        raise ValueError("Manager column cannot be None")
    if not isinstance(manager_cols, collections.Iterable):
        manager_cols = [manager_cols]
    if rf_col is None:
        raise ValueError("Risk free column cannot be None")
    if isinstance(rf_col, collections.Iterable):
        raise ValueError("Risk free column must be a single value")

    # now create lists of the data points by comparing the manager to each comparison
    # TODO this is really ugly, fix
    alpha = []
    beta = []
    betap = []
    betam = []
    r2 = []
    r = []
    rp = []
    te = []
    ap = []
    ir = []
    tr = []
    for mc in manager_cols:
        manager, index, rf = pas.extract_returns_bmark_rf(data_series, mc, index_col, rf_col)
        manager_u, index_u, rf_u = pas.extract_returns_bmark_rf_partial(data_series, mc, index_col, rf_col, lower=False)
        manager_d, index_d, rf_d = pas.extract_returns_bmark_rf_partial(data_series, mc, index_col, rf_col)
        alpha.append(pas.capm(manager, index, rf)[0])
        beta.append(pas.capm(manager, index, rf)[1])
        r2.append(pas.capm(manager, index, rf)[2])
        betap.append(pas.capm(manager_u, index_u, rf_u)[1])
        betam.append(pas.capm(manager_d, index_d, rf_d)[1])
        r.append(pas.correlation(manager, index)[0])
        rp.append(pas.correlation(manager, index)[1])
        te.append(pas.tracking_error(manager, index))
        ap.append(pas.active_premium(manager, index))
        ir.append(pas.information_ratio(manager, index))
        tr.append(pas.treynor_ratio(manager, index, rf))
    st_data = {'Alpha': alpha,
               'Beta': beta,
               'Beta+': betap,
               'Beta-': betam,
               'R2': r2,
               'Correlation': r,
               'Correlation p-value': rp,
               'Tracking Error': te,
               'Active Premium': ap,
               'Information Ratio': ir,
               'Treynor Ratio': tr}

    df = pd.DataFrame.from_dict(st_data, orient='index')

    # replace the column names
    colnames = [' vs '.join([data_series.columns[x], data_series.columns[index_col]]) for x in manager_cols]
    # rename the columns
    df = replace_col_names(df, colnames)

    return df
                        data.even.apply(position)).dropna()
    # strategy 2: short on even days, long on odd days
    strategy2_return = (data['Close'].pct_change() *
                        -data.even.apply(position)).dropna()

    strategy1_return.describe()
    strategy2_return.describe()

    pas.vol(snp500_return)
    pas.beta(strategy1_return, snp500_return)
    pas.beta(strategy2_return, snp500_return)
    pas.correlation(strategy1_return, snp500_return)
    pas.sharpe_ratio(snp500_return, 0.03)
    pas.sharpe_ratio(strategy1_return, 0.03)
    pas.sharpe_ratio(strategy2_return, 0.03)
    pas.capm(strategy1_return, snp500_return)
    pas.capm(strategy2_return, snp500_return)

    pas.annualized_return(snp500_return, datetime.date(1928, 1, 3),
                          datetime.date(2020, 2, 12))
    pas.annualized_return(strategy1_return, datetime.date(1928, 1, 3),
                          datetime.date(2020, 2, 12))
    pas.annualized_return(strategy2_return, datetime.date(1928, 1, 3),
                          datetime.date(2020, 2, 12))

    data['snp500_return'] = data['Close'].pct_change()
    pat.stats_table(data, manager_col=8)

    data['strategy1_return'] = (data['Close'].pct_change() *
                                data.even.apply(position))
    pat.stats_table(data, manager_col=9)