src/luck_v_skill.py
def fit(self,min_obs=120,risk_free='RF',market_RF=True,
            market_return='Mkt',fund_RF=False,*args,**kwargs):
        '''Fit regressions to the fund and factor data. If data not passed in yet,
        pass in fund_data and factor_data here. Takes, pd.DataFrames, np.arrays
        if you also pass fund and factor names, or paths to .csv files.
        INPUT
        -----
        min_obs: int, 0 <= min_obs <= n_obs
            - minimum number of not NaN values in fund data to be included
        risk_free: str, Default = 'RF'
            - name of risk free rate column in factors dataframe
        market_RF: bool, Default=True
            - market returns are net risk free rate. If False, pass in col for
            market_return so it can be netted
        market_return: str, Default = 'Mkt'
            - name of market return col in factors dataframe
        fund_RF: bool, Default=False
            - fund returns are net risk free rate. If False, returns net of
            risk free rate are calculated and used in regressions
        OUTPUT
        ------
        self: fitted AlphaEvaluator with statistics on original data.
        '''
        # check for updates to parameters
        self.min_obs = min_obs
        self._has_sim = False
        self._has_percentiles = None

        # check for modifications
        if "load_kwgs" in kwargs.keys():
            self.load_data(**kwargs["load_kwgs"])
        if "olskwgs" in kwargs.keys():
            if 'maxLag' in kwargs["olskwgs"].keys():
                self.maxLag = kwargs["olskwgs"]['maxLag']
        if (self.Y_raw is None) or (self.X_raw is None):
            raise ValueError("Need Data!")

        # Make data regression friendly
        self.Y = self.Y_raw.loc[:, (self.Y_raw.count().values > min_obs)]
        self.X = self.X_raw.copy()

        if (not fund_RF) or (not market_RF):
            try:
                self.RF_series
            except:
                try:
                    self.RF_series = self.X.pop(risk_free) # risk free rate series
                except ValueError:
                    print("No risk free data found in factors dataframe!")

        if not market_RF:
            self.X[market_return] -= self.RF_series
        if not fund_RF:
            self.Y = self.Y.sub(self.RF_series,axis=0)

        self.funds = list(self.Y.columns)
        self.factors = list(self.X.columns)
        if 'const' not in self.X.columns:
            self.X.insert(0, 'const', float(1)) # insert column of ones for the constant in regression

        # Make space for regression results
        coeff_names = ['Alpha']
        for factor in self.factors:
            coeff_names.append(factor)

        # Create empty coefficient, standard error
        self._coeff = pd.DataFrame(index=coeff_names, columns = self.funds)
        self._coeffSE = pd.DataFrame(index=coeff_names, columns = self.funds)

        # make array of non-null sample counts for each fund
        self._n_i = (~np.isnan(self.Y.values)).sum(axis=0)

        # Run regressions for each fund
        for fund in range(len(self.funds)):
            beta,hac_bse = OLS_HAC(Y = self.Y.iloc[:,fund].values,
                                   X = self.X.values, add_const=False,
                                   maxLag = self.maxLag) # run OLS
            # store outcomes
            self._coeff.iloc[:,fund] = beta
            self._coeffSE.iloc[:,fund] = hac_bse

        # Record that regressions are done
        self._is_fit = True

        # Calculate t-statistics
        self._tstats = self._coeff/self._coeffSE

        # rename indices in _tstats dataframe with 't(index)'
        rename_dict = {}
        for coeff in coeff_names:
            rename_dict[coeff] = 't({})'.format(coeff)
        self._tstats.rename(rename_dict,axis='index',inplace=True)

        # store residuals and regression standard error
        Y_pred = np.dot(self.X.values,self._coeff.values)
        self._resids = self.Y.values - Y_pred # residuals
        orig_SSR = np.nansum(self._resids**2,0)           # sum squared residuals
        self._SE_resid = (orig_SSR**(1/2))/(self._n_i-self.X.shape[1]) # standard errors of residuals
        self._SE_resid_ratio = self._SE_resid/self._SE_resid.mean() # ratio of SE's to mean SE
        return self
Fit regressions to the fund and factor data. If data not passed in yet,
        pass in fund_data and factor_data here. Takes, pd.DataFrames, np.arrays
        if you also pass fund and factor names, or paths to .csv files.
        INPUT
        -----
        min_obs: int, 0 <= min_obs <= n_obs
            - minimum number of not NaN values in fund data to be included
        risk_free: str, Default = 'RF'
            - name of risk free rate column in factors dataframe
        market_RF: bool, Default=True
            - market returns are net risk free rate. If False, pass in col for
            market_return so it can be netted
        market_return: str, Default = 'Mkt'
            - name of market return col in factors dataframe
        fund_RF: bool, Default=False
            - fund returns are net risk free rate. If False, returns net of
            risk free rate are calculated and used in regressions
        OUTPUT
        ------
        self: fitted AlphaEvaluator with statistics on original data.
        
Fit regressions to the fund and factor data. If data not passed in yet,
pass in fund_data and factor_data here. Takes, pd.DataFrames, np.arrays
if you also pass fund and factor names, or paths to .csv files.
INPUT

int, 0 <= min_obs <= n_obs
minimum number of not NaN values in fund data to be included
risk_free: str, Default = 'RF'
name of risk free rate column in factors dataframe
market_RF: bool, Default=True
market returns are net risk free rate. If False, pass in col for
market_return so it can be netted
market_return: str, Default = 'Mkt'
name of market return col in factors dataframe
fund_RF: bool, Default=False
fund returns are net risk free rate. If False, returns net of
risk free rate are calculated and used in regressions
OUTPUT

fitted AlphaEvaluator with statistics on original data.
Fit regressions to the fund and factor data . If data not passed in yet pass in fund_data and factor_data here . Takes pd . DataFrames np . arrays if you also pass fund and factor names or paths to . csv files . INPUT int 0 < = min_obs < = n_obs minimum number of not NaN values in fund data to be included risk_free : str Default = ' RF ' name of risk free rate column in factors dataframe market_RF : bool Default = True market returns are net risk free rate . If False pass in col for market_return so it can be netted market_return : str Default = ' Mkt ' name of market return col in factors dataframe fund_RF : bool Default = False fund returns are net risk free rate . If False returns net of risk free rate are calculated and used in regressions OUTPUT fitted AlphaEvaluator with statistics on original data .
def fit(self,min_obs=120,risk_free='RF',market_RF=True,
            market_return='Mkt',fund_RF=False,*args,**kwargs):
        self.min_obs = min_obs
        self._has_sim = False
        self._has_percentiles = None
        if "load_kwgs" in kwargs.keys():
            self.load_data(**kwargs["load_kwgs"])
        if "olskwgs" in kwargs.keys():
            if 'maxLag' in kwargs["olskwgs"].keys():
                self.maxLag = kwargs["olskwgs"]['maxLag']
        if (self.Y_raw is None) or (self.X_raw is None):
            raise ValueError("Need Data!")
        self.Y = self.Y_raw.loc[:, (self.Y_raw.count().values > min_obs)]
        self.X = self.X_raw.copy()
        if (not fund_RF) or (not market_RF):
            try:
                self.RF_series
            except:
                try:
                    self.RF_series = self.X.pop(risk_free) 
                except ValueError:
                    print("No risk free data found in factors dataframe!")
        if not market_RF:
            self.X[market_return] -= self.RF_series
        if not fund_RF:
            self.Y = self.Y.sub(self.RF_series,axis=0)
        self.funds = list(self.Y.columns)
        self.factors = list(self.X.columns)
        if 'const' not in self.X.columns:
            self.X.insert(0, 'const', float(1)) 
        coeff_names = ['Alpha']
        for factor in self.factors:
            coeff_names.append(factor)
        self._coeff = pd.DataFrame(index=coeff_names, columns = self.funds)
        self._coeffSE = pd.DataFrame(index=coeff_names, columns = self.funds)
        self._n_i = (~np.isnan(self.Y.values)).sum(axis=0)
        for fund in range(len(self.funds)):
            beta,hac_bse = OLS_HAC(Y = self.Y.iloc[:,fund].values,
                                   X = self.X.values, add_const=False,
                                   maxLag = self.maxLag) 
            self._coeff.iloc[:,fund] = beta
            self._coeffSE.iloc[:,fund] = hac_bse
        self._is_fit = True
        self._tstats = self._coeff/self._coeffSE
        rename_dict = {}
        for coeff in coeff_names:
            rename_dict[coeff] = 't({})'.format(coeff)
        self._tstats.rename(rename_dict,axis='index',inplace=True)
        Y_pred = np.dot(self.X.values,self._coeff.values)
        self._resids = self.Y.values - Y_pred 
        orig_SSR = np.nansum(self._resids**2,0)           
        self._SE_resid = (orig_SSR**(1/2))/(self._n_i-self.X.shape[1]) 
        self._SE_resid_ratio = self._SE_resid/self._SE_resid.mean() 
        return self
def fit ( self , min_obs = 120 , risk_free = 'RF' , market_RF = True , market_return = 'Mkt' , fund_RF = False , * args , ** kwargs ) : self . min_obs = min_obs self . _has_sim = False self . _has_percentiles = None if "load_kwgs" in kwargs . keys ( ) : self . load_data ( ** kwargs [ "load_kwgs" ] ) if "olskwgs" in kwargs . keys ( ) : if 'maxLag' in kwargs [ "olskwgs" ] . keys ( ) : self . maxLag = kwargs [ "olskwgs" ] [ 'maxLag' ] if ( self . Y_raw is None ) or ( self . X_raw is None ) : raise ValueError ( "Need Data!" ) self . Y = self . Y_raw . loc [ : , ( self . Y_raw . count ( ) . values > min_obs ) ] self . X = self . X_raw . copy ( ) if ( not fund_RF ) or ( not market_RF ) : try : self . RF_series except : try : self . RF_series = self . X . pop ( risk_free ) except ValueError : print ( "No risk free data found in factors dataframe!" ) if not market_RF : self . X [ market_return ] -= self . RF_series if not fund_RF : self . Y = self . Y . sub ( self . RF_series , axis = 0 ) self . funds = list ( self . Y . columns ) self . factors = list ( self . X . columns ) if 'const' not in self . X . columns : self . X . insert ( 0 , 'const' , float ( 1 ) ) coeff_names = [ 'Alpha' ] for factor in self . factors : coeff_names . append ( factor ) self . _coeff = pd . DataFrame ( index = coeff_names , columns = self . funds ) self . _coeffSE = pd . DataFrame ( index = coeff_names , columns = self . funds ) self . _n_i = ( ~ np . isnan ( self . Y . values ) ) . sum ( axis = 0 ) for fund in range ( len ( self . funds ) ) : beta , hac_bse = OLS_HAC ( Y = self . Y . iloc [ : , fund ] . values , X = self . X . values , add_const = False , maxLag = self . maxLag ) self . _coeff . iloc [ : , fund ] = beta self . _coeffSE . iloc [ : , fund ] = hac_bse self . _is_fit = True self . _tstats = self . _coeff / self . _coeffSE rename_dict = { } for coeff in coeff_names : rename_dict [ coeff ] = 't({})' . format ( coeff ) self . _tstats . rename ( rename_dict , axis = 'index' , inplace = True ) Y_pred = np . dot ( self . X . values , self . _coeff . values ) self . _resids = self . Y . values - Y_pred orig_SSR = np . nansum ( self . _resids ** 2 , 0 ) self . _SE_resid = ( orig_SSR ** ( 1 / 2 ) ) / ( self . _n_i - self . X . shape [ 1 ] ) self . _SE_resid_ratio = self . _SE_resid / self . _SE_resid . mean ( ) return self
Fit regressions to the fund and factor data.
Fit regressions to the fund and factor data .
'''Fit regressions to the fund and factor data. If data not passed in yet,
        pass in fund_data and factor_data here. Takes, pd.DataFrames, np.arrays
        if you also pass fund and factor names, or paths to .csv files.
        INPUT
        -----
        min_obs: int, 0 <= min_obs <= n_obs
            - minimum number of not NaN values in fund data to be included
        risk_free: str, Default = 'RF'
            - name of risk free rate column in factors dataframe
        market_RF: bool, Default=True
            - market returns are net risk free rate. If False, pass in col for
            market_return so it can be netted
        market_return: str, Default = 'Mkt'
            - name of market return col in factors dataframe
        fund_RF: bool, Default=False
            - fund returns are net risk free rate. If False, returns net of
            risk free rate are calculated and used in regressions
        OUTPUT
        ------
        self: fitted AlphaEvaluator with statistics on original data.
        ''' # check for updates to parameters # check for modifications # Make data regression friendly # risk free rate series # insert column of ones for the constant in regression # Make space for regression results # Create empty coefficient, standard error # make array of non-null sample counts for each fund # Run regressions for each fund # run OLS # store outcomes # Record that regressions are done # Calculate t-statistics # rename indices in _tstats dataframe with 't(index)' # store residuals and regression standard error # residuals # sum squared residuals # standard errors of residuals # ratio of SE's to mean SE
self
min_obs
risk_free
market_RF
market_return
fund_RF