Skip to content

Commit

Permalink
Added a bunch of .md files for the filesystem. .md files are scraped …
Browse files Browse the repository at this point in the history
…from the admb-examples webpage.
  • Loading branch information
gregorylburgess committed Feb 23, 2016
1 parent 66d7db2 commit e9f9d45
Show file tree
Hide file tree
Showing 116 changed files with 5,249 additions and 216 deletions.
19 changes: 19 additions & 0 deletions admb-tricks/adjoint-code-1/adjoint-code-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1 class="documentFirstHeading" id="parent-fieldname-title">
Adjoint code
</h1><div class="" id="parent-fieldname-text-7112b712-3412-4557-befe-926d372a3ca0">
<h3>Why to write adjoint code?</h3>
<p>When you write code for variable objects in ADMB, all the derivatives are calculated for you. To accomplish this, ADMB must save derivative information for variable objects, which can take a lot of memory and be time consuming for functions that are called many times. The purpose of writing adjoint code is to reduce the amount of derivative information that must be calculated and stored in order to speed up the process. </p>
<h3>Approach for simple functions</h3>
<p>Chapter 13 of the <a class="internal-link" href="http://www.admb-project.org/documentation/manuals/admb-user-manuals" title="ADMB User Manuals">ADMB manual</a> describes how to write and debug adjoint code for simple functions that take between 1 and 4 independent variables. The .tpl files are included below.</p>
<p><a class="internal-link" href="adjoint-code-1/Adjoint_1var.tpl" title="Adjoint_1var.tpl">1 independent variable</a></p>
<p><a class="internal-link" href="adjoint-code-1/Adjoint_2var.tpl" title="Adjoint_2var.tpl">2 independent variables</a></p>
<p><a class="internal-link" href="adjoint-code-1/Adjoint_4var.tpl" title="Adjoint_4var.tpl">4 independent variables</a> </p>
<h3>Structured approach for more complex functions</h3>
<p>For more complicated functions the previous approach becomes untenable and a more structured approach to calculate the derivatives is taken in the following example. Notice that to calculate the derivatives, every line of code in the function is repeated in the opposite order and the corresponding derivatives are calculated.</p>
<p><a class="internal-link" href="adjoint-code-1/Adjoint_4var_str.tpl" title="Adjoint_4var_str.tpl">4 independent variables with structured calculation of derivatives</a> </p>
<h3>General adjoint code</h3>
<p>In the previous examples the adjoint code has been used for a simple function which has from 1 to 4 independent variables. When functions can take any number of independent variables and return any number of dependent variables a more general approach is required. Steve Martell provided an example of writing adjoint code to numerically solve the Baranov catch equation and both its .tpl and its documentation are included below.</p>
<p><a class="internal-link" href="adjoint-code-1/AdJointCodeBaranov.pdf" title="AdJointCodeBaranov.pdf">Baranov example documentation</a></p>
<p><a class="internal-link" href="adjoint-code-1/barnov.tpl" title="barnov.tpl">Baranov example</a></p>
<p> </p>
</div>
31 changes: 31 additions & 0 deletions admb-tricks/admb-tricks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# admb-tricks

[Math][7]
Various undocumented techniques and tricks useful for developing ADMB programs

[Adjoint code][1]
: Why to write adjoint code and alternative approaches to do it.

[Parameterization][2]
: Examples of how to (and not to) parameterize mathematical functions and statistical models

[Splines][3]
: Different aspects of spline models

[Variance calculations][4]
: How variance is calculated in ADMB

[Covariance Calculations][5]
: A short document with accompanying R code that details (1) the functions used to bound parameters, (2) the method for calculating a bounded covariance matrix, and (3) what is stored in the binary admodel.hes and admodel.cov files and how the user can utilize this information to gain more control over an MCMC chain.

Document Actions

* [RSS feed][6]

[1]: admb-tricks/adjoint-code-1
[2]: admb-tricks/parameterization
[3]: admb-tricks/splines-1
[4]: admb-tricks/variance-calculations
[5]: admb-tricks/covariance-calculations
[6]: admb-tricks/RSS ""
[7]: admb-tricks/math
31 changes: 31 additions & 0 deletions admb-tricks/admb-tricks.md~
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# admb-tricks

[Math][7]
Various undocumented techniques and tricks useful for developing ADMB programs

[Adjoint code][1]
: Why to write adjoint code and alternative approaches to do it.

[Parameterization][2]
: Examples of how to (and not to) parameterize mathematical functions and statistical models

[Splines][3]
: Different aspects of spline models

[Variance calculations][4]
: How variance is calculated in ADMB

[Covariance Calculations][5]
: A short document with accompanying R code that details (1) the functions used to bound parameters, (2) the method for calculating a bounded covariance matrix, and (3) what is stored in the binary admodel.hes and admodel.cov files and how the user can utilize this information to gain more control over an MCMC chain.

Document Actions

* [RSS feed][6]

[1]: admb-tricks/adjoint-code-1
[2]: admb-tricks/parameterization
[3]: admb-tricks/splines-1
[4]: admb-tricks/variance-calculations
[5]: admb-tricks/covariance-calculations
[6]: admb-tricks/RSS ""
[7]: admb-tricks/math
19 changes: 19 additions & 0 deletions admb-tricks/covariance-calculations/covariance-calculations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Covariance Calculations

A short document with accompanying R code that details (1) the functions used to bound parameters, (2) the method for calculating a bounded covariance matrix, and (3) what is stored in the binary admodel.hes and admodel.cov files and how the user can utilize this information to gain more control over an MCMC chain.

![File][1] [Covariance Calculations][2]
: A document detailing the calculations used internally for calculating the covariance matrix with bounded parameters.

![File][3] [R code to interact with admodel.cov][4]
: R code for reproducing the covariance calculations and manipulating the admodel.cov file.

Document Actions

* [RSS feed][5]

[1]: http://www.admb-project.org/pdf.png
[2]: covariance-calculations/covariance-calculations/view.html
[3]: http://www.admb-project.org/application.png
[4]: covariance-calculations/r-code-to-interact-with-admodel.cov/view.html
[5]: covariance-calculations/RSS ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Covariance matrices

Parameterization via the Cholesky factor (Incomplete example)

If you want to estimate the parameters of a covariance matrix S you must ensure that the resulting matrix is 1) symmetric and 2) positive definite. To achieve this you do not parameterize S directly, but rather its Cholesky factor L, i.e. S = LL', see

  <http://en.wikipedia.org/wiki/Cholesky_decomposition>



The following two step procedure is recommended:

1) Parameterize the correlation matrix C via the Cholesky factor as explained here

[Correlation matrix][1]

2) Scale C with the standard deviations to obtained S.



Complete example given in C.tpl and C.dat.



**Constrained covariance matrices**

Sometimes you want elements in the C (or S) to be zero, say S(1,2) = 0, meaning the element 1 and 2 are uncorrelated. An example of how to achieve this is provided here:

[constrained_cor.tpl][2]

[constrained_cor.dat][3]



[1]: covariance-matrices/correlation matrix.pdf "Correlation matrix"
[2]: covariance-matrices/constrained_cor.tpl "constrained_cor.tpl"
[3]: covariance-matrices/constrained_cor.dat "constrained_cor.dat"
13 changes: 13 additions & 0 deletions admb-tricks/parameterization/parameterization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Parameterization

Examples of how to (and not to) parameterize mathematical functions and statistical models

[Covariance matrices][1]
: How to parameterize a covariance matrix

Document Actions

* [RSS feed][2]

[1]: parameterization/covariance-matrices.html
[2]: parameterization/RSS ""
13 changes: 13 additions & 0 deletions admb-tricks/splines-1/splines-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Splines

Different aspects of spline models

[Splines I][1]
: notes and examples related to cubic splines

Document Actions

* [RSS feed][2]

[1]: splines-1/splines.html
[2]: splines-1/RSS ""
31 changes: 31 additions & 0 deletions admb-tricks/splines-1/splines/splines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Splines I

Filed under: [Splines][1]

notes and examples related to cubic splines

![File text/x-perl][2] [splines.tpl][3]
: ADMB code with a spline (very rough)

![File PDF document][4] [Example results][5]
: notes: grey lines are from MCMC samples, third derivative looks rough only because of numerical precision

![File chemical/x-mopac-input][6] [splines.dat][7]
: data file to go with the model in splines.tpl

![File][2] [R code][8]
: read output from splines model and plot results shown in spline_example.pdf

Document Actions

* [RSS feed][9]

[1]: http://www.admb-project.org/@@search?Subject:list=Splines
[2]: http://www.admb-project.org/application.png
[3]: splines/splines.tpl/view.html
[4]: http://www.admb-project.org/pdf.png
[5]: splines/spline_example.pdf/view.html
[6]: http://www.admb-project.org/unknown.png
[7]: splines/splines.dat/view.html
[8]: splines/splines_code.R/view.html
[9]: splines/RSS ""
3 changes: 3 additions & 0 deletions admb-tricks/variance-calculations/variance-calculations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1 class="documentFirstHeading" id="parent-fieldname-title">
Variance calculations
</h1>None
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<h1 class="documentFirstHeading" id="parent-fieldname-title">
Covariance in RE models
</h1><div class="" id="parent-fieldname-text-48768352-ac5f-48bb-acc8-aa954cd47dcc">
<p>In RE (random effects) models there are two types of parameters, x (parameter in the ordinary sense) and u (random effect). This documents tries to shed some light on how the variance of x and u are calculated. The covariance matrix of the x vector is based on the (marginal) likelihood, obtained via the Laplace approximation, and corresponds to the way covariance matrices are calculated in non-RE models in ADMB.</p>
<p> </p>
<p><strong>Theory</strong></p>
<p>Here is what the user manual says about the variance of u (the manual talks about "theta" instead of "x"): <a class="internal-link" href="variance-in-re-models/usermanual.pdf" title="usermanual.pdf">usermanual.pdf</a> A few words can be added to this.</p>
<p> </p>
<p>The formula is based on the Law of total variance: <a class="external-link" href="http://en.wikipedia.org/wiki/Law_of_total_variance">http://en.wikipedia.org/wiki/Law_of_total_variance</a></p>
<p>In our context this says: </p>
<p>  Var(u) = E<sub>x</sub>[var(u|x)] + var<sub>x</sub>(E(u|theta)</p>
<p>The expectation "E<sub>x</sub>" is obtained simply by inserting the point estimate of x into "var(u|x)". The second term is based on the "delta method" which is used elsewhere in ADMB, in combination with the covariance matrix of x (described above). Everything in these calculations are conditional on "data".</p>
<p> </p>
<p> </p>
<p> </p>
<p><strong>Example: simple hierarchical model</strong></p>
<p>Consider the following simple Gaussian hierarchical model:</p>
<pre> Prior on x: x = e1
u|x: u = x + e2
y|u: y = u + e3
// where e1, e2, e3 are all distributed N(0,1)</pre>
<p>R code for the covariance matrix</p>
<pre>S = matrix(0,3,3,row=c("x","u","y"),col=c("x","u","y"))
S[,]=1
S[2:3,2:3]=2
S[3,3]=3
S12_3 = S[1:2,1:2] - S[1:2,3]%*%solve(S[3,3])%*%S[3,1:2]</pre>
<div>We are interested in the conditional variance of x and u given data (y).</div>
<pre>&gt; sqrt(diag(S12_3))
x u
0.8164966 0.8164966
&gt; cov2cor(S12_3)
x u
x 1.0 0.5
u 0.5 1.0</pre>
<div> </div>
<div><strong>Corresponding quantities in ADMB</strong></div>
<div>An implementation of this model in ADMB is:</div>
<pre> DATA_SECTION
number y
!! y=10.0;
PARAMETER_SECTION
init_number x
random_effects_vector u(1,1)
objective_function_value f
PROCEDURE_SECTION
f = 0.0;
f -= -0.5*square(x); // Prior on x: x = e1
f -= -0.5*square(u(1)-x); // u|x: u = x + e2
f -= -0.5*square(y-u(1)); // y|u: y = u + e3
// where e1, e2, e3 are all distributed N(0,1); standard normal
GLOBALS_SECTION
#include "getbigs.cpp"</pre>
<p>NOTE: as of Nov 29 2012 you need to include "getbigs.cpp" due to a recently discovered bug in ADMB.</p>
<p>The result you get when you run ADMB matches those from R:</p>
<pre>D:\tmp\tmp&gt;more simple_variance.cor
The logarithm of the determinant of the hessian = 0.405465
index name value std dev 1
1 x 3.3335e+000 8.1650e-001 1.0000
2 u 6.6668e+000 8.1650e-001 0.5000 1.0000</pre>
</div>
14 changes: 14 additions & 0 deletions by-field-of-application/by-field-of-application.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# By field of application

[Medical/Biometrics][1]
[Stochastic volatility collection][2]
[Ecology][3]

Document Actions

* [RSS feed][4]

[1]: by-field-of-application/medical-biometrics.html
[2]: by-field-of-application/stochastic-volatility-collection.html
[3]: by-field-of-application/copy_of_medical-biometrics.html
[4]: by-field-of-application/RSS ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Ecology

[Mark-Recapture random effects model][1]
[Estimation of detection function ][2]
: Illustrates the likelihood based estimation of the detection function (perpendicular distance) in line transect surveys

[Delta smelt life cycle model ][3]
: A state-space multistage model to evaluate population impacts in the presence of density dependence

[Theta-logistic population growth model][4]

Document Actions

* [RSS feed][5]

[1]: ../mark-recapture/mark-recapture-random-effects-model.html
[2]: ../sampling/line-transect-methods/estimation-of-detection-function/estimation-of-detection-function.html
[3]: ../state-space-models/delta-smelt-life-cycle-model/delta-smelt-life-cycle-model.html
[4]: ../state-space-models/theta-logistic-population-growth-model/theta-logistic-population-growth-model.html
[5]: copy_of_medical-biometrics/RSS ""
22 changes: 22 additions & 0 deletions by-field-of-application/medical-biometrics/medical-biometrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Medical/Biometrics

[A discrete valued time series model; Polio data][1]
: Illustrate how a time series of count data can be modelled as a GLMM with a Poisson response

[A time series of Poisson counts; Polio data][2]
: A serially correlated time series of Poisson counts using a GLMM framework

[A discrete valued time series model; Polio data][3]
: Illustrate how a time series of count data can be modelled as a GLMM with a Poisson response

[Weibull regression with censoring][4]

Document Actions

* [RSS feed][5]

[1]: ../state-space-models/a-discrete-valued-time-series-model.html
[2]: ../glmm-generalized-linear-mixed-models/count-data/a-discrete-valued-time-series-model/a-discrete-valued-time-series-model-polio-data.html
[3]: ../glmm-generalized-linear-mixed-models/count-data/a-discrete-valued-time-series-model.html
[4]: ../survival-analysis/weibull-regression-with-censoring.html
[5]: medical-biometrics/RSS ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Stochastic volatility models

Stochastic volatility models are used to model financial time series with time-varying volatility.

An important property of financial time series is that volatility, defined as the standard deviation of log-returns, is time-varying. There are two types of models used for analyzing time series with varying volatility: GARCH models and stochastic volatility (SV) models. In GARCH models the conditional volatility is a deterministic function of previous returns and volatilities, while in SV-models volatility is assumed to follow a latent stochastic process. Although SV-models have favorable properties compared to GARCH, they are less used in practice. The reason for this is that they are much harder to fit. The problem is that due to non-linear dependence between returns and latent volatilities, it is not possible to find a closed form expression for the likelihood function for SV models. Much work has been done to overcome this problem, but most approaches are either inefficient or hard to implement. Here it is shown how several SV-models can be easily fit to financial returns using ADMB-RE.

1. [**The basic SV-model and simple extensions**.][1] In this section the basic SV-model is introduced, and it is shown how simple modifications can produce models that also can capture skewness and higher kurtosis than the basic model. 
2. [**SV-models with leverage effect**. ][2]We often see an increase in volatility after a drop in returns. This phenomenon is called leverage effect and seems to be particularly important for stocks. Leverage effect can be modeled by correlated noise terms in the retun and the volatility equations. Models with correlated noise terms have a more complicated structure, and are therefore treated in a separate section.











[1]: stochastic-volatility-collection/the-basic-sv-model-and-simple-extensions-1/modeller.html "The basic SV model and simple extensions"
[2]: stochastic-volatility-collection/sv-models-with-leverage-effect/sv-models-with-leverage-effect.html "SV models with leverage effect"
Loading

0 comments on commit e9f9d45

Please sign in to comment.