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

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		double sum = 0;
		int n = list.size() - 1;
		if (n <= 2) {
			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, 3);
		}
		double stDev = Statistics.stdDevUnbiased(rates);

		if(stDev <= IndicatorConst.MIN_DENOMINATOR_VALUE) {
			return null;
		}

		Double result = n * sumSquare / (Math.pow(stDev, 3) * (n - 1) * (n - 2));
		return DataUtil.handleOverflowData(result);
	}

}
