Пример #1
0
 /**
  * Guassian distribution density
  *
  * @param int $x        	
  * @return float
  */
 protected function gaussianDensity($x)
 {
     $y = -(1 / 2) * pow($x / $this->deviation, 2);
     $y = exp($y);
     $y = 1 / sqrt(2 * pi()) * $y;
     return $y;
 }
Пример #2
0
 public function inverse($p)
 {
     $Y = $p->x - $this->x0;
     $X = $p->y - $this->y0;
     $rotI = $Y / $this->R;
     $rotB = 2 * (atan(exp(X / $this->R)) - Sourcemap_Proj::PI / 4.0);
     $b = asin(cos($this->b0) * sin($rotB) + sin($this->b0) * cos($rotB) * cos($rotI));
     $I = atan(sin($rotI) / (cos($this->b0) * cos($rotI) - sin($this->b0) * tan($rotB)));
     $lambda = $this->lambda0 + $I / $this->alpha;
     $S = 0.0;
     $phy = $b;
     $prevPhy = -1000.0;
     $iteration = 0;
     while (abs($phy - $prevPhy) > 1.0E-7) {
         if (++$iteration > 20) {
             throw new Exception("Infinity...");
         }
         //S = log(tan(Sourcemap_Proj::PI / 4.0 + $phy / 2.0));
         $S = 1.0 / $this->alpha * (log(tan(Sourcemap_Proj::PI / 4.0 + $b / 2.0)) - $this->K) + $this->e * log(tan(Sourcemap_Proj::PI / 4.0 + asin($this->e * sin($phy)) / 2.0));
         $prevPhy = $phy;
         $phy = 2.0 * atan(exp($S)) - Sourcemap_Proj::PI / 2.0;
     }
     $p->x = $lambda;
     $p->y = $phy;
     return $p;
 }
 /**
  * Computes the humidex index.
  *
  * @param   integer  $t      Temperature in celcius.
  * @param   integer  $dew    Dew point temperature in celcius.
  * @return  float   The computed humidex.
  *
  * @since    2.0.0
  */
 private function get_humidex($t, $dew)
 {
     $dew = $dew + 273.15;
     $e = 6.11 * exp(5417.753 * (1 / 273.16 - 1 / $dew));
     $result = $t + 0.5555 * ($e - 10);
     return round($result);
 }
function sa($members)
{
    $T = 100.0;
    $now_temp = 10000;
    $n = count($members);
    $status = 0;
    $best = 1000000000;
    while ($T > 0) {
        do {
            $t1 = rand(0, $n - 1);
            $t2 = rand(0, $n - 1);
        } while ($t1 == $t2);
        $now = sqr(abs($members[$t1]['chosen1'] - $t1)) + sqr(abs($members[$t2]['chosen1'] - $t2));
        $new = sqr(abs($members[$t1]['chosen1'] - $t2)) + sqr(abs($members[$t2]['chosen1'] - $t1));
        if ($new < $now || lcg_value() <= exp(($now - $new) / $T)) {
            $temp = $members[$t1];
            $members[$t1] = $members[$t2];
            $members[$t2] = $temp;
            $status += $new - $now;
            if ($status < $best) {
                $res = $members;
            }
        }
        $T -= 0.02;
    }
    return $res;
}
Пример #5
0
 /**
  * @param float $a
  * @param float $b
  *
  * @return float
  */
 public function compute($a, $b)
 {
     $score = 2 * Product::scalar($a, $b);
     $squares = Product::scalar($a, $a) + Product::scalar($b, $b);
     $result = exp(-$this->gamma * ($squares - $score));
     return $result;
 }
 public function MetersToLatLon($mx, $my)
 {
     $lon = $mx / $this->originShift * 180.0;
     $lat = $my / $this->originShift * 180.0;
     $lat = 180 / pi() * (2 * atan(exp($lat * pi() / 180.0)) - pi() / 2.0);
     return array($lat, $lon);
 }
Пример #7
0
 /**
  * Poisson distribution - probability mass function
  * A discrete probability distribution that expresses the probability of a given number of events
  * occurring in a fixed interval of time and/or space if these events occur with a known average
  * rate and independently of the time since the last event.
  * https://en.wikipedia.org/wiki/Poisson_distribution
  *
  *                              λᵏℯ^⁻λ
  * P(k events in an interval) = ------
  *                                k!
  *
  * @param  int   $k events in the interval
  * @param  float $λ average number of successful events per interval
  *
  * @return float The Poisson probability of observing k successful events in an interval
  */
 public static function PMF(int $k, float $λ) : float
 {
     Support::checkLimits(self::LIMITS, ['k' => $k, 'λ' => $λ]);
     $λᵏℯ^−λ = pow($λ, $k) * exp(-$λ);
     $k! = Combinatorics::factorial($k);
     return $λᵏℯ^−λ / $k!;
 }
 public function getIntersect($dp = 0)
 {
     if ($dp != 0) {
         return round(exp($this->_intersect), $dp);
     }
     return exp($this->_intersect);
 }
 /**
  * @throws RegressionException
  */
 public function calculate()
 {
     if ($this->sourceSequence === null) {
         throw new RegressionException('The input sequence is not set');
     }
     if (count($this->sourceSequence) < $this->dimension) {
         throw new RegressionException(sprintf('The dimension of the sequence of at least %s', $this->dimension));
     }
     foreach ($this->sourceSequence as $k => $v) {
         if ($v[1] !== null) {
             $this->sumIndex[0] += $v[0];
             $this->sumIndex[1] += $v[1];
             $this->sumIndex[2] += $v[0] * $v[0] * $v[1];
             $this->sumIndex[3] += $v[1] * log($v[1]);
             $this->sumIndex[4] += $v[0] * $v[1] * log($v[1]);
             $this->sumIndex[5] += $v[0] * $v[1];
         }
     }
     $denominator = $this->sumIndex[1] * $this->sumIndex[2] - $this->sumIndex[5] * $this->sumIndex[5];
     $A = exp(($this->sumIndex[2] * $this->sumIndex[3] - $this->sumIndex[5] * $this->sumIndex[4]) / $denominator);
     $B = ($this->sumIndex[1] * $this->sumIndex[4] - $this->sumIndex[5] * $this->sumIndex[3]) / $denominator;
     foreach ($this->sourceSequence as $i => $val) {
         $coordinate = [$val[0], $A * exp($B * $val[0])];
         $this->resultSequence[] = $coordinate;
     }
     $this->equation = sprintf('y = %s+ e^(%sx)', round($A, 2), round($B, 2));
     $this->push();
 }
Пример #10
0
function relh($tmpc, $dwpc)
{
    $e = 6.112 * exp(17.67 * (double) $dwpc / ((double) $dwpc + 243.5));
    $es = 6.112 * exp(17.67 * (double) $tmpc / ((double) $tmpc + 243.5));
    $relh = $e / $es * 100.0;
    return round($relh, 0);
}
 static function metersToLatLon($mx, $my)
 {
     $lng = $mx / self::originShift() * 180.0;
     $lat = $my / self::originShift() * 180.0;
     $lat = 180 / pi() * (2 * atan(exp($lat * pi() / 180.0)) - pi() / 2.0);
     return new GoogleMapPoint($lat, $lng);
 }
Пример #12
0
 public function classify($string)
 {
     if ($this->total_samples === 0) {
         return array();
     }
     $tokens = $this->tokenizer->tokenize($string);
     $total_score = 0;
     $scores = array();
     foreach ($this->subjects as $subject => $subject_data) {
         $subject_data['prior_value'] = log($subject_data['count_samples'] / $this->total_samples);
         $this->subjects[$subject] = $subject_data;
         $scores[$subject] = 0;
         foreach ($tokens as $token) {
             $count = isset($this->tokens[$token][$subject]) ? $this->tokens[$token][$subject] : 0;
             $scores[$subject] += log(($count + 1) / ($subject_data['count_tokens'] + $this->total_tokens));
         }
         $scores[$subject] = $subject_data['prior_value'] + $scores[$subject];
         $total_score += $scores[$subject];
     }
     $min = min($scores);
     $sum = 0;
     foreach ($scores as $subject => $score) {
         $scores[$subject] = exp($score - $min);
         $sum += $scores[$subject];
     }
     $total = 1 / $sum;
     foreach ($scores as $subject => $score) {
         $scores[$subject] = $score * $total;
     }
     arsort($scores);
     $max = max($scores);
     $maxs = array_search(max($scores), $scores);
     return $maxs;
 }
Пример #13
0
 public function evaluate()
 {
     if (is_array($result = parent::evaluate())) {
         throw new ParsingException('exp accept only one argument');
     }
     return (double) exp($result);
 }
Пример #14
0
 /**
  * @throws RegressionException
  */
 public function calculate()
 {
     if ($this->sourceSequence === null) {
         throw new RegressionException('The input sequence is not set');
     }
     if (count($this->sourceSequence) < $this->dimension) {
         throw new RegressionException(sprintf('The dimension of the sequence of at least %s', $this->dimension));
     }
     $k = 0;
     foreach ($this->sourceSequence as $k => $v) {
         if ($v[1] !== null) {
             $this->sumIndex[0] += log($v[0]);
             $this->sumIndex[1] += log($v[0]) * log($v[1]);
             $this->sumIndex[2] += log($v[1]);
             $this->sumIndex[3] += pow(log($v[0]), 2);
         }
     }
     $k += 1;
     $B = ($k * $this->sumIndex[1] - $this->sumIndex[2] * $this->sumIndex[0]) / ($k * $this->sumIndex[3] - $this->sumIndex[0] * $this->sumIndex[0]);
     $A = exp(($this->sumIndex[2] - $B * $this->sumIndex[0]) / $k);
     foreach ($this->sourceSequence as $i => $val) {
         $coordinate = [$val[0], $A * pow($val[0], $B)];
         $this->resultSequence[] = $coordinate;
     }
     $this->equation = sprintf('y = %s + x^%s', round($A, 2), round($B, 2));
     $this->push();
 }
 /**
  * @param array $data
  *
  * @return int|void
  */
 public function pValue(array $data)
 {
     $n = count($data);
     if ($n < 5) {
         throw new TooLessDataException();
     }
     $data = $this->normalize($data);
     sort($data, SORT_NUMERIC);
     $nd = new NormalDistribution();
     $s = 0.0;
     foreach ($data as $i => $v) {
         $cdf = $nd->normalCdf($v);
         $i++;
         $s += (2.0 * $i - 1.0) * log($cdf) + (2 * ($n - $i) + 1) * log(1 - $cdf);
     }
     $a2 = -$n - 1 / $n * $s;
     $a2 = $a2 * (1 + 0.75 / $n + 2.25 / pow($n, 2));
     if ($a2 >= 0.6) {
         return exp(1.2937 - 5.709 * $a2 + 0.0186 * pow($a2, 2));
     }
     if (0.34 < $a2 and $a2 < 0.6) {
         return exp(0.9177 - 4.279 * $a2 - 1.38 * pow($a2, 2));
     }
     if (0.2 < $a2 and $a2 < 0.34) {
         return 1 - exp(-8.318 + 42.796 * $a2 - 59.938 * pow($a2, 2));
     }
     if ($a2 <= 0.2) {
         return 1 - exp(-13.436 + 101.14 * $a2 - 223.73 * pow($a2, 2));
     }
 }
Пример #16
0
function expm1($x) {
    if (abs($x) < 1e-5) {
        return $x + 0.5*$x*$x;
    } else {
        return exp($x) - 1.0;
    }
}
Пример #17
0
 /**
  * Compute the denominators which is the predicted expectation of
  * each feature given a set of weights L and a set of features for
  * each document for each class.
  * 
  * @param $feature_array All the data known about the training set
  * @param $l The current set of weights to be initialized
  * @return void
  */
 protected function prepareFprime(array &$feature_array, array &$l)
 {
     $this->denominators = array();
     foreach ($feature_array as $offset => $doc) {
         $numerator = array_fill_keys(array_keys($doc), 0.0);
         $denominator = 0.0;
         foreach ($doc as $cl => $f) {
             if (!is_array($f)) {
                 continue;
             }
             $tmp = 0.0;
             foreach ($f as $i) {
                 $tmp += $l[$i];
             }
             $tmp = exp($tmp);
             $numerator[$cl] += $tmp;
             $denominator += $tmp;
         }
         foreach ($doc as $class => $features) {
             if (!is_array($features)) {
                 continue;
             }
             foreach ($features as $fi) {
                 if (!isset($this->denominators[$fi])) {
                     $this->denominators[$fi] = 0;
                 }
                 $this->denominators[$fi] += $numerator[$class] / $denominator;
             }
         }
     }
 }
Пример #18
0
 public function __construct($ltDays = 42, $stDays = 7)
 {
     $this->ltDays = $ltDays;
     $this->stDays = $stDays;
     $this->ltExp = exp(-1 / $this->ltDays);
     $this->stExp = exp(-1 / $this->stDays);
 }
Пример #19
0
 public function key($key)
 {
     if ($this->numeric($key)) {
         $this->pushValue(floatval($key));
     } else {
         if ($key == "+") {
             $this->pushValue($this->pop() + $this->pop());
         } else {
             if ($key == "-") {
                 $t = $this->pop();
                 $this->push($this->pop() - $t);
             } else {
                 if ($key == "*") {
                     $this->pushValue($this->pop() * $this->pop());
                 } else {
                     if ($key == "enter") {
                         $this->push();
                     } else {
                         if ($key == "clx") {
                             $this->r[0] = 0;
                         } else {
                             if ($key == "/") {
                                 $t = $this->pop();
                                 if ($t != 0) {
                                     $this->pushValue($this->pop() / $t);
                                 }
                             } else {
                                 if ($key == "x^y") {
                                     $this->pushValue(exp(log($this->pop()) * $this->pop()));
                                 } else {
                                     if ($key == "clr") {
                                         $this->r[0] = 0;
                                         $this->r[1] = 0;
                                         $this->r[2] = 0;
                                         $this->r[3] = 0;
                                     } else {
                                         if ($key == "chs") {
                                             $this->r[0] = -$this->r[0];
                                         } else {
                                             if ($key == "sin") {
                                                 $this->pushValue(sin(deg2rad($this->pop())));
                                             } else {
                                                 if ($key == "chs") {
                                                     $this->r[0] = -$this->r[0];
                                                 } else {
                                                     throw new Exception("can't do key: " . $key);
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Пример #20
0
function cmpByRatingLeech(&$a, &$b)
{
    $aPeer = max($a["totalLeech"] + $a["totalSeed"], $a['sum_peers']);
    $bPeer = max($b["totalLeech"] + $b["totalSeed"], $b['sum_peers']);
    $a['sortVal'] = exp(-$aPeer / 5000.0) * exp($a['kinopoiskRating']);
    $b['sortVal'] = exp(-$bPeer / 5000.0) * exp($b['kinopoiskRating']);
    return $a["sortVal"] < $b["sortVal"];
}
Пример #21
0
 public function fromPointToLatLon(Coord2D $point)
 {
     $origin = $this->_pixelOrigin;
     $lng = ($point->x - $origin->x) / $this->_pixelsPerLonDegree;
     $latRadians = ($point->y - $origin->y) / -$this->_pixelsPerLonRadian;
     $lat = $this->radiansToDegrees(2 * atan(exp($latRadians)) - pi() / 2);
     return new GeoPoint($lat, $lng);
 }
Пример #22
0
 /**
  * Gets the probaibiliy using the pmf function of a certain outcome
  * @param float $x Outcome
  * @return float
  * @throws UnexpectedValueException if x is not an int
  */
 public function get_probability($x)
 {
     if (is_int($x)) {
         return gmp_fact($this->n) / (gmp_fact($x) * gmp_fact($this->n - $x)) * exp($this->p, $x) * exp(1 - $this->p, $this->n - $x);
     } else {
         throw new UnexpectedValueException('UnexpectedValueException occured because BionomailDistribution is not continous so x cannot be a non-integer');
     }
 }
Пример #23
0
 public function get_probability($x)
 {
     if (is_numeric($x)) {
         return exp($this->lambda, $x) * exp(M_E, $this->lambda * -1) / gmp_fact($x);
     } else {
         throw new UnexpectedValueException();
     }
 }
Пример #24
0
 function fromPixelToLL($x, $y, $zoom)
 {
     $e = $this->zc[$zoom];
     $f = ($x - $e) / $this->Bc[$zoom];
     $g = ($y - $e) / -$this->Cc[$zoom];
     $h = 180.0 / M_PI * (2 * atan(exp($g)) - 0.5 * M_PI);
     return array($f, $h);
 }
Пример #25
0
 /**
  * Cumulative distribution function
  *
  * f(x;λ) = 1 − ℯ^⁻λx  x ≥ 0
  *        = 0          x < 0
  *
  * @param float $λ often called the rate parameter
  * @param float $x the random variable
  *
  * @return float
  */
 public static function CDF(float $x, float $λ) : float
 {
     if ($x < 0) {
         return 0;
     }
     Support::checkLimits(self::LIMITS, ['x' => $x, 'λ' => $λ]);
     return 1 - exp(-$λ * $x);
 }
Пример #26
0
function createResultArray($ro, $steps)
{
    $resultArray = array();
    for ($k = 0; $k < $steps; $k++) {
        $resultArray[] = pow($ro, $k) * exp(-$ro) / factorial($k);
    }
    return $resultArray;
}
Пример #27
0
 private function computeBrevityPenalty($referenceLength, $translationLength)
 {
     if ($translationLength <= $referenceLength) {
         return exp(1 - $referenceLength / $translationLength);
     } else {
         return 1;
     }
 }
Пример #28
0
 /**
 * 		<table border="1">
     <tr><td><strong>Humidex Range</strong></td><td><strong>Degree of Comfort</strong></td></tr>
     <tr><td>20-29</td><td style="color: #4CD900;">Comfortable</td></tr>
     <tr><td>30-39</td><td style="color: #FFD800;">Some Discomfort</td></tr>
     <tr><td>40-45</td><td style="color: #FF6A00;">Great Discomfort</td></tr>
     <tr><td>>45</td><td style="color: #FF0000;">Dangerous</td></tr>
     </table>
 *
 * @return float
 */
 public function calc() : float
 {
     $dewpointK = $this->getDewPoint() + 273.15;
     $e = 6.11 * exp(5417.753 * (1 / 273.16 - 1 / $dewpointK));
     $h = 0.5555 * ($e - 10.0);
     $humidex = $this->getTemp() + $h;
     return $humidex;
 }
Пример #29
0
 /**
  * Laplace distribution - cumulative distribution function
  * From -∞ to x (lower CDF)
  * https://en.wikipedia.org/wiki/Laplace_distribution
  *
  *        1     / x - μ \
  * F(x) = - exp|  ------ |       if x < μ
  *        2     \   b   /
  *
  *            1     /  x - μ \
  * F(x) = 1 - - exp| - ------ |  if x ≥ μ
  *            2     \    b   /
  *
  * @param  number $x
  * @param  number $μ location parameter
  * @param  number $b scale parameter (diversity)  b > 0
  *
  * @return  float
  */
 public static function CDF($x, $μ, $b) : float
 {
     Support::checkLimits(self::LIMITS, ['x' => $x, 'μ' => $μ, 'b' => $b]);
     if ($x < $μ) {
         return 1 / 2 * exp(($x - $μ) / $b);
     }
     return 1 - 1 / 2 * exp(-($x - $μ) / $b);
 }
Пример #30
0
function neyron(array $w, array $x)
{
    $result = 0;
    foreach ($w as $key => $value) {
        $result += $w[$key] * $x[$key];
    }
    return 1 / (1 + exp(-$result));
}