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 SMDDLPM1 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 lpm1Fun = new LPMFunction(FunctionUtil.getFundRetList(list), new SMDDLPM1());
		double LPM1 = lpm1Fun.value(0);
		return LPM1;
	}

	@Override
	public double calculate(double tau, List<Double> dataList) {
		double sum = 0;
		tau = -tau;
		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 unit;
			unit = Math.pow(omega, 2) * pdf;
			unit += (tau - mean) * cdf;
			sum += unit;
		}
		return dataList.size() == 0 ? 0 : sum / dataList.size();
	}
}
