Skip to content

Instantly share code, notes, and snippets.

@sglyon
Created August 5, 2014 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sglyon/f48f6260fbdb53b96e96 to your computer and use it in GitHub Desktop.
Save sglyon/f48f6260fbdb53b96e96 to your computer and use it in GitHub Desktop.
jinja2 templates for website

The template is called example.tmpl and it contains the lecture source as well as defines various blocks.

Each of the blocks has a default argument that is simply the content that is found in the current python only version of the site.

The cool thing is how we can use this system to easily generate lecture files for both languages, by only having to define the blocks for julia. This happens in example_jl.tmpl.

Notice I don't do anything beyond filling in the blocks here. i.e. there is no actual exposition here -- just replacing the default block parameters with their julia equivalent.

The python script run_example.py shows how we could load the template and evaluate it. If you have python run the file it will print out the python and julia versions of the file for you.

The file output shows what happens when we execute the python script.

We already have this functionality in hand---in the file {% block discretervfile %} `discrete_rv.py <https://github.com/jstac/quant-econ/blob/master/quantecon/discrete_rv.py>`_{% endblock %}
The module is part of the :ref:`quantecon <gs_qe>` package, and defines a class ``DiscreteRV`` that can be used as follows
{% block discrete_rv_example %}
.. 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])
{% endblock %}
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 {% block mc_tools_href %}`mc_tools.py <https://github.com/jstac/quant-econ/blob/master/quantecon/mc_tools.py>`_{% endblock %} from the :ref:`quantecon <gs_qe>` package
We repeat the contents here
.. literalinclude:: {% block mc_tools_path %}_static/programs/quantecon/mc_tools.py{% endblock %}
{% extends "finite_markov.tmpl" %}
{% block discrete_rv_href %}`discrete_rv.jl <https://github.com/spencerlyon2/QuantEcon.jl/blob/master/src/discrete_rv.jl>`_{% endblock %}
{% block discrete_rv_example %}
.. 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
{% endblock %}
{% block mc_tools_path %}_static/programs_jl/src/mc_tools.jl{% endblock %}
{% block mc_tools_href %}`mc_tools.jl <https://github.com/spencerlyon2/QuantEcon.jl/blob/master/src/mc_tools.jl>`_{% endblock %}
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
from __future__ import print_function
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader("./"))
spacers = "#" * 78 + "\n" + "#" * 78
# print python version
print("Running Python example")
print(spacers)
t = env.get_template("finite_markov.tmpl")
print(t.render())
# print julia version
print("\n\n" + spacers)
print("Running Julia example")
print(spacers + "\n\n")
t = env.get_template("finite_markov_jl.tmpl")
print(t.render())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment