• No se han encontrado resultados

CAPÍTULO III. METODOLOGÍA DE ESTUDIO

3.5. Análisis de respuesta dinámica de sitio (ARS)

3.5.1. Procedimiento de análisis de respuesta de sitio

Contents

• Useful Libraries Overview Plotting Probability

Working with Data

Optimization, Roots and Fixed Points Others Topics

Further Reading

Overview

While Julia lacks the massive scientific ecosystem of Python, it has successfully attracted a small army of enthusiastic and talented developers

As a result, its package system is moving towards a critical mass of useful, well written libraries In addition, a major advantage of Julia libraries is that, because Julia itself is sufficiently fast, there is less need to mix in low level languages like C and Fortran

As a result, most Julia libraries are written exclusively in Julia

Not only does this make the libraries more portable, it makes them much easier to dive into, read, learn from and modify

In this lecture we introduce a few of the Julia libraries that we’ve found particularly useful for quantitative work in economics

Plotting

There are already several libraries for generating figures in Julia

• Winston

• Gadfly

• PyPlot

Of these, the most mature from the point of view of the end user is PyPlot

In fact PyPlot is just a Julia front end to the excellent Python plotting libraryMatplotlib

In the following we provide some basic information on how to install and work with this library

Installing PyPlot The one disadvantage of PyPlot is that it not only requires Python but also a lot of the scientific Python back end

Moreover, the scientific Python tools are extremely useful and easily accessible from Julia via

PyCall

We discussed installing Anaconda and PyPlothere

Usage The most important source of information about PyPlot is thedocumentation for Mat- plotlib itself

There are also many useful examples available on the Matplotlib website and elsewhere

The Procedural API Matplotlib has a straightforward plotting API that essentially replicates the plotting routines in MATLAB

These plotting routines can be expressed in Julia with almost identical syntax We’ve already seen some examples of this in earlier lectures

Here’s another example using PyPlot

x = linspace(0, 10, 200) y = sin(x)

plot(x, y, "b-", linewidth=2) The resulting figure looks as follows

The Object Oriented API Matplotlib also has a more “Pythonic” object orientated API that power users will prefer

Since Julia doesn’t bundle objects with methods in the same way that Python does, plots based on this API don’t follow exactly the same syntax that they do in Matplotlib

Fortunately the differences are consistent and after seeing some examples you will find it easy to translate one into the other

Here’s an example of the syntax we’re discussing, which in this case generates exactly the same plot using PyPlot x = linspace(0, 10, 200) y = sin(x) fig, ax = subplots() ax[:plot](x, y, "b-", linewidth=2)

In this case we get nothing extra and have to accept more complexity and a less attractive syntax However, it is a little more explicit and this turns out to be convenient as we move to more sophis- ticated plots

Here’s a similar plot with a bit more customization using PyPlot

x = linspace(0, 10, 200) y = sin(x)

fig, ax = subplots()

ax[:plot](x, y, "r-", linewidth=2, label="sine function", alpha=0.6) ax[:legend](loc="upper center")

The resulting figure has a legend at the top center

We can render the legend in LaTeX by changing theax[:plot]line to ax[:plot](x, y, "r-", linewidth=2, label=L"$y = \sin(x)$", alpha=0.6) Note theLin front of the string to indicate LaTeX mark up

The result looks as follows

Here’s another example, which helps illustrate how to put multiple plots on one figure using PyPlot

u = Uniform()

fig, ax = subplots() x = linspace(-4, 4, 150) for i in 1:3

# == Compute normal pdf from randomly generated mean and std == #

m, s = rand(u) * 2 - 1, rand(u) + 1

d = Normal(m, s) y = pdf(d, x)

# == Plot current pdf == #

ax[:plot](x, y, linewidth=2, alpha=0.6, label="draw $i") end

ax[:legend]()

Multiple Subplots A figure containingnrows andmcolumns of subplots can be created by the call

fig, axes = subplots(num_rows, num_cols) Here’s an example

using PyPlot

using Distributions u = Uniform()

num_rows, num_cols = 3, 2

fig, axes = plt.subplots(num_rows, num_cols, figsize=(8, 12)) subplot_num = 0

for i in 1:num_rows for j in 1:num_cols

ax = axes[i, j] subplot_num += 1

# == Generate a normal sample with random mean and std == #

m, s = rand(u) * 2 - 1, rand(u) + 1

d = Normal(m, s) x = rand(d, 100)

# == Histogram the sample == #

ax[:hist](x, alpha=0.6, bins=20)

ax[:set_title]("histogram $subplot_num") ax[:set_xticks]([-4, 0, 4])

ax[:set_yticks]([]) end

end

The resulting figure is as follows

3D Plots Here’s an example of how to create a 3D plot using PyPlot

using Distributions using QuantEcon: meshgrid n = 50 x = linspace(-3, 3, n) y = x z = Array(Float64, n, n) f(x, y) = cos(x^2 + y^2) / (1 + x^2 + y^2) for i in 1:n for j in 1:n z[j, i] = f(x[i], y[j]) end end fig = figure(figsize=(8,6)) ax = fig[:gca](projection="3d")

ax[:set_zlim](-0.5, 1.0) xgrid, ygrid = meshgrid(x, y)

ax[:plot_surface](xgrid, ygrid, z, rstride=2, cstride=2, cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25) It creates this figure

Probability

Functions for manipulating probability distributions and generating random variables are sup- plied by the excellentDistributionspackage

This package hasfirst-rate documentationso we’ll restrict ourselves to a few comments

The calls to create instances of various random variables take the form d = DistributionName(params)

Here are several examples

• Normal with meanmand standard deviations

d = Normal(m, s)with defaultsm = 0ands = 1 • Uniform on interval[a,b]

d = Uniform(a, b)with defaultsa = 0andb = 1 • Binomial overntrials with success probabilityp

d = Binomial(n, p)with defaultsn = 1andp = 0.5

• random draws

• pdf (density), cdf (distribution function), quantiles, etc. • mean, variance, kurtosis, etc.

For example,

• To generatekdraws from the instanceduserand(d, k) • To obtain the mean of the distribution usemean(d)

• To evaluate the probability density function ofdatxusepdf(d, x) Further details on the interface can be foundhere

Several multivariate distributions are also implemented

Working with Data

A useful package for working with data isDataFrames

The most important data type provided is aDataFrame, a two dimensional array for storing het- erogeneous data

Although data can be heterogeneous within a DataFrame, the contents of the columns must be homogeneous

This is analogous to adata.framein R, aDataFramein Pandas (Python) or, more loosely, a spread- sheet in Excel

The DataFrames package also supplies a DataArray type, which is like a one dimensional DataFrame

In terms of working with data, the advantage of aDataArrayover a standard numerical array is that it can handle missing values

Here’s an example julia> using DataFrames

julia> commodities = ["crude", "gas", "gold", "silver"] 4-element Array{ASCIIString,1}:

"crude" "gas" "gold" "silver"

julia> last = @data([4.2, 11.3, 12.1, NA]) # Create DataArray

4-element DataArray{Float64,1}: 4.2

11.3 12.1 NA

4x2 DataFrame

|---|---|---| | Row # | commod | price | | 1 | "crude" | 4.2 | | 2 | "gas" | 11.3 | | 3 | "gold" | 12.1 | | 4 | "silver" | NA |

Columns of the DataFrame can be accessed by name julia> df[:price] 4-element DataArray{Float64,1}: 4.2 11.3 12.1 NA julia> df[:commod] 4-element DataArray{ASCIIString,1}: "crude" "gas" "gold" "silver"

The DataFrames package provides a number of methods for acting on DataFrames A simple one isdescribe()

julia> describe(df) commod Length 4 Type ASCIIString NAs 0 NA% 0.0% Unique 4 price Min 4.2 1st Qu. 7.75 Median 11.3 Mean 9.200000000000001 3rd Qu. 11.7 Max 12.1 NAs 1 NA% 25.0%

There are also functions for splitting, merging and other data munging operations

Data can be read from and written to CSV files using syntaxdf = readtable("data_file.csv") andwritetable("data_file.csv", df)respectively

Optimization, Roots and Fixed Points

Let’s look briefly at the optimization and root finding algorithms

Roots A root of a real function f on[a,b]is anx∈ [a,b]such that f(x) =0 For example, if we plot the function

f(x) =sin(4(x−1/4)) +x+x20−1 (1.6) withx∈[0, 1]we get

The unique root is approximately 0.408

One common root-finding algorithm is theNewton-Raphson method

This is implemented asnewton()in theRootspackage and is called with the function and an initial guess

julia> using Roots

julia> f(x) = sin(4 * (x - 1/4)) + x + x^20 - 1

f (generic function with 1 method) julia> newton(f, 0.2)

0.40829350427936706

The Newton-Raphson method uses local slope information, which can lead to failure of conver- gence for some initial conditions

julia> newton(f, 0.7) -1.0022469256696989

For this reason most modern solvers use more robust “hybrid methods”, as does Roots’sfzero() function

julia> fzero(f, 0, 1) 0.40829350427936706

Optimization For constrained, univariate minimization a useful option isoptimize()from the

Optimpackage

This function defaults to a robust hybrid optimization routine called Brent’s method julia> using Optim

julia> optimize(x -> x^2, -1.0, 1.0) Results of Optimization Algorithm

* Algorithm: Brent's Method

* Search Interval: [-1.000000, 1.000000] * Minimum: -0.000000

* Value of Function at Minimum: 0.000000 * Iterations: 5

* Convergence: max(|x - x_upper|, |x - x_lower|) <= 2*(1.5e-08*|x|+2.2e-16): true * Objective Function Calls: 6

For other optimization routines, including least squares and multivariate optimization, see the documentation

A number of alternative packages for optimization can be found atJuliaOpt

Others Topics

Numerical Integration The base library contains a function calledquadgk()that performs Gaus- sian quadrature

julia> quadgk(x -> cos(x), -2pi, 2pi)

(5.644749237155177e-15,4.696156369056425e-22)

This is an adaptive Gauss-Kronrod integration technique that’s relatively accurate for smooth functions

However, its adaptive implementation makes it slow and not well suited to inner loops For this kind of integration you can use the quadrature routines from QuantEcon julia> using QuantEcon

julia> nodes, weights = qnwlege(65, -2pi, 2pi);

julia> integral = do_quad(x -> cos(x), nodes, weights) -2.912600716165059e-15

Let’s time the two implementations

julia> @time quadgk(x -> cos(x), -2pi, 2pi)

elapsed time: 2.732162971 seconds (984420160 bytes allocated, 40.55% gc time) julia> @time do_quad(x -> cos(x), nodes, weights)

elapsed time: 0.002805691 seconds (1424 bytes allocated)

We get similar accuracy with a speed up factor approaching three orders of magnitude

More numerical integration (and differentiation) routines can be found in the packageCalculus

Linear Algebra The standard library contains many useful routines for linear algebra, in addi- tion to standard functions such asdet(),inv(),eye(), etc.

Routines are available for • Cholesky factorization • LU decomposition

• Singular value decomposition, • Schur factorization, etc.

Seeherefor further details

Further Reading

The full set of libraries available under the Julia packaging system can be browsed at

TWO

INTRODUCTORY APPLICATIONS

This section of the course contains relatively simple applications, one purpose of which is to teach you more about the Python programming environment

2.1 Linear Algebra

Contents • Linear Algebra Overview Vectors Matrices

Solving Systems of Equations Eigenvalues and Eigenvectors Further Topics

Overview

One of the single most useful branches of mathematics you can learn is linear algebra

For example, many applied problems in economics, finance, operations research and other fields of science require the solution of a linear system of equations, such as

y1= ax1+bx2

y2 =cx1+dx2 or, more generally,

y1 =a11x1+a12x2+· · ·+a1kxk ..

.

yn =an1x1+an2x2+· · ·+ankxk

(2.1)

The objective here is to solve for the “unknowns”x1, . . . ,xk givena11, . . . ,ankandy1, . . . ,yn When considering such problems, it is essential that we first consider at least some of the following questions

• Does a solution actually exist?

• Are there in fact many solutions, and if so how should we interpret them? • If no solution exists, is there a best “approximate” solution?

• If a solution exists, how should we compute it? These are the kinds of topics addressed by linear algebra

In this lecture we will cover the basics of linear and matrix algebra, treating both theory and computation

We admit some overlap withthis lecture, where operations on Julia arrays were first explained Note that this lecture is more theoretical than most, and contains background material that will be used in applications as we go along

Vectors

Avector of lengthnis just a sequence (or array, or tuple) of nnumbers, which we write as x = (x1, . . . ,xn)orx= [x1, . . . ,xn]

We will write these sequences either horizontally or vertically as we please

(Later, when we wish to perform certain matrix operations, it will become necessary to distinguish between the two)

The set of alln-vectors is denoted byRn

For example,R2is the plane, and a vector inR2is just a point in the plane

Traditionally, vectors are represented visually as arrows from the origin to the point The following figure represents three vectors in this manner

If you’re interested, the Julia code for producing this figure ishere

Vector Operations The two most common operators for vectors are addition and scalar multi- plication, which we now describe

As a matter of definition, when we add two vectors, we add them element by element

x+y=      x1 x2 .. . xn      +      y1 y2 .. . yn      :=      x1+y1 x2+y2 .. . xn+yn     

Scalar multiplication is an operation that takes a numberγand a vectorxand produces

γx :=      γx1 γx2 .. .     

Scalar multiplication is illustrated in the next figure

In Julia, a vector can be represented as a one dimensionalArray

JuliaArraysallow us to express scalar multiplication and addition with a very natural syntax julia> x = ones(3) 3-element Array{Float64,1}: 1.0 1.0 1.0 julia> y = [2, 4, 6] 3-element Array{Int64,1}: 2 4 6 julia> x + y 3-element Array{Float64,1}: 3.0 5.0 7.0

julia> 4x # equivalent to 4 * x and 4 .* x

3-element Array{Float64,1}: 4.0

4.0 4.0

Inner Product and Norm Theinner productof vectors x,y∈Rnis defined as x0y:= n

i=1 xiyi Two vectors are calledorthogonalif their inner product is zero

Thenormof a vectorxrepresents its “length” (i.e., its distance from the zero vector) and is defined as

kxk:= √x0x:=

n

i=1

x2i !1/2

The expressionkx−ykis thought of as the distance betweenxandy

Continuing on from the previous example, the inner product and norm can be computed as fol- lows

julia> dot(x, y) # Inner product of x and y

12.0

julia> sum(x .* y) # Gives the same result

12.0

julia> norm(x) # Norm of x

1.7320508075688772

julia> sqrt(sum(x.^2)) # Gives the same result

1.7320508075688772

New vectors created in this manner are calledlinear combinationsofA

In particular,y ∈Rnis a linear combination ofA:={a

1, . . . ,ak}if

y=β1a1+· · ·+βkakfor some scalarsβ1, . . . ,βk

In this context, the valuesβ1, . . . ,βkare called thecoefficientsof the linear combination The set of linear combinations ofAis called thespanofA

The next figure shows the span of A= {a1,a2}inR3

The span is a 2 dimensional plane passing through these two points and the origin

The code for producing this figure can be foundhere

Examples If Acontains only one vectora1 ∈ R2, then its span is just the scalar multiples ofa1, which is the unique line passing through botha1and the origin

IfA={e1,e2,e3}consists of thecanonical basis vectorsofR3, that is

e1 :=   1 0 0  , e2 :=   0 1 0  , e3:=   0 0 1  

then the span ofAis all ofR3, because, for anyx= (x1,x2,x3)∈ R3, we can write

x= x1e1+x2e2+x3e3 Now considerA0 ={e1,e2,e1+e2}

Ify= (y1,y2,y3)is any linear combination of these vectors, theny3 =0 (check it) HenceA0fails to span all ofR3

Linear Independence As we’ll see, it’s often desirable to find families of vectors with relatively large span, so that many vectors can be described by linear operators on a few vectors

The condition we need for a set of vectors to have a large span is what’s called linear independence In particular, a collection of vectors A:={a1, . . . ,ak}inRnis said to be

• linearly dependentif some strict subset of Ahas the same span asA

• linearly independentif it is not linearly dependent

Put differently, a set of vectors is linearly independent if no vector is redundant to the span, and linearly dependent otherwise

To illustrate the idea, recall the figure that showed the span of vectors {a1,a2}in R3 as a plane through the origin

If we take a third vectora3and form the set{a1,a2,a3}, this set will be • linearly dependent ifa3lies in the plane

• linearly independent otherwise

As another illustration of the concept, sinceRncan be spanned bynvectors (see the discussion of canonical basis vectors above), any collection ofm>nvectors inRnmust be linearly dependent The following statements are equivalent to linear independence ofA:={a1, . . . ,ak} ⊂Rn

1. No vector inAcan be formed as a linear combination of the other elements 2. If β1a1+· · ·βkak =0 for scalarsβ1, . . . ,βk, thenβ1=· · · =βk =0

(The zero in the first expression is the origin ofRn)

Unique Representations Another nice thing about sets of linearly independent vectors is that each element in the span has a unique representation as a linear combination of these vectors In other words, if A:={a1, . . . ,ak} ⊂Rnis linearly independent and

y=β1a1+· · ·βkak

then no other coefficient sequenceγ1, . . . ,γkwill produce the same vectory Indeed, if we also havey=γ1a1+· · ·γkak, then

(β1−γ1)a1+· · ·+ (βk−γk)ak =0 Linear independence now impliesγi =βifor alli

Matrices

Ann×kmatrix is a rectangular arrayAof numbers withnrows andkcolumns: A=      a11 a12 · · · a1k a21 a22 · · · a2k .. . ... ... an1 an2 · · · ank     

Often, the numbers in the matrix represent coefficients in a system of linear equations, as discussed at the start of this lecture

For obvious reasons, the matrixAis also called a vector if eithern=1 ork=1

In the former case,Ais called arow vector, while in the latter it is called acolumn vector

Ifn= k, then Ais calledsquare

The matrix formed by replacingaijbyajifor everyiandjis called thetransposeof A, and denoted

A0 orA>

IfA= A0, then Ais calledsymmetric

For a square matrixA, theielements of the formaiifori=1, . . . ,nare called theprincipal diagonal

Documento similar