Skip to content

Commit

Permalink
CLN: fix naming on ddp conversion routines
Browse files Browse the repository at this point in the history
  • Loading branch information
sglyon committed Aug 1, 2017
1 parent 9938ff0 commit 79a829c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
22 changes: 11 additions & 11 deletions quantecon/markov/ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,43 +456,43 @@ def _check_action_feasibility(self):
'violated for state {s}'.format(s=s)
)

def to_sa_formulation(self, sparse=True):
def to_sa_pair_form(self, sparse=True):
"""
Convert this instance of `DiscreteDP` to SA-pair form
Parameters
----------
sparse : bool, optional(default=True)
Should the `Q` matrix be stored as a sparse matrix.
Should the `Q` matrix be stored as a sparse matrix?
If true the CSR format is used
Returns
-------
ddp_sa : DiscreteDP
The correspnoding DiscreteDP instance in SA form
The correspnoding DiscreteDP instance in SA-pair form
Notes
-----
If this instance is already in SA form then it is returned
If this instance is already in SA-pair form then it is returned
un-modified
"""

if self._sa_pair:
return self
else:
s_ind, a_ind = np.where(np.isfinite(self.R))
s_ind, a_ind = np.where(self.R > - np.inf)
RL = self.R[s_ind, a_ind]
if sparse:
QL = sp.csr_matrix(self.Q[s_ind, a_ind])
else:
QL = self.Q[s_ind, a_ind]
return DiscreteDP(RL, QL, self.beta, s_ind, a_ind)

def to_full_formulation(self):
def to_product_form(self):
"""
Convert this instance of `DiscreteDP` to the "full" form.
Convert this instance of `DiscreteDP` to the "product" form.
The full form uses the version of the init method taking
The product form uses the version of the init method taking
`R`, `Q` and `beta`.
Parameters
Expand All @@ -501,18 +501,18 @@ def to_full_formulation(self):
Returns
-------
ddp_sa : DiscreteDP
The correspnoding DiscreteDP instance in full form
The correspnoding DiscreteDP instance in product form
Notes
-----
If this instance is already in full form then it is returned
If this instance is already in product form then it is returned
un-modified
"""
if self._sa_pair:
ns = self.num_states
na = self.a_indices.max() + 1
R = np.zeros((ns, na))
R = np.full((ns, na), -np.inf)
R[self.s_indices, self.a_indices] = self.R
Q = np.zeros((ns, na, ns))
if self._sparse:
Expand Down
20 changes: 10 additions & 10 deletions quantecon/markov/tests/test_ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_ddp_beta_0():
v_init = [0, 0, 0]

ddp0 = DiscreteDP(R, Q, beta)
ddp1 = ddp0.to_sa_formulation()
ddp1 = ddp0.to_sa_pair_form()
methods = ['vi', 'pi', 'mpi']

for ddp in [ddp0, ddp1]:
Expand Down Expand Up @@ -252,8 +252,8 @@ def test_ddp_beta_1_not_implemented_error():
beta = 1

ddp0 = DiscreteDP(R, Q, beta)
ddp1 = ddp0.to_sa_formulation()
ddp2 = ddp0.to_sa_formulation(sparse=False)
ddp1 = ddp0.to_sa_pair_form()
ddp2 = ddp0.to_sa_pair_form(sparse=False)

solution_methods = \
['value_iteration', 'policy_iteration', 'modified_policy_iteration']
Expand All @@ -264,7 +264,7 @@ def test_ddp_beta_1_not_implemented_error():
assert_raises(NotImplementedError, getattr(ddp, method))


def test_ddp_to_sa_and_to_full():
def test_ddp_to_sa_and_to_product():
n, m = 3, 2
R = np.array([[0, 1], [1, 0], [0, 1]])
Q = np.empty((n, m, n))
Expand All @@ -275,12 +275,12 @@ def test_ddp_to_sa_and_to_full():
sparse_Q = sparse.coo_matrix(np.full((6, 3), 1/3))

ddp = DiscreteDP(R, Q, beta)
ddp_sa = ddp.to_sa_formulation()
ddp_sa2 = ddp_sa.to_sa_formulation()
ddp_sa3 = ddp.to_sa_formulation(sparse=False)
ddp2 = ddp_sa.to_full_formulation()
ddp3 = ddp_sa2.to_full_formulation()
ddp4 = ddp.to_full_formulation()
ddp_sa = ddp.to_sa_pair_form()
ddp_sa2 = ddp_sa.to_sa_pair_form()
ddp_sa3 = ddp.to_sa_pair_form(sparse=False)
ddp2 = ddp_sa.to_product_form()
ddp3 = ddp_sa2.to_product_form()
ddp4 = ddp.to_product_form()

# make sure conversion worked
for ddp_s in [ddp_sa, ddp_sa2, ddp_sa3]:
Expand Down

0 comments on commit 79a829c

Please sign in to comment.