Passed
Push — master ( a639aa...cb526f )
by Jordan
02:41
created

Exponential   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 56
c 1
b 0
f 0
dl 0
loc 170
ccs 43
cts 43
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A rangeRandom() 0 18 6
A rangePdf() 0 19 3
A random() 0 12 1
A pdf() 0 22 2
A __construct() 0 13 2
A cdf() 0 24 2
1
<?php
2
3
namespace Samsara\Fermat\Values\Distribution;
4
5
use Samsara\Exceptions\UsageError\IntegrityConstraint;
6
use Samsara\Exceptions\UsageError\OptionalExit;
7
use Samsara\Fermat\Numbers;
8
use Samsara\Fermat\Types\Distribution;
9
use Samsara\Fermat\Provider\PolyfillProvider;
10
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface;
11
use Samsara\Fermat\Values\ImmutableDecimal;
12
13
class Exponential extends Distribution
14
{
15
16
    /**
17
     * @var ImmutableDecimal
18
     */
19
    private $lambda;
20
21
    /**
22
     * Exponential constructor.
23
     *
24
     * @param int|float|DecimalInterface $lambda This is the *rate parameter* not the *scale parameter*
25
     *
26
     * @throws IntegrityConstraint
27
     */
28 9
    public function __construct($lambda)
29
    {
30 9
        $lambda = Numbers::makeOrDont(Numbers::IMMUTABLE, $lambda);
31
32 9
        if (!$lambda->isPositive()) {
0 ignored issues
show
Bug introduced by
The method isPositive() does not exist on Samsara\Fermat\Types\Bas...Numbers\NumberInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Samsara\Fermat\Types\Base\Number. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        if (!$lambda->/** @scrutinizer ignore-call */ isPositive()) {
Loading history...
33 1
            throw new IntegrityConstraint(
34 1
                'Lambda must be positive',
35 1
                'Provide a positive lambda',
36 1
                'Exponential distributions work on time to occurrence; the mean time to occurrence (lambda) must be positive'
37
            );
38
        }
39
40 9
        $this->lambda = $lambda;
0 ignored issues
show
Documentation Bug introduced by
It seems like $lambda can also be of type Samsara\Fermat\Values\MutableDecimal. However, the property $lambda is declared as type Samsara\Fermat\Values\ImmutableDecimal. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
41 9
    }
42
43
    /**
44
     * @param int|float|DecimalInterface $x
45
     *
46
     * @return ImmutableDecimal
47
     * @throws IntegrityConstraint
48
     */
49 2
    public function cdf($x, int $scale = 10): ImmutableDecimal
50
    {
51
52 2
        $x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x);
53
        /** @var ImmutableDecimal $one */
54 2
        $one = Numbers::makeOne();
55
56 2
        if (!$x->isPositive()) {
57 1
            throw new IntegrityConstraint(
58 1
                'X must be positive',
59 1
                'Provide a positive x',
60 1
                'Exponential distributions work on time to occurrence; the time to occurrence (x) must be positive'
61
            );
62
        }
63
64 1
        $internalScale = $scale + 2;
65
66
        /** @var ImmutableDecimal $e */
67 1
        $e = Numbers::makeE($internalScale);
68
69
        /** @var ImmutableDecimal $cdf */
70 1
        $cdf = $one->subtract($e->pow($x->multiply($this->lambda)->multiply(-1)))->truncateToScale($scale);
71
72 1
        return $cdf;
73
74
    }
75
76
    /**
77
     * @param $x
78
     *
79
     * @return ImmutableDecimal
80
     * @throws IntegrityConstraint
81
     */
82 3
    public function pdf($x, int $scale = 10): ImmutableDecimal
83
    {
84
85 3
        $x = Numbers::makeOrDont(Numbers::IMMUTABLE, $x);
86
87 3
        if (!$x->isPositive()) {
88 1
            throw new IntegrityConstraint(
89 1
                'X must be positive',
90 1
                'Provide a positive x',
91 1
                'Exponential distributions work on time to occurrence; the time to occurrence (x) must be positive'
92
            );
93
        }
94
95 2
        $internalScale = $scale + 2;
96
97
        /** @var ImmutableDecimal $e */
98 2
        $e = Numbers::makeE($internalScale);
99
100
        /** @var ImmutableDecimal $pdf */
101 2
        $pdf = $this->lambda->multiply($e->pow($this->lambda->multiply(-1)->multiply($x)))->truncateToScale(10);
102
103 2
        return $pdf;
104
105
    }
106
107
    /**
108
     * @param $x1
109
     * @param $x2
110
     *
111
     * @return ImmutableDecimal
112
     * @throws IntegrityConstraint
113
     */
114 4
    public function rangePdf($x1, $x2, int $scale = 10): ImmutableDecimal
115
    {
116 4
        $x1 = Numbers::makeOrDont(Numbers::IMMUTABLE, $x1);
117 4
        $x2 = Numbers::makeOrDont(Numbers::IMMUTABLE, $x2);
118
119 4
        if (!$x1->isPositive() || !$x2->isPositive()) {
120 3
            throw new IntegrityConstraint(
121 3
                'X must be positive',
122 3
                'Provide a positive x',
123 3
                'Exponential distributions work on time to occurrence; the time to occurrence (x) must be positive'
124
            );
125
        }
126
127 1
        $internalScale = $scale + 2;
128
129
        /** @var ImmutableDecimal $rangePdf */
130 1
        $rangePdf = $this->pdf($x2, $internalScale)->subtract($this->pdf($x1, $internalScale))->abs()->truncateToScale($scale);
131
132 1
        return $rangePdf;
133
    }
134
135
    /**
136
     * @return ImmutableDecimal
137
     *
138
     * @codeCoverageIgnore
139
     */
140
    public function random(): ImmutableDecimal
141
    {
142
143
        $randomInt = PolyfillProvider::randomInt(0, PHP_INT_MAX);
144
        $one = Numbers::makeOne();
145
        $u = Numbers::make(Numbers::IMMUTABLE, $randomInt, 20);
146
        $u = $u->divide(PHP_INT_MAX);
147
148
        /** @var ImmutableDecimal $random */
149
        $random = $one->subtract($u)->ln()->divide($this->lambda->multiply(-1));
150
151
        return $random;
152
153
    }
154
155
    /**
156
     * @param int|float|DecimalInterface $min
157
     * @param int|float|DecimalInterface $max
158
     * @param int $maxIterations
159
     *
160
     * @return ImmutableDecimal
161
     * @throws OptionalExit
162
     *
163
     * @codeCoverageIgnore
164
     */
165
    public function rangeRandom($min = 0, $max = PHP_INT_MAX, int $maxIterations = 20): ImmutableDecimal
166
    {
167
168
        $i = 0;
169
170
        do {
171
            $randomNumber = $this->random();
172
            $i++;
173
        } while (($randomNumber->isGreaterThan($max) || $randomNumber->isLessThan($min)) && $i < $maxIterations);
174
175
        if ($randomNumber->isGreaterThan($max) || $randomNumber->isLessThan($min)) {
176
            throw new OptionalExit(
177
                'All random numbers generated were outside of the requested range',
178
                'A suitable random number, restricted by the $max ('.$max.') and $min ('.$min.'), could not be found within '.$maxIterations.' iterations'
0 ignored issues
show
Bug introduced by
Are you sure $max of type Samsara\Fermat\Types\Bas...nterface|double|integer can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

178
                'A suitable random number, restricted by the $max ('./** @scrutinizer ignore-type */ $max.') and $min ('.$min.'), could not be found within '.$maxIterations.' iterations'
Loading history...
Bug introduced by
Are you sure $min of type Samsara\Fermat\Types\Bas...nterface|double|integer can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

178
                'A suitable random number, restricted by the $max ('.$max.') and $min ('./** @scrutinizer ignore-type */ $min.'), could not be found within '.$maxIterations.' iterations'
Loading history...
179
            );
180
        }
181
182
        return $randomNumber;
183
    }
184
185
}