|
jinja_qe_example|⇒ python run_example.py |
|
Running Python example |
|
############################################################################## |
|
############################################################################## |
|
We already have this functionality in hand---in the file `discrete_rv.py <https://github.com/jstac/quant-econ/blob/master/quantecon/discrete_rv.py>`_ |
|
|
|
The module is part of the :ref:`quantecon <gs_qe>` package, and defines a class ``DiscreteRV`` that can be used as follows |
|
|
|
|
|
|
|
.. sourcecode:: ipython |
|
|
|
In [64]: from quantecon import DiscreteRV |
|
|
|
In [65]: psi = (0.1, 0.9) |
|
|
|
In [66]: d = DiscreteRV(psi) |
|
|
|
In [67]: d.draw(5) |
|
Out[67]: array([0, 1, 1, 1, 1]) |
|
|
|
|
|
|
|
Here |
|
|
|
* ``psi`` is understood to be a discrete distribution on the set of outcomes ``0, ..., len(psi) - 1`` |
|
|
|
* ``d.draw(5)`` generates 5 independent draws from this distribution |
|
|
|
|
|
Let's now write a function that generates time series from a specified pair :math:`P, \psi` |
|
|
|
Our function will take the following three arguments |
|
|
|
* A stochastic matrix ``P``, |
|
|
|
* An initial state or distribution ``init`` |
|
|
|
* A positive integer ``sample_size`` representing the length of the time series the function should return |
|
|
|
Let's allow ``init`` to either be |
|
|
|
* an integer in :math:`0,\ldots, n-1` providing a fixed starting value for :math:`X_0`, or |
|
|
|
* a discrete distribution on this same set that corresponds to the initial distribution :math:`\psi` |
|
|
|
In the latter case, a random starting value for :math:`X_0` is drawn from the distribution ``init`` |
|
|
|
The function should return a time series (sample path) of length ``sample_size`` |
|
|
|
One solution to this problem can be found in file `mc_tools.py <https://github.com/jstac/quant-econ/blob/master/quantecon/mc_tools.py>`_ from the :ref:`quantecon <gs_qe>` package |
|
|
|
We repeat the contents here |
|
|
|
.. literalinclude:: _static/programs/quantecon/mc_tools.py |
|
|
|
|
|
############################################################################## |
|
############################################################################## |
|
Running Julia example |
|
############################################################################## |
|
############################################################################## |
|
|
|
|
|
We already have this functionality in hand---in the file `discrete_rv.py <https://github.com/jstac/quant-econ/blob/master/quantecon/discrete_rv.py>`_ |
|
|
|
The module is part of the :ref:`quantecon <gs_qe>` package, and defines a class ``DiscreteRV`` that can be used as follows |
|
|
|
|
|
.. sourcecode:: julia |
|
|
|
julia> import QuantEcon: DiscreteRV, draw |
|
|
|
julia> psi = [0.1, 0.9] |
|
2-element Array{Float64,1}: |
|
0.1 |
|
0.9 |
|
|
|
julia> d = DiscreteRV(psi) |
|
DiscreteRV{Float64}([0.1,0.9],[0.1,1.0]) |
|
|
|
julia> draw(d, 5) |
|
5-element Array{Int64,1}: |
|
2 |
|
1 |
|
2 |
|
2 |
|
2 |
|
|
|
|
|
Here |
|
|
|
* ``psi`` is understood to be a discrete distribution on the set of outcomes ``0, ..., len(psi) - 1`` |
|
|
|
* ``d.draw(5)`` generates 5 independent draws from this distribution |
|
|
|
|
|
Let's now write a function that generates time series from a specified pair :math:`P, \psi` |
|
|
|
Our function will take the following three arguments |
|
|
|
* A stochastic matrix ``P``, |
|
|
|
* An initial state or distribution ``init`` |
|
|
|
* A positive integer ``sample_size`` representing the length of the time series the function should return |
|
|
|
Let's allow ``init`` to either be |
|
|
|
* an integer in :math:`0,\ldots, n-1` providing a fixed starting value for :math:`X_0`, or |
|
|
|
* a discrete distribution on this same set that corresponds to the initial distribution :math:`\psi` |
|
|
|
In the latter case, a random starting value for :math:`X_0` is drawn from the distribution ``init`` |
|
|
|
The function should return a time series (sample path) of length ``sample_size`` |
|
|
|
One solution to this problem can be found in file `mc_tools.jl <https://github.com/spencerlyon2/QuantEcon.jl/blob/master/src/mc_tools.jl>`_ from the :ref:`quantecon <gs_qe>` package |
|
|
|
We repeat the contents here |
|
|
|
.. literalinclude:: _static/programs_jl/src/mc_tools.jl |