package com.smppw.core.reta.calc;


import com.smppw.common.pojo.dto.CompoundRet;
import com.smppw.common.pojo.enums.Frequency;
import com.smppw.common.pojo.enums.Indicator;
import com.smppw.core.reta.IndicatorFactory;
import com.smppw.core.reta.Rate;

import java.util.List;

/**
 * @author zhengsongliang
 * @datetime 2017年7月25日 下午3:26:16
 */
public class Jensen extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		int count = list.size() - 1;
		Double beta = IndicatorFactory.getInstance().get(Indicator.Beta).calc(list, frequency, ifAnnualized);
		if (beta == null)
			return null;

		double fundSum = 0;
		double bmkSum = 0;
		double rfSum = 0;
		for (CompoundRet value : list) {
			if (value.getRetOfFund() == null || value.getRetOfBmk() == null || value.getRetOfRf() == null)
				continue;
			fundSum += value.getRetOfFund();
			bmkSum += value.getRetOfBmk();
			rfSum += value.getRetOfRf();
		}
		double retAvg = fundSum / count;
		double bmkAvg = bmkSum / count;
		double rfAvg = rfSum / count;
		double jensen = retAvg - rfAvg - beta * (bmkAvg - rfAvg);
		if (ifAnnualized)
			jensen *= frequency.getNum();
		return jensen;
	}

}
