src/AlphaEvaluator_MP.py
def simulate_MP2(n_sim,Y,X,betas,betas_se,random_seed=None,verbose=False,
                 sim_std=0,need_orig_stats=False,sim_cutoff=15,maxLag=mLag,
                 *args,**kwargs):
    '''Simulate fund returns. Stores the simulation results under the
    attributes with _sim suffix. This simulation process uses a slightly different
    multiprocessing technique. I'm not sure which is faster yet, so I've left it
    here.

    UPDATE DOC STRING
    '''
    start_time = time.time() # start timer
    if need_orig_stats:
        if "statskwgs" in kwargs.keys():
            betas, betas_se, Y, X = orig_stats_MP(funds=Y,factors=X,
                                                  **kwargs["statskwgs"])
        else:
            betas, betas_se, Y, X = orig_stats_MP(funds=Y,factors=X)

    # Check input parameters for any modifications to behavior
    if random_seed is not None:
        np.random.seed(seed=random_seed)

    # check for change in max lag function
    if 'olskwgs' in kwargs.keys():
        if 'maxLag' in kwargs['olskwgs'].keys():
            maxLag = kwargs['olskwgs']['maxLag']

    # parameters
    n_obs, n_funds = Y.shape
    std_alpha = sim_std/np.sqrt(12)
    orig_betas = betas[1:,:]

    if verbose:
        print("Annual standard deviation: {:.2f}, Monthly standard deviation alpha: {:.2f}"\
              .format(sim_std, std_alpha))

    # Populate target output vectors to be filled in with loop:
    alphas_se_sim = np.zeros((n_funds,n_sim))
    alphas_se_sim.fill(np.nan)
    alphas_sim = np.zeros((n_funds,n_sim))
    alphas_sim.fill(np.nan)

    # randomized indices for simulations
    sim_indices = np.random.randint(0, n_obs, size=(n_obs,n_sim))

    # regressions
    if verbose:
        print("Starting {:,} regressions...".format(n_sim*n_funds))

    resids = Y - X.dot(betas)
    fund_obs = (~np.isnan(Y)).sum(0)
    SSR = np.nansum(resids**2,0)
    RSE = SSR**(1/2)/(fund_obs-X.shape[1])
    RSE_ratio = RSE/RSE.mean()

    # zip args for pool.starmap function
    zip_args = zip([sim_indices[:,ss] for ss in range(n_sim)],repeat(Y),
                    repeat(X),repeat(resids),repeat(orig_betas),
                    repeat(std_alpha),repeat(maxLag),
                    repeat(sim_cutoff),repeat(RSE_ratio))

    part_func = partial(sim_alpha,Y=Y,X=X,resids=resids,betas=orig_betas,
                        std_alpha=std_alpha,maxLag=maxLag,
                        sim_cutoff=sim_cutoff,RSE_ratio=RSE_ratio)

    # Begin pooling cpu's
    p = Pool()
    result = p.map(part_func,[sim_indices[:,ss] for ss in range(n_sim)])
    p.close()
    p.join()

    # assign results from Pool to arrays
    for ss,r in enumerate(result):
        alphas_sim[:,ss] = r[0]
        alphas_se_sim[:,ss] = r[1]

    # DONE
    if verbose:
        print("Simulations complete in {:.3f} seconds!".format(time.time()-start_time))

    return alphas_sim, alphas_se_sim
Simulate fund returns. Stores the simulation results under the
    attributes with _sim suffix. This simulation process uses a slightly different
    multiprocessing technique. I'm not sure which is faster yet, so I've left it
    here.

    UPDATE DOC STRING
    
Simulate fund returns. Stores the simulation results under the
attributes with _sim suffix. This simulation process uses a slightly different
multiprocessing technique. I'm not sure which is faster yet, so I've left it
here.

UPDATE DOC STRING
Simulate fund returns . Stores the simulation results under the attributes with _sim suffix . This simulation process uses a slightly different multiprocessing technique . I ' m not sure which is faster yet so I ' ve left it here . UPDATE DOC STRING
def simulate_MP2(n_sim,Y,X,betas,betas_se,random_seed=None,verbose=False,
                 sim_std=0,need_orig_stats=False,sim_cutoff=15,maxLag=mLag,
                 *args,**kwargs):
    start_time = time.time() 
    if need_orig_stats:
        if "statskwgs" in kwargs.keys():
            betas, betas_se, Y, X = orig_stats_MP(funds=Y,factors=X,
                                                  **kwargs["statskwgs"])
        else:
            betas, betas_se, Y, X = orig_stats_MP(funds=Y,factors=X)
    if random_seed is not None:
        np.random.seed(seed=random_seed)
    if 'olskwgs' in kwargs.keys():
        if 'maxLag' in kwargs['olskwgs'].keys():
            maxLag = kwargs['olskwgs']['maxLag']
    n_obs, n_funds = Y.shape
    std_alpha = sim_std/np.sqrt(12)
    orig_betas = betas[1:,:]
    if verbose:
        print("Annual standard deviation: {:.2f}, Monthly standard deviation alpha: {:.2f}"\
              .format(sim_std, std_alpha))
    alphas_se_sim = np.zeros((n_funds,n_sim))
    alphas_se_sim.fill(np.nan)
    alphas_sim = np.zeros((n_funds,n_sim))
    alphas_sim.fill(np.nan)
    sim_indices = np.random.randint(0, n_obs, size=(n_obs,n_sim))
    if verbose:
        print("Starting {:,} regressions...".format(n_sim*n_funds))
    resids = Y - X.dot(betas)
    fund_obs = (~np.isnan(Y)).sum(0)
    SSR = np.nansum(resids**2,0)
    RSE = SSR**(1/2)/(fund_obs-X.shape[1])
    RSE_ratio = RSE/RSE.mean()
    zip_args = zip([sim_indices[:,ss] for ss in range(n_sim)],repeat(Y),
                    repeat(X),repeat(resids),repeat(orig_betas),
                    repeat(std_alpha),repeat(maxLag),
                    repeat(sim_cutoff),repeat(RSE_ratio))
    part_func = partial(sim_alpha,Y=Y,X=X,resids=resids,betas=orig_betas,
                        std_alpha=std_alpha,maxLag=maxLag,
                        sim_cutoff=sim_cutoff,RSE_ratio=RSE_ratio)
    p = Pool()
    result = p.map(part_func,[sim_indices[:,ss] for ss in range(n_sim)])
    p.close()
    p.join()
    for ss,r in enumerate(result):
        alphas_sim[:,ss] = r[0]
        alphas_se_sim[:,ss] = r[1]
    if verbose:
        print("Simulations complete in {:.3f} seconds!".format(time.time()-start_time))
    return alphas_sim, alphas_se_sim
def simulate_MP2 ( n_sim , Y , X , betas , betas_se , random_seed = None , verbose = False , sim_std = 0 , need_orig_stats = False , sim_cutoff = 15 , maxLag = mLag , * args , ** kwargs ) : start_time = time . time ( ) if need_orig_stats : if "statskwgs" in kwargs . keys ( ) : betas , betas_se , Y , X = orig_stats_MP ( funds = Y , factors = X , ** kwargs [ "statskwgs" ] ) else : betas , betas_se , Y , X = orig_stats_MP ( funds = Y , factors = X ) if random_seed is not None : np . random . seed ( seed = random_seed ) if 'olskwgs' in kwargs . keys ( ) : if 'maxLag' in kwargs [ 'olskwgs' ] . keys ( ) : maxLag = kwargs [ 'olskwgs' ] [ 'maxLag' ] n_obs , n_funds = Y . shape std_alpha = sim_std / np . sqrt ( 12 ) orig_betas = betas [ 1 : , : ] if verbose : print ( "Annual standard deviation: {:.2f}, Monthly standard deviation alpha: {:.2f}" . format ( sim_std , std_alpha ) ) alphas_se_sim = np . zeros ( ( n_funds , n_sim ) ) alphas_se_sim . fill ( np . nan ) alphas_sim = np . zeros ( ( n_funds , n_sim ) ) alphas_sim . fill ( np . nan ) sim_indices = np . random . randint ( 0 , n_obs , size = ( n_obs , n_sim ) ) if verbose : print ( "Starting {:,} regressions..." . format ( n_sim * n_funds ) ) resids = Y - X . dot ( betas ) fund_obs = ( ~ np . isnan ( Y ) ) . sum ( 0 ) SSR = np . nansum ( resids ** 2 , 0 ) RSE = SSR ** ( 1 / 2 ) / ( fund_obs - X . shape [ 1 ] ) RSE_ratio = RSE / RSE . mean ( ) zip_args = zip ( [ sim_indices [ : , ss ] for ss in range ( n_sim ) ] , repeat ( Y ) , repeat ( X ) , repeat ( resids ) , repeat ( orig_betas ) , repeat ( std_alpha ) , repeat ( maxLag ) , repeat ( sim_cutoff ) , repeat ( RSE_ratio ) ) part_func = partial ( sim_alpha , Y = Y , X = X , resids = resids , betas = orig_betas , std_alpha = std_alpha , maxLag = maxLag , sim_cutoff = sim_cutoff , RSE_ratio = RSE_ratio ) p = Pool ( ) result = p . map ( part_func , [ sim_indices [ : , ss ] for ss in range ( n_sim ) ] ) p . close ( ) p . join ( ) for ss , r in enumerate ( result ) : alphas_sim [ : , ss ] = r [ 0 ] alphas_se_sim [ : , ss ] = r [ 1 ] if verbose : print ( "Simulations complete in {:.3f} seconds!" . format ( time . time ( ) - start_time ) ) return alphas_sim , alphas_se_sim
Simulate fund returns.
Simulate fund returns .
'''Simulate fund returns. Stores the simulation results under the
    attributes with _sim suffix. This simulation process uses a slightly different
    multiprocessing technique. I'm not sure which is faster yet, so I've left it
    here.

    UPDATE DOC STRING
    ''' # start timer # Check input parameters for any modifications to behavior # check for change in max lag function # parameters # Populate target output vectors to be filled in with loop: # randomized indices for simulations # regressions # zip args for pool.starmap function # Begin pooling cpu's # assign results from Pool to arrays # DONE
n_sim
Y
X
betas
betas_se
random_seed
verbose
sim_std
need_orig_stats
sim_cutoff
maxLag