0

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:

enter image description here

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)?

lakshmen
  • 1,005
  • Can you write out the exact formulation of the optimization problem? It should look something like $\min_x f(x) ,, \mathrm{s.t.} ,, g(x) \lt 0$, or something similar. – AnonSubmitter85 Feb 07 '14 at 19:19
  • how do you do that? I am not sure how to do that? I need the cell InterestTree{1}(1,2) = actualvalue(in this case:93.7129). I don't want to minimize but want it to be equal. Need some guidance on this.. – lakshmen Feb 07 '14 at 19:43
  • If you want them to be equal, then you can minimize their absolute difference. For instance, the trivial problem $\min_x \vert y-x \vert$ has the solution $x=y$. – AnonSubmitter85 Feb 07 '14 at 19:59
  • I will write the code for the optimization and post it tmr and reply when I am done.Can help to check whether it is right? – lakshmen Feb 07 '14 at 21:02
  • @AnonSubmitter85 I have written the function for the interestratetree.. How do u optimize the function that u have written? Need some guidance on that. – lakshmen Feb 07 '14 at 22:00

1 Answers1

1

I think you're close. I am assuming that your goal is to find the values of InterestTree such that some function of it is equal to actualValue. (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 InterestTree and let $f(\mathbf{x})$ be the function that returns the value InterestTree{1}(1,2). Lastly, let $\beta$ be actualValue. 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 diff to

diff = (InterestTree{1}(1,2) - actualValue)^2;

To 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 ITree function so that InterestTree is a vector and not a cell array. Once you have done that, define a function handle that you can pass to the optimizer:

fhand = @(x)ITree(x, alpha, actualValue, volatility);

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 (x in the above) and get the cost function with fhand(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 are

x_optimal = fminunc(fhand, x0);

or

x_optimal = fminsearch(fhand, x0);

There are other optimizaers, but start with these and see if they will solve this problem.

  • First of all, thanks for the help. I have rewritten the code using the vectors. Now it is working, but the accuracy is not good. In excel, I get 6.56 whereas in matlab, i get 6.54. how to improve this? – lakshmen Feb 09 '14 at 12:27
  • Another qn how do I output the values for Interest and DiscountedValue as well? it just outputs optimal value for alpha... – lakshmen Feb 09 '14 at 12:34
  • Code is edited by the way. – lakshmen Feb 09 '14 at 12:44
  • @lakesh Check the value of the exit code from the optimizer to see why it stopped. See the documentation if you don't know what I am talking about. You can specify a value for the termination tolerance to get a more accurate solution if the optimizer is correctly solving the problem. See the optimoptions() and optimset() functions for details on the available options. You can also try fminsearch rather than fminunc and see if that improves the answer. Also, read through the examples provided with Matlab, as they should help you understand how to best use the tools. – AnonSubmitter85 Feb 09 '14 at 19:26
  • @lakesh It's kind of a hack to get other variables out of the optimizer. One options is to print them to file or to the terminal. Another option is to use global variables, which will let you write their values to a location you can read them from afterwards. – AnonSubmitter85 Feb 09 '14 at 19:28
  • moreover, the value goes to infinity sometimes. it is weird. I tried using x_optimal = fminbnd(fhand,0,10); where i restrict the values of x to be positive and between 0 to 10. but i still get huge numbers... – lakshmen Feb 09 '14 at 19:34
  • @lakesh Sorry, but I am not familiar enough with the problem to know about its stability. Some cost surfaces are very well behaved and will let you start anywhere and get the correct answer. Other problems have very unstable cost surfaces that require you to give a fairly accurate initial estimate in order to converge to the correct solution. You can try moving x0 closer/farther from the known solution and see what happens. It might also be a programming error on your part. I can't really say. – AnonSubmitter85 Feb 09 '14 at 19:43
  • why is it when i give the range for the variable, it still goes out of that range? I clearly state the range of 0 to 10 in fminbnd(fhand,0,10); – lakshmen Feb 09 '14 at 20:04
  • @lakesh That should be impossible. I'd guess that the error is on your part. Make sure that your function meets the requirements of fminbnd(). – AnonSubmitter85 Feb 09 '14 at 20:19
  • Seriously need your help. not sure where the problem is... I can put the actual data in for you to try as well. – lakshmen Feb 09 '14 at 21:05
  • @lakesh I aim to help, but am a busy man and cannot promise anything. The best source of help for you will without out a doubt be the Matlab documentation and a search engine. Work through the examples and understand what it is you are trying to do and how the functions work. Know what type of minimization is being used and whether or not it is appropriate for your problem. Also, make sure that your problem is amenable to being solved through minimization in the first place -- not all problems are. If it's not, then all of this effort and time will have been in vain. – AnonSubmitter85 Feb 09 '14 at 23:21
  • I have solved it. Error on the coding part. Coding logic mistake. Thanks a lot and learned a lot on optimization. – lakshmen Feb 10 '14 at 09:14
  • @lakesh Glad to hear it. Best of luck to you. – AnonSubmitter85 Feb 10 '14 at 19:22