package com.smppw.core.reta.calc;

import com.smppw.common.pojo.dto.CompoundRet;
import com.smppw.common.pojo.enums.Frequency;
import com.smppw.core.reta.Rate;
import com.smppw.utils.FunctionUtil;
import org.apache.commons.math3.distribution.NormalDistribution;

import java.util.List;

/**
 * @author zhengsongliang
 */
public class SMDDLPM2 extends Rate implements LPM {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		List<Double> a = FunctionUtil.getFundRetList(list);
		Double sum = 0D;
		for (Double unit : a) {
			if (unit == null) {
				continue;
			}
			sum += unit;
		}
		if (list.size() <= 1 || sum == 0)
			return null;
		LPMFunction lpm2Fun = new LPMFunction(FunctionUtil.getFundRetList(list), new SMDDLPM2());
		double LPM2 = lpm2Fun.value(0);
		return LPM2;
	}

	@Override
	public double calculate(double tau, List<Double> dataList) {
		double sum = 0;
		for (double mean : dataList) {
			double omega = FunctionUtil.omega(dataList);
			NormalDistribution normalDis = new NormalDistribution(mean, omega);
			double pdf = normalDis.density(tau);
			double cdf = normalDis.cumulativeProbability(tau);
			double left = (tau - mean) * Math.pow(omega, 2) * pdf;
			double middle = Math.pow(tau - mean, 2);
			double right = Math.pow(omega, 2);
			double unit = left + (middle + right) * cdf;
			sum += unit;
		}
		return dataList.size() == 0 ? 0 : sum / dataList.size();
	}

}
