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
 */
public class Alpha extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		double fundRetSum = 0;
		double bmkRetSum = 0;
		for (CompoundRet unit : list) {
			if (unit.getRetOfFund() == null || unit.getRetOfBmk() == null) {
				continue;
			}
			fundRetSum += unit.getRetOfFund();
			bmkRetSum += unit.getRetOfBmk();
		}
		Double beta = null;
		Rate rate = IndicatorFactory.getInstance().get(Indicator.Beta);
		beta = rate.calc(list, frequency, ifAnnualized);
		if (beta == null) {
			return null;
		}
		Double alpha = fundRetSum / (list.size() - 1) - beta * bmkRetSum / (list.size() - 1);
		return ifAnnualized ? alpha * frequency.getNum() : alpha;
	}

}
