You're correct that the (0, 50-price) is wrong. should just be
1 2 3
|
return max((50 - price),
(0.53*recursive_function(k+1,i+1,price*1.1)+
0.47*recursive_function(k+1,i-1,price/1.1)
|
So
50-price
is the a part in max and
1 2
|
(0.53*recursive_function(k+1,i+1,price*1.1)+
0.47*recursive_function(k+1,i-1,price/1.1))
|
is the b part of max.
You're right about i not making any difference in price. I am using i currently as more of a check to see what node k is referring to. When together, i and k give a nearly unique view of where on the binomial (or trinomial) tree you are for any given price.
So I understand your output for the recursive function but if I try to take it a step further and get what I need out of the function which is for the function to hold the average price of and I change the function to the following:
1 2 3 4 5 6 7 8 9 10 11
|
float recursive_function(int k, int i, float price, float avg_price){
if(k==10)
return max(0,50-avg_price)
else
return max((0, 50 - avg_price),
(0.53*recursive_function(k+1,i+1,price*1.1, (avg_price +price*1.1)/(k+1))+
0.47*recursive_function(k+1,i-1,price/1.1, (avg_price + price/1.1)/(k+1))
}
|
let's adjust the input to more reasonable numbers of
1 2
|
no_of_divisions = 3;
recursive_function(0,0,60,60)
|
Intuitively I thought that this would give me an output of:
1 2 3 4 5 6 7
|
recursive_function(0, 0,60,60 )
recursive_function(1, 1, 66,63)
recursive_function(2, 2, 72.6,66.2)
recursive_function(2, 0, 60, 62)
recursive_function(1, -1, 54.54, 57.27)
recursive_function(2, 0, 60, 58.18)
recursive_function(2, -2,49.58,54.07)
|
I do not get those values for the avg_price part when I setup the recursive_function how I have it in this post and that is where I am tripping up in my bigger, more complex code.
I'm sorry, I know I am throwing something completely different at you than I had in the earlier post but I think you just reconfirmed that my understanding of the functionality of the recursive code is correct but my implementation of the avg part is where I am getting tangled.
So as you can see with the avg_price added into the code I also hold onto the overall path information that it takes to get to any given value.
This is the part that is really killing me.
The only way I have been able to come close to having the average actually follow through the code is by doing a sum of the entire path and then dividing by (k+1) at the end but I believe this compromises the recursive functions integrity and is the reason for my mistakes. The way I have been trying to get the avg is as follows:
1 2 3 4 5 6 7 8 9 10 11
|
float recursive_function(int k, int i, float price,float total_price){
if(k==10)
return max(0,50-total_price/(k+1))
else
return max((0, 50 - total_price/(k+1)),
(0.53*recursive_function(k+1,i+1,price*1.1, total_price+price*1.1)+
0.47*recursive_function(k+1,i-1,total_price + price/1.1))
}
|
When I do it like that I get the correct avg_prices for the
return max(0,50-total_price/(k+1))
and
return max((0, 50 - total_price/(k+1))
BUT
1 2
|
(0.53*recursive_function(k+1,i+1,price*1.1, total_price+price*1.1)+
0.47*recursive_function(k+1,i-1,total_price + price/1.1))
|
because I feel like taking the avg outside of this function is ignoring the 0.53 and 0.47 multiplication. This analysis also makes sense when considering how close I am to getting the correct values in my other code. I am only a few decimal places off which suggests to me that it is this multiplication that is not being taken into account as it should be. (in the other code I have the multiplied numbers are like 0.501033 and 0.49...).
I do not believe that I am getting the correct values for the
Also a quick note****
your output has a small error, it should actually come out as:
1 2 3 4 5 6 7
|
recursive_function(8, 0, 4.0)
recursive_function(9, 1, 4.4)
recursive_function(10, 2, 4.84)
recursive_function(10, 0, 4.0)
recursive_function(9, -1, 3.63)
recursive_function(10, 0, 4.0)
recursive_function(10, -2, 3.30)
|
Thank you for your help!! I really appreciate it!