Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
MarketShareEstimation | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
getRankFromMarketShare | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getMarketShareFromRank | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Business\Sales |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Business\Sales; |
16 | |
17 | /** |
18 | * Market share calculations (Zipf function) |
19 | * |
20 | * This class can be used to calculate the market share based on a rank or vice versa |
21 | * the rank based on a marketshare in a Zipf distributed market. |
22 | * |
23 | * @package phpOMS\Business\Sales |
24 | * @license OMS License 2.0 |
25 | * @link https://jingga.app |
26 | * @since 1.0.0 |
27 | */ |
28 | final class MarketShareEstimation |
29 | { |
30 | /** |
31 | * Constructor |
32 | * |
33 | * @since 1.0.0 |
34 | * @codeCoverageIgnore |
35 | */ |
36 | private function __construct() |
37 | { |
38 | } |
39 | |
40 | /** |
41 | * Calculate rank (r) based on market share (m) |
42 | * |
43 | * @latex r = \sqrt[s]{\frac{1}{m \times \sum_{n=1}^N{\frac{1}{n^{s}}}}} |
44 | * |
45 | * @param int $participants The amount of existing participants in the market or compentitors (N) |
46 | * @param float $marketShare The absolute own market share (m) |
47 | * @param float $modifier Distribution modifier (s) |
48 | * |
49 | * @return int Returns the rank |
50 | * |
51 | * @since 1.0.0 |
52 | */ |
53 | public static function getRankFromMarketShare(int $participants, float $marketShare, float $modifier = 1.0) : int |
54 | { |
55 | $sum = 0.0; |
56 | for ($i = 0; $i < $participants; ++$i) { |
57 | $sum += 1 / \pow($i + 1, $modifier); |
58 | } |
59 | |
60 | return (int) \round(\pow(1 / ($marketShare * $sum), 1 / $modifier)); |
61 | } |
62 | |
63 | /** |
64 | * Calculate market share (m) based on rank (r) |
65 | * |
66 | * @latex m = \frac{\frac{1}{r^{s}}}{\sum_{n=1}^N{\frac{1}{n^{s}}}} |
67 | * |
68 | * @param int $participants The amount of existing participants in the market or compentitors (N) |
69 | * @param int $rank The absolute own rank in the market (r) |
70 | * @param float $modifier Distribution modifier (s) |
71 | * |
72 | * @return float Returns the Market share |
73 | * |
74 | * @since 1.0.0 |
75 | */ |
76 | public static function getMarketShareFromRank(int $participants, int $rank, float $modifier = 1.0) : float |
77 | { |
78 | $sum = 0.0; |
79 | for ($i = 0; $i < $participants; ++$i) { |
80 | $sum += 1 / \pow($i + 1, $modifier); |
81 | } |
82 | |
83 | return (1 / \pow($rank, $modifier)) / $sum; |
84 | } |
85 | } |