package com.smppw.core.reta.calc;


import com.smppw.constants.IndicatorConst;
import com.smppw.common.pojo.dto.CompoundRet;
import com.smppw.common.pojo.enums.Frequency;
import com.smppw.core.reta.Rate;
import com.smppw.utils.DataUtil;
import com.smppw.utils.Statistics;

import java.util.ArrayList;
import java.util.List;

public class InformationRatio extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		List<Double> dValues = new ArrayList<>();
		for (CompoundRet value : list) {
			if (value.getRetOfFund() == null || value.getRetOfBmk() == null)
				continue;
			dValues.add(value.getRetOfFund() - value.getRetOfBmk());
		}
		double sum = 0;
		for (Double dValue : dValues) {
			sum += dValue;
		}

		double avg;
		if (list.size() <= 1) {
			// sum一定为0
			avg = IndicatorConst.ZERO;
		} else {
			avg = sum / (list.size() - 1);
		}

		double TracingError = Statistics.stdDevUnbiased(dValues);
//		if (TracingError == 0) {
//			return DataUtil.handleZeroValue(avg);
//		}

		Double informationRatio = DataUtil.divNearZeroValue(avg, TracingError);
		if (informationRatio == null) {
			return null;
		}
		if (ifAnnualized) {
			informationRatio = Rate.AnnualizedBySqrt(informationRatio, frequency);
		}

		return DataUtil.handleOverflowData(informationRatio);
	}

}
