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 java.util.List;

public class DownsideStdDev extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		if (list.size() <= 1) {
			return null;
		}
		double downsideStdDev = 0.0;
		for (CompoundRet value : list) {
			if (value.getRetOfFund() == null || value.getRetOfRf() == null) {
				continue;
			}
			double min = value.getRetOfFund() - value.getRetOfRf();
			if (min < 0) {
				downsideStdDev += Math.pow(min, 2);
			}
		}
		downsideStdDev /= ((list.size() - 1) - 1);
		downsideStdDev = Math.pow(downsideStdDev, 0.5);
		return ifAnnualized ? Rate.AnnualizedBySqrt(downsideStdDev, frequency) : downsideStdDev;
	}

}
