Beispiel #1
0
func S1(r *rng.GslRng, xp interface{}, stepSize float64) {
	ptr := xp.(*float64)
	oldX := *ptr
	u := rng.Uniform(r)
	newX := u*2*stepSize - stepSize + oldX
	*ptr = newX
}
Beispiel #2
0
func TestRng(t *testing.T) {
	var n int = 10
	rng.EnvSetup()
	T := rng.DefaultRngType()
	r := rng.RngAlloc(T)
	for i := 0; i < n; i++ {
		u := rng.Uniform(r)
		fmt.Printf("%.5f\n", u)
	}
	fmt.Println()
}
Beispiel #3
0
func TestHistogram(t *testing.T) {

	h := histogram.Histogram2dAlloc(10, 10)
	histogram.Histogram2dSetRangesUniform(h, 0.0, 1.0, 0.0, 1.0)
	histogram.Histogram2dAccumulate(h, 0.3, 0.3, 1)
	histogram.Histogram2dAccumulate(h, 0.8, 0.1, 5)
	histogram.Histogram2dAccumulate(h, 0.7, 0.9, 0.5)

	rng.EnvSetup()
	T := rng.DefaultRngType()
	r := rng.RngAlloc(T)

	hDim := h.Dim()
	p := histogram.Histogram2dPdfAlloc(hDim[0], hDim[1])
	histogram.Histogram2dPdfInit(p, h)
	for i := 0; i < 1000; i++ {
		u := rng.Uniform(r)
		v := rng.Uniform(r)
		_, x, y := histogram.Histogram2dPdfSample(p, u, v)
		fmt.Printf("%g %g\n", x, y)
	}
}
Beispiel #4
0
func TestSort(t *testing.T) {
	var n int = 100000
	var k int = 5
	x := make([]float64, n)
	small := make([]float64, k)
	rng.EnvSetup()
	T := rng.DefaultRngType()
	r := rng.RngAlloc(T)
	for i := 0; i < n; i++ {
		x[i] = rng.Uniform(r)
	}
	sort.SortSmallest(small, k, x, 1, n)
	fmt.Printf("%d smallest values from %d\n", k, n)
	for i := 0; i < k; i++ {
		fmt.Printf("%d: %.18f\n", i, small[i])
	}
}
Beispiel #5
0
func TestSortVectorIndex(t *testing.T) {
	var n int = 10000
	var k int = 5
	v := vector.VectorAlloc(n)
	p := permutation.PermutationAlloc(n)
	rng.EnvSetup()
	T := rng.DefaultRngType()
	r := rng.RngAlloc(T)
	for i := 0; i < n; i++ {
		vector.Set(v, i, rng.Uniform(r))
	}
	sort.SortVectorIndex(p, v)
	pData := p.Slice_().([]int)
	for i := 0; i < k; i++ {
		vpi := vector.Get(v, pData[i])
		fmt.Printf("order = %d, value = %g\n", i, vpi)
	}
}
Beispiel #6
0
func TestSiman(t *testing.T) {

	params := &siman.GslSimanParams{
		NumTries:   N_TRIES,
		ItersFixed: ITERS_FIXED_T,
		StepSize:   STEP_SIZE,
		K:          K,
		TInitial:   T_INITIAL,
		Mu:         MU_T,
		TMin:       T_MIN,
	}
	siman.InitializeGslSimanParams(params)

	var xInitial float64 = 15.5

	rng.EnvSetup()
	T := rng.DefaultRngType()
	r := rng.RngAlloc(T)
	fmt.Println(rng.Uniform(r))
	siman.Solve(r, &xInitial, E1, S1, M1, P1, nil, nil, nil, params)
}
Beispiel #7
0
func TestRobust(t *testing.T) {
	var p int = 2 // linear fit

	var a float64 = 1.45 // data slope
	var b float64 = 3.88 // data intercept

	var n int = 20

	X := matrix.MatrixAlloc(n, p)
	x := vector.VectorAlloc(n)
	y := vector.VectorAlloc(n)

	c := vector.VectorAlloc(p)
	cOls := vector.VectorAlloc(p)
	cov := matrix.MatrixAlloc(p, p)

	r := rng.RngAlloc(rng.DefaultRngType())

	// generate linear dataset
	for i := 0; i < n-3; i++ {
		dx := 10.0 / (float64(n) - 1.0)
		ei := rng.Uniform(r)
		xi := -5.0 + float64(i)*dx
		yi := a*xi + b

		vector.Set(x, i, xi)
		vector.Set(y, i, yi+ei)
	}

	// add a few outliers
	vector.Set(x, n-3, 4.7)
	vector.Set(y, n-3, -8.3)

	vector.Set(x, n-2, 3.5)
	vector.Set(y, n-2, -6.7)

	vector.Set(x, n-1, 4.1)
	vector.Set(y, n-1, -6.0)

	// construct design matrix X for linear fit
	for i := 0; i < n; i++ {
		xi := vector.Get(x, i)
		matrix.Set(X, i, 0, 1.0)
		matrix.Set(X, i, 1, xi)
	}

	// perform robust and OLS fit
	DoFit(multifit.GSL_MULTIFIT_ROBUST_OLS, X, y, cOls, cov)
	DoFit(multifit.GSL_MULTIFIT_ROBUST_BISQUARE, X, y, c, cov)

	// output data and model
	for i := 0; i < n; i++ {
		xi := vector.Get(x, i)
		yi := vector.Get(y, i)
		v := matrix.Row(X, i).Vector()
		_, yRob, _ := multifit.RobustEst(v, c, cov)
		_, yOls, _ := multifit.RobustEst(v, cOls, cov)

		fmt.Printf("%g %g %g %g\n", xi, yi, yRob, yOls)
	}

	fmt.Printf("# best fit: Y = %g + %g X\n", vector.Get(c, 0), vector.Get(c, 1))
	fmt.Printf("# covariance matrix:\n")
	fmt.Printf("# [ %+.5e, %+.5e\n", matrix.Get(cov, 0, 0), matrix.Get(cov, 0, 1))
	fmt.Printf("#   %+.5e, %+.5e\n", matrix.Get(cov, 1, 0), matrix.Get(cov, 1, 1))
}