From 392b732969a4e9283b99fdc8a7194624f6d6404a Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 25 Dec 2015 13:53:06 +0100 Subject: [PATCH] Finishing finance basic function implementation --- Math/Finance/Depreciation.php | 0 Math/Finance/FinanceFormulas.php | 1085 +++++++++++++++++++++++++++--- Math/Finance/Loan.php | 134 ++++ Math/Finance/StockBonds.php | 398 +++++++++++ 4 files changed, 1507 insertions(+), 110 deletions(-) create mode 100644 Math/Finance/Depreciation.php create mode 100644 Math/Finance/Loan.php create mode 100644 Math/Finance/StockBonds.php diff --git a/Math/Finance/Depreciation.php b/Math/Finance/Depreciation.php new file mode 100644 index 000000000..e69de29bb diff --git a/Math/Finance/FinanceFormulas.php b/Math/Finance/FinanceFormulas.php index 6560f029b..3fd06b370 100644 --- a/Math/Finance/FinanceFormulas.php +++ b/Math/Finance/FinanceFormulas.php @@ -15,10 +15,25 @@ */ namespace phpOMS\Math\Finance; +use phpOMS\Math\Statistic\Average; + +/** + * Finance class. + * + * @category Log + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class FinanceFormulas { /** + * Annual Percentage Yield + * * @param \float $r Stated annual interest rate * @param \int $n number of times compounded * @@ -33,6 +48,8 @@ class FinanceFormulas } /** + * Annual Percentage Yield + * * @param \float $apy Annual percentage yield * @param \int $n Number of times compounded * @@ -47,20 +64,8 @@ class FinanceFormulas } /** - * @param \float $apy Annual percentage yield - * @param \float $r Stated annual interest rate + * Annuity - Future Value * - * @return \float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function getNumberOfCompoundsOfAPY(\float $apy, \float $r) : \float - { - return 0.0; - } - - /** * @param \float $P Periodic payment * @param \float $r Stated annual interest rate * @param \int $n Number of periods @@ -76,6 +81,8 @@ class FinanceFormulas } /** + * Annuity - Future Value + * * @param \float $fva Future value annuity * @param \float $P Periodic payment * @param \float $r Stated annual interest rate @@ -91,6 +98,8 @@ class FinanceFormulas } /** + * Future Value + * * @param \float $fva Future value annuity * @param \float $r Stated annual interest rate * @param \int $n Number of periods @@ -105,22 +114,10 @@ class FinanceFormulas return $fva / ((pow(1 + $r, $n) - 1) / $r); } - /** - * @param \float $fva Future value annuity - * @param \float $P Periodic payment - * @param \int $n Number of periods - * - * @return \float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function getRateOfFVA(\float $fva, \float $P, \int $n) : \float - { - return 0.0; - } /** + * Annuity - Future Value w/ Continuous Compounding + * * @param \float $cf Cach flow * @param \float $r Rate * @param \int $t Time @@ -136,6 +133,8 @@ class FinanceFormulas } /** + * Annuity - Future Value w/ Continuous Compounding + * * @param \float $fvacc Future value annuity continuous compoinding * @param \float $r Rate * @param \int $t Time @@ -151,21 +150,8 @@ class FinanceFormulas } /** - * @param \float $fvacc Future value annuity continuous compoinding - * @param \float $cf Cach flow - * @param \int $t Time + * Annuity - Future Value w/ Continuous Compounding * - * @return \float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function getRateOfFVACC(\float $fvacc, \float $cf, \int $t) : \float - { - return 0.0; - } - - /** * @param \float $fvacc Future value annuity continuous compoinding * @param \float $cf Cach flow * @param \float $r Rate @@ -181,6 +167,8 @@ class FinanceFormulas } /** + * Annuity - Payment (PV) + * * @param \float $pv Present value * @param \float $r Rate per period * @param \int $n Number of periods @@ -196,6 +184,8 @@ class FinanceFormulas } /** + * Annuity - Payment (PV) + * * @param \float $p Payment * @param \float $pv Present value * @param \float $r Rate per period @@ -211,21 +201,8 @@ class FinanceFormulas } /** - * @param \float $p Payment - * @param \float $pv Present value - * @param \int $n Number of periods + * Annuity - Payment (PV) * - * @return \float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function getRatePerPeriodOfAPPV(\float $p, \float $pv, \int $n) : \float - { - return 0.0; - } - - /** * @param \float $p Payment * @param \float $r Rate per period * @param \int $n Number of periods @@ -241,6 +218,8 @@ class FinanceFormulas } /** + * Annuity - Payment (FV) + * * @param \float $fv Present value * @param \float $r Rate per period * @param \int $n Number of periods @@ -256,6 +235,8 @@ class FinanceFormulas } /** + * Annuity - Payment (FV) + * * @param \float $p Payment * @param \float $fv Present value * @param \float $r Rate per period @@ -271,21 +252,8 @@ class FinanceFormulas } /** - * @param \float $p Payment - * @param \float $fv Present value - * @param \int $n Rate per period + * Annuity - Payment (FV) * - * @return \float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function getRatePerPeriodOfAPFV(\float $p, \float $fv, \int $n) : \float - { - return 0.0; - } - - /** * @param \float $p Payment * @param \float $r Present value * @param \int $n Rate per period @@ -301,6 +269,8 @@ class FinanceFormulas } /** + * Annuity - Payment Factor (PV) + * * @param \float $r Rate per period * @param \int $n Number of periods * @@ -315,20 +285,8 @@ class FinanceFormulas } /** - * @param \float $p Payment factor - * @param \int $n Number of periods + * Annuity - Payment Factor (PV) * - * @return \float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function getRatePfAPFPV(\float $p, \int $n) : \float - { - return 0.0; - } - - /** * @param \float $p Payment factor * @param \float $r Rate per period * @@ -343,6 +301,8 @@ class FinanceFormulas } /** + * Annuity - Present Value + * * @param \float $P Periodic payment * @param \float $r Stated annual interest rate * @param \int $n Number of periods @@ -358,6 +318,8 @@ class FinanceFormulas } /** + * Annuity - Present Value + * * @param \float $pva Future value annuity * @param \float $P Periodic payment * @param \float $r Stated annual interest rate @@ -373,6 +335,8 @@ class FinanceFormulas } /** + * Annuity - Present Value + * * @param \float $pva Future value annuity * @param \float $r Stated annual interest rate * @param \int $n Number of periods @@ -388,21 +352,8 @@ class FinanceFormulas } /** - * @param \float $pva Future value annuity - * @param \float $P Periodic payment - * @param \int $n Number of periods + * Annuity - PV Factor * - * @return \float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function getRateOfPVA(\float $pva, \float $P, \int $n) : \float - { - return 0.0; - } - - /** * @param \float $r Rate per period * @param \int $n Number of periods * @@ -417,22 +368,10 @@ class FinanceFormulas } /** - * @param \float $p Payment factor - * @param \int $n Number of periods + * Annuity - PV Factor * - * @return \float - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function getRateOfPVAF(\float $p, \int $n) : \float - { - return 0.0; - } - - /** * @param \float $p Payment factor - * @param \float $r Number of periods + * @param \float $r Rete per period * * @return \int * @@ -443,4 +382,930 @@ class FinanceFormulas { return (int) round(-log(-($p * $r - 1)) / log(1 + $r)); } + + /** + * Annuity Due - Present Value + * + * @param \float $P Periodic payment + * @param \float $r Rate per period + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPresentValueOfAnnuityDue(\float $P, \float $r, \int $n) : \float + { + return $P + $P * ((1 - pow(1 + $r, -($n - 1))) / $r); + } + + /** + * Annuity Due - Present Value + * + * Using alternative formula for PV + * + * @param \float $PV Present value + * @param \float $r Rate per period + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPeriodicPaymentOfPVAD(\float $PV, \float $r, \int $n) : \float + { + return $PV * $r / (1 - pow(1 + $r, -$n)) * 1 / (1 + $r); + } + + /** + * Annuity Due - Present Value + * + * @param \float $PV Present value + * @param \float $P Periodic payment + * @param \float $r Rate per period + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPeriodsOfPVAD(\float $PV, \float $P, \float $r) : \int + { + return (int) round((($PV - $P) / $P * $r - 1) / log(1 + $r) + 1); + } + + /** + * Annuity Due - Future Value + * + * @param \float $P Periodic payment + * @param \float $r Rate per period + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getFutureValueOfAnnuityDue(\float $P, \float $r, \int $n) : \float + { + return (1 + $r) * $P * (pow(1 + $r, $n) - 1) / $r; + } + + /** + * Annuity Due - Future Value + * + * @param \float $FV Future value + * @param \float $r Rate per period + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPeriodicPaymentOfFVAD(\float $FV, \float $r, \int $n) : \float + { + return $FV / ((1 + $r) * ((pow(1 + $r, $n) - 1) / $r)); + } + + /** + * Annuity Due - Future Value + * + * @param \float $FV Future value + * @param \float $P Periodic payment + * @param \float $r Rate per period + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPeriodsOfFVAD(\float $FV, \float $P, \float $r) : \int + { + return (int) round(log($FV / (1 + $r) / $P * $r + 1) / log(1 + $r)); + } + + /** + * Asset to Sales Ratio + * + * @param \float $assets Assets + * @param \float $revenue Revenue + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getAssetToSalesRatio(\float $assets, \float $revenue) : \float + { + return $assets / $revenue; + } + + /** + * Asset Turnover Ratio + * + * @param \float $assets Assets + * @param \float $revenue Revenue + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getAssetTurnoverRatio(\float $assets, \float $revenue) : \float + { + return $revenue / $assets; + } + + /** + * Average Collection Period + * + * @param \float $receivables Receivables turnover (use getReceivablesTurnover) + * @param \int $period Period + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getAverageCollectionPeriod(\float $receivables, \int $period = 365) : \float + { + return $period / $receivables; + } + + /** + * Receivables Turnover + * + * @param \float $sales Sales in period + * @param \float $receivables Avg. account receivables + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getReceivablesTurnover(\float $sales, \float $receivables) : \float + { + return $sales / $receivables; + } + + /** + * Compound Interest + * + * @param \float $P Principal + * @param \float $r Rate per period + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getCompoundInterest(\float $P, \float $r, \int $n) : \float + { + return $P * (pow(1 + $r, $n) - 1); + } + + /** + * Continuous Compounding + * + * @param \float $P Principal + * @param \float $r Rate per period + * @param \int $t Time + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getContinuousCompounding(\float $P, \float $r, \int $t) : \float + { + return $P * exp($r * $t); + } + + /** + * Current Ratio + * + * @param \float $assets Assets + * @param \float $liabilities Liabilities + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getCurrentRaio(\float $assets, \float $liabilities) : \float + { + return $assets / $liabilities; + } + + /** + * Days in Inventory + * + * @param \float $inventory Inventory turnover + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDaysInInventory(\float $inventory) : \float + { + return 365 / $inventory; + } + + /** + * Debt Coverage Ratio + * + * @param \float $income Net operating income + * @param \float $service Debt service + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDebtCoverageRatio(\float $income, \float $service) : \float + { + return $income / $service; + } + + /** + * Debt Ratio + * + * @param \float $liabilities Total liabilities + * @param \float $assets Total assets + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDebtRatio(\float $liabilities, \float $assets) : \float + { + return $liabilities / $assets; + } + + /** + * Debt to Equity Ratio (D/E) + * + * @param \float $liabilities Total liabilities + * @param \float $equity Total assets + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDebtToEquityRatio(\float $liabilities, \float $equity) : \float + { + return $liabilities / $equity; + } + + /** + * Debt to Income Ratio (D/I) + * + * @param \float $payments Periodic payments + * @param \float $income Periodic income + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDebtToIncomeRatio(\float $payments, \float $income) : \float + { + return $payments / $income; + } + + /** + * Discounted Payback Period + * + * @param \float $CF Periodic cash flow + * @param \float $O1 Initial Investment (Outflow) + * @param \float $r Rate + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDiscountedPaybackPeriod(\float $CF, \float $O1, \float $r) : \float + { + return log(1 / (1 - $O1 * $r / $CF)) / log(1 + $r); + } + + /** + * Doubling Time + * + * @param \float $r Rate of return + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDoublingTime(\float $r) : \float + { + return log(2) / log(1 + $r); + } + + /** + * Doubling Time - Continuous Compounding + * + * @param \float $r Rate of return + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDoublingTimeContinuousCompounding(\float $r) : \float + { + return log(2) / $r; + } + + /** + * Equivalent Annual Annuity + * + * @param \float $NPV Net present value + * @param \float $r Rate per period + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getEquivalentAnnualAnnuity(\float $NPV, \float $r, \int $n) : \float + { + return $r * $NPV / (1 - pow(1 + $r, -$n)); + } + + /** + * Equivalent Annual Annuity + * + * @param \float $C Equivalent annuity cash flow + * @param \float $NPV Net present value + * @param \float $r Rate per period + * + * @return \int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPeriodsOfEAA(\float $C, \float $NPV, \float $r) : \int + { + return (int) round(log(-$r * $NPV / $C + 1) / log(1 + $r)); + } + + /** + * Equivalent Annual Annuity + * + * @param \float $C Net present value + * @param \float $r Rate per period + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getNetPresentValueOfEAA(\float $C, \float $r, \int $n) : \float + { + return $C / $r * (1 - pow(1 + $r, -$n)); + } + + /** + * Free Cash Flow to Equity (FCFE) + * + * @param \float $income Net income + * @param \float $depamo Depreciation & amortisation + * @param \float $capital Capital expenses + * @param \float $wc Change in working capital + * @param \float $borrowing Net Borrowing + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getFreeCashFlowToEquity(\float $income, \float $depamo, \float $capital, \float $wc, \float $borrowing) : \float + { + return $income + $depamo - $capital - $wc + $borrowing; + } + + /** + * Free Cash Flow to Firm (FCFF) + * + * @param \float $ebit EBIT + * @param \float $t Tax rate + * @param \float $depamo Depreciation & amortisation + * @param \float $capital Capital expenses + * @param \float $wc Change in working capital + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getFreeCashFlowToFirm(\float $ebit, \float $t, \float $depamo, \float $capital, \float $wc) : \float + { + return $ebit * (1 - $t) + $depamo - $capital - $wc; + } + + /** + * Future Value + * + * @param \float $C Cash flow at period 0 + * @param \float $r Rate of return + * @param \int $n Numbers of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getFutureValue(\float $C, \float $r, \int $n) : \float + { + return $C * pow(1 + $r, $n); + } + + /** + * Future Value - Continuous Compounding + * + * @param \float $PV Present value + * @param \float $r Rate of return + * @param \int $t Time + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getFutureValueContinuousCompounding(\float $PV, \float $r, \int $t) : \float + { + return $PV * exp($r * $t); + } + + /** + * Future Value Factor + * + * @param \float $r Rate of return + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getFutureValueFactor(\float $r, \int $n) : \float + { + return pow(1 + $r, $n); + } + + /** + * Future Value Factor + * + * @param array $r Rate of return + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getGeometricMeanReturn(array $r) : \float + { + return Average::geometricMean($r) - 1; + } + + /** + * Growing Annuity - Future Value + * + * @param \float $P First payment + * @param \float $r Rate of return + * @param \float $g Growth rate + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getGrowingAnnuityFV(\float $P, \float $r, \float $g, \int $n) : \float + { + return $P * (pow(1 + $r, $n) - pow(1 + $g, $n)) / ($r - $g); + } + + /** + * Growing Annuity - Payment (PV) + * + * @param \float $PV Present value + * @param \float $r Rate of return + * @param \float $g Growth rate + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getGrowingAnnuityPaymentPV(\float $PV, \float $r, \float $g, \int $n) : \float + { + return $PV * ($r - $g) / (1 - pow((1 + $g) / (1 + $r), $n)); + } + + /** + * Growing Annuity - Payment (FV) + * + * @param \float $FV Present value + * @param \float $r Rate of return + * @param \float $g Growth rate + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getGrowingAnnuityPaymentFV(\float $FV, \float $r, \float $g, \int $n) : \float + { + return $FV * ($r - $g) / (pow(1 + $r, $n) - pow(1 + $g, $n)); + } + + /** + * Growing Annuity - Present Value + * + * @param \float $P First payment + * @param \float $r Rate of return + * @param \float $g Growth rate + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getGrowingAnnuityPV(\float $P, \float $r, \float $g, \int $n) : \float + { + return $P / ($r - $g) * (1 - pow((1 + $g) / (1 + $r), $n)); + } + + /** + * Growing Perpetuity - Present Value + * + * @param \float $D Dividend or coupon at period 1 + * @param \float $r Rate of return + * @param \float $g Growth rate + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getGrowingPerpetuityPV(\float $D, \float $r, \float $g) : \float + { + return $D / ($r - $g); + } + + /** + * Interest Coverage Ratio + * + * @param \float $ebit EBIT + * @param \float $expense Interest expense + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getInterestCoverageRatio(\float $ebit, \float $expense) : \float + { + return $ebit / $expense; + } + + /** + * Inventory Turnover Ratio + * + * @param \float $sales Sales + * @param \float $inventory Inventory + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getInventoryTurnoverRatio(\float $sales, \float $inventory) : \float + { + return $sales / $inventory; + } + + /** + * Net Present Value + * + * @param array $C Cash flow ($C[0] = initial investment) + * @param \float $r Discount rate + * + * @return \float + * + * @throws + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getNetPresentValue(array $C, \float $r) : \float + { + $count = count($C); + + if ($count === 0) { + throw new \Exception('Dimension'); + } + + $npv = -$C[0]; + + for ($i = 1; $i < $count; $i++) { + $npv += $C[$i] / pow(1 + $r, $i); + } + + return $npv; + } + + /** + * Net Profit Margin + * + * @param \float $income Net income + * @param \float $sales Sales revenue + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getNetProfitMargin(\float $income, \float $sales) : \float + { + return $income / $sales; + } + + /** + * Net Working Capital + * + * @param \float $assets Current assets + * @param \float $liabilities Current liabilities + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getNetWorkingCapital(\float $assets, \float $liabilities) : \float + { + return $assets - $liabilities; + } + + /** + * Number of Periods - PV & FV + * + * @param \float $FV Future value + * @param \float $PV Present value + * @param \float $r Rate per period + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getNumberOfPeriodsPVFV(\float $FV, \float $PV, \float $r) : \float + { + return log($FV / $PV) / log(1 + $r); + } + + /** + * Payback Period + * + * @param \float $investment Initial investment + * @param \float $cash Periodic cash flow + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPaybackPeriod(\float $investment, \float $cash) : \float + { + return $investment / $cash; + } + + /** + * Perpetuity + * + * @param \float $D Dividend or coupon per period + * @param \float $r Discount rate + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPresentValueOfPerpetuity(\float $D, \float $r) : \float + { + return $D / $r; + } + + /** + * Number of Periods - PV & FV + * + * @param \float $C Cash flow at period 1 + * @param \float $r Rate of return + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPresentValue(\float $C, \float $r, \int $n) : \float + { + return $C / pow(1 + $r, $n); + } + + /** + * PV - Continuous Compounding + * + * @param \float $C Cash flow + * @param \float $r Rate of return + * @param \int $t Time + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPresentValueContinuousCompounding(\float $C, \float $r, \int $t) : \float + { + return $C / exp($r * $t); + } + + /** + * Present Value Factor + * + * @param \float $r Rate of return + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPresentValueFactor(\float $r, \int $n) : \float + { + return 1 / pow(1 + $r, $n); + } + + /** + * Quick Ratio + * + * @param \float $assets Quick assets (current assets - inventory) + * @param \float $liabilities Current liabilities + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getQuickRatio(\float $assets, \float $liabilities) : \float + { + return $assets / $liabilities; + } + + /** + * Rate of Inflation + * + * @param \float $oldCPI Consumer price index old + * @param \float $newCPI Consumer price index new + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getRateOfOnflation(\float $oldCPI, \float $newCPI) : \float + { + return $newCPI / $oldCPI - 1; + } + + /** + * Real Rate of Return + * + * @param \float $nominal Nominal rate + * @param \float $inflation Inflation rate + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getRealRateOfReturn(\float $nominal, \float $inflation) : \float + { + return (1 + $nominal) / (1 + $inflation) - 1; + } + + /** + * Receivables Turnover Ratio + * + * @param \float $sales Sales revenue + * @param \float $receivable Avg. accounts receivable + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getReceivablesTurnoverRatio(\float $sales, \float $receivable) : \float + { + return $sales / $receivable; + } + + /** + * Receivables Turnover Ratio + * + * @param \float $income Net income + * @param \float $dividends Dividends + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getRetentionRatio(\float $income, \float $dividends) : \float + { + return ($income - $dividends) / $income; + } + + /** + * Return on Assets (ROA) + * + * @param \float $income Net income + * @param \float $assets Avg. total assets + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getReturnOnAssets(\float $income, \float $assets) : \float + { + return $income / $assets; + } + + /** + * Return on Equity (ROE) + * + * @param \float $income Net income + * @param \float $equity Avg. stockholder's equity + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getReturnOnEquity(\float $income, \float $equity) : \float + { + return $income / $equity; + } + + /** + * Return on Investment (ROI) + * + * @param \float $earnings Earnings + * @param \float $investment Initial investment + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getReturnOnInvestment(\float $earnings, \float $investment) : \float + { + return $earnings / $investment - 1; + } + + /** + * Simple Interest + * + * @param \float $P Principal + * @param \float $r Rate + * @param \int $t Time + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getSimpleInterest(\float $P, \float $r, \int $t) : \float + { + return $P * $r * $t; + } + } diff --git a/Math/Finance/Loan.php b/Math/Finance/Loan.php new file mode 100644 index 000000000..f7feb613a --- /dev/null +++ b/Math/Finance/Loan.php @@ -0,0 +1,134 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Finance; + +/** + * Finance class. + * + * @category Log + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Loan +{ + /** + * Balloon Loan - Payments + * + * @param \float $PV Present value + * @param \float $r Rate per period + * @param \int $n Number of periods + * @param \float $balloon Balloon balance + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPaymentsOnBalloonLoan(\float $PV, \float $r, \int $n, \float $balloon = 0) : \float + { + return ($PV - $balloon / pow(1 + $r, $n)) * $r / (1 - pow(1 + $r, -$n)); + } + + /** + * Loan - Balloon Balance + * + * @param \float $PV Present value (original balance) + * @param \float $P Payment + * @param \float $r Rate per payment + * @param \int $n Number of payments + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getBalloonBalanceOfLoan(\float $PV, \float $P, \float $r, \int $n) : \float + { + return $PV * pow(1 + $r, $n) - $P * (pow(1 + $r, $n) - 1) / $r; + } + + /** + * Loan - Payment + * + * @param \float $PV Present value (original balance) + * @param \float $r Rate per period + * @param \int $n Number of periods + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getLoanPayment(\float $PV, \float $r, \int $n) : \float + { + return $r * $PV / (1 - pow(1 + $r, -$n)); + } + + /** + * Loan - Remaining Balance + * + * @param \float $PV Present value (original balance) + * @param \float $P Payment + * @param \float $r Rate per payment + * @param \int $n Number of payments + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getRemainingBalanceLoan(\float $PV, \float $P, \float $r, \int $n) : \float + { + return $PV * pow(1 + $r, $n) - $P * (pow(1 + $r, $n) - 1) / $r; + } + + /** + * Loan to Deposit Ratio + * + * @param \float $loans Loans + * @param \float $deposits Deposits + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getLoanToDepositRatio(\float $loans, \float $deposits) : \float + { + return $loans / $deposits; + } + + /** + * Loan to Value (LTV) + * + * @param \float $loan Loan amount + * @param \float $collateral Value of collateral + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getLoanToValueRatio(\float $loan, \float $collateral) : \float + { + return $loan / $collateral; + } + +} diff --git a/Math/Finance/StockBonds.php b/Math/Finance/StockBonds.php new file mode 100644 index 000000000..9c37ee61c --- /dev/null +++ b/Math/Finance/StockBonds.php @@ -0,0 +1,398 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Finance; + +/** + * Finance class. + * + * @category Log + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class StockBonds +{ + /** + * Bond Equivalent Yield + * + * @param \float $fv Face value + * @param \float $price Price + * @param \int $days Days to maturity + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getBondEquivalentYield(\float $fv, \float $price, \int $days) : \float + { + return ($fv - $price) / $price * 365 / $days; + } + + /** + * Book Value per Share + * + * @param \float $total Total common stockholder's Equity + * @param \int $common Number of common shares + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getBookValuePerShare(\float $total, \int $common) : \float + { + return $total / $common; + } + + /** + * Capital Asset Pricing Model (CAPM) + * + * @param \float $rf Risk free rate + * @param \float $beta Risk to invest in a stock relative to the risk of the market + * @param \float $r Return on the market + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getExpectedReturnCAPM(\float $rf, \float $beta, \float $r) : \float + { + return $rf + $beta * ($r - $rf); + } + + /** + * Capital Gains Yield + * + * @param \float $P0 Old stock price + * @param \float $P1 New stock price + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getCapitalGainsYield(\float $P0, \float $P1) : \float + { + return $P1 / $P0 - 1; + } + + /** + * Current Yield + * + * @param \float $coupons Annual coupons + * @param \float $price Current bond price + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getCurrentYield(\float $coupons, \float $price) : \float + { + return $coupons / $price; + } + + /** + * Diluted Earnings per Share + * + * @param \float $income Net Income + * @param \float $avg Avg. shares + * @param \float $other Other convertible instruments + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDilutedEarningsPerShare(\float $income, \float $avg, \float $other) : \float + { + return $income / ($avg + $other); + } + + /** + * Dividend Payout Ratio + * + * @param \float $dividends Dividends + * @param \float $income Net income + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDividendPayoutRatio(\float $dividends, \float $income) : \float + { + return $dividends / $income; + } + + /** + * Dividend Yield + * + * @param \float $dividends Dividends + * @param \float $price Initial price for the period + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDividendYield(\float $dividends, \float $price) : \float + { + return $dividends / $price; + } + + /** + * Dividend Yield + * + * @param \float $dividends Dividends + * @param \int $shares Initial price for the period + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getDividendsPerShare(\float $dividends, \int $shares) : \float + { + return $dividends / $shares; + } + + /** + * Earnings Per Share + * + * @param \float $income Net income + * @param \float $shares Weighted avg. outstanding shares + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getEarningsPerShare(\float $income, \float $shares) : \float + { + return $income / $shares; + } + + /** + * Equity Multiplier + * + * @param \float $assets Total assets + * @param \float $equity Stockholder's equity + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getEquityMultiplier(\float $assets, \float $equity) : \float + { + return $assets / $equity; + } + + /** + * Holding Period Return + * + * @param array $r Rate of return + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getHoldingPeriodReturn(array $r) : \float + { + $hpr = 1.0; + + foreach ($r as $value) { + $hpr *= 1 + $value; + } + + return $hpr - 1; + } + + /** + * Net Asset Value + * + * @param \float $assets Fund assets + * @param \float $liabilities Fund liabilities + * @param \int $shares Outstanding shares + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getNetAssetValue(\float $assets, \float $liabilities, \int $shares) : \float + { + return ($assets - $liabilities) / $shares; + } + + /** + * Price to Book Value + * + * @param \float $market Market price per share + * @param \float $book Book value per share + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPriceToBookValue(\float $market, \float $book) : \float + { + return $market / $book; + } + + /** + * Price to Earnings (P/E Ratio) + * + * @param \float $price Price per share + * @param \float $earnings Earnings per share + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPriceEarningsRatio(\float $price, \float $earnings) : \float + { + return $price / $earnings; + } + + /** + * Price to Sales (P/S Ratio) + * + * @param \float $price Price per share + * @param \float $sales Sales per share + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPriceToSalesRatio(\float $price, \float $sales) : \float + { + return $price / $sales; + } + + /** + * Stock - PV with Constant Growth + * + * @param \float $dividend Estimated dividends for next period + * @param \float $r Required rate of return + * @param \float $g Growth rate + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getPresentValueOfStockConstantGrowth(\float $dividend, \float $r, \float $g = 0.0) : \float + { + return $dividend / ($r - $g); + } + + /** + * Tax Equivalent Yield + * + * @param \float $free Tax free yield + * @param \float $tax Tax rate + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getTaxEquivalentYield(\float $free, \float $tax) : \float + { + return $free / (1 - $tax); + } + + /** + * Total Stock Return + * + * @param \float $P0 Initial stock price + * @param \float $P1 Ending stock price + * @param \float $D Dividends + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getTotalStockReturn(\float $P0, \float $P1, \float $D) : \float + { + return ($P1 - $P0 + $D) / $P0; + } + + /** + * Yield to Maturity + * + * @param \float $C Coupon/interest payment + * @param \float $F Face value + * @param \float $P Price + * @param \int $n Years to maturity + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getYieldToMaturity(\float $C, \float $F, \float $P, \int $n) : \float + { + return ($C + ($F - $P) / $n) / (($F + $P) / 2); + } + + /** + * Zero Coupon Bond Value + * + * @param \float $F Face value + * @param \float $r Rate or yield + * @param \int $t Time to maturity + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getZeroCouponBondValue(\float $F, \float $r, \int $t) : \float + { + return $F / pow(1 + $r, $t); + } + + /** + * Zero Coupon Bond Effective Yield + * + * @param \float $F Face value + * @param \float $PV Present value + * @param \int $n Time to maturity + * + * @return \float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getZeroCouponBondEffectiveYield(\float $F, \float $PV, \int $n) : \float + { + return pow($F / $PV, 1 / $n) - 1; + } + +}