Exemple #1
0
/// <summary>
/// 峰度
/// </summary>
// = "sample"
func Kurtosis(Ra *utils.SlidingWindow) (float64, error) {
	if Ra == nil || Ra.Count() <= 3 {
		return math.NaN(), errors.New("In Kurtosis, Ra == nil || Ra.Count() <= 3")
	}

	n := float64(Ra.Count())
	method := "sample_excess"
	switch method {
	case "sample_excess": //kurtosis = sum((x-mean(x))^4/var(x)^2)*n*(n+1)/((n-1)*(n-2)*(n-3)) - 3*(n-1)^2/((n-2)*(n-3))
		var_data, err := Variance(Ra)
		if err != nil {
			return math.NaN(), err
		}
		add_Sliding, err := utils.Add(-Ra.Average(), Ra)
		if err != nil {
			return math.NaN(), err
		}
		pow_Sliding, err := utils.Power(add_Sliding, 4.0)
		if err != nil {
			return math.NaN(), err
		}
		multi_Sliding, err := utils.Multi(1.0/math.Pow(var_data, 2.0), pow_Sliding)
		if err != nil {
			return math.NaN(), err
		}
		return multi_Sliding.Sum()*n*(n+1.0)/((n-1.0)*(n-2.0)*(n-3.0)) - 3*(n-1.0)*(n-1.0)/((n-2.0)*(n-3.0)), nil
	default:
		return math.NaN(), errors.New("In Kurtosis, method is default")
	}
	return math.NaN(), nil
}
Exemple #2
0
/// <summary>
/// 偏度
/// </summary>
// default = "moment"
func Skewness(Ra *utils.SlidingWindow) (float64, error) {
	if Ra == nil || Ra.Count() <= 2 {
		return math.NaN(), errors.New("In Skewness, Ra == nil || Ra.Count() <= 2")
	}

	n := float64(Ra.Count())
	method := "moment"
	switch method {
	//"moment", "fisher", "sample"
	case "moment": //skewness = sum((x-mean(x))^3/sqrt(var(x)*(n-1)/n)^3)/length(x)
		var_data, err := Variance(Ra)
		if err != nil {
			return math.NaN(), err
		}
		add_Sliding, err := utils.Add(-Ra.Average(), Ra)
		if err != nil {
			return math.NaN(), err
		}
		pow_Sliding, err := utils.Power(add_Sliding, 3.0)
		if err != nil {
			return math.NaN(), err
		}
		multi_Sliding, err := utils.Multi(1.0/math.Pow(var_data*(n-1.0)/n, 1.5), pow_Sliding)
		if err != nil {
			return math.NaN(), err
		}
		return multi_Sliding.Sum() / n, nil
	default:
		return math.NaN(), errors.New("In Skewness, method is default")
	}
	return math.NaN(), nil
}
Exemple #3
0
/// <summary>
/// epsilon与R中不同,但似乎没有影响
/// Specific risk is the standard deviation of the error term in the
/// regression equation.
/// </summary>
func SpecificRisk(Ra *utils.SlidingWindow, Rb *utils.SlidingWindow, scale float64, Rf float64) (float64, error) {
	//Period = Frequency(Ra)
	alpha, err := Alpha2(Ra, Rb, Rf)
	if err != nil {
		return math.NaN(), err
	}
	beta, err := Beta2(Ra, Rb, Rf)
	if err != nil {
		return math.NaN(), err
	}
	add_Ra_Sliding, err := utils.Add(-Rf, Ra)
	if err != nil {
		return math.NaN(), err
	}
	add_Rb_Sliding, err := utils.Add(-Rf, Rb)
	if err != nil {
		return math.NaN(), err
	}
	multi_beta_Slidinig, err := utils.Multi(beta, add_Rb_Sliding)
	if err != nil {
		return math.NaN(), err
	}
	sub_Ra_Beta, err := utils.Sub(add_Ra_Sliding, multi_beta_Slidinig)
	if err != nil {
		return math.NaN(), err
	}
	epsilon, err := utils.Add(-alpha, sub_Ra_Beta)
	if err != nil {
		return math.NaN(), err
	}
	var_eps, err := Variance(epsilon)
	if err != nil {
		return math.NaN(), err
	}
	var result = math.Sqrt(var_eps*float64(epsilon.Count()-1)/float64(epsilon.Count())) * math.Sqrt(float64(scale))
	return result, nil
}
Exemple #4
0
/// <summary>
///  Upside Risk is the similar of semideviation taking the return above the
///  Minimum Acceptable Return instead of using the mean return or zero.
///  (一般来说,非对称类的比较,单求此统计量意义有限)
/// </summary>
func UpsideRisk(Ra *utils.SlidingWindow, MAR float64, stat string) (float64, error) {
	r, err := utils.AboveValue(Ra, MAR)
	if err != nil {
		return math.NaN(), err
	}
	var length float64
	method := "subset"
	switch method {
	case "full":
		length = float64(Ra.Count())
		break
	case "subset":
		length = float64(r.Count())
		break
	default:
		return math.NaN(), errors.New("In Upside Risk, method is default !!!")
	}
	if length <= 0 {
		return 0, nil
	}
	var result float64
	switch stat {
	case "risk":
		add_Sliding, err := utils.Add(-MAR, r)
		if err != nil {
			return math.NaN(), err
		}
		pow_Sliding, err := utils.Power(add_Sliding, 2.0)
		if err != nil {
			return math.NaN(), err
		}
		multi_Sliding, err := utils.Multi(1.0/length, pow_Sliding)
		if err != nil {
			return math.NaN(), err
		}
		result = math.Sqrt(multi_Sliding.Sum())
		break
	case "variance":
		add_Sliding, err := utils.Add(-MAR, r)
		if err != nil {
			return math.NaN(), err
		}
		pow_Sliding, err := utils.Power(add_Sliding, 2.0)
		if err != nil {
			return math.NaN(), err
		}
		multi_Sliding, err := utils.Multi(1.0/length, pow_Sliding)
		if err != nil {
			return math.NaN(), err
		}
		result = multi_Sliding.Sum()
		break
	case "potential":
		add_Sliding, err := utils.Add(-MAR, r)
		if err != nil {
			return math.NaN(), err
		}
		multi_Slding, err := utils.Multi(1.0/length, add_Sliding)
		if err != nil {
			return math.NaN(), err
		}
		result = multi_Slding.Sum()
		break
	default:
		return math.NaN(), errors.New("In UpSide Risk, method is default !!!")
	}

	return result, nil
}
Exemple #5
0
/// <summary>
/// Appraisal ratio is the Jensen's alpha adjusted for specific risk. The numerator
/// is divided by specific risk instead of total risk.
/// </summary>
func AppraisalRatio(Ra *utils.SlidingWindow, Rb *utils.SlidingWindow, scale float64, Rf float64, method string) (float64, error) {
	var result = 0.0
	switch method {
	case "appraisal":
		be_data, err := Beta2(Ra, Rb, Rf)
		if err != nil {
			return math.NaN(), err
		}
		multi_Sliding, err := utils.Multi(be_data, Rb)
		if err != nil {
			return math.NaN(), err
		}
		sub_Sliding, err := utils.Sub(Ra, multi_Sliding)
		if err != nil {
			return math.NaN(), err
		}
		al_data, err := Alpha2(Ra, Rb, Rf)
		if err != nil {
			return math.NaN(), err
		}
		epsilon, err := utils.Add(-al_data, sub_Sliding)
		if err != nil {
			return math.NaN(), err
		}
		add_Sliding, err := utils.Add(-epsilon.Average(), epsilon)
		if err != nil {
			return math.NaN(), err
		}
		pow_Sliding, err := utils.Power(add_Sliding, 2)
		if err != nil {
			return math.NaN(), err
		}
		specifikRisk := math.Sqrt(pow_Sliding.Sum()/float64(epsilon.Count())) * math.Sqrt(float64(scale))
		jsa_data, err := JensenAlpha2(Ra, Rb, Rf, scale)
		if err != nil {
			return math.NaN(), err
		}
		result = jsa_data / specifikRisk
		break
	case "modified":
		jsa2_data, err := JensenAlpha2(Ra, Rb, Rf, scale)
		if err != nil {
			return math.NaN(), err
		}
		be2_data, err := Beta2(Ra, Rb, Rf)
		if err != nil {
			return math.NaN(), err
		}
		result = jsa2_data / be2_data
		break
	case "alternative":
		jsa2_data, err := JensenAlpha2(Ra, Rb, Rf, scale)
		if err != nil {
			return math.NaN(), err
		}
		sr_data, err := SystematicRisk(Ra, Rb, scale, Rf)
		if err != nil {
			return math.NaN(), err
		}
		result = jsa2_data / sr_data
		break
	default:
		return math.NaN(), errors.New("In AppraisalRatio, method is default !!!")
	}
	return result, nil
}