Can someone please help me?? I keep getting a debug assertion failed message saying my vector subscript is out of range. Unfortunately i do not know where the problem lies
Here's my code for a trinomial tree to calculate a american put option:
//set size of the vectors at the end of the tree
S_M.resize(steps+1);
V_M_right.resize(steps+1);
V_M_left.resize(steps);
//calculate last step in the tree with final values and payoff at time of maturaty
for(int j=0;j<=steps;j++)
{
S_M[j]=stockprice(S0,u,d,j,steps,T,r,div);
V_M_right[j]=max(K-S_M[j],0.0);
}
//calculate for the last-1 step using the step after
for (int i=steps-1; i>=0; i--)
{
S_M.pop_back();
for(int j=0; j<=i; j++)
{
//stockprice at time t
S_M[j]=stockprice(S0,u,d,j,i,delta_t*i,r,div);
//value of the payoff of an american put, risk neutral based
V_M_left[j]=max(max(K-S_M[j],0.0),exp(-r*delta_t)*(p_u*V_M_right[j+1]+p_m*V_M_right[j] + p_d*V_M_right[j-1]));
}
V_M_right.pop_back();
V_M_right=V_M_left;
V_M_left.pop_back();
}
//save option value at t=0
V0[n]=V_M_right[0];
}