コード例 #1
0
ファイル: funcs.go プロジェクト: noblehng/bosun
// forecast_lr returns the number of seconds a linear regression predicts the
// series will take to reach y_val.
func (e *State) forecast_lr(dps Series, args ...float64) float64 {
	const tenYears = time.Hour * 24 * 365 * 10
	yVal := args[0]
	var x []float64
	var y []float64
	for k, v := range dps {
		x = append(x, float64(k.Unix()))
		y = append(y, v)
	}
	var slope, intercept, _, _, _, _ = stats.LinearRegression(x, y)
	it := (yVal - intercept) / slope
	var i64 int64
	if it < math.MinInt64 {
		i64 = math.MinInt64
	} else if it > math.MaxInt64 {
		i64 = math.MaxInt64
	} else if math.IsNaN(it) {
		i64 = e.now.Unix()
	} else {
		i64 = int64(it)
	}
	t := time.Unix(i64, 0)
	s := -e.now.Sub(t)
	if s < -tenYears {
		s = -tenYears
	} else if s > tenYears {
		s = tenYears
	}
	return s.Seconds()
}
コード例 #2
0
ファイル: metric2.go プロジェクト: bxy09/gfstat
func AlphaBeta(c MetricCalculator) (alpha, beta float64, err error) {
	const (
		AlphaKey = "Alpha"
		BetaKey  = "Beta"
	)
	var exist bool
	alpha, exist = c.scalarCache[AlphaKey]
	if !exist {
		rb := c.BenchRatio()
		rp := c.PortfolioRatio()
		xRBench := make([]float64, len(rb))
		xRPortfolio := make([]float64, len(rp))
		periodRf := Rf / c.Period()
		for i, p := range rb {
			xRBench[i] = p - periodRf
		}
		for i, p := range rp {
			xRPortfolio[i] = p - periodRf
		}
		beta, alpha, _, _, _, _ = stats.LinearRegression(rb, rp)
		c.scalarCache[AlphaKey] = alpha
		c.scalarCache[BetaKey] = beta
		return
	}
	beta, exist = c.scalarCache[BetaKey]
	if !exist {
		err = errors.New(BetaKey + " absent, internal error!")
		return
	}
	return
}
コード例 #3
0
ファイル: capm.go プロジェクト: bxy09/gfstat
func Beta(Ra, Rb, Rf *utils.SlidingWindow) (float64, error) {
	xRb, err := Excess(Rb, Rf)
	if err != nil {
		return math.NaN(), err
	}
	xRa, err := Excess(Ra, Rf)
	if err != nil {
		return math.NaN(), err
	}
	var slope, _, _, _, _, _ = stats.LinearRegression(xRb.Data(), xRa.Data())
	return slope, nil
}
コード例 #4
0
ファイル: capm.go プロジェクト: bxy09/gfstat
/// <summary>
/// 阿尔法
/// </summary>
/// <param name="Ra"></param>
/// <param name="Rb"></param>
/// <param name="Rf"></param>
/// <returns></returns>????
func Alpha(Ra, Rb, Rf *utils.SlidingWindow) (float64, error) {
	xRb, err := Excess(Rb, Rf)
	if err != nil {
		return math.NaN(), err
	}
	xRa, err := Excess(Ra, Rf)
	if err != nil {
		return math.NaN(), err
	}
	var _, intercept, _, _, _, _ = stats.LinearRegression(xRb.Data(), xRa.Data())
	return intercept, nil
}
コード例 #5
0
ファイル: funcs.go プロジェクト: noblehng/bosun
// line_lr generates a series representing the line up to duration in the future.
func line_lr(dps Series, d time.Duration) Series {
	var x []float64
	var y []float64
	sortedDPS := NewSortedSeries(dps)
	var maxT time.Time
	if len(sortedDPS) > 1 {
		maxT = sortedDPS[len(sortedDPS)-1].T
	}
	for _, v := range sortedDPS {
		xv := float64(v.T.Unix())
		x = append(x, xv)
		y = append(y, v.V)
	}
	var slope, intercept, _, _, _, _ = stats.LinearRegression(x, y)
	s := make(Series)
	// First point in the regression line
	s[maxT] = float64(maxT.Unix())*slope + intercept
	// Last point
	last := maxT.Add(d)
	s[last] = float64(last.Unix())*slope + intercept
	return s
}