예제 #1
0
def Return_excess (R, Rf = 0):
    """
    :param R: a matrix, data frame, or timeSeries of returns
    :param Rf: a measure of the risk free rate, whether a period average (a single number) or a timeseries vector
    :return: A timeseries of the calculated series
    Excess Return = Return - Risk Free Rate(Return of benchmark)
    """

    """
    check for index
    """
    """
    Here should be period check for Scale
    """
    """
    Here should be check for mutilple Return matrix and set columns names
    """

    # if type(Rf) is pd.Series:
    R=ut.frame_type(R)
    Rf=ut.frame_type(Rf)
    if type(Rf) is pd.DataFrame:
        Rf=Rf.reindex(R.index.join(Rf.index,how='outer'),fill_value=0)


    result=R-Rf

    return result
예제 #2
0
def CAPM_jensenAlpha (Ra, Rb, Rf = 0):
    calcul = True
    # Ra = checkData(Ra, method="matrix")
    # Rb = checkData(Rb, method="matrix")
    Ra=Ra.dropna()
    Rb=Rb.dropna()

    Ra=ut.frame_type(Ra)
    Rb=ut.frame_type(Rb)

    if (Ra.shape[1]==1):
         #period = Frequency(Ra)
        period=252
        Rp = ((1 + Ra).prod())**(period / Ra.shape[0]) - 1
        Rpb = ((1 + Rb).prod())**(period / Rb.shape[0]) - 1
        result = Rp - Rf - CAPM_beta(Ra,Rb,Rf) * (Rpb - Rf)
    else:
        result = pd.DataFrame([],columns=Ra.columns)
        for i in range(Ra.shape[1]):
            result.iloc[:,i] = CAPM_jensenAlpha(Ra.iloc[:,i],Rb.iloc[:,i],Rf)
    return result
예제 #3
0
def ES(R=None,p=0.95,method='modified',
       portfolio_method='single',
       weights=None,
       mu=None,
       sigma=None,
       m3=None,
       m4=None,
       clean=None,
       invert=True,
       operational=True):
    if type(R) is not pd.DataFrame:
        R=pd.DataFrame([R])
    R=R.dropna()
    R = ut.frame_type(R)
    if (portfolio_method == 'single'):
        if weights is None:
            ES = pd.DataFrame([],columns=R.columns)
            for i in R.columns:
                if method=='modified' and operational==True:
                    ES[i] =operES_CornishFisher(R[i],p)
                elif method=='modified' and operational==False:
                    ES[i]=ES_CornishFiser(R[i],p)
                elif method=='gaussian':
                    ES[i]=ES_Gaussian(R[i],p)
                elif method=='historical':
                    ES[i]=ES_historical(R[i],p)
        else:
            if(weights.__len__()!=R.shape[1]):
                raise NameError ("number of items in weights not equal to number of columns in R")
            weights.index=R.columns
            if(mu is None):
                mu=R.mean()
            if(sigma is None):
                sigma=R.cov()
            if(method=='modified'):
                if (m3 is None):
                    m3 = M3_MM(R,mu=mu)
                if (m4 is None):
                    m4 = M4_MM(R,mu=mu)
                ES=mES_MM(w=weights,mu=mu,sigma=sigma,M3=m3,M4=m4,p=p)
            elif (method=='gaussian'):
                ES=GES_MM(w=weights,mu=mu,sigma=sigma,p=p)
            elif (method=='historical'):
                ES=ES_historical(R,p).dot(weights)
                ES=pd.DataFrame([ES],columns=['ES_Historical_Weighted'])

        for i in ES.columns:
            if (ES[i][0]  < 0):
                warnings.warn ('ES calculation produces unreliable result (inverse risk) for column %s: %s'% (i,ES[i][0]))
                ES[i] = None
            elif (ES[i][0] >1):
                warnings.warn('ES calculation produces unreliable result (risk over 100%) for column %s: %s'% (i,ES[i][0]))
                ES[i] = 1
            if (invert):
                try:
                    ES[i]=-ES[i]
                except TypeError:
                    pass
    elif (portfolio_method=='component'):
        if(weights is None):
            weights=pd.Series([1/R.shape[1]]*R.shape[1])
            weights.index=R.columns
        else:
            if weights.__len__()!=R.shape[1]:
                raise NameError ("number of items in weights not equal to number of columns in R")
        #weights=weights.reindex(R.index,method='ffill')
        weights.index=R.columns
        if(mu is None):
            mu=R.mean()
        if(sigma is None):
            sigma=R.cov()
        if(method=='modified'):
            if (m3 is None):
                m3 = M3_MM(R,mu=mu)
            if (m4 is None):
                m4 = M4_MM(R,mu=mu)
            if (operational==True):
                ES=operES_CornishFisher_portfolio(p,weights,mu,sigma,m3,m4)
            else:
                ES=ES_CornishFisher_portfolio(p,weights,mu,sigma,m3,m4)
        elif(method=='gaussian'):
            ES=ES_Gaussian_portfolio(p,weights,mu,sigma)
        elif(method=='historical'):
            ES=ES_historical_portfolio(R,p,weights)
    return ES