Skip to content

ENH: Add state_values to DiscreteDP - #832

Merged
mmcky merged 7 commits into
QuantEcon:mainfrom
HG-Cheng:feature-ddp-values
Aug 12, 2026
Merged

mmcky merged 7 commits into
QuantEcon:mainfrom
HG-Cheng:feature-ddp-values

Conversation

@HG-Cheng

@HG-Cheng HG-Cheng commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

This PR partially addresses #248 by adding state_values support to
DiscreteDP.

Changes:

  • Add an optional state_values argument to DiscreteDP.
  • Validate state_values consistently with MarkovChain.
  • Preserve state_values when converting between product and
    state-action-pair formulations.
  • Pass state_values to the MarkovChain returned by controlled_mc.
  • Add tests for storage, validation, propagation, and representation
    conversion.

action_values and num_actions are intentionally left out of this PR,
following the review discussion.

@coveralls

coveralls commented May 30, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 90.57% (+0.02%) from 90.546% — HG-Cheng:feature-ddp-values into QuantEcon:main

@oyamad

oyamad commented May 30, 2026

Copy link
Copy Markdown
Member

@HG-Cheng Thank you for the contribution!

Main comments

  1. I propose that we only add state_values and remove action_values (and num_actions) in this PR.

    Please see Adding State-Action Pair Support QuantEcon.jl#94 (comment):
    We are implicit which of the two approaches in that comment we are following (although I believe approach 1 is the legitimate approach). We need a discussion to decide on which approach to pick (or even whether to pick one explicitly). I propose not to discuss it here, focusing only on adding state_values in this PR.

  2. Please pass state_values=state_values to MarkovChain in controlled_mc: https://github.com/HG-Cheng/QuantEcon.py/blob/fff5405ae7d4f66df60bb2ae39a943504be19c59/quantecon/markov/ddp.py#L1004
    This is where adding state_values is most useful.

Other comments

Please do not include changes in the files .vscode/settings.json and _matrix_eqn.py in this PR.

@HG-Cheng

Copy link
Copy Markdown
Contributor Author

"Hi @oyamad, thanks for the detailed review and guidance!

I completely understand. I will remove action_values and num_actions to keep this PR focused on state_values only, and we can leave the state-action pair discussion for the future.

I will pass state_values=state_values to MarkovChain in controlled_mc as suggested.

Apologies for the accidental inclusion of .vscode/settings.json and _matrix_eqn.py! They leaked in from my local workspace and another task. I will remove them from this branch right away.

I'll push the updated commits shortly!"

@HG-Cheng HG-Cheng changed the title ENH: Add state_values and action_values to DiscreteDP (Fixes #248) ENH: Add state_values to DiscreteDP Jul 14, 2026
@HG-Cheng

Copy link
Copy Markdown
Contributor Author

Thanks again for the review. I've updated the PR to keep it focused on
state_values only, removed the unrelated changes, and propagated
state_values to the controlled MarkovChain.

I also synced the branch with the latest main. The full local test suite passes locally (605 passed).

@oyamad oyamad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @HG-Cheng! One main request: please follow what is done in MarkovChain:

  • Implement state_values as a @property with a validating setter (storing to self._state_values), and have __init__ delegate to it by self.state_values = state_values, as in MarkovChain.__init__.
  • You might also use MarkovChain's error messages and setter docstring verbatim (e.g., 'state_values must be an array_like of length n').

@HG-Cheng

HG-Cheng commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @oyamad!

I've updated DiscreteDP.state_values to follow the existing MarkovChain implementation:

  • added a state_values property with a validating setter;
  • stored the value in self._state_values;
  • changed __init__ to delegate through self.state_values = state_values;
  • aligned the validation messages and setter docstring with MarkovChain;
  • added tests for reassignment, unsetting, and invalid assignment after construction.

The focused tests and the full local test suite pass (605 passed).

Could you please take another look when convenient? Thanks!

@oyamad oyamad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HG-Cheng Thanks!

@mmcky This is ready to merge.

@oyamad oyamad added the ready label Aug 7, 2026
@mmcky

mmcky commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

thanks so much @HG-Cheng and @oyamad

@mmcky
mmcky merged commit bbedcda into QuantEcon:main Aug 12, 2026
13 checks passed
mmcky pushed a commit that referenced this pull request Aug 16, 2026
* DOC: Correct the state_values docstring in MarkovChain

The attribute does not default to integers 0 through n-1; it is None
when unset, in which case the states are represented by their indices
(as the Attributes entry and the simulate docstring already state).

Co-Authored-By: Claude Fable 5 <[email protected]>

* ENH: Refine DiscreteDP state_values; record in DPSolveResult

Refine the state_values support added in #832:

* Correct the docstring: the attribute is None when unset (in which
  case the states are represented by their indices), and the values
  may be 2-dimensional (row per state).
* Record state_values in DPSolveResult.
* Reorganize the tests into a class covering both formulations,
  2-dimensional values, the solution pipeline, and carry-over by
  to_sa_pair_form/to_product_form.

Cf. #248

Co-Authored-By: Claude Fable 5 <[email protected]>

* ENH: Add action_values to DiscreteDP

Add an action_values keyword argument to DiscreteDP, symmetric to
state_values. The values play no role in the solution algorithms.
DPSolveResult records action_values, and gains a sigma_values property
returning the optimal policy decoded to action values, i.e.,
action_values[sigma] (sigma itself if action_values is None).

The semantics follow the "universal action set" approach (approach 2
of QuantEcon/QuantEcon.jl#94 (comment); cf.
QuantEcon/QuantEcon.jl#402): action_values labels a common action set
A with A(s) a subset of A for each state s. In the state-action pairs
formulation, its length defines the number of actions and may exceed
a_indices.max() + 1, so a universal action set strictly containing the
union of the feasible sets is representable in both formulations.
to_sa_pair_form/to_product_form carry action_values unchanged, with
to_product_form restoring the full number of columns, so actions
feasible at no state survive the round trip.

Cf. #248

Co-Authored-By: Claude Fable 5 <[email protected]>

* ENH: Add util.index_dict

Add `index_dict`, which builds a Numba typed dict mapping each value
in an array of unique values (scalars or rows) to its index, usable
both from the interpreter and inside jitted functions. This is the
value-to-index translation layer for building DiscreteDP instances in
value space (with `state_values`/`action_values` attached), and for
the interface proposed in #228.

Notable: Numba typed dicts hash floats by bit pattern, so -0.0 and
0.0 are distinct keys (unlike Python dicts); -0.0 is therefore
rejected at build time.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[email protected]>
@mmcky mmcky mentioned this pull request Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants