<commit_before>#
# Copyright 2015 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division

import numpy as np
import pandas as pd
import scipy as sp
from scipy import stats
import theano.tensor as tt

import matplotlib.pyplot as plt

import pymc3 as pm

from . import _seaborn as sns
from .timeseries import cum_returns


def model_returns_t_alpha_beta(data, bmark, samples=2000):
    """Run Bayesian alpha-beta-model with T distributed returns.

    This model estimates intercept (alpha) and slope (beta) of two
    return sets. Usually, these will be algorithm returns and
    benchmark returns (e.g. S&P500). The data is assumed to be T
    distributed and thus is robust to outliers and takes tail events
    into account.  If a pandas.DataFrame is passed as a benchmark, then
    multiple linear regression is used to estimate alpha and beta.

    Parameters
    ----------
    returns : pandas.Series
        Series of simple returns of an algorithm or stock.
    bmark : pandas.DataFrame
        DataFrame of benchmark returns (e.g., S&P500) or risk factors (e.g.,
        Fama-French SMB, HML, and UMD).
        If bmark has more recent returns than returns_train, these dates
        will be treated as missing values and predictions will be
        generated for them taking market correlations into account.
    samples : int (optional)
        Number of posterior samples to draw.

    Returns
    -------
    model : pymc.Model object
        PyMC3 model containing all random variables.
    trace : pymc3.sampling.BaseTrace object
        A PyMC3 trace object that contains samples for each parameter
        of the posterior.
    """

    bmark = bmark.dropna()

    if data.shape[0] != bmark.shape[0]:
        data = pd.Series(data, index=bmark.index)

    if bmark.ndim > 1:
        Nbmark = bmark.shape[1]
    else:
        Nbmark = 1
    data_no_missing = data.dropna()

    with pm.Model() as model:
        sigma = pm.HalfCauchy(
            'sigma',
            beta=1,
            testval=data_no_missing.values.std())
        nu = pm.Exponential('nu_minus_two', 1. / 10., testval=.3)

        # alpha and beta
        X = pd.DataFrame(bmark.loc[data_no_missing.index])
        X.loc[:, 'ones'] = 1.
        y = data_no_missing
        alphabeta_init = np.linalg.lstsq(X, y)[0]  # [:2]

        alpha_reg = pm.Normal('alpha', mu=0, sd=.1, testval=alphabeta_init[-1])
        beta_reg = pm.Normal('beta', mu=0, sd=1,
                             testval=alphabeta_init[:-1], shape=Nbmark)
        bmark_theano = tt.as_tensor_variable(bmark.ix[data_no_missing.index].T)
        mu_reg = alpha_reg + tt.dot(beta_reg, bmark_theano)
        pm.T('returns',
             nu=nu + 2,
             mu=mu_reg,
             sd=sigma,
             observed=data)
        start = pm.find_MAP(fmin=sp.optimize.fmin_powell)<commit_msg>BUG Fix issue with passing 1D benchmark.<commit_after>#
# Copyright 2015 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division

import numpy as np
import pandas as pd
import scipy as sp
from scipy import stats
import theano.tensor as tt

import matplotlib.pyplot as plt

import pymc3 as pm

from . import _seaborn as sns
from .timeseries import cum_returns


def model_returns_t_alpha_beta(data, bmark, samples=2000):
    """Run Bayesian alpha-beta-model with T distributed returns.

    This model estimates intercept (alpha) and slope (beta) of two
    return sets. Usually, these will be algorithm returns and
    benchmark returns (e.g. S&P500). The data is assumed to be T
    distributed and thus is robust to outliers and takes tail events
    into account.  If a pandas.DataFrame is passed as a benchmark, then
    multiple linear regression is used to estimate alpha and beta.

    Parameters
    ----------
    returns : pandas.Series
        Series of simple returns of an algorithm or stock.
    bmark : pandas.DataFrame
        DataFrame of benchmark returns (e.g., S&P500) or risk factors (e.g.,
        Fama-French SMB, HML, and UMD).
        If bmark has more recent returns than returns_train, these dates
        will be treated as missing values and predictions will be
        generated for them taking market correlations into account.
    samples : int (optional)
        Number of posterior samples to draw.

    Returns
    -------
    model : pymc.Model object
        PyMC3 model containing all random variables.
    trace : pymc3.sampling.BaseTrace object
        A PyMC3 trace object that contains samples for each parameter
        of the posterior.
    """
    if data.shape[0] != bmark.shape[0]:
        data = pd.Series(data, index=bmark.index)

    data_no_missing = data.dropna()

    if bmark.ndim == 1:
        bmark = pd.DataFrame(bmark)

    bmark = bmark.loc[data_no_missing.index]
    n_bmark = bmark.shape[1]

    with pm.Model() as model:
        sigma = pm.HalfCauchy(
            'sigma',
            beta=1,
            testval=data_no_missing.values.std())
        nu = pm.Exponential('nu_minus_two', 1. / 10., testval=.3)

        # alpha and beta
        X = bmark.loc[data_no_missing.index]
        X.loc[:, 'ones'] = 1.
        y = data_no_missing
        alphabeta_init = np.linalg.lstsq(X, y)[0]

        alpha_reg = pm.Normal('alpha', mu=0, sd=.1, testval=alphabeta_init[-1])
        beta_reg = pm.Normal('beta', mu=0, sd=1,
                             testval=alphabeta_init[:-1], shape=n_bmark)
        bmark_theano = tt.as_tensor_variable(bmark.values.T)
        mu_reg = alpha_reg + tt.dot(beta_reg, bmark_theano)
        pm.T('returns',
             nu=nu + 2,
             mu=mu_reg,
             sd=sigma,
             observed=data)
        start = pm.find_MAP(fmin=sp.optimize.fmin_powell)
{'fraction_non_alphanumeric': 0.08574380165289257, 'fraction_numerical': 0.012249114521841794, 'mean_word_length': 2.6124733475479744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1958229', 'n_tokens_mistral': 2075, 'n_tokens_neox': 1976, 'n_words': 723}