Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Greater than problem, please help

Subject: Greater than problem, please help

From: Orhan Unal

Date: 21 Apr, 2012 20:26:08

Message: 1 of 5

Hi all,

I've got a big problem. You see I'm creating a code for American binary option pricing based on the binomial trees method. You don't need to know how this works or anything, I just need you to assist me with a part of my code that's not doing what it's supposed to be doing. The part I'm stuck on involves creating a column vector called tree which holds numeric values. K is a number known as the strike price and is used in the for loop and then Tree is recalculated, this time holding values that are ones or zeros. The following line

Tree = S0*up.^((steps:-1:0)').*down.^((0:steps)')

produces the following results

Tree =

  147.3273
  121.4127
  100.0565
   82.4567
   67.9528
   56.0000
   46.1497
   38.0321
   31.3423
   25.8293
   21.2859

Then the code

for i=1:steps+1
if K-Tree > 0
    Tree(i) = 1;
else
    Tree(i) = 0;
end
end

Tree

produces the following results

Tree =

     0
     0
     0
     0
     0
     1
     1
     1
     1
     1
     1

This works fine for all values that AREN'T K=56. But you see, when K=56 notice how it corresponds to the value 56 in the first Tree results; When computing K-Tree, 56-56 should return 0 in the second Tree results in that position. However, I get the value 1 and it's seriously confusing me because I don't know how to fix it, I've tried so many things out and nothing seems to work. Please help me, thanks.

Subject: Greater than problem, please help

From: dpb

Date: 21 Apr, 2012 20:46:18

Message: 2 of 5

On 4/21/2012 3:26 PM, Orhan Unal wrote:
...

> ... The following line
>
> Tree = S0*up.^((steps:-1:0)').*down.^((0:steps)')
>
> produces the following results
>
> Tree =
>
...
> 82.4567
> 67.9528
> 56.0000
> 46.1497
...

>
> Then the code
>
> for i=1:steps+1
> if K-Tree > 0
> Tree(i) = 1;
> else
> Tree(i) = 0;
> end
> end

This isn't your problem (keep reading), but...

The above "the Matlab way" (tm) is just

Tree(K>Tree)=1;

...

> This works fine for all values that AREN'T K=56. But you see, when K=56
> notice how it corresponds to the value 56 in the first Tree results;
> When computing K-Tree, 56-56 should return 0 in the second Tree results
> in that position. However, I get the value 1 and it's seriously
> confusing me because I don't know how to fix it, I've tried so many
> things out and nothing seems to work. Please help me, thanks.

Try the following at the command line and I think you'll see the reason
for the "problem".

num2str(Tree,15)

The value that appears to be identically 56 will, indeed show up to be
just slightly off. The reason is it was computed w/ a floating point
expression whereas the integer value you're comparing to to is, indeed,
exact.

You'll need a tolerance on the comparison if it's critical that the
decision point be there; of course, the actual internally represented
value that was calculated wasn't _quite_ there so that's a question of
what should you do w/ the decision at other breakpoints.

Or, perhaps if this is pricing you should round to the nearest
fractional cent that's traded; I don't know the answer to that.

Look at the following link to get the gist of what's going on in more
detail...

<http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F>

--

Subject: Greater than problem, please help

From: ImageAnalyst

Date: 21 Apr, 2012 20:56:34

Message: 3 of 5

It's because it's not 56 exactly, and I do mean "EXACTLY" out to 64
bits precision.

Please read and understand this FAQ entry:
http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

Subject: Greater than problem, please help

From: Roger Stafford

Date: 21 Apr, 2012 22:47:37

Message: 4 of 5

"Orhan Unal" <unalo@aston.ac.uk> wrote in message <jmv550$mi9$1@newscl01ah.mathworks.com>...
> ........
> for i=1:steps+1
> if K-Tree > 0
> Tree(i) = 1;
> else
> Tree(i) = 0;
> end
> end
> .......
- - - - - - - - - -
  There is a quite serious error in your code that hasn't been pointed out to you yet. When you write:

 if K-Tree > 0
    Tree(i) = 1;
 else
    Tree(i) = 0;
 end

with the eleven-element logical vector "K-Tree > 0", the 'if' condition in matlab is not considered to be true unless all elements of that vector are true. In your case of descending sizes it didn't become true for all elements until you had replaced enough of the earlier, larger values with zeros in the for-loop. I am sure this is not what you had in mind. You should have written:

 if K-Tree(i) > 0
    Tree(i) = 1;
 else
    Tree(i) = 0;
 end

so that the logical quantity "K-Tree(i) > 0" is only a single scalar quantity (presumably the one you really had in mind.)

  I would recommend experimenting with logical vectors in 'if' statements to verify that what I have said here is true.

  (It might have been better if Mathworks had decided to respond to such questionable syntax with an error message. The results from this mistake can be very seriously in error.)

Roger Stafford

Subject: Greater than problem, please help

From: ImageAnalyst

Date: 22 Apr, 2012 03:40:14

Message: 5 of 5

Good catch Roger. And of course the entire program could have been
written in one line like this:

Tree = K > Tree;

But since they don't recommend changing data types (from double to
logical, for speed purposes) it would be better yet to do this:

logicalTree = K > Tree;

or if you wanted an integer you could do this:

integerTree = int32(K > Tree);

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us