Link.FYI

Pastebin

Create New My Pastes

Code (PHP) pasted on 2019-04-01, 20:17 Raw Source

  1. <?php
  2.  
  3. namespace Finances;
  4.  
  5. use Finances\models\Stock;
  6.  
  7. /**
  8.  * Class FinanceUtil
  9.  * @package Finances
  10.  */
  11. class FinanceUtil
  12. {
  13.     /**
  14.      * @param Stock $stock
  15.      *
  16.      * @return float
  17.      */
  18.     public function getAverageYield(Stock $stock)
  19.     {
  20.         $rates = $stock->getRates();
  21.         $nth = count($rates);
  22.         $first = array_key_first($rates);
  23.         $last = array_key_last($rates);
  24.         return (pow($rates[$last][1] / $rates[$first][0], 1 / $nth) - 1) * 100;
  25.     }
  26.  
  27.     /**
  28.      * @param Stock $stock
  29.      *
  30.      * @return array
  31.      */
  32.     public function getProbability(Stock $stock)
  33.     {
  34.         $mean = $this->getExpectedYield($stock);
  35.         $stddev = $this->getStandardDeviation($stock);
  36.         return [
  37.             '70%' => (round($mean - $stddev, 3)) . '%; ' . (round($mean + $stddev, 3)) . '%',
  38.             '95%' => (round($mean - (2 * $stddev), 3)) . '%; ' . (round($mean + (2 * $stddev), 3)) . '%',
  39.             '99%' => (round($mean - (3 * $stddev), 3)) . '%; ' . (round($mean + (3 * $stddev), 3)) . '%'
  40.         ];
  41.     }
  42.  
  43.     /**
  44.      * @param Stock $stock
  45.      *
  46.      * @return float
  47.      */
  48.     public function getExpectedYield(Stock $stock)
  49.     {
  50.         $rates = $stock->getRates();
  51.         $nth = count($rates);
  52.         $yields = 0;
  53.         foreach ($rates as $year => $interval) {
  54.             $yields += $this->getYield($stock, $year);
  55.         }
  56.         return (1 / $nth) * $yields;
  57.     }
  58.  
  59.     /**
  60.      * @param Stock $stock
  61.      * @param $year
  62.      *
  63.      * @return float
  64.      */
  65.     public function getYield(Stock $stock, $year)
  66.     {
  67.         $rates = $stock->getRates();
  68.         return (($rates[$year][1] - $rates[$year][0]) / $rates[$year][0]) * 100;
  69.     }
  70.  
  71.     /**
  72.      * @param Stock $stock
  73.      *
  74.      * @return float
  75.      */
  76.     public function getStandardDeviation(Stock $stock)
  77.     {
  78.         $rates = $stock->getRates();
  79.         $nth = count($rates);
  80.         $mean = $this->getExpectedYield($stock);
  81.         $yields = 0;
  82.         foreach ($rates as $year => $interval) {
  83.             $yields += pow($this->getYield($stock, $year) - $mean, 2);
  84.         }
  85.         return sqrt((1 / $nth) * $yields);
  86.     }
  87.  
  88.     /**
  89.      * @param Stock $stockA
  90.      * @param Stock $stockB
  91.      *
  92.      * @return float
  93.      */
  94.     public function getCovariance(Stock $stockA, Stock $stockB)
  95.     {
  96.         $ratesA = $stockA->getRates();
  97.         $ratesB = $stockB->getRates();
  98.         $cntA = count($ratesA);
  99.         $cntB = count($ratesB);
  100.         if ($cntA > $cntB) {
  101.             $n = $cntA;
  102.             $iterator = $ratesA;
  103.         } else {
  104.             $n = $cntB;
  105.             $iterator = $ratesB;
  106.         }
  107.         $meanA = $this->getExpectedYield($stockA);
  108.         $meanB = $this->getExpectedYield($stockB);
  109.  
  110.         $sum = 0;
  111.         foreach ($iterator as $year => $interval) {
  112.             $yieldA = $this->getYield($stockA, $year);
  113.             $yieldB = $this->getYield($stockB, $year);
  114.             $sum += ($yieldA - $meanA) * ($yieldB * $meanB);
  115.         }
  116.         return ($sum / $n) / ($meanA * $meanB);
  117.     }
  118.  
  119.     /**
  120.      * @param Stock $stockA
  121.      * @param Stock $stockB
  122.      *
  123.      * @return float
  124.      */
  125.     public function getBetaFactor(Stock $stockA, Stock $stockB){
  126.  
  127.     }
  128. }