/**
  * Returns the log-log interpolated or extrapolated y-values using the
  * supplied x- and y-value arrays.
  *
  * @param xs x-values of some function
  * @param ys y-values of some function
  * @param x value at which to find y
  * @return the log-log interpolated y-values
  */
 public static function findLogLogYArrays($xs, $ys, $x)
 {
     for ($i = 0; $i < count($x); $i++) {
         $y[] = RTGM_Util::findLogLogY($xs, $ys, $x[$i]);
     }
     return $y;
 }
 /**
  * Maked the internal iterative RTGM calculation.
  *
  * @throws {Exception}
  *      if internal RTGM calculation exceeds the maximum iterations
  */
 public function calculate()
 {
     $this->_resetJsonStructure();
     // uniform hazard ground motion
     $uhgm = RTGM_Util::findLogLogX($this->hazCurve->xs, $this->hazCurve->ys, $this->afe4uhgm);
     $this->jsonStructure['uhgm'] = $uhgm;
     $this->rtgmIters = array();
     $this->riskIters = array();
     // For adequate discretization of fragility curves...
     $upsampHazCurve = $this->logResample($this->hazCurve, $uhgm);
     $this->jsonStructure['upsampledHazardCurve'] = $upsampHazCurve;
     $this->jsonStructure['originalHCMin'] = min($this->hazCurve->xs);
     $this->jsonStructure['originalHCMax'] = max($this->hazCurve->xs);
     $errorRatio = NAN;
     // Iterative calculation of RTGM
     for ($i = 0; $i < self::MAX_ITERATIONS; $i++) {
         $this->_newJsonIteration();
         $rtgmTmp;
         $riskTmp;
         if ($i == 0) {
             $rtgmTmp = $uhgm;
         } else {
             if ($i == 1) {
                 $rtgmTmp = $this->rtgmIters[0] * $errorRatio;
             } else {
                 $rtgmTmp = RTGM_Util::findLogLogY($this->riskIters, $this->rtgmIters, $this->target_risk);
             }
         }
         // Generate fragility curve corresponding to current guess for RTGM
         $fc = new FragilityCurve($rtgmTmp, $upsampHazCurve, $this->beta);
         $this->_pushJsonIteration('pdf', $fc->getPdf()->ys);
         $this->_pushJsonIteration('cdf', $fc->getCdf()->ys);
         /* Calculate risk using fragility curve generated above & upsampled
            hazard curve. */
         $riskTmp = $this->riskIntegral($fc->getPdf(), $upsampHazCurve);
         // Check risk calculated above against target risk
         $errorRatio = $this->checkRiskAgainstTarget($riskTmp);
         $this->riskIters[] = $riskTmp;
         $this->rtgmIters[] = $rtgmTmp;
         $this->_pushJsonIteration('rtgm', $rtgmTmp);
         // Exit if ratio of calculated and target risks is within tolerance
         if ($errorRatio == 1) {
             break;
         }
         // If number of iterations has reached specified maximum, exit loop
         if ($i == self::MAX_ITERATIONS) {
             throw new Exception("RTGM: max # iterations reached: " . self::MAX_ITERATIONS);
         }
     }
     if ($errorRatio != 1) {
         $this->rtgm = NAN;
     } else {
         $this->rtgm = $this->rtgmIters[count($this->rtgmIters) - 1];
     }
     $this->jsonStructure['rtgm'] = $this->rtgm;
     $this->riskCoeff = $this->rtgm / $uhgm;
     $this->jsonStructure['riskCoefficient'] = $this->riskCoeff;
     for ($i = 0; $i < count($this->riskIters); $i++) {
         $this->riskIters[$i] = $this->riskIters[$i] / $this->target_risk;
     }
 }
 $rtgmTmp = 24.000128833913;
 $riskTmp = 0.009838107787837501;
 $target_risk = -log(1 - 0.01) / 50;
 $uhgm = 0.6753857172769;
 $usampling_factor = 1.05;
 $rtgm = new RTGM($xs, $ys, Frequency::SA_1P00, 0.8);
 // RGTM_Util method tests
 $a = array(1.0, 3.0, 5.0, 7.0, 8.0, 9.0, 10.0);
 notify('Binary search found', 2, RTGM_Util::binary_search($a, 5.0));
 notify('Binary search not found', -4, RTGM_Util::binary_search($a, 6.0));
 notify('Build Sequence', 178, count(RTGM_Util::buildSequence(log($min_sa), log($xs[count($xs) - 1]), log($usampling_factor), true)));
 notify('Build Sequence Inf/NaN', 0, count(RTGM_Util::buildSequence(INF, NAN, 1, true)));
 $a2 = array(1, 2, 3);
 notify('Exp', array(2.718281828459, 7.3890560989307, 20.085536923188), RTGM_Util::exp($a2));
 notify('Find Log Log X', $uhgm, RTGM_Util::findLogLogX($xs, $ys, $afe4uhgm));
 notify('Find Log Log Y', $afe4uhgm, RTGM_Util::findLogLogY($xs, $ys, $uhgm));
 notify('Log Normal Cumulative Prob', 0.012893887361471, RTGM_Util::logNormalCumProb(1.3, 1.6, 0.6));
 notify('Log Normal Cumulative Prob (0)', 0.0, RTGM_Util::logNormalCumProb(0.0, 1.6, 0.6));
 notify('Log Normal Density', 0.042613954514149, RTGM_Util::logNormalDensity(1.3, 1.6, 0.6));
 notify('Log Normal Density (0)', 0.0, RTGM_Util::logNormalDensity(0.0, 1.6, 0.6));
 $a3 = array(1, 2, 3);
 notify('Multiply', array(3, 8, 15), RTGM_Util::multiply($a3, array(3, 4, 5)));
 $a4 = array(10, 20, 30);
 notify('Scale', array(1, 2, 3), RTGM_Util::scale($a4, 0.1));
 notify('Trapz', $riskTmp, RTGM_Util::trapz($xs, $ys));
 // Statistics method tests
 notify('ErfInv', 0.47693627620447, Statistics::erfInv(0.5));
 notify('InverseCumulativeProbabilty', 0.67448975019608, Statistics::inverseCumulativeProbability(0.75));
 notify('InverseCumulativeProbabilty (3 args)', 2.0046938501176, Statistics::inverseCumulativeProbability(0.75, 1.6, 0.6));
 try {
     Statistics::inverseCumulativeProbability(2.5);