Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
22 / 22
CRAP
100.00% covered (success)
100.00%
1 / 1
StockBonds
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
22 / 22
24
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 getBondEquivalentYield
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBookValuePerShare
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExpectedReturnCAPM
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCapitalGainsYield
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCurrentYield
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDilutedEarningsPerShare
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDividendPayoutRatio
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDividendYield
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDividendsPerShare
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEarningsPerShare
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEquityMultiplier
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHoldingPeriodReturn
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getNetAssetValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPriceToBookValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPriceEarningsRatio
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPriceToSalesRatio
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPresentValueOfStockConstantGrowth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTaxEquivalentYield
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTotalStockReturn
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getYieldToMaturity
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getZeroCouponBondValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getZeroCouponBondEffectiveYield
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Business\Finance
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Business\Finance;
16
17/**
18 * Finance class.
19 *
20 * @package phpOMS\Business\Finance
21 * @license OMS License 2.0
22 * @link    https://jingga.app
23 * @since   1.0.0
24 *
25 * @SuppressWarnings(PHPMD.CamelCaseParameterName)
26 * @SuppressWarnings(PHPMD.CamelCaseVariableName)
27 */
28final class StockBonds
29{
30    /**
31     * Constructor
32     *
33     * @since 1.0.0
34     * @codeCoverageIgnore
35     */
36    private function __construct()
37    {
38    }
39
40    /**
41     * Bond Equivalent Yield
42     *
43     * @param float $fv    Face value
44     * @param float $price Price
45     * @param int   $days  Days to maturity
46     *
47     * @return float
48     *
49     * @since 1.0.0
50     */
51    public static function getBondEquivalentYield(float $fv, float $price, int $days) : float
52    {
53        return ($fv - $price) / $price * 365 / $days;
54    }
55
56    /**
57     * Book Value per Share
58     *
59     * @param float $total  Total common stockholder's Equity
60     * @param int   $common Number of common shares
61     *
62     * @return float
63     *
64     * @since 1.0.0
65     */
66    public static function getBookValuePerShare(float $total, int $common) : float
67    {
68        return $total / $common;
69    }
70
71    /**
72     * Capital Asset Pricing Model (CAPM)
73     *
74     * @param float $rf   Risk free rate
75     * @param float $beta Risk to invest in a stock relative to the risk of the market
76     * @param float $r    Return on the market
77     *
78     * @return float
79     *
80     * @since 1.0.0
81     */
82    public static function getExpectedReturnCAPM(float $rf, float $beta, float $r) : float
83    {
84        return $rf + $beta * ($r - $rf);
85    }
86
87    /**
88     * Capital Gains Yield
89     *
90     * @param float $P0 Old stock price
91     * @param float $P1 New stock price
92     *
93     * @return float
94     *
95     * @since 1.0.0
96     */
97    public static function getCapitalGainsYield(float $P0, float $P1) : float
98    {
99        return $P1 / $P0 - 1;
100    }
101
102    /**
103     * Current Yield
104     *
105     * @param float $coupons Annual coupons
106     * @param float $price   Current bond price
107     *
108     * @return float
109     *
110     * @since 1.0.0
111     */
112    public static function getCurrentYield(float $coupons, float $price) : float
113    {
114        return $coupons / $price;
115    }
116
117    /**
118     * Diluted Earnings per Share
119     *
120     * @param float $income Net Income
121     * @param float $avg    Avg. shares
122     * @param float $other  Other convertible instruments
123     *
124     * @return float
125     *
126     * @since 1.0.0
127     */
128    public static function getDilutedEarningsPerShare(float $income, float $avg, float $other) : float
129    {
130        return $income / ($avg + $other);
131    }
132
133    /**
134     * Dividend Payout Ratio
135     *
136     * @param float $dividends Dividends
137     * @param float $income    Net income
138     *
139     * @return float
140     *
141     * @since 1.0.0
142     */
143    public static function getDividendPayoutRatio(float $dividends, float $income) : float
144    {
145        return $dividends / $income;
146    }
147
148    /**
149     * Dividend Yield
150     *
151     * @param float $dividends Dividends
152     * @param float $price     Initial price for the period
153     *
154     * @return float
155     *
156     * @since 1.0.0
157     */
158    public static function getDividendYield(float $dividends, float $price) : float
159    {
160        return $dividends / $price;
161    }
162
163    /**
164     * Dividend Yield
165     *
166     * @param float $dividends Dividends
167     * @param int   $shares    Initial price for the period
168     *
169     * @return float
170     *
171     * @since 1.0.0
172     */
173    public static function getDividendsPerShare(float $dividends, int $shares) : float
174    {
175        return $dividends / $shares;
176    }
177
178    /**
179     * Earnings Per Share
180     *
181     * @param float $income Net income
182     * @param float $shares Weighted avg. outstanding shares
183     *
184     * @return float
185     *
186     * @since 1.0.0
187     */
188    public static function getEarningsPerShare(float $income, float $shares) : float
189    {
190        return $income / $shares;
191    }
192
193    /**
194     * Equity Multiplier
195     *
196     * @param float $assets Total assets
197     * @param float $equity Stockholder's equity
198     *
199     * @return float
200     *
201     * @since 1.0.0
202     */
203    public static function getEquityMultiplier(float $assets, float $equity) : float
204    {
205        return $assets / $equity;
206    }
207
208    /**
209     * Holding Period Return
210     *
211     * @param array<int|float> $r Rate of return
212     *
213     * @return float
214     *
215     * @since 1.0.0
216     */
217    public static function getHoldingPeriodReturn(array $r) : float
218    {
219        $hpr = 1.0;
220
221        foreach ($r as $value) {
222            $hpr *= 1 + $value;
223        }
224
225        return $hpr - 1;
226    }
227
228    /**
229     * Net Asset Value
230     *
231     * @param float $assets      Fund assets
232     * @param float $liabilities Fund liabilities
233     * @param int   $shares      Outstanding shares
234     *
235     * @return float
236     *
237     * @since 1.0.0
238     */
239    public static function getNetAssetValue(float $assets, float $liabilities, int $shares) : float
240    {
241        return ($assets - $liabilities) / $shares;
242    }
243
244    /**
245     * Price to Book Value
246     *
247     * @param float $market Market price per share
248     * @param float $book   Book value per share
249     *
250     * @return float
251     *
252     * @since 1.0.0
253     */
254    public static function getPriceToBookValue(float $market, float $book) : float
255    {
256        return $market / $book;
257    }
258
259    /**
260     * Price to Earnings (P/E Ratio)
261     *
262     * @param float $price    Price per share
263     * @param float $earnings Earnings per share
264     *
265     * @return float
266     *
267     * @since 1.0.0
268     */
269    public static function getPriceEarningsRatio(float $price, float $earnings) : float
270    {
271        return $price / $earnings;
272    }
273
274    /**
275     * Price to Sales (P/S Ratio)
276     *
277     * @param float $price Price per share
278     * @param float $sales Sales per share
279     *
280     * @return float
281     *
282     * @since 1.0.0
283     */
284    public static function getPriceToSalesRatio(float $price, float $sales) : float
285    {
286        return $price / $sales;
287    }
288
289    /**
290     * Stock - PV with Constant Growth
291     *
292     * @param float $dividend Estimated dividends for next period
293     * @param float $r        Required rate of return
294     * @param float $g        Growth rate
295     *
296     * @return float
297     *
298     * @since 1.0.0
299     */
300    public static function getPresentValueOfStockConstantGrowth(float $dividend, float $r, float $g = 0.0) : float
301    {
302        return $dividend / ($r - $g);
303    }
304
305    /**
306     * Tax Equivalent Yield
307     *
308     * @param float $free Tax free yield
309     * @param float $tax  Tax rate
310     *
311     * @return float
312     *
313     * @since 1.0.0
314     */
315    public static function getTaxEquivalentYield(float $free, float $tax) : float
316    {
317        return $free / (1 - $tax);
318    }
319
320    /**
321     * Total Stock Return
322     *
323     * @param float $P0 Initial stock price
324     * @param float $P1 Ending stock price
325     * @param float $D  Dividends
326     *
327     * @return float
328     *
329     * @since 1.0.0
330     */
331    public static function getTotalStockReturn(float $P0, float $P1, float $D) : float
332    {
333        return ($P1 - $P0 + $D) / $P0;
334    }
335
336    /**
337     * Yield to Maturity
338     *
339     * @param float $C Coupon/interest payment
340     * @param float $F Face value
341     * @param float $P Price
342     * @param int   $n Years to maturity
343     *
344     * @return float
345     *
346     * @since 1.0.0
347     */
348    public static function getYieldToMaturity(float $C, float $F, float $P, int $n) : float
349    {
350        return ($C + ($F - $P) / $n) / (($F + $P) / 2);
351    }
352
353    /**
354     * Zero Coupon Bond Value
355     *
356     * @param float $F Face value
357     * @param float $r Rate or yield
358     * @param int   $t Time to maturity
359     *
360     * @return float
361     *
362     * @since 1.0.0
363     */
364    public static function getZeroCouponBondValue(float $F, float $r, int $t) : float
365    {
366        return $F / \pow(1 + $r, $t);
367    }
368
369    /**
370     * Zero Coupon Bond Effective Yield
371     *
372     * @param float $F  Face value
373     * @param float $PV Present value
374     * @param int   $n  Time to maturity
375     *
376     * @return float
377     *
378     * @since 1.0.0
379     */
380    public static function getZeroCouponBondEffectiveYield(float $F, float $PV, int $n) : float
381    {
382        return \pow($F / $PV, 1 / $n) - 1;
383    }
384}