double EuroBewerter::max_call(double t, double T, double* X0, int D, double Strike, double r, double delta, double sigma) { T = T - t; double summe = 0; for (int d = 0; d < D; ++d) { double d_minus = (log(X0[d] / Strike) + (r - delta - sigma * sigma / 2.) * T) / (sigma * sqrt(T)); double d_plus = d_minus + sigma * sqrt(T); Polynom ganz; double eins[1] = { 1. }; ganz.set(eins, 1); double I; double max = -100000; double min = 10000000; for (int dd = 0; dd < D; ++dd) if (dd != d) { // printf("\nd_plus %f, d_minus %f,", d_plus, d_minus); // printf("verschiebefaktor %f\n", // sigma * sqrt(T) // + log(X0[d] / X0[dd]) / (sigma * sqrt(T))); // double pp[18] = { 0.5, 0.3960985, 0, -0.061485, 0, 0.007456, 0, // -5.84946E-4, 0, 2.920034E-5, 0, -9.15823E-7, 0, // 1.740319E-8, 0, -1.826093E-10, 0, 8.10495E-13 }; double pp[10] = { 0.50000000000009, 0.38567951086190133, 0, -0.05010672697589501, 0, 0.004103597701237448, 0, -1.631010612321749E-4, 0, 2.4428290978202304E-6 }; Polynom p; p.set(pp, 10); double v = sigma * sqrt(T) + log(X0[d] / X0[dd]) / (sigma * sqrt(T)); max = v > max ? v : max; min = v < min ? v : min; p.verschieben(v); // printf("I innen%f\n",integralExpQ(&p, maxi(-d_plus, -5. + min), 5. - max)); ganz.multiply_with(&p); } I = integralExpQ(&ganz, maxi(-d_plus, -5. + min), 5. - max); // printf("I aussen%f\n", I); summe += X0[d] * exp(-delta * T) / sqrt(2 * 3.141592654) * I; } double prod = 1; for (int d = 0; d < D; ++d) { double d_minus = (log(X0[d] / Strike) + (r - delta - sigma * sigma / 2.) * T) / (sigma * sqrt(T)); prod *= (1 - (1 - cnd(-d_minus))); } double zu = -Strike * exp(-r * T) + Strike * exp(-r * T) * prod; double ergebnis = (summe + zu) * exp(-r * t); //return erf(0.1); double e1 = 0; for (int d = 0; d < D; ++d) e1 = maxi(e1, call(t, T, X0[d], Strike, r, delta, sigma)); return maxi(ergebnis, e1); }
int RDividePolyByDouble(Polynom* _pPoly, Double* _pDouble, Polynom** _pPolyOut) { bool bComplex1 = _pPoly->isComplex(); bool bComplex2 = _pDouble->isComplex(); bool bScalar1 = _pPoly->getRows() == 1 && _pPoly->getCols() == 1; bool bScalar2 = _pDouble->getRows() == 1 && _pDouble->getCols() == 1; Polynom *pTemp = NULL; //use only if _pPoly is scalar and _pDouble not. int iRowResult = 0; int iColResult = 0; int *piRank = NULL; /* if(bScalar1 && bScalar2) { iRowResult = 1; iColResult = 1; piRank = new int[1]; piRank[0] = _pPoly->get(0)->getRank(); } else */ if (bScalar1 == false && bScalar2 == false) { // call overload return 0; } if (bScalar2) { double dblDivR = _pDouble->get(0); double dblDivI = _pDouble->getImg(0); (*_pPolyOut) = _pPoly->clone()->getAs<Polynom>(); if (_pDouble->isComplex()) { (*_pPolyOut)->setComplex(true); } for (int i = 0 ; i < _pPoly->getSize() ; i++) { bool bComplex1 = _pPoly->isComplex(); bool bComplex2 = _pDouble->isComplex(); SinglePoly* pC = (*_pPolyOut)->get(i); if (bComplex1 == false && bComplex2 == false) { iRightDivisionRealMatrixByRealMatrix(pC->get(), 1, &dblDivR, 0, pC->get(), 1, pC->getSize()); } else if (bComplex1 == true && bComplex2 == false) { iRightDivisionComplexMatrixByRealMatrix(pC->get(), pC->getImg(), 1, &dblDivR, 0, pC->get(), pC->getImg(), 1, pC->getSize()); } else if (bComplex1 == false && bComplex2 == true) { iRightDivisionRealMatrixByComplexMatrix(pC->get(), 1, &dblDivR, &dblDivI, 0, pC->get(), pC->getImg(), 1, pC->getSize()); } else if (bComplex1 == true && bComplex2 == true) { iRightDivisionComplexMatrixByComplexMatrix(pC->get(), pC->getImg(), 1, &dblDivR, &dblDivI, 0, pC->get(), pC->getImg(), 1, pC->getSize()); } } return 0; } if (bScalar1) { //in this case, we have to create a temporary square polinomial matrix iRowResult = _pDouble->getCols(); iColResult = _pDouble->getRows(); piRank = new int[iRowResult * iRowResult]; int iMaxRank = _pPoly->getMaxRank(); for (int i = 0 ; i < iRowResult * iRowResult ; i++) { piRank[i] = iMaxRank; } pTemp = new Polynom(_pPoly->getVariableName(), iRowResult, iRowResult, piRank); if (bComplex1 || bComplex2) { pTemp->setComplex(true); } SinglePoly *pdblData = _pPoly->get(0); for (int i = 0 ; i < iRowResult ; i++) { pTemp->set(i, i, pdblData); } } (*_pPolyOut) = new Polynom(_pPoly->getVariableName(), iRowResult, iColResult, piRank); delete[] piRank; if (bComplex1 || bComplex2) { (*_pPolyOut)->setComplex(true); } if (bScalar2) { //[p] * cst for (int i = 0 ; i < _pPoly->getSize() ; i++) { SinglePoly *pPolyIn = _pPoly->get(i); double* pRealIn = pPolyIn->get(); double* pImgIn = pPolyIn->getImg(); SinglePoly *pPolyOut = (*_pPolyOut)->get(i); double* pRealOut = pPolyOut->get(); double* pImgOut = pPolyOut->getImg(); if (bComplex1 == false && bComplex2 == false) { iRightDivisionRealMatrixByRealMatrix( pRealIn, 1, _pDouble->getReal(), 0, pRealOut, 1, pPolyOut->getSize()); } else if (bComplex1 == false && bComplex2 == true) { iRightDivisionRealMatrixByComplexMatrix( pRealIn, 1, _pDouble->getReal(), _pDouble->getImg(), 0, pRealOut, pImgOut, 1, pPolyOut->getSize()); } else if (bComplex1 == true && bComplex2 == false) { iRightDivisionComplexMatrixByRealMatrix( pRealIn, pImgIn, 1, _pDouble->getReal(), 0, pRealOut, pImgOut, 1, pPolyOut->getSize()); } else if (bComplex1 == true && bComplex2 == true) { iRightDivisionComplexMatrixByComplexMatrix( pRealIn, pImgIn, 1, _pDouble->getReal(), _pDouble->getImg(), 0, pRealOut, pImgOut, 1, pPolyOut->getSize()); } } } else if (bScalar1) { for (int i = 0 ; i < pTemp->get(0)->getSize() ; i++) { Double *pCoef = pTemp->extractCoef(i); Double *pResultCoef = new Double(iRowResult, iColResult, pCoef->isComplex()); double *pReal = pResultCoef->getReal(); double *pImg = pResultCoef->getImg(); if (bComplex1 == false && bComplex2 == false) { double dblRcond = 0; iRightDivisionOfRealMatrix( pCoef->getReal(), iRowResult, iRowResult, _pDouble->getReal(), _pDouble->getRows(), _pDouble->getCols(), pReal, iRowResult, iColResult, &dblRcond); } else { double dblRcond = 0; iRightDivisionOfComplexMatrix( pCoef->getReal(), pCoef->getImg(), iRowResult, iRowResult, _pDouble->getReal(), _pDouble->getImg(), _pDouble->getRows(), _pDouble->getCols(), pReal, pImg, iRowResult, iColResult, &dblRcond); } (*_pPolyOut)->insertCoef(i, pResultCoef); delete pCoef; delete pResultCoef; } } return 0; }