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 Kurtosis extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		double sum = 0;
		int n = list.size() - 1;
		if (n <= 3) {
			return null;
		}

		List<Double> rates = new ArrayList<>();
		for (CompoundRet value : list) {
			if (value.getRetOfFund() == null) {
				continue;
			}
			sum += value.getRetOfFund();
			rates.add(value.getRetOfFund());
		}

		double avg = sum / n;
		double sumSquare = 0;
		for (CompoundRet value : list) {
			if (value.getRetOfFund() == null) {
				continue;
			}
			sumSquare += Math.pow(value.getRetOfFund() - avg, 4);
		}

		double stDev = Statistics.stdDevUnbiased(rates);
		if(stDev <= IndicatorConst.MIN_DENOMINATOR_VALUE) {
			return null;
		}
		sumSquare /= Math.pow(stDev, 4);
		double temp1 = 1.0 * n * (n + 1) / (n - 1) / (n - 2) / (n - 3);
		double temp2 = 3 * Math.pow(n - 1, 2) / (n - 2) / (n - 3);

		Double result =  sumSquare * temp1 - temp2;
		return DataUtil.handleOverflowData(result);
	}

}
