lakshmen
lakshmen

Reputation: 29064

Input of 0 returns a NaN in Matlab

Nelson-Siegel is a method used to model interest rates. More info on Nelson-Siegel:http://en.wikipedia.org/wiki/Fixed-income_attribution.

I have written a code like this:

function [ interest ] = Nelson_Siegel(s)
 beta0 = 0.0408; beta1 = -0.0396; beta2 = -0.0511; tau= 1.614;
 interest = beta0 + beta1*(tau/s)*(1-exp(-s/tau))+beta2*((tau/s)*(1-exp(-s/tau))-exp(-s/tau));
end

The problem is when I input a value of 0, it gives me a value of NaN. It should be equal to 0 instead. Not sure where it is going wrong.

Upvotes: 0

Views: 128

Answers (2)

Bob Gilmore
Bob Gilmore

Reputation: 13758

It's because of this term:

T = beta1*(tau/s)*(1-exp(-s/tau))

As s ⇒ 0, τ/s ⇒ ∞, and

1-exp(-s/τ) ⇒ 1-e⁰ = 1-1 = 0

Thus:

s ⇒ 0 ∴ T ⇒ β₁·∞·0 ⇒ NaN

since ∞·0 isn't a proper number.

Upvotes: 3

chappjc
chappjc

Reputation: 30579

A solution to your divide-by-zero problem could be to compare against eps, and consider that as a special case:

function [ interest ] = Nelson_Siegel(s)
  beta0 = ...
  if abs(s)<eps,
    interest = 0;
  else
    interest = beta0 + beta1*(tau/s)*...
  end
end

Upvotes: 2

Related Questions