I would like to calibrate a interest rate tree using the optimization tool in matlab. Need some guidance on doing it.
The interest rate tree looks like this:

How it works:
3.73% = 2.5%*exp(2*0.2)
96.40453 = (0.5*100 + 0.5*100)/(1+3.73%)
94.15801 = (0.5*96.40453+ 0.5*97.56098)/(1+2.50%)
The value of 2.5% is arbitrary and the upper node is obtained by multiplying with an exponential of 2*volatility(here it is 20%).
I need to optimize the problem by varying different values for the lower node.
How do I do this optimization in Matlab?
What I have tried so far?
InterestTree{1}(1,1) = 0.03;
size = size(InterestTree,2);
InterestTree{size-1}(1,size-1)= 2.5/100;
InterestTree{size}(2,:) = 100;
InterestTree{size-1}(1,size-2)= (2.5*exp(2*0.2))/100;
InterestTree{size-1}(2,size-1)=(0.5*InterestTree{size}(2,size)+0.5*InterestTree{size}(2,size-1))/(1+InterestTree{size-1}(1,size-1));
j = size-2;
InterestTree{size-1}(2,size-2)=(0.5*InterestTree{size}(2,j+1)+0.5*InterestTree{size}(2,j))/(1+InterestTree{size-1}(1,j));
InterestTree{size-2}(2,size-2)=(0.5*InterestTree{size-1}(2,j+1)+0.5*InterestTree{size-1}(2,j))/(1+InterestTree{size-2}(1,j));
New Edit:
function [ diff ] = InterestTreeComputation(Interest,DiscountedValue,alpha)
total= size(Interest,1);
n = ceil(0.5*(-1+sqrt(1+8*total)));
DiscountedValue(end:-1:(end-n+1)) = 100;
m=0;
for i=total-n:-1:total-2*n+2
Interest(i)= (alpha*exp(2^(m)*0.005))/100;
m = m+1;
end
for j= total-n:-1:1
columnnumber =ceil(0.5*(-1+sqrt(1+8*j)));
DiscountedValue(j) = (0.5*DiscountedValue(j+columnnumber)+0.5*DiscountedValue(j+columnnumber+1))/(1+Interest(j));
end
Data = xlsread('InterestData.xlsx');
ActualValue = Data(:,4);
diff = (DiscountedValue(1) - ActualValue(n-1))^2;
end
How I call it:
clear all;close all;clc;
Interest = [0.0954;0;0];
DiscountedValue =zeros(3,1);
Interest = [Interest; zeros(3,1)];
DiscountedValue = [DiscountedValue; zeros(3,1)];
fhand = @(x)InterestTreeComputation(Interest,DiscountedValue,x);
x0 = 2.5;
x_optimal = fminunc(fhand, x0);
How do you optimize this function? Is it like this => x = fminunc(@ITree,x0)?
I think you're close. I am assuming that your goal is to find the values of
InterestTreesuch that some function of it is equal toactualValue. (You really need to include this information in your question. Remember that I, or anybody else here, know nothing about the problem other than what you have written. You need to be explicit in what it is you are trying to do. You are much more likely to get effective help that way.)The first step is to define your cost function. Let $\mathbf{x}$ be
InterestTreeand let $f(\mathbf{x})$ be the function that returns the valueInterestTree{1}(1,2). Lastly, let $\beta$ beactualValue. We want to find $\mathbf{x}$ such that $f(\mathbf{x}) = \beta$. The error for any estimate of $\mathbf{x}$ will then be $\epsilon(\mathbf{x}) = \vert f(\mathbf{x}) - \beta \vert$. However, the absolute value function is not convex and convexity is a desirable property of cost functions (you can look this up or just trust me). Thus, we will usually want to use the squared error as the cost function. That is, we want to solve$$ \min_{\mathbf{x}} \vert f(\mathbf{x}) - \beta \vert^2. $$
So you'll want to change the value of
difftoTo solve this problem in Matlab requires that we write a function that will return the value of the cost function given a vector. Thus, you will need to rewrite your
ITreefunction so thatInterestTreeis a vector and not a cell array. Once you have done that, define a function handle that you can pass to the optimizer:We are only searching over
x, so that is the only variable that the optimizer needs to know about. The above function handle lets the optimizer estimate a value for the interest tree (xin the above) and get the cost function withfhand(x). Once you have defined this function handle, that last step is to choose an initial estimate of the vector that represents interest tree,x0, and carry out the optimization. Two built-in optimizers you can try areor
There are other optimizaers, but start with these and see if they will solve this problem.