src/luck_v_skill.py
def simulate(self,n_simulations,random_seed=None,verbose=False,sim_std=0,
        sim_cutoff=15,*args,**kwargs):
        '''Simulate fund returns. Stores the simulation results under the
        attributes with _sim suffix.

        UPDATE DOC STRING
        '''
        start_time = time.time() # start timer

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

        # check that model is already fit, if not, fit it.
        if not self._is_fit:
            if verbose:
                print("Need to fit data first!")
            if "fitkwgs" in kwargs.keys():
                self.fit(**kwargs['fitkwgs'])
            else:
                self.fit()

        # check for change in max lag function
        if 'olskwgs' in kwargs.keys():
            if 'maxLag' in kwargs['olskwgs'].keys():
                self.maxLag = kwargs['olskwgs']['maxLag']
        # parameters
        n_obs = self.Y.shape[0]
        n_factors = self.X.shape[1]-1
        n_funds = self.Y.shape[1]
        self.sim_std = sim_std
        std_alpha = sim_std/np.sqrt(12)
        orig_betas = self._coeff.values[1:,:] # .sort_values(by='Alpha', axis=1, ascending=False)
        self.n_simulations = n_simulations

        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:
        self._tstats_sim = np.zeros((n_funds,n_simulations))
        self._tstats_sim.fill(np.nan)
        self._alphas_sim = np.zeros((n_funds,n_simulations))
        self._alphas_sim.fill(np.nan)

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

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

        # Loop through each simulation run:
        for ss in range(n_simulations):
            # create X_sim and Y_sim
            Y_sim,X_sim = self._sim_YX(ridx = sim_indices[:,ss],
                                       betas= orig_betas,
                                       std_alpha = std_alpha)

            # make mask and counts for observed returns
            not_nan = ~np.isnan(Y_sim)
            n_obs_sim = not_nan.sum(axis=0)
            #Loop through each fund:
            for jj in range(n_funds):
                if n_obs_sim[jj]>= sim_cutoff:
                    # identify simulated returns and factor series
                    y_sim, x_sim = Y_sim[not_nan[:,jj],jj], X_sim[not_nan[:,jj],:]

                    # HAC OLS and store alpha and t(alpha)
                    beta,hac_bse = OLS_HAC(y_sim,x_sim,maxLag=self.maxLag)
                    self._alphas_sim[jj,ss] = beta[0]
                    self._tstats_sim[jj,ss] = beta[0]/hac_bse[0]

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

        self._has_sim = True

        return self
Simulate fund returns. Stores the simulation results under the
        attributes with _sim suffix.

        UPDATE DOC STRING
        
Simulate fund returns. Stores the simulation results under the
attributes with _sim suffix.

UPDATE DOC STRING
Simulate fund returns . Stores the simulation results under the attributes with _sim suffix . UPDATE DOC STRING
def simulate(self,n_simulations,random_seed=None,verbose=False,sim_std=0,
        sim_cutoff=15,*args,**kwargs):
        start_time = time.time() 
        self._has_percentiles = None
        if random_seed is not None:
            np.random.seed(seed=random_seed)
        if not self._is_fit:
            if verbose:
                print("Need to fit data first!")
            if "fitkwgs" in kwargs.keys():
                self.fit(**kwargs['fitkwgs'])
            else:
                self.fit()
        if 'olskwgs' in kwargs.keys():
            if 'maxLag' in kwargs['olskwgs'].keys():
                self.maxLag = kwargs['olskwgs']['maxLag']
        n_obs = self.Y.shape[0]
        n_factors = self.X.shape[1]-1
        n_funds = self.Y.shape[1]
        self.sim_std = sim_std
        std_alpha = sim_std/np.sqrt(12)
        orig_betas = self._coeff.values[1:,:] 
        self.n_simulations = n_simulations
        if verbose:
            print("Annual standard deviation: {:.2f}, Monthly standard deviation alpha: {:.2f}"\
                  .format(sim_std, std_alpha))
        self._tstats_sim = np.zeros((n_funds,n_simulations))
        self._tstats_sim.fill(np.nan)
        self._alphas_sim = np.zeros((n_funds,n_simulations))
        self._alphas_sim.fill(np.nan)
        sim_indices = np.random.randint(0, n_obs, size=(n_obs,n_simulations))
        if verbose:
            print("Starting {:,} regressions...".format(n_simulations*n_funds))
        for ss in range(n_simulations):
            Y_sim,X_sim = self._sim_YX(ridx = sim_indices[:,ss],
                                       betas= orig_betas,
                                       std_alpha = std_alpha)
            not_nan = ~np.isnan(Y_sim)
            n_obs_sim = not_nan.sum(axis=0)
            for jj in range(n_funds):
                if n_obs_sim[jj]>= sim_cutoff:
                    y_sim, x_sim = Y_sim[not_nan[:,jj],jj], X_sim[not_nan[:,jj],:]
                    beta,hac_bse = OLS_HAC(y_sim,x_sim,maxLag=self.maxLag)
                    self._alphas_sim[jj,ss] = beta[0]
                    self._tstats_sim[jj,ss] = beta[0]/hac_bse[0]
        if verbose:
            print("Simulations complete in {:.2f} seconds!".format(time.time()-start_time))
        self._has_sim = True
        return self
def simulate ( self , n_simulations , random_seed = None , verbose = False , sim_std = 0 , sim_cutoff = 15 , * args , ** kwargs ) : start_time = time . time ( ) self . _has_percentiles = None if random_seed is not None : np . random . seed ( seed = random_seed ) if not self . _is_fit : if verbose : print ( "Need to fit data first!" ) if "fitkwgs" in kwargs . keys ( ) : self . fit ( ** kwargs [ 'fitkwgs' ] ) else : self . fit ( ) if 'olskwgs' in kwargs . keys ( ) : if 'maxLag' in kwargs [ 'olskwgs' ] . keys ( ) : self . maxLag = kwargs [ 'olskwgs' ] [ 'maxLag' ] n_obs = self . Y . shape [ 0 ] n_factors = self . X . shape [ 1 ] - 1 n_funds = self . Y . shape [ 1 ] self . sim_std = sim_std std_alpha = sim_std / np . sqrt ( 12 ) orig_betas = self . _coeff . values [ 1 : , : ] self . n_simulations = n_simulations if verbose : print ( "Annual standard deviation: {:.2f}, Monthly standard deviation alpha: {:.2f}" . format ( sim_std , std_alpha ) ) self . _tstats_sim = np . zeros ( ( n_funds , n_simulations ) ) self . _tstats_sim . fill ( np . nan ) self . _alphas_sim = np . zeros ( ( n_funds , n_simulations ) ) self . _alphas_sim . fill ( np . nan ) sim_indices = np . random . randint ( 0 , n_obs , size = ( n_obs , n_simulations ) ) if verbose : print ( "Starting {:,} regressions..." . format ( n_simulations * n_funds ) ) for ss in range ( n_simulations ) : Y_sim , X_sim = self . _sim_YX ( ridx = sim_indices [ : , ss ] , betas = orig_betas , std_alpha = std_alpha ) not_nan = ~ np . isnan ( Y_sim ) n_obs_sim = not_nan . sum ( axis = 0 ) for jj in range ( n_funds ) : if n_obs_sim [ jj ] >= sim_cutoff : y_sim , x_sim = Y_sim [ not_nan [ : , jj ] , jj ] , X_sim [ not_nan [ : , jj ] , : ] beta , hac_bse = OLS_HAC ( y_sim , x_sim , maxLag = self . maxLag ) self . _alphas_sim [ jj , ss ] = beta [ 0 ] self . _tstats_sim [ jj , ss ] = beta [ 0 ] / hac_bse [ 0 ] if verbose : print ( "Simulations complete in {:.2f} seconds!" . format ( time . time ( ) - start_time ) ) self . _has_sim = True return self
Simulate fund returns.
Simulate fund returns .
'''Simulate fund returns. Stores the simulation results under the
        attributes with _sim suffix.

        UPDATE DOC STRING
        ''' # start timer # Check input parameters for any modifications to behavior # check that model is already fit, if not, fit it. # check for change in max lag function # parameters # .sort_values(by='Alpha', axis=1, ascending=False) # Populate target output vectors to be filled in with loop: # randomized indices for simulations # regressions # Loop through each simulation run: # create X_sim and Y_sim # make mask and counts for observed returns #Loop through each fund: # identify simulated returns and factor series # HAC OLS and store alpha and t(alpha) # DONE
self
n_simulations
random_seed
verbose
sim_std
sim_cutoff