src/AlphaEvaluator_MP.py
def orig_stats(funds,factors,min_obs=120,risk_free='RF',market_RF=True,
               market_return='Mkt',fund_RF=False,maxLag=mLag,
               *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
    -----
    funds: pandas.DataFrame
        - tickers for columns, dates on index. Values should be returns net
        risk-free rate
    factors: pandas.DataFrame
        - factors for columns, dates on index. Dates should line up with funds.
    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
    ------
    '''
    start_time=time.time()
    # check for updates to data
    X = factors.copy()
    Y = funds.copy()
    if (not fund_RF) or (not market_RF):
        try:
            RF_series = X.pop(risk_free)
        except ValueError:
            print("No risk free factor in factors data!")

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

    # Get rid of mostly empty funds
    Y = Y.values.astype(np.float64)
    fund_obs = (~np.isnan(Y)).sum(0)
    keep_funds = fund_obs > min_obs
    tickers = list(funds.columns[keep_funds])

    # Update Y
    Y = Y[:,keep_funds]
    fund_obs = ~np.isnan(Y) # n observations for each fund

    # Store factor data and names
    FFFs = list(X.columns)
    X = X.values.astype(np.float64)

    # add constant if necessary
    if 'const' not in factors.columns:
        X = np.c_[np.ones(X.shape[0]), X]

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

    # Create empty coefficient, standard error
    betas = np.zeros((len(coeff_names),len(tickers)))
    betas.fill(np.nan)
    betas_se = np.zeros(betas.shape)
    betas_se.fill(np.nan)

    # POOLS: run regressions on multiple cpus
    ys = [Y[fund_obs[:,fund],fund] for fund in range(len(tickers))]
    xs = [X[fund_obs[:,fund],:] for fund in range(len(tickers))]

    p = Pool()
    result = p.starmap(OLS_HAC,zip(ys,xs,repeat(False),repeat(maxLag)))
    p.close()
    p.join()

    for i,r in enumerate(result):
        betas[:,i],betas_se[:,i] = r

    print("Total time elapsed {:.2f} ms".format(1000*(time.time()-start_time)))
    return betas, betas_se, Y, X, tickers
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
    -----
    funds: pandas.DataFrame
        - tickers for columns, dates on index. Values should be returns net
        risk-free rate
    factors: pandas.DataFrame
        - factors for columns, dates on index. Dates should line up with funds.
    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
    ------
    
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

pandas.DataFrame
tickers for columns, dates on index. Values should be returns net
risk-free rate
factors: pandas.DataFrame
factors for columns, dates on index. Dates should line up with funds.
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
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 pandas . DataFrame tickers for columns dates on index . Values should be returns net risk - free rate factors : pandas . DataFrame factors for columns dates on index . Dates should line up with funds . 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
def orig_stats(funds,factors,min_obs=120,risk_free='RF',market_RF=True,
               market_return='Mkt',fund_RF=False,maxLag=mLag,
               *args,**kwargs):
    start_time=time.time()
    X = factors.copy()
    Y = funds.copy()
    if (not fund_RF) or (not market_RF):
        try:
            RF_series = X.pop(risk_free)
        except ValueError:
            print("No risk free factor in factors data!")
    if not market_RF:
        X[market_return] -= RF_series
    if not fund_RF:
        Y = Y.sub(RF_series,axis=0)
    Y = Y.values.astype(np.float64)
    fund_obs = (~np.isnan(Y)).sum(0)
    keep_funds = fund_obs > min_obs
    tickers = list(funds.columns[keep_funds])
    Y = Y[:,keep_funds]
    fund_obs = ~np.isnan(Y) 
    FFFs = list(X.columns)
    X = X.values.astype(np.float64)
    if 'const' not in factors.columns:
        X = np.c_[np.ones(X.shape[0]), X]
    coeff_names = ['Alpha']
    for factor in FFFs:
        coeff_names.append(factor)
    betas = np.zeros((len(coeff_names),len(tickers)))
    betas.fill(np.nan)
    betas_se = np.zeros(betas.shape)
    betas_se.fill(np.nan)
    ys = [Y[fund_obs[:,fund],fund] for fund in range(len(tickers))]
    xs = [X[fund_obs[:,fund],:] for fund in range(len(tickers))]
    p = Pool()
    result = p.starmap(OLS_HAC,zip(ys,xs,repeat(False),repeat(maxLag)))
    p.close()
    p.join()
    for i,r in enumerate(result):
        betas[:,i],betas_se[:,i] = r
    print("Total time elapsed {:.2f} ms".format(1000*(time.time()-start_time)))
    return betas, betas_se, Y, X, tickers
def orig_stats ( funds , factors , min_obs = 120 , risk_free = 'RF' , market_RF = True , market_return = 'Mkt' , fund_RF = False , maxLag = mLag , * args , ** kwargs ) : start_time = time . time ( ) X = factors . copy ( ) Y = funds . copy ( ) if ( not fund_RF ) or ( not market_RF ) : try : RF_series = X . pop ( risk_free ) except ValueError : print ( "No risk free factor in factors data!" ) if not market_RF : X [ market_return ] -= RF_series if not fund_RF : Y = Y . sub ( RF_series , axis = 0 ) Y = Y . values . astype ( np . float64 ) fund_obs = ( ~ np . isnan ( Y ) ) . sum ( 0 ) keep_funds = fund_obs > min_obs tickers = list ( funds . columns [ keep_funds ] ) Y = Y [ : , keep_funds ] fund_obs = ~ np . isnan ( Y ) FFFs = list ( X . columns ) X = X . values . astype ( np . float64 ) if 'const' not in factors . columns : X = np . c_ [ np . ones ( X . shape [ 0 ] ) , X ] coeff_names = [ 'Alpha' ] for factor in FFFs : coeff_names . append ( factor ) betas = np . zeros ( ( len ( coeff_names ) , len ( tickers ) ) ) betas . fill ( np . nan ) betas_se = np . zeros ( betas . shape ) betas_se . fill ( np . nan ) ys = [ Y [ fund_obs [ : , fund ] , fund ] for fund in range ( len ( tickers ) ) ] xs = [ X [ fund_obs [ : , fund ] , : ] for fund in range ( len ( tickers ) ) ] p = Pool ( ) result = p . starmap ( OLS_HAC , zip ( ys , xs , repeat ( False ) , repeat ( maxLag ) ) ) p . close ( ) p . join ( ) for i , r in enumerate ( result ) : betas [ : , i ] , betas_se [ : , i ] = r print ( "Total time elapsed {:.2f} ms" . format ( 1000 * ( time . time ( ) - start_time ) ) ) return betas , betas_se , Y , X , tickers
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
    -----
    funds: pandas.DataFrame
        - tickers for columns, dates on index. Values should be returns net
        risk-free rate
    factors: pandas.DataFrame
        - factors for columns, dates on index. Dates should line up with funds.
    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
    ------
    ''' # check for updates to data # Get rid of mostly empty funds # Update Y # n observations for each fund # Store factor data and names # add constant if necessary # Make space for regression results # Create empty coefficient, standard error # POOLS: run regressions on multiple cpus
funds
factors
min_obs
risk_free
market_RF
market_return
fund_RF
maxLag