Code (PHP) pasted on 2019-04-01, 20:17 Raw Source
- <?php
- namespace Finances;
- use Finances\models\Stock;
- /**
- * Class FinanceUtil
- * @package Finances
- */
- class FinanceUtil
- {
- /**
- * @param Stock $stock
- *
- * @return float
- */
- public function getAverageYield(Stock $stock)
- {
- $rates = $stock->getRates();
- $first = array_key_first($rates);
- $last = array_key_last($rates);
- }
- /**
- * @param Stock $stock
- *
- * @return array
- */
- public function getProbability(Stock $stock)
- {
- $mean = $this->getExpectedYield($stock);
- $stddev = $this->getStandardDeviation($stock);
- return [
- ];
- }
- /**
- * @param Stock $stock
- *
- * @return float
- */
- public function getExpectedYield(Stock $stock)
- {
- $rates = $stock->getRates();
- $yields = 0;
- foreach ($rates as $year => $interval) {
- $yields += $this->getYield($stock, $year);
- }
- return (1 / $nth) * $yields;
- }
- /**
- * @param Stock $stock
- * @param $year
- *
- * @return float
- */
- public function getYield(Stock $stock, $year)
- {
- $rates = $stock->getRates();
- return (($rates[$year][1] - $rates[$year][0]) / $rates[$year][0]) * 100;
- }
- /**
- * @param Stock $stock
- *
- * @return float
- */
- public function getStandardDeviation(Stock $stock)
- {
- $rates = $stock->getRates();
- $mean = $this->getExpectedYield($stock);
- $yields = 0;
- foreach ($rates as $year => $interval) {
- }
- }
- /**
- * @param Stock $stockA
- * @param Stock $stockB
- *
- * @return float
- */
- public function getCovariance(Stock $stockA, Stock $stockB)
- {
- $ratesA = $stockA->getRates();
- $ratesB = $stockB->getRates();
- if ($cntA > $cntB) {
- $n = $cntA;
- $iterator = $ratesA;
- } else {
- $n = $cntB;
- $iterator = $ratesB;
- }
- $meanA = $this->getExpectedYield($stockA);
- $meanB = $this->getExpectedYield($stockB);
- $sum = 0;
- foreach ($iterator as $year => $interval) {
- $yieldA = $this->getYield($stockA, $year);
- $yieldB = $this->getYield($stockB, $year);
- $sum += ($yieldA - $meanA) * ($yieldB * $meanB);
- }
- return ($sum / $n) / ($meanA * $meanB);
- }
- /**
- * @param Stock $stockA
- * @param Stock $stockB
- *
- * @return float
- */
- public function getBetaFactor(Stock $stockA, Stock $stockB){
- }
- }