// object binomial extends App { import scala.math._ type BinomialTree = Array[Array[Double]] def makeTree(T: Int): BinomialTree = { val B: BinomialTree = new Array(1+T) for (t <- 0 to T) B(t) = new Array(1+t) B } def constVol(c0: Double, T: Int, U: Double, D: Double) = { val C = makeTree(T) C(0)(0) = c0 for (t <- 1 to T) { C(t)(0) = C(t - 1)(0) * D for (u <- 1 to t) C(t)(u) = C(t - 1)(u - 1) * U } C } def backRecurse(C: BinomialTree, tAgg: (Double, Double) => Double, CE: (Double, Double) => Double) = { val T = C.length - 1 val V = makeTree(T) for (u <- 0 to T) V(T)(u) = C(T)(u) for (t <- T - 1 to 0 by -1) for (u <- 0 to t) V(t)(u) = tAgg(CE(V(t + 1)(u + 1), V(t + 1)(u)), C(t)(u)) V } def siCE(p: Double, cRRA: Double, uV: Double, dV: Double) = { val power = 1 - cRRA // assume cRRA away from one pow(p * pow(uV, power) + (1-p) * pow(dV, power), 1 / power) } val alphaValues = List(2.0, 20.0, 40.0) val beta = 0.03 val gamma = 2.0 val delta = 1.0 / 1.5 val muHi = 0.05 val muLo = 0.00 val sigma = 0.03 val nPeriods = 560 val horizon = 10.0 val h = horizon / nPeriods val pHi = (1 + (muHi / sigma) * sqrt(h)) / 2.0 val pLo = (1 + (muLo / sigma) * sqrt(h)) / 2.0 val upGrowth = exp(sigma * sqrt(h)) val dnGrowth = 1 / upGrowth val C = constVol(100, nPeriods, upGrowth, dnGrowth) val discount = exp(-beta * h) val tAgg = siCE(discount, delta, _: Double, _: Double) val hiCE = siCE(pHi, gamma, _: Double, _: Double) val loCE = siCE(pLo, gamma, _: Double, _: Double) for (alpha <- alphaValues) { val U = backRecurse(C, tAgg, (x, y) => siCE(0.5, alpha, hiCE(x, y), loCE(x, y))) println("For alpha = " + alpha + " the time-zero utility is " + U(0)(0)) } // }