Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzarruk committed Aug 5, 2016
1 parent a73b3d3 commit 9a5433a
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 86 deletions.
11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Package: Rtauchen
Type: Package
Title: Discretization of AR(1) processes using the method by Tauchen (1986)
Title: Discretization of AR(1) Processes using the Method by Tauchen
(1986)
Version: 1.0
Date: 2016-08-01
Author: David Zarruk Valencia & Rodrigo Azuero Melo
URL: https://github.com/rodazuero/Rtauchen
URL: https://github.com/davidzarruk/Rtauchen
Maintainer: David Zarruk Valencia <davidzarruk@gmail.com>
Description: Method for discretizing AR(1) processes by choosing a finite set of points and transition probabilities, so the resulting finite-state Markov Chain mimics the original process.
Description: Description: Discretize AR process.
License: GPL (>= 2)
Imports: stats, pnorm
Packaged: 2016-08-04 21:56:05 UTC; rodrigoazuero
Imports: stats
Packaged: 2016-08-05 21:47:04 UTC; rodrigoazuero
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exportPattern("^[[:alpha:]]+")
importFrom("stats", "pnorm")
importFrom("stats","pnorm")
68 changes: 18 additions & 50 deletions R/tauchen.R
Original file line number Diff line number Diff line change
@@ -1,73 +1,41 @@

# Corro un archivo con los parametros

Tgrid = function(n, ssigma, llambda, m){
Tgrid=function(ne, ssigma_eps, llambda_eps, m){

grid = array(0, dim = n)
ssigma_y = sqrt(ssigma^2/(1-llambda^2))
grid = array(0, dim = ne)
ssigma_y = sqrt(ssigma_eps^2/(1-llambda_eps^2))

estep = 2*ssigma_y*m / (n-1)
for(ii in 1:1:n){
grid[ii] = -m*sqrt((ssigma^2)/(1-llambda^2)) + (ii-1)*estep
estep = 2*ssigma_y*m / (ne-1)
for(ii in 1:1:ne){
grid[ii] = -m*sqrt((ssigma_eps^2)/(1-llambda_eps^2)) + (ii-1)*estep
}
return(grid)
return(grid)
}

Tmatrix = function(n, ssigma, llambda, m){
Rtauchen=function(ne, ssigma_eps, llambda_eps, m){

grid = Tgrid(n, ssigma, llambda, m)
grid = Tgrid(ne, ssigma_eps, llambda_eps, m)

if(n > 1){
if(ne > 1){
w = grid[2] - grid[1]
P = array(0, dim = c(n,n))
P = array(0, dim = c(ne,ne))

for(jj in 1:n){
for(kk in 1:n){
for(jj in 1:ne){
for(kk in 1:ne){
if(kk == 1){
P[jj,kk] = pnorm((grid[kk] - llambda*grid[jj]+(w/2))/ssigma)
P[jj,kk] = pnorm((grid[kk] - llambda_eps*grid[jj]+(w/2))/ssigma_eps)
}
else if(kk == n){
P[jj,kk] = 1- pnorm((grid[kk] - llambda*grid[jj]-(w/2))/ssigma)
else if(kk == ne){
P[jj,kk] = 1- pnorm((grid[kk] - llambda_eps*grid[jj]-(w/2))/ssigma_eps)
}
else{
P[jj,kk] = pnorm((grid[kk] - llambda*grid[jj]+(w/2))/ssigma)-pnorm((grid[kk] - llambda*grid[jj]-(w/2))/ssigma)
P[jj,kk] = pnorm((grid[kk] - llambda_eps*grid[jj]+(w/2))/ssigma_eps)-pnorm((grid[kk] - llambda_eps*grid[jj]-(w/2))/ssigma_eps)
}
}
}
} else{
P = 1
}
return(P)
}



nTgrid = function(n, ssigma, llambda, m, dim){

n = 9
m = 3
dimension = dim(ssigma)
grid = array(0, dim = c(dimension[1], n))

llambda = array(c(0.7, 0.2, 0.3, 0.5), dim = c(2,2))
ssigma = array(c(0.1, 0, 0, 0.1), dim = c(2,2))

ssigma_y = array(0, dim = c(dimension[1], dimension[1]))
error = 1
tol = 10^-5
while(error > tol){
ssigma_y_p = llambda%*%ssigma_y%*%t(llambda) + ssigma
error = norm(ssigma_y_p - ssigma_y)
ssigma_y = ssigma_y_p
}

for(jj in 1:1:dimension[1]){
estep = 2*sqrt(ssigma_y[jj, jj])*m / (n-1)
for(ii in 1:1:n){
grid[jj, ii] = -sqrt(ssigma_y[jj, jj])*m + (ii-1)*estep
}
}


return(grid)
}
}
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Rtauchen
=======
[![Build Status](https://travis-ci.org/rodazuero/Rtauchen.png)](https://travis-ci.org/rodazuero/Rtauchen)
[![Build Status](https://travis-ci.org/davidzarruk/Rtauchen.png)](https://travis-ci.org/rodazuero/Rtauchen)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/Rtauchen)](https://cran.r-project.org/package=Rtauchen)

RTauchen uses [Tauchen's (1986)](http://apps.eui.eu/Personal/Researchers/georgd/tauchen.pdf) method for discretizing AR(1) processes by choosing a finite set of points and transition probabilities, so the resulting finite-state Markov Chain mimics the original process. This method is computationally faster and numerically more stable than other methods (for example, Gaussian quadrature). This method is very popular in computational macroeconomics (See for example [Deaton (1989)](http://www.nber.org/papers/w3196), [Sargent and Ljungqvist (2012)](https://books.google.com/books?hl=es&lr=&id=H-PxCwAAQBAJ&oi=fnd&pg=PR5&ots=T-BOYPfzA7&sig=BMqWTp_3mAsSiyqQKRGm6xGClNU#v=onepage&q&f=false) and [Aiyagari (1994)](http://www.jstor.org/stable/2118417?seq=1#page_scan_tab_contents)).
Expand All @@ -15,15 +15,15 @@ install.packages("Rtauchen")
# Github installation
# install.packages("devtools")
devtools::install_github("rodazuero/Rtauchen")
devtools::install_github("davidzarruk/Rtauchen")
```


## Example 1
This example computes the transition probability matrix of the finite-state Markov chain approximation of an AR(1) process with:
n = 5 (points in the Markov chain), ssigma = 0.02, lambda = 0.95, m = 3
``` r
results = Tmatrix(5, 0.02, 0.98, 3)
results = Rtauchen(5, 0.02, 0.98, 3)

results
# [,1] [,2] [,3] [,4] [,5]
Expand All @@ -45,8 +45,3 @@ results

```






Binary file removed Tauchen 1986.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion demo/example1.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ library(Rtauchen)
# ssigma = 0.02
# lambda = 0.95
# m = 3
results = Tmatrix(5, 0.02, 0.98, 3)
results = Rtauchen(5, 0.02, 0.98, 3)

results
# [,1] [,2] [,3] [,4] [,5]
Expand Down
11 changes: 2 additions & 9 deletions demo/example2.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
library(Rtauchen)

# This example computes the grid of points of the finite-state Markov chain approximation of an AR(1) process with:
# n = 5 points in the Markov chain
# ssigma = 0.02
# lambda = 0.95
# m = 3
results = Tgrid(5, 0.02, 0.98, 3)

results
# [1] -0.3015113 -0.1507557 0.0000000 0.1507557 0.3015113
# Include description here
results = Rtauchen(2, 1.0e-5, 0.1,0.4)
14 changes: 7 additions & 7 deletions man/Rtauchen.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
Rtauchen(ne, ssigma_eps, llambda_eps, m)
}
\arguments{
\item{ne}{Number of points in grid}
\item{ne}{Number of points of the grid of the finite-state Markov chain that mimics the AR(1) process}

\item{ssigma_eps}{Standard deviation of exogenous shock}
\item{ssigma_eps}{Standard deviation of exogenous shock in the AR(1) process}

\item{llambda_eps}{persistence parameter}
\item{llambda_eps}{Persistence parameter of the AR(1) process}

\item{m}{Tauchen parameter}
\item{m}{Tauchen parameter for the width of the process (number of standard deviations of the AR(1) process covered by the grid)}
}
\value{
a matrix with the corresponding transition matrix
A matrix with the corresponding to the transition matrix of the finite-state Markov chain that approximates the AR(1) process
}
\description{
Discretize AR1 process
This function generates a matrix of transition probabilites of a finite-state Markov chain that mimics an AR(1) process with persistence parameter llamda, standard deviation ssigma and a fixed parameter m.
}
\details{
Discretize AR process
See Tauchen (1986) for details.
}
\examples{
results = Rtauchen(2, 1.0e-5, 0.1,0.4)
Expand Down
10 changes: 5 additions & 5 deletions man/Tgrid.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
\alias{Tgrid}
\title{Tgrid}
\usage{
Tgrid(n, ssigma, llambda, m)
Tgrid(ne, ssigma_eps, llambda_eps, m)
}
\arguments{
\item{n}{Number of points of the grid of the finite-state Markov chain that mimics the AR(1) process.}
\item{ne}{Number of points of the grid of the finite-state Markov chain that mimics the AR(1) process}

\item{ssigma}{Standard deviation of exogenous shock in the AR(1) process.}
\item{ssigma_eps}{Standard deviation of exogenous shock in the AR(1) process}

\item{llambda}{Persistence parameter of the AR(1) process.}
\item{llambda_eps}{Persistence parameter of the AR(1) process}

\item{m}{Tauchen parameter for the width of the process (number of standard deviations of the AR(1) process covered by the grid).}
\item{m}{Tauchen parameter for the width of the process (number of standard deviations of the AR(1) process covered by the grid)}
}
\value{
An array with the grid points of a finite-state Markov chain which approximates the original AR(1) process.
Expand Down

0 comments on commit 9a5433a

Please sign in to comment.