Last active
August 29, 2015 13:55
-
-
Save harrygr/8735850 to your computer and use it in GitHub Desktop.
Modified Dietz Return
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Calculates the Modified-Dietz return given arrays of MIMO | |
* MIMO is an associative array of dates to values | |
* | |
* @param DateTime $startDate Start date for return period | |
* @param DateTime $endDate End date for return period | |
* @param float $BMV Start value of the portfolio | |
* @param float $EMV End value of the portfolio | |
* @param array $MIMO A date-indexed associative array of the cash in/out of the portfolio | |
* @return float | |
*/ | |
function mod_dietz(DateTime $startDate, DateTime $endDate, $BMV, $EMV, $MIMO = array()){ | |
$CD = $startDate->diff($endDate); | |
$SumWiFi = 0; | |
$F = 0; | |
foreach ( $MIMO as $date=>$Fi ){ | |
if ($date) { | |
$date = new DateTime($date); | |
//Only take into account the MIMO if it falls between the start and end dates. | |
if ( $date >= $startDate && $date < $endDate ){ | |
$Di = $date->diff($startDate); | |
$Wi = ($CD->days - $Di->days) / $CD->days; | |
$SumWiFi += $Wi * $Fi; | |
$F += $Fi; | |
} | |
} | |
} | |
if ( $BMV + $SumWiFi != 0 ) { | |
return ($EMV - $BMV - $F)/($BMV + $SumWiFi); | |
} else { | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment