def add_MC_uncertainty(self, Markov_states, transition_matrix): """Add a Markov chain process. Parameters ---------- Markov_states: list of matrix-like Markov state spaces in each stage. The shape of matrix-like must be (p,q) where q is the dimension index of the Markov chain and p is the index of the Markov states transition_matrix: list of matrix-like Markov chain transition matrices in each stage. The shape of must be compatible with the Markov states start: start period (inclusive) of the Markov chain process end: end period (exclusive) of the Markov chain process The dimension of all entries in Markov states and transition matrices must be in the form of: Markov_states: [1], [p_{1}], ... , [p_{T-1}] transition_matrix: [[1]], [1,p_{1}], [p_{1},p_{2}], [p_{T-2},p_{T-1}] where p_1,...p_{T-1} are integers. Examples -------- >>> add_MC_uncertainty( ... Markov_states=[[[0]],[[4],[6]],[[4],[6]]], ... transition_matrix=[ ... [[1]], ... [[0.5,0.5]], ... [[0.3,0.7],[0.7,0.3]] ... ] ... ) Three dimensional Markov chain >>> add_MC_uncertainty( ... Markov_states=[[[0]],[[4,6,5],[6,3,4]],[[4,6,5],[6,3,4]]], ... transition_matrix=[ ... [[1]], ... [[0.5,0.5]], ... [[0.3,0.7],[0.7,0.3]] ... ] ... ) """ if hasattr(self, "Markovian_uncertainty") or hasattr( self, "Markov_states"): raise ValueError("Markovian uncertainty has already added!") info = check_Markov_states_and_transition_matrix( Markov_states, transition_matrix, self.T) self.dim_Markov_states, self.n_Markov_states = info self.Markov_states = Markov_states self.transition_matrix = [ numpy.array(item) for item in transition_matrix ] self._type = 'Markov chain'
def add_MC_uncertainty( self, Markov_states, transition_matrix ): """Add a Markov chain process. Parameters ---------- Markov_states: list of array-like Markov state spaces in each stage. transition_matrix: list of matrix-like Markov chain transition matrices in each stage. start: start period (inclusive) of the Markov chain process end: end period (exclusive) of the Markov chain process The dimension of all entries in Markov states and transition matrices must be in the form of: Markov_states: [1], [p_{1}], ... , [p_{T-1}] transition_matrix: [[1]], [1,p_{1}], [p_{1},p_{2}], [p_{T-2},p_{T-1}] where p_1,...p_{T-1} are integers. Examples -------- Suppose there are three stages. add_MC_uncertainty( Markov_states=[ [0.2], [0.3,0.5], [0.4,0.6] ], transition_matrix=[ [[1]], [[0.2,0.8]], [[0.6,0.4],[0.3,0.7]] ] ) """ if hasattr(self, "Markovian_uncertainty") or hasattr(self,"Markov_states"): raise ValueError("Markovian uncertainty has already added!") info = check_Markov_states_and_transition_matrix( Markov_states, transition_matrix, self.T ) self.dim_Markov_states,self.n_Markov_states = info self.Markov_states = Markov_states self.transition_matrix = transition_matrix self._type = 'Markov chain'
def discretize(self, n_samples=None, random_state=None, replace=True, n_Markov_states=None, method='SA', n_sample_paths=None, Markov_states=None, transition_matrix=None, int_flag=0): """Discretize Markovian continuous uncertainty by k-means or (robust) stochasitic approximation. Parameters ---------- n_samples: int, optional, default=None number of i.i.d. samples to generate for stage-wise independent randomness. random_state: None | int | instance of RandomState, optional, default=None If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by numpy.random. replace: bool, optional, default=True Indicates generating i.i.d. samples with/without replacement for stage-wise independent randomness. n_Markov_states: list | int, optional, default=None If list, it specifies different dimensions of Markov state space over time. Length of the list should equal length of the Markovian uncertainty. If int, it specifies dimensions of Markov state space. Note: If the uncertainties are int, trained Markov states will be rounded to integers, and duplicates will be removed. In such cases, there is no guaranttee that the number of Markov states is n_Markov_states. method: binary, optional, default=0 'input': the approximating Markov chain is given by user input ( through specifying Markov_states and transition_matrix) 'SAA': use k-means to train Markov chain. 'SA': use stochastic approximation to train Markov chain. 'RSA': use robust stochastic approximation to train Markov chain. n_sample_paths: int, optional, default=None number of sample paths to train the Markov chain. Markov_states/transition_matrix: matrix-like, optional, default=None The user input of approximating Markov chain. """ if n_samples is not None: if isinstance(n_samples, (numbers.Integral, numpy.integer)): if n_samples < 1: raise ValueError("n_samples should be bigger than zero!") n_samples = ([1] + [n_samples] * (self.T - 1)) elif isinstance(n_samples, (abc.Sequence, numpy.ndarray)): if len(n_samples) != self.T: raise ValueError( "n_samples list should be of length {} rather than {}!" .format(self.T, len(n_samples))) if n_samples[0] != 1: raise ValueError( "The first stage model should be deterministic!") else: raise ValueError("Invalid input of n_samples!") # discretize stage-wise independent continuous distribution random_state = check_random_state(random_state) for t in range(1, self.T): self.models[t]._discretize(n_samples[t], random_state, replace) if n_Markov_states is None and method != 'input': return if method == 'input' and (Markov_states is None or transition_matrix is None): return if n_Markov_states is not None: if isinstance(n_Markov_states, (numbers.Integral, numpy.integer)): if n_Markov_states < 1: raise ValueError( "n_Markov_states should be bigger than zero!") n_Markov_states = ([1] + [n_Markov_states] * (self.T - 1)) elif isinstance(n_Markov_states, (abc.Sequence, numpy.ndarray)): if len(n_Markov_states) != self.T: raise ValueError( "n_Markov_states list should be of length {} rather than {}!" .format(self.T, len(n_Markov_states))) if n_Markov_states[0] != 1: raise ValueError( "The first stage model should be deterministic!") else: raise ValueError("Invalid input of n_Markov_states!") from msppy.discretize import Markovian if method in ['RSA', 'SA', 'SAA']: markovian = Markovian( f=self.Markovian_uncertainty, n_Markov_states=n_Markov_states, n_sample_paths=n_sample_paths, int_flag=int_flag, ) if method in ['RSA', 'SA', 'SAA']: self.Markov_states, self.transition_matrix = getattr( markovian, method)() elif method == 'input': dim_Markov_states, n_Markov_states = ( check_Markov_states_and_transition_matrix( Markov_states=Markov_states, transition_matrix=transition_matrix, T=self.T, )) if dim_Markov_states != self.dim_Markov_states: raise ValueError( "The dimension of the given sample path " + "generator is not the same as the given Markov chain " + "approximation!") self.Markov_states = Markov_states self.transition_matrix = [ numpy.array(item) for item in transition_matrix ] self._flag_discrete = 1 self.n_Markov_states = n_Markov_states if method in ['RSA', 'SA', 'SAA']: return markovian