本文整理汇总了Golang中github.com/bxy09/gfstat/performance/utils.SlidingWindow.Data方法的典型用法代码示例。如果您正苦于以下问题:Golang SlidingWindow.Data方法的具体用法?Golang SlidingWindow.Data怎么用?Golang SlidingWindow.Data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/bxy09/gfstat/performance/utils.SlidingWindow
的用法示例。
在下文中一共展示了SlidingWindow.Data方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: Kappa
/// <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
}
示例3: 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
}
示例4: Variance
/// <summary>
/// 方差
/// </summary>
func Variance(Ra *utils.SlidingWindow) (float64, error) {
if Ra == nil || Ra.Count() <= 1 {
return math.NaN(), errors.New("In Variance, Ra == nil || Ra.Count() <= 1")
}
result := 0.0
mean := Ra.Average()
for i := 0; i < Ra.Count(); i++ {
result += (Ra.Data()[i] - mean) * (Ra.Data()[i] - mean)
}
return result / (float64)(Ra.Count()-1), nil
}
示例5: 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
}
示例6: 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
}
示例7: 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
}
示例8: Calculate
/// <param name="prices"></param>
/// <param name="method"></param>
/// <returns></returns>
func Calculate(prices *utils.SlidingWindow, method string) (*utils.SlidingWindow, error) {
if prices == nil {
return nil, errors.New("Prices Utils Sliding Window is nil")
}
if prices.Count() == 0 {
return nil, errors.New("Returns Windows content is Zero")
}
lastPrice := prices.First()
returns, err := utils.NewSlidingWindow(prices.Count())
if err != nil {
return nil, errors.New("create a Sliding Window is Error !!")
}
switch method {
case "simple":
case "discrete":
for i := 0; i < prices.Count(); i++ {
price := prices.Data()[i]
if lastPrice != 0.0 {
returns.Add(price/lastPrice - 1.0)
} else {
returns.Add(0.0)
}
lastPrice = price
}
case "compound":
case "log":
for i := 0; i < prices.Count(); i++ {
price := prices.Data()[i]
if lastPrice != 0.0 {
returns.Add(math.Log(price / lastPrice))
} else {
returns.Add(0.0)
}
lastPrice = price
}
default:
return nil, errors.New("The input Method is nil !!!")
}
return returns, nil
}
示例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
}
示例10: DRatio
/// <summary>
/// d ratio of the return distribution
/// The d ratio is similar to the Bernado Ledoit ratio but inverted and
/// taking into account the frequency of positive and negative returns.
/// </summary>
func DRatio(Ra *utils.SlidingWindow) (float64, error) {
if Ra == nil {
return math.NaN(), errors.New("In DRatio, Ra == nil")
}
if Ra.Count() <= 0 {
return math.NaN(), errors.New("In DRatio, Ra.Count() <= 0")
}
upList, _ := utils.NewSlidingWindow(Ra.Count())
downList, _ := utils.NewSlidingWindow(Ra.Count())
for i := 0; i < Ra.Count(); i++ {
if Ra.Data()[i] < 0 {
downList.Add(Ra.Data()[i])
} else if Ra.Data()[i] > 0 {
upList.Add(Ra.Data()[i])
}
}
return -(downList.Sum() * float64(downList.Count())) / (float64(upList.Sum()) * float64(upList.Count())), nil
}
示例11: UpDownRatios
/// <summary>
/// 只测试了默认参数
/// Calculate metrics on how the asset in R performed in up and down markets,
/// measured by periods when the benchmark asset was up or down.
/// Up (Down) Capture Ratio: this is a measure of an investment's compound
/// return when the benchmark was up (down) divided by the benchmark's compound
/// return when the benchmark was up (down). The greater (lower) the value, the
/// better.(Up越大越好,Down越小越好)
///
/// Up (Down) Number Ratio: similarly, this is a measure of the number of
/// periods that the investment was up (down) when the benchmark was up (down),
/// divided by the number of periods that the Benchmark was up (down).(Up越大越好,Down越小越好)
///
/// Up (Down) Percentage Ratio: this is a measure of the number of periods that
/// the investment outperformed the benchmark when the benchmark was up (down),
/// divided by the number of periods that the benchmark was up (down). Unlike
/// the prior two metrics, in both cases a higher value is better.(Up、Down均为越大越好)
/// (当市场涨跌时,组合收益率涨跌所占比率,)
/// </summary>
func UpDownRatios(Ra *utils.SlidingWindow, Rb *utils.SlidingWindow) (float64, error) {
var cumRa = 0.0
var cumRb = 0.0
var result = 0.0
method := "Capture"
side := "Up"
switch method {
case "Capture":
switch side {
case "Up":
UpRa, err := utils.NewSlidingWindow(Ra.Count())
if err != nil {
return math.NaN(), err
}
UpRb, err := utils.NewSlidingWindow(Ra.Count())
if err != nil {
return math.NaN(), err
}
for i := 0; i < Ra.Count(); i++ {
if Rb.Data()[i] > 0 {
UpRa.Add(Ra.Data()[i])
UpRb.Add(Rb.Data()[i])
}
}
cumRa = UpRa.Sum()
cumRb = UpRb.Sum()
result = cumRa / cumRb
return result, nil
case "Down":
DnRa, err := utils.NewSlidingWindow(Ra.Count())
if err != nil {
return math.NaN(), err
}
DnRb, err := utils.NewSlidingWindow(Ra.Count())
if err != nil {
return math.NaN(), err
}
for i := 0; i < Ra.Count(); i++ {
if Rb.Data()[i] <= 0 {
DnRa.Add(Ra.Data()[i])
DnRb.Add(Rb.Data()[i])
}
}
cumRa = DnRa.Sum()
cumRb = DnRb.Sum()
result = cumRa / cumRb
return result, nil
default:
return math.NaN(), errors.New("In UpDownRatios, method Default!!")
}
case "Number":
switch side {
case "Up":
UpRa, err := utils.NewSlidingWindow(Ra.Count())
if err != nil {
return math.NaN(), err
}
UpRb, err := utils.NewSlidingWindow(Ra.Count())
if err != nil {
return math.NaN(), err
}
for i := 0; i < Ra.Count(); i++ {
if Ra.Data()[i] > 0 && Rb.Data()[i] > 0 {
UpRa.Add(Ra.Data()[i])
}
}
for i := 0; i < Ra.Count(); i++ {
if Rb.Data()[i] > 0 {
UpRb.Add(Rb.Data()[i])
}
}
//.........这里部分代码省略.........