Пример #1
0
/// <summary>
/// Upside Potential Ratio,compared to Sortino, was a further improvement, extending the
/// measurement of only upside on the numerator, and only downside of the
/// denominator of the ratio equation.
/// (分子只考虑超过MAR部分,分母只考虑DownsideDeviation的下跌风险)
/// </summary>
func UpsidePotentialRatio(Ra *utils.SlidingWindow, MAR float64) (float64, error) {
	//var r = Ra.Where<float64>(singleData => singleData > MAR).ToList<float64>();
	r, err := utils.AboveValue(Ra, MAR)
	if err != nil {
		return math.NaN(), err
	}
	var length int
	method := "subset"
	switch method {
	case "full":
		length = Ra.Count()
		break
	case "subset":
		length = r.Count()
		break
	default:
		return math.NaN(), errors.New("In UpsidePotentialRatio, method is default !!!")
	}
	add_Sliding, err := utils.Add(-MAR, r)
	if err != nil {
		return math.NaN(), err
	}
	dd2Data, err := DownsideDeviation2(Ra, MAR)
	if err != nil {
		return math.NaN(), err
	}
	var result = (add_Sliding.Sum() / float64(length)) / dd2Data
	return result, nil
}
Пример #2
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
}
Пример #3
0
/// <summary>
/// Kappa is a generalized downside risk-adjusted performance measure.
/// To calculate it, we take the difference of the mean of the distribution
/// to the target and we divide it by the l-root of the lth lower partial
/// moment. To calculate the lth lower partial moment we take the subset of
/// returns below the target and we sum the differences of the target to
/// these returns. We then return return this sum divided by the length of
/// the whole distribution.
/// (非年化的超MAR平均收益率通过l阶根的低于MAR的收益率序列的l阶矩)
/// </summary>
func Kappa(Ra *utils.SlidingWindow, MAR float64, l float64) (float64, error) {
	undervalues, err := utils.NewSlidingWindow(Ra.Count())
	if err != nil {
		return math.NaN(), err
	}
	for i := 0; i < Ra.Count(); i++ {
		if Ra.Data()[i] < MAR {
			undervalues.Add(Ra.Data()[i])
		}
	}

	var n = float64(Ra.Count())
	var m = float64(Ra.Average())
	neg_Sliding, err := utils.Negative(undervalues)
	if err != nil {
		return math.NaN(), err
	}
	add_Sliding, err := utils.Add(MAR, neg_Sliding)
	if err != nil {
		return math.NaN(), err
	}
	pow_Sliding, err := utils.Power(add_Sliding, float64(l))
	if err != nil {
		return math.NaN(), err
	}
	var temp = pow_Sliding.Sum() / n
	return (m - MAR) / math.Pow(temp, (1.0/float64(l))), nil
}
Пример #4
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
}
Пример #5
0
/// <summary>
/// 收益率序列的几何均值,非年化
/// </summary>
func MeanGeometric(Ra *utils.SlidingWindow) (float64, error) {
	if Ra.Count() <= 0 {
		return math.NaN(), errors.New("In MeanGeometric, Ra.Count() <= 0")
	}
	add_Sliding, _ := utils.Add(1, Ra)
	log_Sliding, _ := utils.Log(add_Sliding)
	return math.Exp(log_Sliding.Average()) - 1.0, nil
}
Пример #6
0
/// <summary>
/// To calculate Mean absolute deviation we take
/// the sum of the absolute value of the difference between the returns and the mean of the returns
/// and we divide it by the number of returns.
/// (描述收益率偏离均值得一个指标)
/// </summary>
func MeanAbsoluteDeviation(Ra *utils.SlidingWindow) (float64, error) {
	if Ra.Count() <= 0 {
		return math.NaN(), errors.New("In MeanAbsoluteDeviation, Ra.Count() <= 0")
	}
	add_Sliding, _ := utils.Add(-Ra.Average(), Ra)
	ads_Sliding, _ := utils.Abs(add_Sliding)
	return ads_Sliding.Sum() / float64(Ra.Count()), nil
}
Пример #7
0
/// <param name="returns"></param>
/// <returns></returns>
func Centered(returns *utils.SlidingWindow) (*utils.SlidingWindow, error) {
	if returns == nil {
		return nil, errors.New("Centered Sliding window is nil")
	}
	if returns.Count() == 0 {
		return nil, errors.New("Centered Count is Zero !!!")
	}
	return utils.Add(-returns.Average(), returns)
}
Пример #8
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
}
Пример #9
0
func PainRatio(Ra *utils.SlidingWindow, Rf float64, scale float64) (float64, error) {
	PI, err := PainIndex(Ra)
	if err != nil {
		return math.NaN(), err
	}
	n := Ra.Count()
	add_Sliding, err := utils.Add(1.0, Ra)
	if err != nil {
		return math.NaN(), err
	}
	prod_Sliding, err := utils.Prod(add_Sliding)
	if err != nil {
		return math.NaN(), err
	}
	Rp := math.Pow(prod_Sliding, float64(scale)/float64(n)) - 1.0
	Rf = Rf * scale
	return (Rp - Rf) / PI, nil
}
Пример #10
0
/// <param name="returns"></param>
/// <param name="geometric"></param>
/// <returns></returns>
func Cumulative(returns *utils.SlidingWindow, geometric bool) (float64, error) {
	if returns == nil {
		return math.NaN(), errors.New("Cumulative Sliding window is Nil !!!")
	}
	if returns.Count() == 0 {
		return math.NaN(), errors.New("Cumulative Count == 0 !!")
	}
	if !geometric {
		return (returns.Sum()), nil
	} else {
		add_data, err := utils.Add(1.0, returns)
		if err != nil {
			return math.NaN(), err
		}
		prod_data, err := utils.Prod(add_data)
		if err != nil {
			return math.NaN(), err
		}
		return (prod_data - 1.0), nil
	}
}
Пример #11
0
/// <param name="returns"></param>
/// <param name="scale"></param>
/// <param name="geometric"></param>
/// <returns></returns>
func Annualized(returns *utils.SlidingWindow, scale float64, geometric bool) (float64, error) {
	if returns == nil {
		return math.NaN(), errors.New("Returns Utils Sliding Window is nil")
	}
	if returns.Count() == 0 {
		return math.NaN(), errors.New("Returns Windows content is Zero")
	}
	n := returns.Count()
	if geometric {
		add_Sliding, err := utils.Add(1.0, returns)
		if err != nil {
			return math.NaN(), err
		}
		prod_Data, err := utils.Prod(add_Sliding)
		if err != nil {
			return math.NaN(), err
		}
		return math.Pow(prod_Data, float64(scale)/float64(n)) - 1.0, nil
	} else {
		return returns.Average() * float64(scale), nil
	}
}
Пример #12
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
}
Пример #13
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
}
Пример #14
0
/// <param name="returns"></param>
/// <param name="Rf"></param>
/// <returns></returns>
func Excess2(returns *utils.SlidingWindow, Rf float64) (*utils.SlidingWindow, error) {
	return utils.Add(-Rf, returns)
}