Esempio n. 1
0
    def regression(self, X, years, monthEndDate='lastMonthEnd'):
        """"OLS regression the ReturnSeries self is the dependent variable
        Args:
            X(ReturnFrame, ReturnSeries): independent variables in ReturnFrame for 1 or more variables, ReturnSeries for 1
            monthEndDate(str, pd.TimeStamp): str format 'yyyy-mm-dd'
            years(int): sets start date 1st day of the month # years from end date
        Returns:
            tuple(params(DataFrame), r squared(float)): params contains coefficients, tvalues, pvalues"""

        # set start and end date
        end = dtf.get_month_end(self, monthEndDate)
        start = dtf.relative_start(end, years)

        # prepare variable arrays
        y = self[start:end]
        if isinstance(X, ReturnSeries):
            X = ReturnFrame(X.loc[start:end]).copy()
        elif isinstance(X, ReturnFrame):
            X = X.loc[start:end, :].copy()
        X['Alpha'] = 1  # add intercept (see statmodels docs)

        # fit model and prepare parameter Dataframe to return
        model = sm.OLS(y, X)
        result = model.fit()
        params = pd.concat([result.params, result.tvalues, result.pvalues],
                           axis=1).rename(columns={
                               0: "params",
                               1: "tvalues",
                               2: "pvalues"
                           })
        return params, result.rsquared
Esempio n. 2
0
 def compounded_series(self, monthStartDate, monthEndDate='lastMonthEnd'):
     # returns a compounded return series starting from monthStartDate till monthEndDate
     end = dtf.get_month_end(self, monthEndDate)
     start = dtf.ts_date(monthStartDate) - pd.offsets.BMonthBegin(n=0)
     period = self[start:end]
     compoundedSrs = pd.Series(index=period.index)
     for i in range(len(period)):
         if i == 0:
             compoundedSrs.iloc[i] = period.iloc[0]
         else:
             compoundedSrs.iloc[i] = (compoundedSrs.iloc[i - 1] +
                                      1) * (period.iloc[i] + 1) - 1
     return compoundedSrs
Esempio n. 3
0
 def annualize(self, years, monthEndDate='lastMonthEnd'):
     """"Annualizes return  over a period going back # years from monthEndDate
     Args:
         years(int):period history in years which will be used for annualizing
         monthEndDate(pd.Timestamp, str): default "lastMonthEnd" selects the last day month end business day in the
         data. Dates can be given in strings as "yyyy-mm-dd" data will select month end of month in date string same
          as for Timestamps.
     Returns:
         tuple( return, sigma): returns a tuple with the annualized return and standard deviation respectively
         """
     end = dtf.get_month_end(self, monthEndDate)
     start = dtf.relative_start(end, years)
     period = self[start:end]
     r_annualized = (_chainlink(period) + 1)**(251 / len(period)) - 1
     sigma_annualized = period.std() * (251**(0.5))
     return (r_annualized, sigma_annualized
             )  # period removed from the return
Esempio n. 4
0
 def compounded_return(self, monthStartDate, monthEndDate='lastMonthEnd'):
     # returns a  single compounded return over period monthStartDate to monthEndDate
     end = dtf.get_month_end(self, monthEndDate)
     start = dtf.ts_date(monthStartDate) - pd.offsets.BMonthBegin(n=0)
     return _chainlink(self[start:end])
Esempio n. 5
0
 def month_return(self, monthEndDate='lastMonthEnd'):
     end = dtf.get_month_end(self, monthEndDate)
     start = end - pd.offsets.BMonthBegin(n=1)
     return _chainlink(self[start:end])
Esempio n. 6
0
    # date determines the end date of the period over which the calculations are performed
    date = 'lastMonthEnd'

    # each int in yearList determines the period timespan going back from date
    yearList = [1, 3, 5]

    # variables to determine sheet and file output names
    sheetName = 'factsheet'
    fileName = 'factsheet.xlsx'

    # ===== no more declarations code executes below ======

    # get data
    df = dataModule.get_data()
    date4xl = dtf.get_month_end(df, date=date).date()

    # add column for y variable in regression
    df.loc[:, y] = df.loc[:, fund] - df.loc[:, rf_rate]

    # Create output dataframes
    dfS = get_riskstats(df=df, fund=fund, benchmark=benchmark, rf_rate=rf_rate, yearList=yearList, date=date)
    dfR = get_returns(df=df, fund=fund, benchmark=benchmark, yearList=yearList, date=date)
    dfPyr = get_period_yearly_returns(df=df, fund=fund, benchmark=benchmark, yearList=yearList)

    # Create graph
    create_graph(df=df, fund=fund, benchmark=benchmark, yearList=yearList, date=date)

    # perform multifactor regression regression over the longest year period in yearList, y = fund returns - rf
    regOutput, r2 = df.regression(y, betas, max(yearList), date)
Esempio n. 7
0
print()
print(rdf.head())
print()

# accessing a ReturnSeries
rsrsF = rdf.loc[:, fund]
print('rsrs data type: {}'.format(type(rsrsF)))
print()
print(rsrsF.head())
print()

rsrsBM = rdf.loc[:, benchmark]
rsrsRF = rdf.loc[:, rf_rate]

# functions that use years and monthEndDate use the 2 functions below from date_functions.py
endDate = dtf.get_month_end(rsrsF, date=date)
startDate = dtf.relative_start(endDate, years=3)
print('The sample for the 3 year statistics start at {} and ends on {} \n'.
      format(startDate, endDate))

# calculate stats based on a ReturnSeries in the ReturnFrame
annualized_return_3Y, annualized_standard_dev_3Y = rsrsF.annualize(
    years=3, monthEndDate=date)

print(
    'The annualized return and standard deviation based on the last 3 years are:'
    '\n Return: {:.2%} \n Standard Deviation: {:.2%}'.format(
        annualized_return_3Y, annualized_standard_dev_3Y))
print('\n Without formatting numbers: \n {} , {}'.format(
    annualized_return_3Y, annualized_standard_dev_3Y))
print()