我编写了自己的修改后的dietz回报函数来计算具有外部现金流动的投资组合的回报。与 excel 解决方案相比,它似乎工作正常,但我遇到了结果错误的情况。当正确结果为 0.30% 时,我的函数返回 0.23%。
这是功能:
//Calculates the Modified-Dietz return given arrays of MIMO.
//MIMO is an associative array of dates to values
function modDietz($startDate, $endDate, $BMV, $EMV, $MIMO){
if( strtotime($startDate) !== false ){
//Date was passed as a string; convert it to php date
$startDate = strtotime($startDate);
}
if( strtotime($endDate) !== false ){
//Date was passed as a string; convert it to php date
$endDate = strtotime($endDate);
}
//Convert seconds to days
$CD = ($startDate - $endDate)/(60*60*24);
$i = 0;
$SumWiFi = 0;
$F = 0;
foreach ($MIMO as $date=>$Fi){
if( strtotime($date) !== false ){
//Date was passed as a string; convert it to php date
$date = strtotime($date);
}
//Only take into account the MIMO if it falls between the start and end dates.
if ($date >= $startDate && $date <= $endDate){
$Di = ($date - $startDate) / (60*60*24);
$Wi = ($CD - $Di) / $CD;
$SumWiFi += $Wi * $Fi;
$F += $Fi;
}
}
if ($BMV + $SumWiFi != 0) {
return ($EMV - $BMV - $F)/($BMV + $SumWiFi);
} else {
return 0;
}
}
这是我提供给它的数据,它给出了错误的结果:
Dates:
Array
(
[0] => 2013-04-30
[1] => 2013-03-31
)
Values:
Array
(
[2013-03-31] => 4990430.0
[2013-04-30] => 5991710.1
)
MIMO:
Array
(
[2013-04-19] => -600.0
[2013-04-23] => 1000000.0
[2013-04-29] => -13750
)
这是产生结果的行:
$mdReturn = modDietz($dates[1], $dates[0], $values[$dates[1]], $values[$dates[0]], $mimo);
我觉得 29 日的 Money-Out 可能是原因 - 我不太确定在此函数中处理日期的最佳方法。目前,我只是对作为格式传递的字符串使用 string-to-date,yyyy-mm-dd
如上面的数组所示。
知道我在哪里出错了吗?