本文整理汇总了Golang中github.com/bxy09/gfstat/performance/utils.SlidingWindow类的典型用法代码示例。如果您正苦于以下问题:Golang SlidingWindow类的具体用法?Golang SlidingWindow怎么用?Golang SlidingWindow使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SlidingWindow类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: 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
示例2: 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
示例3: 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
示例4: 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
示例5: Drawdowns
/// <summary>
/// Calculate the drawdown levels in a timeseries
/// </summary>
//= true
func Drawdowns(Rb *utils.SlidingWindow) ([]float64, error) {
Ra := Rb.Data()
if Ra == nil || len(Ra) <= 0 {
return nil, errors.New("In Drawdowns, Ra == nil")
}
geometric := 1
curReturn := 1.0
curMaxReturn := 1.0 + Ra[0]
result := []float64{}
if geometric == 1 {
for _, r := range Ra {
curReturn = curReturn * (1.0 + r)
if curReturn > curMaxReturn {
curMaxReturn = curReturn
}
result = append(result, curReturn/curMaxReturn-1.0)
}
} else {
for _, r := range Ra {
curReturn = curReturn + r
if curReturn > curMaxReturn {
curMaxReturn = curReturn
}
result = append(result, curReturn/curMaxReturn-1.0)
}
}
return result, nil
}
开发者ID:bxy09,项目名称:gfstat,代码行数:35,代码来源:performance1.go
示例6: 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
示例7: 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
示例8: 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
示例9: 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
示例10: 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
示例11: ProspectRatio
/// <summary>
/// Prospect ratio is a ratio used to penalise loss since most people feel loss
/// greater than gain
/// (经验类型调整收益率,给损失赋予更大的权重)
/// </summary>
func ProspectRatio(Ra *utils.SlidingWindow, MAR float64) (float64, error) {
var n = Ra.Count()
SigD, err := DownsideDeviation2(Ra, MAR)
if err != nil {
return math.NaN(), err
}
positivevalues, negativevalues, err := utils.PosNegValues(Ra)
if err != nil {
return math.NaN(), err
}
var result = ((positivevalues.Sum()+2.25*negativevalues.Sum())/float64(n) - MAR) / SigD
return result, nil
}
开发者ID:bxy09,项目名称:gfstat,代码行数:20,代码来源:performance1.go
示例12: DownsideFrequency
/// <summary>
/// downside frequency of the return distribution
/// To calculate Downside Frequency, we take the subset of returns that are
/// less than the target (or Minimum Acceptable Returns (MAR)) returns and
/// divide the length of this subset by the total number of returns.
/// </summary>
func DownsideFrequency(Ra *utils.SlidingWindow, MAR *utils.SlidingWindow) (float64, error) {
if Ra == nil {
return math.NaN(), errors.New("In DownsideFrequency, Ra == nil")
}
if Ra.Count() <= 0 {
return math.NaN(), errors.New("In DownsideFrequency, Ra.Count() <= 0")
}
len := 0.0
for i := 0; i < Ra.Count(); i++ {
if Ra.Data()[i] < MAR.Data()[i] {
len++
}
}
return len / float64(Ra.Count()), nil
}
开发者ID:bxy09,项目名称:gfstat,代码行数:22,代码来源:performance2.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: PainRatio
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
}
开发者ID:bxy09,项目名称:gfstat,代码行数:18,代码来源:performance1.go
示例15: Cumulative
/// <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
}
}
开发者ID:bxy09,项目名称:gfstat,代码行数:24,代码来源:return.go
示例16: 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
示例17: 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
示例18: 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
示例19: 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
示例20: MSquared
/// <summary>
/// M squared is a risk adjusted return useful to judge the size of relative
/// performance between differents portfolios. With it you can compare portfolios
/// with different levels of risk.
/// (使得不同组合的收益率可比的调整措施)
/// </summary>
func MSquared(Ra *utils.SlidingWindow, Rb *utils.SlidingWindow, scale float64, Rf float64) (float64, error) {
var n = Ra.Count()
Rp, err := Annualized(Ra, scale, true)
if err != nil {
return math.NaN(), err
}
var_Ra_data, err := Variance(Ra)
if err != nil {
return math.NaN(), err
}
sigp := math.Sqrt(var_Ra_data*float64(n-1)/float64(n)) * math.Sqrt(float64(scale))
if err != nil {
return math.NaN(), err
}
var_Rb_data, err := Variance(Rb)
if err != nil {
return math.NaN(), err
}
var sigm = math.Sqrt(var_Rb_data*float64(n-1)/float64(n)) * math.Sqrt(float64(scale))
//var result = (Rp-Rf)*sigp/sigm + Rf//Source
Rf = Rf * scale
var result = (Rp-Rf)*sigm/sigp + Rf
return result, nil
}
开发者ID:bxy09,项目名称:gfstat,代码行数:30,代码来源:performance2.go
注:本文中的github.com/bxy09/gfstat/performance/utils.SlidingWindow类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论