/// <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 }
func (this *P2S2FWrapper) Process(AssetPriceReturns, AssetPriceBenchMark []float64, date []time.Time) (float64, error) { if AssetPriceReturns == nil || AssetPriceBenchMark == nil { return math.NaN(), errors.New("The Input RA or RB are Error !!!") } var err error Period := getPeriod(date) if Period == 2520 { AssetPriceReturns, err = reorganizeInputPrice(date, AssetPriceReturns) AssetPriceBenchMark, err = reorganizeInputPrice(date, AssetPriceBenchMark) if err != nil { return math.NaN(), errors.New("Reorganize Minutes Price Error !!!") } Period = 252 } if this.param1 == 252 { this.param1 = Period } if this.param2 == 252 { this.param2 = Period } if this.param1 == 0.03 { this.param1 = this.param1 / Period } if this.param2 == 0.03 { this.param2 = this.param2 / Period } Price, err := utils.NewSlidingWindow(len(AssetPriceReturns)) if err != nil { return math.NaN(), err } for _, val := range AssetPriceReturns { Price.Add(val) } Ra, err := Calculate(Price, "discrete") if err != nil { return math.NaN(), err } Price_Bench, err := utils.NewSlidingWindow(len(AssetPriceBenchMark)) if err != nil { return math.NaN(), err } for _, val := range AssetPriceBenchMark { Price_Bench.Add(val) } Rb, err := Calculate(Price_Bench, "discrete") if err != nil { return math.NaN(), err } return this.function(Ra, Rb, this.param1, this.param2) }
/// <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 }
/// <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 }
func (this *P1S1F1SWrapper) Process(AssetPriceReturns, AssetPriceBenchMark []float64, date []time.Time) (float64, error) { if AssetPriceReturns == nil { return math.NaN(), errors.New("The Input RA is Error !!!") } var err error Period := getPeriod(date) if Period == 2520 { AssetPriceReturns, err = reorganizeInputPrice(date, AssetPriceReturns) if err != nil { return math.NaN(), errors.New("Reorganize Minutes Price Error !!!") } Period = 252 } if this.param == 252 { this.param = Period } Price, err := utils.NewSlidingWindow(len(AssetPriceReturns)) if err != nil { return math.NaN(), err } for _, val := range AssetPriceReturns { Price.Add(val) } Ra, err := Calculate(Price, "discrete") if err != nil { return math.NaN(), err } if this.param == 0.03 { this.param = this.param / Period } return this.function(Ra, this.param, this.str) }
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 }
/// <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 }
/// <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 }
func Test_Performance(t *testing.T) { filename := "testData/555665746e955230ea000001_profit_table.csv" data, bench_mark, date := ReadFromCSV(filename) Price, _ := utils.NewSlidingWindow(len(data)) for _, val := range data { Price.Add(val) } Ra, _ := Calculate(Price, "discrete") Save_CSV_Data(Ra.Data()) for key, _ := range PerformanceMap { result, err := PerformanceMap[key].Process(data, bench_mark, date) fmt.Println(key, " Result: ", result, "Error: ", err) } }
/// <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 }
/// <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]) } } cumRa = float64(UpRa.Count()) cumRb = float64(UpRb.Count()) 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 Ra.Data()[i] < 0 && Rb.Data()[i] < 0 { DnRa.Add(Ra.Data()[i]) } } for i := 0; i < Ra.Count(); i++ { if Rb.Data()[i] < 0 { DnRb.Add(Rb.Data()[i]) } } cumRa = float64(DnRa.Count()) cumRb = float64(DnRb.Count()) result = cumRa / cumRb return result, nil default: return math.NaN(), errors.New("In UpDownRatios, method default 2 error !!!") } case "Percent": 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] > Rb.Data()[i] && 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]) } } cumRa = float64(UpRa.Count()) cumRb = float64(UpRb.Count()) 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 Ra.Data()[i] > Rb.Data()[i] && Rb.Data()[i] < 0 { DnRa.Add(Ra.Data()[i]) } } for i := 0; i < Ra.Count(); i++ { if Rb.Data()[i] < 0 { DnRb.Add(Rb.Data()[i]) } } cumRa = float64(DnRa.Count()) cumRb = float64(DnRb.Count()) result = cumRa / cumRb return result, nil default: return math.NaN(), errors.New("In UpDownRatios, method default 3 is Error !!!") } default: return math.NaN(), errors.New("In UpDownRatios, method default 4 is Error !!!") } return math.NaN(), nil }