當前位置: 首頁>>代碼示例>>Golang>>正文


Golang SlidingWindow.Count方法代碼示例

本文整理匯總了Golang中github.com/bxy09/gfstat/performance/utils.SlidingWindow.Count方法的典型用法代碼示例。如果您正苦於以下問題:Golang SlidingWindow.Count方法的具體用法?Golang SlidingWindow.Count怎麽用?Golang SlidingWindow.Count使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/bxy09/gfstat/performance/utils.SlidingWindow的用法示例。


在下文中一共展示了SlidingWindow.Count方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Skewness

/// <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
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:36,代碼來源:performance1.go

示例2: Beta2

func Beta2(Ra, Rb *utils.SlidingWindow, Rf float64) (float64, error) {
	RfList, err := utils.CreateList(Rf, Ra.Count())
	if err != nil {
		return math.NaN(), err
	}
	return Beta(Ra, Rb, RfList)
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:7,代碼來源:capm.go

示例3: Kurtosis

/// <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
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:35,代碼來源:performance1.go

示例4: BurkeRatio

/// <summary>
/// To calculate Burke ratio we take the difference between the portfolio
/// return and the risk free rate and we divide it by the square root of the
/// sum of the square of the drawdowns. To calculate the modified Burke ratio
/// we just multiply the Burke ratio by the square root of the number of datas.
/// (一種調整收益率的計算方式,調整是通過drawdown的平方和進行的)
/// </summary>
func BurkeRatio(Ra *utils.SlidingWindow, Rf float64, scale float64) (float64, error) {
	var len = Ra.Count()
	var in_drawdown = false
	var peak = 1
	var temp = 0.0
	drawdown, err := utils.NewSlidingWindow(len)
	if err != nil {
		return math.NaN(), err
	}
	for i := 1; i < len; i++ {
		if Ra.Data()[i] < 0 {
			if !in_drawdown {
				peak = i - 1
				in_drawdown = true
			}
		} else {
			if in_drawdown {
				temp = 1.0
				for j := peak + 1; j < i; j++ {
					temp = temp * (1.0 + Ra.Data()[j])
				}
				drawdown.Add(temp - 1.0) //Source
				in_drawdown = false
			}
		}
	}

	if in_drawdown {
		temp = 1.0
		for j := peak + 1; j < len; j++ {
			temp = temp * (1.0 + Ra.Data()[j])
		}
		drawdown.Add(temp - 1.0) //Source
		//drawdown.Add((temp - 1.0) * 100.0)
		in_drawdown = false
	}
	//var Rp = Annualized(Ra, scale, true) - 1.0--->Source
	Rp, err := Annualized(Ra, scale, true)
	if err != nil {
		return math.NaN(), err
	}
	var result float64

	if drawdown.Count() != 0 {
		pow_Sliding, err := utils.Power(drawdown, 2)
		if err != nil {
			return math.NaN(), err
		}
		Rf = Rf * scale
		result = (Rp - Rf) / math.Sqrt(pow_Sliding.Sum())
	} else {
		result = 0
	}

	modified := true
	if modified {
		result = result * math.Sqrt(float64(len))
	}
	return result, nil
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:67,代碼來源:performance1.go

示例5: DownsideDeviation2

//= "full"
// = false
func DownsideDeviation2(Ra *utils.SlidingWindow, MAR float64) (float64, error) {
	if Ra == nil || Ra.Count() <= 0 {
		return math.NaN(), errors.New("In DownsideDeviation2, Ra == nil || Ra.Count() <= 0")
	}
	newMAR, _ := utils.CreateList(MAR, Ra.Count())
	return DownsideDeviation(Ra, newMAR)
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:9,代碼來源:performance1.go

示例6: UpsidePotentialRatio

/// <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
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:35,代碼來源:performance1.go

示例7: SharpeRatio

/// <summary>
/// calculate a traditional or modified Sharpe Ratio of Return over StdDev or
/// VaR or ES
///
/// The Sharpe ratio is simply the return per unit of risk (represented by
/// variability).  In the classic case, the unit of risk is the standard
/// deviation of the returns.
/// </summary>
func SharpeRatio(Ra *utils.SlidingWindow, Rf_val float64, scale float64) (float64, error) {
	Rf, err := utils.CreateList(Rf_val, Ra.Count())
	if err != nil {
		return math.NaN(), err
	}
	xR, err := Excess(Ra, Rf)
	if err != nil {
		return math.NaN(), err
	}
	numerator := 0.0
	denominator := 0.0
	annualize := 1
	if annualize == 1 {
		denominator, err = StdDev_Annualized(Ra, scale)
		if err != nil {
			return math.NaN(), err
		}
		numerator, err = Annualized(xR, scale, true)
		if err != nil {
			return math.NaN(), err
		}
	} else {
		denominator, err = StdDev(Ra)
		if err != nil {
			return math.NaN(), err
		}
		numerator = xR.Average()
	}

	return numerator / denominator, nil
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:39,代碼來源:performance1.go

示例8: UpsideFrequency

/// <summary>
/// subset of returns that are
/// more than the target (or Minimum Acceptable Returns (MAR)) returns and
/// divide the length of this subset by the total number of returns.
/// (超過MAR的頻率)
/// </summary>
func UpsideFrequency(Ra *utils.SlidingWindow, MAR float64) (float64, error) {
	aboveMAR, err := utils.AboveValue(Ra, MAR)
	if err != nil {
		return math.NaN(), err
	}
	return float64(aboveMAR.Count()) / float64(Ra.Count()), nil
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:13,代碼來源:performance1.go

示例9: MeanAbsoluteDeviation

/// <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
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:14,代碼來源:performance1.go

示例10: MeanGeometric

/// <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
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:11,代碼來源:performance1.go

示例11: DownsideDeviation

/// <summary>
/// downside risk (deviation, variance) of the return distribution
/// Downside deviation, semideviation, and semivariance are measures of downside
/// risk.
/// </summary>
// = "full"
// = false
//func DownsideDeviation(Ra *utils.SlidingWindow, MAR *utils.SlidingWindow, method string, potential bool) float64 {
func DownsideDeviation(Ra *utils.SlidingWindow, MAR *utils.SlidingWindow) (float64, error) {
	if Ra == nil {
		return math.NaN(), errors.New("In DownsideDeviation, Ra == nil")
	}
	if Ra.Count() <= 0 {
		return math.NaN(), errors.New("In DownsideDeviation, Ra.Count() <= 0")
	}

	r, err := utils.NewSlidingWindow(Ra.Count())
	if err != nil {
		return math.NaN(), err
	}

	newMAR, err := utils.NewSlidingWindow(Ra.Count())
	if err != nil {
		return math.NaN(), err
	}
	len := 0.0
	result := 0.0
	for i := 0; i < Ra.Count(); i++ {
		if Ra.Data()[i] < MAR.Data()[i] {
			r.Add(Ra.Data()[i])
			newMAR.Add(MAR.Data()[i])
		}
	}

	potential := false
	method := "subset"

	if method == "full" {
		len = float64(Ra.Count())
	} else if method == "subset" {
		len = float64(r.Count())
	} else {
		return math.NaN(), errors.New("In DownsideDeviation, method default !!!")
	}
	if newMAR.Count() <= 0 || r.Count() <= 0 || len <= 0 {
		return math.NaN(), errors.New("In DownsideDeviation, newMAR.Count() <= 0 || r.Count() <= 0 || len <= 0")
	}
	if potential {
		sub_Sliding, err := utils.Sub(newMAR, r)
		if err != nil {
			return math.NaN(), err
		}
		result = sub_Sliding.Sum() / len
	} else {
		sub_Sliding, err := utils.Sub(newMAR, r)
		if err != nil {
			return math.NaN(), err
		}
		pow_Sliding, err := utils.Power(sub_Sliding, 2.0)
		if err != nil {
			return math.NaN(), err
		}
		result = math.Sqrt(pow_Sliding.Sum() / len)
	}
	return result, nil
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:66,代碼來源:performance2.go

示例12: Centered

/// <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)
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:11,代碼來源:return.go

示例13: Excess

func Excess(returns, Rf *utils.SlidingWindow) (*utils.SlidingWindow, error) {
	result, err := utils.NewSlidingWindow(returns.Count())
	if err != nil {
		return nil, err
	}
	for i := 0; i < returns.Count(); i++ {
		result.Add(returns.Data()[i] - Rf.Data()[i])
	}
	return result, nil
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:10,代碼來源:return.go

示例14: FamaBeta

/// <summary>
/// Fama beta is a beta used to calculate the loss of diversification. It is made
/// so that the systematic risk is equivalent to the total portfolio risk.
/// </summary>
func FamaBeta(Ra *utils.SlidingWindow, Rb *utils.SlidingWindow, Ra_sclae float64, Rb_scale float64) (float64, error) {
	var n1 = Ra.Count()
	var n2 = Rb.Count()
	var_Ra, err := Variance(Ra)
	if err != nil {
		return math.NaN(), err
	}
	var_Rb, err := Variance(Rb)
	if err != nil {
		return math.NaN(), err
	}
	var result = math.Sqrt(var_Ra*float64(n1-1)/float64(n1)) * math.Sqrt(float64(Ra_sclae)) / (math.Sqrt(var_Rb*float64(n2-1)/float64(n2)) * math.Sqrt(float64(Rb_scale)))
	return result, nil
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:18,代碼來源:performance2.go

示例15: Relative

/// <param name="Ra"></param>
/// <param name="Rb"></param>
/// <returns></returns>
func Relative(Ra, Rb *utils.SlidingWindow) (*utils.SlidingWindow, error) {
	res4Ra := 1.0
	res4Rb := 1.0
	result, err := utils.NewSlidingWindow(Ra.Count())
	if err != nil {
		return nil, err
	}
	for i := 0; i < Ra.Count(); i++ {
		res4Ra = res4Ra * (1 + Ra.Data()[i])
		res4Rb = res4Rb * (1 + Rb.Data()[i])
		result.Add(res4Ra / res4Rb)
	}
	return result, nil
}
開發者ID:bxy09,項目名稱:gfstat,代碼行數:17,代碼來源:return.go


注:本文中的github.com/bxy09/gfstat/performance/utils.SlidingWindow.Count方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。