/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
 Copyright (C) 2005 StatPro Italia srl

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <http://quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/
#include "stdafx.h"
#include "TrinomialTree.hpp"

namespace QuantLib {

	//moved most of the initialization to new Rejeb tree class
	template <class T>
    NewTrinomialTree<T>::NewTrinomialTree(
                        const boost::shared_ptr<StochasticProcess1D>& process,
                        const TimeGrid& timeGrid,
                        bool isPositive)
    : Tree<T>(timeGrid.size()), dx_(1, 0.0), timeGrid_(timeGrid) {
        x0_ = process->x0();
	}

	//basically old Trinomial Tree class, nothing else added or changed
	RejebTree::RejebTree(const boost::shared_ptr<StochasticProcess1D>& process, const TimeGrid& timeGrid, 
		 bool isPositive) : NewTrinomialTree<RejebTree>(process, timeGrid, isPositive) {

						 Size nTimeSteps = timeGrid.size() - 1;
						 Integer jMin = 0;
						 Integer jMax = 0;

						 for (Size i=0; i<nTimeSteps; i++) {
							 Time t = timeGrid[i];
							 Time dt = timeGrid.dt(i);

							 //Variance must be independent of x
							 Real v2 = process->variance(t, 0.0, dt);
							 Volatility v = std::sqrt(v2);
							 dx_.push_back(v*std::sqrt(3.0));

							 Branching branching;
							 for (Integer j=jMin; j<=jMax; j++) {
								 Real x = x0_ + j*dx_[i];
								 Real m = process->expectation(t, x, dt);
								 Integer temp = Integer(std::floor((m-x0_)/dx_[i+1] + 0.5));

								 if (isPositive) {
									 while (x0_+(temp-1)*dx_[i+1]<=0) {
										 temp++;
									 }
								 }

								 Real e = m - (x0_ + temp*dx_[i+1]);
								 Real e2 = e*e;
								 Real e3 = e*std::sqrt(3.0);

								 Real p1 = (1.0 + e2/v2 - e3/v)/6.0;
								 Real p2 = (2.0 - e2/v2)/3.0;
								 Real p3 = (1.0 + e2/v2 + e3/v)/6.0;

								 branching.add(temp, p1, p2, p3);
							 }
							 branchings_.push_back(branching);

							 jMin = branching.jMin();
							 jMax = branching.jMax();
						 }
	}

	/*new Trinomial tree class. Calculates branch probabilities using the Kamrad Ritchken model,
	see Rubinstein, "On the Relation Between Binomial and Trinomial Option Pricing Models"
	*/
	KamradRitchken::KamradRitchken(const boost::shared_ptr<StochasticProcess1D>& process, const TimeGrid& timeGrid,
		Real lambda, bool isPositive) : NewTrinomialTree<KamradRitchken>(process, timeGrid, isPositive),
		lambda_(lambda) {

				 Size nTimeSteps = timeGrid.size() - 1;
				 Integer jMin = 0;
				 Integer jMax = 0;

				 for(Size i = 0; i < nTimeSteps; i++){
					 Time t = timeGrid[i];
					 Time dt = timeGrid.dt(i);

					 Real v2 = process->variance(t, 0.0, dt);
					 Volatility v = std::sqrt(v2);
					 dx_.push_back(v * lambda_);

					 Branching branching;
					 for (Integer j=jMin; j<=jMax; j++) {
						 Real x = x0_ + j*dx_[i];
						 Real m = process->expectation(t, x, dt);
						 Integer temp = Integer(std::floor((m-x0_)/dx_[i+1] + 0.5));

						 if (isPositive) {
							 while (x0_+(temp-1)*dx_[i+1]<=0) {
								 temp++;
							 }
						 }

						 Real driftPerStep_ = process->drift(0.0, x0_) * dt;

						 Real p1 = (1/(2*lambda*lambda)) + (1/(2*lambda))*(driftPerStep_/std::sqrt(v2));
						 Real p2 = 1 - (1/(lambda*lambda));
						 Real p3 = (1/(2*lambda*lambda)) - (1/(2*lambda))*(driftPerStep_/std::sqrt(v2));

						 branching.add(temp, p1, p2, p3);
					 }
					 branchings_.push_back(branching);

					 jMin = branching.jMin();
					 jMax = branching.jMax();
				 }
	}


}