10. (Code Reduction Using Variadic Parameters) The algorithms for forward and backward induction in Layer 2 of...

Question:

10. (Code Reduction Using Variadic Parameters)

The algorithms for forward and backward induction in Layer 2 of the software framework use tuples. In particular, binomial lattices use tuples with two components while trinomial lattices use tuples with three components. The current design leads to duplicate code and we would like to create a single algorithm for forward induction and a single algorithm for backward induction. This goal should be achievable by using the fact that the second template parameter in the Level 1 lattice class tells us how many components to use as output (in the case of forward induction) or as input (in the case of backward induction). For example, we could use a fixed-size array std::array where N =

2 for binomial lattices and N = 3 for trinomial lattices or the more exotic case of variadic templates in a later version.

We implement the algorithms for forward and backward induction for plain options using std::array instead of std::tuple<>. To motivate what we mean, we give the function prototype and code of the forward induction algorithm:

template void ForwardInduction

(Lattice& lattice, const std::function

(const Node& node)>& generator, const Node& rootValue)

{

// Initialise the root value lattice[0][0]

std::size_t si = lattice.MinIndex();

lattice[si][0] = rootValue;

std::array tuple;

// Loop from min index to end index, i.e. (min, end]

for (std::size_t n = lattice.MinIndex() + 1;

n <= lattice.MaxIndex(); n++)
{
for (std::size_t i=0;i{
tuple = generator(lattice[n-1][i]);
// lattice[n][i] = std::get<0>(tuple);
// lattice[n][i+1] = std::get<1>(tuple);
for (std::size_t j=0;j<=tuple.size()-1; ++j)
{
lattice[n][i+j] = tuple[j];
}
}
}
}
Here we see the emergence of the lattice type as a template parameter.
Answer the following questions:

a) Create the function to perform backward induction based on the above code style.

b) Test the newforward and backward induction algorithms by pricing plain options using both binomial and trinomial lattices.

c) Extend the code to accommodate early-exercise features.

d) We suspect that the use of tuples will be less efficient than the use of arrays. We may need to re-engineer the code. Do you agree?

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: