ECON 474-625: Numerical Methods for EconomistsAssignment 3: Unconstrained Optimization Methods(Due date: Sunday February 25 before 11:30pm in Learn drop box)
This is an individual assignment. You can help each other, but the final result must be yours. Question 1In this question, we want to compute the market demand using a two good market and a one dimen- tional unconstrained Newton method. You have to answer the question using expression and analitical derivatives. The market is composed of 1,000 consumers with the following utility function: To generate you 1000 consumers, you will proceed as folows:set.seed(seed)
s x0, then s = s/2. This will prevent x1 to become negative. (ii) As long as s x0 | s<(x0-l$I/l$p1)) s <- s/2 The new function should now work for any x0:getsol2(U=ces, x0=80,l=l)
## $sol
## x1 x2
## 1.734754 70.189461
##
## $niter ## [1] 10
getsol2(U=ces, x0=99,l=l)
## $sol
## x1 x2
## 1.734754 70.189461
##
## $niter ## [1] 9 Once your function is stable, remove the argument x0 and set the starting value to a sensible value inside the function. Remember that x2 must be positive.getsol(U=ces, l)
## $sol
## x1 x2 ## 1.734754 70.189461 ##
## $niter ## [1] 8 c) Write a function that computes the total quantity demanded by your market (the sum of all 1,000 consumers) for a given set of prices p = p1, p2 j. Try to make it work for any number of consumers. You would get something like:library(parallel)
marketD(p=c(3,6), par, U=ces, tol=1e-7, maxit=100, mc.cores=4)
## Market demand
## *********************** ## Number of consumers: 1000 ## p1 = 3, p2 = 6
## x1 = 48968.99, x2 = 222166.76
## *********************** Of course, you should all get different values as your par will be different. You are free to create a particular object with its print method if you want, but it is not required.d) Write a function that plots the market demand of either x1 for a given p2 or x2 for a given p1. You would get something like:
Hint: If you want to smooth a function with a few points, you can use spline(). For example:
e) Compare the effect of a 20% income on both demands when the other price is 5. I am expecting something like
Question 2Estimate the ρ from the second assignment using the Newton method and numerical derivative. To do so, you need to have a function that depends on ρ, and returns the SSR. You can use part of your codes from the second assignment. Basically, for each ρ, you need to estimate the model:set.seed(112233)
u <- arima.sim(n=200, model=list(ar=.7)) x1 <- rnorm(200)
x2 <- rnorm(200) y <- 1+x1-x2+u
X <- cbind(Intercept=1,x1,x2) (ssr <- SSR(rho=.5, y, X, "CO"))
## [1] 202.0671
(ssr <- SSR(rho=.5, y, X, "PW")) ## [1] 202.0674After, you need a function that computes the first and second derivative of SSR(ρ).dssr <- dSSR(rho=.5, y, X, "CO",h=.001)) ## [1] -129.2444
(ddssr <- ddSSR(rho=.5, y, X, "PW",h=.001))
## [1] 709.8033 Then, write the algorithm to minimize SSR. You should get:
getrho(rho0=.1, y, X, "CO")
## $rho
## [1] 0.6819157 ##
## $SSR
## [1] 190.3081 ##
## $niter ## [1] 3
getrho(rho0=.1, y, X, "PW") ## $rho
## [1] 0.6819152
##
## $SSR
## [1] 190.3086 ##
## $niter ## [1] 3