boglehead/portfolio.py
def simulate(self, starting_value, n_days=260*10, n_mc=1000,
                 rebalance_frequency=260):
        """this is a stupid simulation that picks a random date to determine
        gains across the portfolio, and is effectively continuously rebalanced.

        run the simulation `n_mc` times over `n_days` days (market is typically
        open ~260 days per year) and rebalance every `rebalance_frequency`
        days.
        """

        # get the universe of dates that we can work with for this portfolio.
        # for now only use dates that have data across all funds
        possible_dates = set(self.funds[0].historical_date_reference.keys())
        for fund in self.funds[1:]:
            possible_dates.intersection_update(
                fund.historical_date_reference.keys()
            )
        possible_dates = list(possible_dates)
        possible_dates.sort()
        possible_dates = possible_dates[1:]  # HACK for Fund.gain

        simulated_final_values = []
        for mc in range(n_mc):
            self.initialize_fund_values(starting_value)

            # # simulate gains based on historical correlations, starting from a
            # # random date in the past
            # i = random.randint(0, len(possible_dates) - 1 - n_days)
            # dates = possible_dates[i:i+n_days]
            # assert len(dates) == n_days
            # for day, date in enumerate(dates):
            #     value = self.simulate_date(date)
            #     if rebalance_frequency and (day+1) % rebalance_frequency == 0:
            #         self.rebalance()

            # simulate gains drawn from historical results. This retains
            # correlations across funds but does not account for day-to-day
            # correlations.
            for day in range(n_days):
                date = random.choice(possible_dates)
                value = self.simulate_date(date)
                if rebalance_frequency and (day+1) % rebalance_frequency == 0:
                    self.rebalance()

            simulated_final_values.append(value)

        return simulated_final_values
this is a stupid simulation that picks a random date to determine
        gains across the portfolio, and is effectively continuously rebalanced.

        run the simulation `n_mc` times over `n_days` days (market is typically
        open ~260 days per year) and rebalance every `rebalance_frequency`
        days.
        
this is a stupid simulation that picks a random date to determine
gains across the portfolio, and is effectively continuously rebalanced.


this is a stupid simulation that picks a random date to determine gains across the portfolio and is effectively continuously rebalanced .
def simulate(self, starting_value, n_days=260*10, n_mc=1000,
                 rebalance_frequency=260):
        possible_dates = set(self.funds[0].historical_date_reference.keys())
        for fund in self.funds[1:]:
            possible_dates.intersection_update(
                fund.historical_date_reference.keys()
            )
        possible_dates = list(possible_dates)
        possible_dates.sort()
        possible_dates = possible_dates[1:]  
        simulated_final_values = []
        for mc in range(n_mc):
            self.initialize_fund_values(starting_value)
            for day in range(n_days):
                date = random.choice(possible_dates)
                value = self.simulate_date(date)
                if rebalance_frequency and (day+1) % rebalance_frequency == 0:
                    self.rebalance()
            simulated_final_values.append(value)
        return simulated_final_values
def simulate ( self , starting_value , n_days = 260 * 10 , n_mc = 1000 , rebalance_frequency = 260 ) : possible_dates = set ( self . funds [ 0 ] . historical_date_reference . keys ( ) ) for fund in self . funds [ 1 : ] : possible_dates . intersection_update ( fund . historical_date_reference . keys ( ) ) possible_dates = list ( possible_dates ) possible_dates . sort ( ) possible_dates = possible_dates [ 1 : ] simulated_final_values = [ ] for mc in range ( n_mc ) : self . initialize_fund_values ( starting_value ) for day in range ( n_days ) : date = random . choice ( possible_dates ) value = self . simulate_date ( date ) if rebalance_frequency and ( day + 1 ) % rebalance_frequency == 0 : self . rebalance ( ) simulated_final_values . append ( value ) return simulated_final_values
this is a stupid simulation that picks a random date to determine
gains across the portfolio, and is effectively continuously rebalanced.
this is a stupid simulation that picks a random date to determine gains across the portfolio and is effectively continuously rebalanced .
"""this is a stupid simulation that picks a random date to determine
        gains across the portfolio, and is effectively continuously rebalanced.

        run the simulation `n_mc` times over `n_days` days (market is typically
        open ~260 days per year) and rebalance every `rebalance_frequency`
        days.
        """ # get the universe of dates that we can work with for this portfolio. # for now only use dates that have data across all funds # HACK for Fund.gain # # simulate gains based on historical correlations, starting from a # # random date in the past # i = random.randint(0, len(possible_dates) - 1 - n_days) # dates = possible_dates[i:i+n_days] # assert len(dates) == n_days # for day, date in enumerate(dates): #     value = self.simulate_date(date) #     if rebalance_frequency and (day+1) % rebalance_frequency == 0: #         self.rebalance() # simulate gains drawn from historical results. This retains # correlations across funds but does not account for day-to-day # correlations.
self
starting_value
n_days
n_mc
rebalance_frequency