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.DataUtil;

import java.util.List;

public class Beta extends Rate {

	@Override
	public Double calc(List<CompoundRet> list, Frequency frequency, boolean ifAnnualized) {
		int n = list.size() - 1;
		double leftUp = 0, rightUp = 0, leftDown = 0, rightDown = 0;
		double rightUpx = 0;
		double rightUpy = 0;
		for (CompoundRet value : list) {
			if (value.getRetOfFund() == null || value.getRetOfBmk() == null) {
				continue;
			}
			leftUp += value.getRetOfFund() * value.getRetOfBmk();

			rightUpx += value.getRetOfBmk();
			rightUpy += value.getRetOfFund();

			leftDown += Math.pow(value.getRetOfBmk(), 2);

			rightDown += value.getRetOfBmk();

		}
		rightUp = rightUpx * rightUpy / n;

		rightDown = Math.pow(rightDown, 2) / n;

//		if (leftDown - rightDown == 0) {
//			return DataUtil.handleZeroValue(leftUp - rightUp);
//		}
//
//		Double result = (leftUp - rightUp) / (leftDown - rightDown);
		return DataUtil.divNearZeroValue(leftUp - rightUp, leftDown - rightDown);
	}

}
