Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Adding Discrete Approximation of VAR Methods #640

Merged
merged 29 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4ae4ee5
Add discrete_var
Smit-create Nov 3, 2022
4709925
Add suggestions by @crondonm
Smit-create Nov 3, 2022
8295961
Add discrete_var in approximation
Smit-create Nov 3, 2022
2e7526d
Add tests
Smit-create Nov 3, 2022
84e751d
add suggestions 1, 2, 5 by @jstac
shlff Nov 5, 2022
ba09926
Add docs of Pi and S in output.
Smit-create Nov 6, 2022
cc5217c
Remove row_major argument and use cartesian from gridtools
Smit-create Nov 6, 2022
c26d265
Modify tests to drop row_major
Smit-create Nov 6, 2022
00f457e
Rename Pi to P
Smit-create Nov 6, 2022
740cf73
use mlinspace
shlff Nov 6, 2022
6c50e1e
Merge branch 'VAR-approximation' of https://github.com/crondonm/Quant…
oyamad Dec 17, 2022
745c0d4
Implement discrete_var using quantecon functions
oyamad Dec 17, 2022
6e3315f
Merge branch 'main' into VAR-approximation-2
oyamad Jan 6, 2023
f4ebd6f
Use `fit_discrete_mc`
oyamad Jan 6, 2023
7f7eec4
Remove unused import
oyamad Jan 8, 2023
9515951
Remove unused import
oyamad Jan 9, 2023
bb91864
adjust test and docstring
HumphreyYang Jan 12, 2023
03e1a8d
Edit docstring
oyamad Jan 22, 2023
ac83c52
Import _lss and _matrix_eqn
oyamad Jan 25, 2023
f8e9551
add new tests and bug fixing
HumphreyYang Feb 15, 2023
4317827
Merge branch 'VAR-approximation-2' into VAR-approximation
HumphreyYang Feb 15, 2023
b7f4497
add tests for non-square C
HumphreyYang Feb 16, 2023
15c4f78
adjust docstring
HumphreyYang Feb 16, 2023
496942d
Minor edits in docstring
oyamad Feb 16, 2023
4392ef9
adjust based on review
HumphreyYang Feb 16, 2023
0a8a7f6
update docstring
HumphreyYang Feb 16, 2023
a85a6cd
set unit Cov for t-distribution
HumphreyYang Feb 17, 2023
d4b5afd
Merge branch 'VAR-approximation' of https://github.com/crondonm/Quant…
oyamad Feb 17, 2023
08dd321
CI: Workaround for windows (#694)
oyamad Feb 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions quantecon/markov/approximation.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ def discrete_var(A,
An instance of `scipy.stats` for a distribution that :math:`{u_t}`
is drawn. It must have a zero mean and unit standard deviation.
If None, then standard normal distribution is used.
order : str, optional(default='C')
('C' or 'F') order in which the cartesian product is enumerated
random_state: a `np.random.RandomState` or `np.random.Generator` instance, or None, optional(default=None)
If None, the `np.random.RandomState` singleton is returned.

Expand Down Expand Up @@ -332,7 +334,7 @@ def discrete_var(A,
if rv is None:
u = random_state.standard_normal(size=(sim_length-1, r))
else:
u = rv.rvs(size=sim_length-1, random_state=random_state)
u = rv.rvs(size=(sim_length-1, r), random_state=random_state)
HumphreyYang marked this conversation as resolved.
Show resolved Hide resolved

v = C @ u.T
x0 = np.zeros(m)
Expand All @@ -352,8 +354,7 @@ def discrete_var(A,

V = [np.linspace(-upper_bounds[i], upper_bounds[i], grid_sizes[i])
for i in range(m)]

# Fit the Markov chain
mc = fit_discrete_mc(X.T, V, order=order)

print(mc)
return mc
69 changes: 56 additions & 13 deletions quantecon/markov/tests/test_approximation.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,41 @@ def setup_method(self):
[ 0.38556417, 0. ],
[ 0.38556417, 0.05387746]]

self.P_out = [[1.61290323e-02, 1.12903226e-01, 0.00000000e+00,
3.70967742e-01, 5.00000000e-01, 0.00000000e+00],
[1.00964548e-04, 8.51048124e-01, 3.82857566e-02, 4.03858192e-04,
1.10111936e-01, 4.93604457e-05],
[0.00000000e+00, 3.02295449e-01, 6.97266822e-01, 0.00000000e+00,
3.85201268e-04, 5.25274456e-05],
[3.60600761e-05, 4.86811027e-04, 0.00000000e+00, 6.97473992e-01,
3.02003137e-01, 0.00000000e+00],
[3.17039037e-05, 1.11090478e-01, 4.55177474e-04, 3.75374219e-02,
8.50778784e-01, 1.06434534e-04],
[0.00000000e+00, 4.45945946e-01, 3.37837838e-01, 0.00000000e+00,
1.89189189e-01, 2.70270270e-02]]
self.S_out_orderF =[
[-0.38556417, -0.05387746],
[ 0.38556417, -0.05387746],
[-0.38556417, 0. ],
[ 0.38556417, 0. ],
[-0.38556417, 0.05387746],
[ 0.38556417, 0.05387746]]

self.P_out = [
[1.61290323e-02, 1.12903226e-01, 0.00000000e+00, 3.70967742e-01,
5.00000000e-01, 0.00000000e+00],
[1.00964548e-04, 8.51048124e-01, 3.82857566e-02, 4.03858192e-04,
1.10111936e-01, 4.93604457e-05],
[0.00000000e+00, 3.02295449e-01, 6.97266822e-01, 0.00000000e+00,
3.85201268e-04, 5.25274456e-05],
[3.60600761e-05, 4.86811027e-04, 0.00000000e+00, 6.97473992e-01,
3.02003137e-01, 0.00000000e+00],
[3.17039037e-05, 1.11090478e-01, 4.55177474e-04, 3.75374219e-02,
8.50778784e-01, 1.06434534e-04],
[0.00000000e+00, 4.45945946e-01, 3.37837838e-01, 0.00000000e+00,
1.89189189e-01, 2.70270270e-02]]

self.P_out_orderF =[
[1.61290323e-02, 3.70967742e-01, 1.12903226e-01, 5.00000000e-01,
0.00000000e+00, 0.00000000e+00],
[3.60600761e-05, 6.97473992e-01, 4.86811027e-04, 3.02003137e-01,
0.00000000e+00, 0.00000000e+00],
[1.00964548e-04, 4.03858192e-04, 8.51048124e-01, 1.10111936e-01,
3.82857566e-02, 4.93604457e-05],
[3.17039037e-05, 3.75374219e-02, 1.11090478e-01, 8.50778784e-01,
4.55177474e-04, 1.06434534e-04],
[0.00000000e+00, 0.00000000e+00, 3.02295449e-01, 3.85201268e-04,
6.97266822e-01, 5.25274456e-05],
[0.00000000e+00, 0.00000000e+00, 4.45945946e-01, 1.89189189e-01,
3.37837838e-01, 2.70270270e-02]]

self.A, self.C, self.S_out, self.P_out = map(np.array,
(self.A, self.C, self.S_out, self.P_out))
Expand All @@ -152,4 +175,24 @@ def test_discretization(self):
self.A, self.C, grid_sizes=self.sizes,
sim_length=self.T, random_state=self.random_state)
assert_allclose(mc.state_values, self.S_out)
assert_allclose(mc.P, self.P_out)
assert_allclose(mc.P, self.P_out)

def test_sp_distributions(self):
mc = discrete_var(
self.A, self.C,
grid_sizes=self.sizes,
sim_length=self.T,
rv=sp.stats.multivariate_normal,
random_state=self.random_state)
assert_allclose(mc.state_values, self.S_out)
assert_allclose(mc.P, self.P_out)

def test_order_F(self):
mc = discrete_var(
self.A, self.C,
grid_sizes=self.sizes,
sim_length=self.T,
order='F',
random_state=self.random_state)
assert_allclose(mc.state_values, self.S_out_orderF)
assert_allclose(mc.P, self.P_out_orderF)