void testTortureExecute (void) { #if 0 va1(4,pts[0],pts[1],pts[2],pts[3]); va2(4,ipts[0],ipts[1],ipts[2],ipts[3]); return; #endif }
void va1(int* x, char* y, ...) { va_list argp; va_start(argp, y); va2(x, y, &argp); va_end(argp); }
void test02(const Array<T>& x) { CIMValue va(x); CIMValue va2(va); CIMValue va3; va3 = va2; // Create a null constructor CIMType type = va.getType(); // get the type of v CIMValue va4(type,true); CIMValue va5; va5 = va4; #ifdef IO if (verbose) { cout << "\n----------------------\n"; XmlWriter::printValueElement(va3, cout); } #endif try { Array<T> t; va3.get(t); PEGASUS_TEST_ASSERT(t == x); PEGASUS_TEST_ASSERT (va3.typeCompatible(va2)); PEGASUS_TEST_ASSERT (!va.isNull()); PEGASUS_TEST_ASSERT (va.isArray()); PEGASUS_TEST_ASSERT (!va2.isNull()); PEGASUS_TEST_ASSERT (va2.isArray()); PEGASUS_TEST_ASSERT (!va3.isNull()); PEGASUS_TEST_ASSERT (va3.isArray()); // Note that this test depends on what is built. Everything has // 2 entries. PEGASUS_TEST_ASSERT (va.getArraySize() == 3); // Confirm that va4 (and va5) is Null, and array and zero length PEGASUS_TEST_ASSERT (va4.isNull()); PEGASUS_TEST_ASSERT (va4.isArray()); PEGASUS_TEST_ASSERT (va4.getArraySize() == 0); PEGASUS_TEST_ASSERT (va4.typeCompatible(va)); PEGASUS_TEST_ASSERT (va5.isNull()); PEGASUS_TEST_ASSERT (va5.isArray()); PEGASUS_TEST_ASSERT (va5.getArraySize() == 0); PEGASUS_TEST_ASSERT (va5.typeCompatible(va)); // Test toMof Buffer mofOutput; MofWriter::appendValueElement(mofOutput, va); // Test toXml Buffer out; XmlWriter::appendValueElement(out, va); XmlWriter::appendValueElement(out, va); // Test toString String valueString = va.toString(); #ifdef IO if (verbose) { cout << "MOF = [" << mofOutput.getData() << "]" << endl; cout << "toString Output [" << valueString << "]" << endl; } #endif // There should be no exceptions to this point so the // catch simply terminates. } catch(Exception& e) { cerr << "Error: " << e.getMessage() << endl; exit(1); } // Test the Null Characteristics try { // Set the initial one to Null va.setNullValue(va.getType(), true, 0); PEGASUS_TEST_ASSERT(va.isNull()); PEGASUS_TEST_ASSERT(va.isArray()); PEGASUS_TEST_ASSERT(va.getArraySize() == 0); va.setNullValue(va.getType(), false); PEGASUS_TEST_ASSERT(va.isNull()); PEGASUS_TEST_ASSERT(!va.isArray()); // get the String and XML outputs for v String valueString2 = va.toString(); Buffer xmlBuffer; XmlWriter::appendValueElement(xmlBuffer, va); Buffer mofOutput2; MofWriter::appendValueElement(mofOutput2, va); #ifdef IO if (verbose) { cout << "MOF NULL = [" << mofOutput2.getData() << "]" << endl; cout << "toString NULL Output [" << valueString2 << "]" << endl; cout << " XML NULL = [" << xmlBuffer.getData() << "]" << endl; } #endif va.clear(); PEGASUS_TEST_ASSERT(va.isNull()); } catch(Exception& e) { cerr << "Error: " << e.getMessage() << endl; exit(1); } }
void BinomialVanillaEngine<T>::calculate() const { DayCounter rfdc = process_->riskFreeRate()->dayCounter(); DayCounter divdc = process_->dividendYield()->dayCounter(); DayCounter voldc = process_->blackVolatility()->dayCounter(); Calendar volcal = process_->blackVolatility()->calendar(); Real s0 = process_->stateVariable()->value(); QL_REQUIRE(s0 > 0.0, "negative or null underlying given"); Volatility v = process_->blackVolatility()->blackVol( arguments_.exercise->lastDate(), s0); Date maturityDate = arguments_.exercise->lastDate(); Rate r = process_->riskFreeRate()->zeroRate(maturityDate, rfdc, Continuous, NoFrequency); Rate q = process_->dividendYield()->zeroRate(maturityDate, divdc, Continuous, NoFrequency); Date referenceDate = process_->riskFreeRate()->referenceDate(); // binomial trees with constant coefficient Handle<YieldTermStructure> flatRiskFree( boost::shared_ptr<YieldTermStructure>( new FlatForward(referenceDate, r, rfdc))); Handle<YieldTermStructure> flatDividends( boost::shared_ptr<YieldTermStructure>( new FlatForward(referenceDate, q, divdc))); Handle<BlackVolTermStructure> flatVol( boost::shared_ptr<BlackVolTermStructure>( new BlackConstantVol(referenceDate, volcal, v, voldc))); boost::shared_ptr<PlainVanillaPayoff> payoff = boost::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff); QL_REQUIRE(payoff, "non-plain payoff given"); Time maturity = rfdc.yearFraction(referenceDate, maturityDate); boost::shared_ptr<StochasticProcess1D> bs( new GeneralizedBlackScholesProcess( process_->stateVariable(), flatDividends, flatRiskFree, flatVol)); TimeGrid grid(maturity, timeSteps_); boost::shared_ptr<T> tree(new T(bs, maturity, timeSteps_, payoff->strike())); boost::shared_ptr<BlackScholesLattice<T> > lattice( new BlackScholesLattice<T>(tree, r, maturity, timeSteps_)); DiscretizedVanillaOption option(arguments_, *process_, grid); option.initialize(lattice, maturity); // Partial derivatives calculated from various points in the // binomial tree (Odegaard) // Rollback to third-last step, and get underlying price (s2) & // option values (p2) at this point option.rollback(grid[2]); Array va2(option.values()); QL_ENSURE(va2.size() == 3, "Expect 3 nodes in grid at second step"); Real p2h = va2[2]; // high-price Real s2 = lattice->underlying(2, 2); // high price // Rollback to second-last step, and get option value (p1) at // this point option.rollback(grid[1]); Array va(option.values()); QL_ENSURE(va.size() == 2, "Expect 2 nodes in grid at first step"); Real p1 = va[1]; // Finally, rollback to t=0 option.rollback(0.0); Real p0 = option.presentValue(); Real s1 = lattice->underlying(1, 1); // Calculate partial derivatives Real delta0 = (p1-p0)/(s1-s0); // dp/ds Real delta1 = (p2h-p1)/(s2-s1); // dp/ds // Store results results_.value = p0; results_.delta = delta0; results_.gamma = 2.0*(delta1-delta0)/(s2-s0); //d(delta)/ds results_.theta = blackScholesTheta(process_, results_.value, results_.delta, results_.gamma); }
void ShapeFunctionTriangleSigned::calc() { // based on the answer in http://answers.unity3d.com/questions/383804/calculate-uv-coordinates-of-3d-point-on-plane-of-m.html // check out also: http://www.had2know.com/academics/triangle-area-perimeter-angle-3-coordinates.html lmx::Vector<double> f(dim), p1(dim), p2(dim), p3(dim); f.writeElement(gp->getX(),0); f.writeElement(gp->getY(),1); f.writeElement(gp->getZ(),2); p1.writeElement(gp->getSupportNodes()[0]->getX(), 0); p1.writeElement(gp->getSupportNodes()[0]->getY(), 1); p1.writeElement(gp->getSupportNodes()[0]->getZ(), 2); p2.writeElement(gp->getSupportNodes()[1]->getX(), 0); p1.writeElement(gp->getSupportNodes()[1]->getY(), 1); p1.writeElement(gp->getSupportNodes()[1]->getZ(), 2); p3.writeElement(gp->getSupportNodes()[2]->getX(), 0); p1.writeElement(gp->getSupportNodes()[2]->getY(), 1); p1.writeElement(gp->getSupportNodes()[2]->getZ(), 2); lmx::Vector<double> f1(dim), f2(dim), f3(dim); // calculate vectors from point f to vertices p1, p2 and p3: f1.subs( p1, f); f2.subs( p2, f); f3.subs( p3, f); lmx::Vector<double> va(dim), va1(dim), va2(dim), va3(dim); double a, a1, a2, a3; va.multElements(p1-p2, p1-p3); va1.multElements(f2, f3); va2.multElements(f3, f1); va3.multElements(f1, f2); lmx::Vector<double> vaa1(dim), vaa2(dim), vaa3(dim); a = va.norm2(); a1 = std::copysign( va1.norm2()/a, va*va1 ); a2 = std::copysign( va2.norm2()/a, va*va2 ); a3 = std::copysign( va3.norm2()/a, va*va3 ); phi.writeElement( a1, 0, 0 ); phi.writeElement( a2, 0, 0 ); phi.writeElement( a3, 0, 0 ); ////////////////////////////////////////////////////////////////// // FIRST DERIVATIVES: ////////////////////////////////////////////////////////////////// // phi.writeElement( // ( gp->supportNodes[1]->gety() - gp->supportNodes[2]->gety() ) // / ( 2*gp->jacobian ), 1, 0 ); // phi.writeElement( // ( gp->supportNodes[2]->getx() - gp->supportNodes[1]->getx() ) // / ( 2*gp->jacobian ), 2, 0 ); // ////////////////////////////////////////////////////////////////// // phi.writeElement( // ( gp->supportNodes[2]->gety() - gp->supportNodes[0]->gety() ) // / ( 2*gp->jacobian ), 1, 1 ); // phi.writeElement( // ( gp->supportNodes[0]->getx() - gp->supportNodes[2]->getx() ) // / ( 2*gp->jacobian ), 2, 1 ); // ////////////////////////////////////////////////////////////////// // phi.writeElement( // ( gp->supportNodes[0]->gety() - gp->supportNodes[1]->gety() ) // / ( 2*gp->jacobian ), 1, 2 ); // phi.writeElement( // ( gp->supportNodes[1]->getx() - gp->supportNodes[0]->getx() ) // / ( 2*gp->jacobian ), 2, 2 ); // cout << "phi = " << phi << endl; }
void BinomialBarrierEngine<T,D>::calculate() const { DayCounter rfdc = process_->riskFreeRate()->dayCounter(); DayCounter divdc = process_->dividendYield()->dayCounter(); DayCounter voldc = process_->blackVolatility()->dayCounter(); Calendar volcal = process_->blackVolatility()->calendar(); Real s0 = process_->stateVariable()->value(); QL_REQUIRE(s0 > 0.0, "negative or null underlying given"); Volatility v = process_->blackVolatility()->blackVol( arguments_.exercise->lastDate(), s0); Date maturityDate = arguments_.exercise->lastDate(); Rate r = process_->riskFreeRate()->zeroRate(maturityDate, rfdc, Continuous, NoFrequency); Rate q = process_->dividendYield()->zeroRate(maturityDate, divdc, Continuous, NoFrequency); Date referenceDate = process_->riskFreeRate()->referenceDate(); // binomial trees with constant coefficient Handle<YieldTermStructure> flatRiskFree( boost::shared_ptr<YieldTermStructure>( new FlatForward(referenceDate, r, rfdc))); Handle<YieldTermStructure> flatDividends( boost::shared_ptr<YieldTermStructure>( new FlatForward(referenceDate, q, divdc))); Handle<BlackVolTermStructure> flatVol( boost::shared_ptr<BlackVolTermStructure>( new BlackConstantVol(referenceDate, volcal, v, voldc))); boost::shared_ptr<StrikedTypePayoff> payoff = boost::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff); QL_REQUIRE(payoff, "non-striked payoff given"); Time maturity = rfdc.yearFraction(referenceDate, maturityDate); boost::shared_ptr<StochasticProcess1D> bs( new GeneralizedBlackScholesProcess( process_->stateVariable(), flatDividends, flatRiskFree, flatVol)); // correct timesteps to ensure a (local) minimum, using Boyle and Lau // approach. See Journal of Derivatives, 1/1994, // "Bumping up against the barrier with the binomial method" // Note: this approach works only for CoxRossRubinstein lattices, so // is disabled if T is not a CoxRossRubinstein or derived from it. Size optimum_steps = timeSteps_; if (boost::is_base_of<CoxRossRubinstein, T>::value && maxTimeSteps_ > timeSteps_ && s0 > 0 && arguments_.barrier > 0) { Real divisor; if (s0 > arguments_.barrier) divisor = std::pow(std::log(s0 / arguments_.barrier), 2); else divisor = std::pow(std::log(arguments_.barrier / s0), 2); if (!close(divisor,0)) { for (Size i=1; i < timeSteps_ ; ++i) { Size optimum = Size(( i*i * v*v * maturity) / divisor); if (timeSteps_ < optimum) { optimum_steps = optimum; break; // found first minimum with iterations>=timesteps } } } if (optimum_steps > maxTimeSteps_) optimum_steps = maxTimeSteps_; // too high, limit } TimeGrid grid(maturity, optimum_steps); boost::shared_ptr<T> tree(new T(bs, maturity, optimum_steps, payoff->strike())); boost::shared_ptr<BlackScholesLattice<T> > lattice( new BlackScholesLattice<T>(tree, r, maturity, optimum_steps)); D option(arguments_, *process_, grid); option.initialize(lattice, maturity); // Partial derivatives calculated from various points in the // binomial tree // (see J.C.Hull, "Options, Futures and other derivatives", 6th edition, pp 397/398) // Rollback to third-last step, and get underlying prices (s2) & // option values (p2) at this point option.rollback(grid[2]); Array va2(option.values()); QL_ENSURE(va2.size() == 3, "Expect 3 nodes in grid at second step"); Real p2u = va2[2]; // up Real p2m = va2[1]; // mid Real p2d = va2[0]; // down (low) Real s2u = lattice->underlying(2, 2); // up price Real s2m = lattice->underlying(2, 1); // middle price Real s2d = lattice->underlying(2, 0); // down (low) price // calculate gamma by taking the first derivate of the two deltas Real delta2u = (p2u - p2m)/(s2u-s2m); Real delta2d = (p2m-p2d)/(s2m-s2d); Real gamma = (delta2u - delta2d) / ((s2u-s2d)/2); // Rollback to second-last step, and get option values (p1) at // this point option.rollback(grid[1]); Array va(option.values()); QL_ENSURE(va.size() == 2, "Expect 2 nodes in grid at first step"); Real p1u = va[1]; Real p1d = va[0]; Real s1u = lattice->underlying(1, 1); // up (high) price Real s1d = lattice->underlying(1, 0); // down (low) price Real delta = (p1u - p1d) / (s1u - s1d); // Finally, rollback to t=0 option.rollback(0.0); Real p0 = option.presentValue(); // Store results results_.value = p0; results_.delta = delta; results_.gamma = gamma; // theta can be approximated by calculating the numerical derivative // between mid value at third-last step and at t0. The underlying price // is the same, only time varies. results_.theta = (p2m - p0) / grid[2]; }