User talk:Oleg.roshka

Trinomial option pricing in Java
Java implementation illustrating american option (see option style article) pricing based on the trinomial expansion. Steps: 1: Declare and initialise S(0) 2: Calculate the jump sizes u; d and m 3: Calculate the transition probabilities pu; pm and pd 4: Build the share price tree 5: Calculate the payoff of the option at maturity, i.e node N 6: Calculate the payoff of the option at the remaining time-steps, starting from N-1 for (int j = numberOfTimeSteps-1; j >= 0; j--) { n = j * 2 + 1 for (int i = 0; i < n; i++) option price at node i = upMove ^ (j-i) option payoff at node i = exp(-r*t) *(pu * Cn+1;j+1 + pm * C[n+1,j] + pd * C[n+1,j-1]) option price = max(option payoff at node i, tree price at node i)      end for end for 7: return option price

Java code
public class TrinomialOptionPricer { /** * Price option using trinomial expansion. *   * @param timeToExpiration * @param spotPrice * @param strikePrice * @param rate * @param volatility * @param dividendYield * @param numberOfTimeSteps * @param isCall * @return */ public double price (  double timeToExpiration,   double spotPrice,   double strikePrice,   double rate,   double volatility,   double dividendYield,   int numberOfTimeSteps,   boolean isCall) { if( numberOfTimeSteps <=0 ) { throw new IllegalArgumentException("Wrong tree size, can't be less or equal 0."); } double timeStep = timeToExpiration / numberOfTimeSteps; double upperBorder = volatility * volatility / ((rate - dividendYield) *      (rate - dividendYield)); if ( timeStep >= upperBorder) { throw new IllegalStateException("Condition on the time step " +    "has not been satisfied (p not in [0..1]). " +      " timeStep=" + timeStep + ", upperBorder=" + upperBorder); } double upMove = Math.exp(volatility * Math.pow(2 * timeStep, 0.5d)); int numberOfTreeLeaves = 2 * numberOfTimeSteps + 1; double[] values = calculatePayoffAtMaturity(   spotPrice,     strikePrice,    numberOfTimeSteps,    numberOfTreeLeaves,    isCall,     upMove); //move to earlier time steps double halfTimeStep = timeStep / 2d; double halfTimeStepDiscountedMove = Math.exp(   rate * halfTimeStep +     volatility * Math.pow(halfTimeStep, 0.5d)   ); double sqrtOfPu = (halfTimeStepDiscountedMove - 1) / (upMove - 1d); double pu = sqrtOfPu * sqrtOfPu; //probabilityOfTheUpMove double sqrtOfPd = (upMove - halfTimeStepDiscountedMove) / (upMove - 1); double pd = sqrtOfPd * sqrtOfPd; //probabilityOfTheDownMove double pm = 1d - pu - pd; //probabilityOfTheLevelMove double discountFactor = Math.exp(-rate * timeStep); double discountedPu = pu * discountFactor; double discountedPd = pd * discountFactor; double discountedPm = pm * discountFactor; double exerciseValue = 0d; for (int j = numberOfTimeSteps - 1; j >= 0 ; j--) { int numberOfTreeLeavesOnThisStep = j * 2 + 1; for (int i = 0; i < numberOfTreeLeavesOnThisStep; i++) { values[i] = discountedPu * values[i] + discountedPm * values[i+1] + discountedPd * values[i+2]; double price = spotPrice * Math.pow(upMove, j - i); if( isCall ) { exerciseValue = price - strikePrice; } else { exerciseValue = strikePrice - price; }   if (values[i] < exerciseValue) { values[i] = exerciseValue; }   }  }  return values[0]; } /** * Calculates possible payoffs at maturity. Package-visible for testing. *  * @param timeToExpiration * @param spotPrice * @param strikePrice * @param rate * @param volatility * @param dividentYield * @param numberOfTimeSteps * @param isCall * @return */ public double[] calculatePayoffsAtMaturity (  double timeToExpiration,   double spotPrice,   double strikePrice,   double rate,   double volatility,   double dividentYield,   int numberOfTimeSteps,   boolean isCall) { if( numberOfTimeSteps <=0 ) { throw new IllegalArgumentException(    "Wrong number of the time steps, can't be less or equal 0."); } double timeStep = timeToExpiration / numberOfTimeSteps; double upperBorder = volatility * volatility / ((rate - dividentYield) *      (rate - dividentYield)); if ( timeStep >= upperBorder) { throw new IllegalStateException("Condition on the time step " +    "has not been satisfied (p not in [0..1]). " +      " timeStep=" + timeStep + ", upperBorder=" + upperBorder); } double upMove = Math.exp(volatility * Math.pow(2 * timeStep, 0.5d)); int numberOfTreeLeaves = 2 * numberOfTimeSteps + 1; double[] values = calculatePayoffAtMaturity(   spotPrice,     strikePrice,    numberOfTimeSteps,    numberOfTreeLeaves,    isCall,     upMove); return values; } private double[] calculatePayoffAtMaturity(  double spotPrice,   double strikePrice,    int numberOfTimeSteps,   int numberOfTreeLeaves,   boolean isCall,   double upMove) { double[] values = new double[numberOfTreeLeaves]; //initial values at time time to exp. for (int i = 0; i < numberOfTreeLeaves; i++) { double priceAtMaturity = spotPrice * Math.pow(upMove, numberOfTimeSteps - i); double value; if( isCall ) { value = priceAtMaturity - strikePrice; } else { value = strikePrice - priceAtMaturity; }  if (value < 0d) { values[i] = 0d; } else { values[i] = value; } }  return values; } }

Nomination of Trinomial option pricing in Java for deletion
A discussion is taking place as to whether the article Trinomial option pricing in Java is suitable for inclusion in Wikipedia according to Wikipedia's policies and guidelines or whether it should be deleted.

The article will be discussed at Articles for deletion/Trinomial option pricing in Java until a consensus is reached, and anyone is welcome to contribute to the discussion. The nomination will explain the policies and guidelines which are of concern. The discussion focuses on high-quality evidence and our policies and guidelines.

Users may edit the article during the discussion, including to improve the article to address concerns raised in the discussion. However, do not remove the article-for-deletion notice from the top of the article. - MrX 02:35, 16 March 2013 (UTC)