Exemplo n.º 1
0
func TestMultimin(t *testing.T) {

	par := []float64{1.0, 2.0, 10.0, 20.0, 30.0}
	f := &multimin.GslMultiminFunction{
		Function:  MyF,
		Dimension: 2,
		Params:    par,
	}
	multimin.InitializeGslMultiminFunction(f)

	/* Starting point */
	x := vector.VectorAlloc(2)
	vector.Set(x, 0, 5.0)
	vector.Set(x, 1, 7.0)

	/* Set initial step sizes to 1 */
	ss := vector.VectorAlloc(2)
	vector.SetAll(ss, 1.0)

	T := multimin.GSL_MULTIMIN_FMINIMIZER_NSIMPLEX2
	s := multimin.FminimizerAlloc(T, 2)
	multimin.FminimizerSet(s, f, x, ss)

	iter := 0

	for {
		iter += 1
		status := gogsl.GslError(multimin.FminimizerIterate(s))
		if status != gogsl.GSL_SUCCESS {
			break
		}
		size := multimin.FminimizerSize(s)
		status = gogsl.GslError(multimin.TestSize(size, 1e-2))
		if status == gogsl.GSL_SUCCESS {
			fmt.Printf("converged to minimum at\n")
		}
		fmt.Printf("%5d %10.3e %10.3e f() = %7.3f size = %.3f\n",
			iter, vector.Get(s.X_(), 0), vector.Get(s.X_(), 1), s.Fval(), size)
		if status != gogsl.GSL_CONTINUE || iter >= 100 {
			break
		}
	}
}
Exemplo n.º 2
0
func TestWeightedQuadtricFit(t *testing.T) {

	var n int = 19

	data := MakeData(0.1, 2.0, 0.1)

	X := matrix.MatrixAlloc(n, 3)
	y := vector.VectorAlloc(n)
	w := vector.VectorAlloc(n)

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

	for i := 0; i < n; i++ {
		xi := data[i*3]
		yi := data[i*3+1]
		ei := data[i*3+2]
		fmt.Printf("%g %g +/- %g\n", xi, yi, ei)

		matrix.Set(X, i, 0, 1.0)
		matrix.Set(X, i, 1, xi)
		matrix.Set(X, i, 2, xi*xi)

		vector.Set(y, i, yi)
		vector.Set(w, i, 1.0/(ei*ei))
	}

	work := multifit.LinearAlloc(n, 3)
	_, chisq := multifit.Wlinear(X, w, y, c, cov, work)

	fmt.Printf("# best fit: Y = %g + %g X + %g X^2\n",
		vector.Get(c, 0), vector.Get(c, 1), vector.Get(c, 2))
	fmt.Printf("# covariance matrix:\n")
	fmt.Printf("[ %+.5e, %+.5e, %+.5e  \n",
		matrix.Get(cov, 0, 0), matrix.Get(cov, 0, 1), matrix.Get(cov, 0, 2))
	fmt.Printf("  %+.5e, %+.5e, %+.5e  \n",
		matrix.Get(cov, 1, 0), matrix.Get(cov, 1, 1), matrix.Get(cov, 1, 2))
	fmt.Printf("  %+.5e, %+.5e, %+.5e ]\n",
		matrix.Get(cov, 2, 0), matrix.Get(cov, 2, 1), matrix.Get(cov, 2, 2))
	fmt.Printf("# chisq = %g\n", chisq)
}
Exemplo n.º 3
0
//gsl_multifit_robust_stats gsl_multifit_robust_statistics (const gsl_multifit_robust_workspace * w)
func RobustStatistics(rw *GslMultifitRobustWorkspace) *GslMultifitRobustStatistics {
	var dof C.size_t
	var numit C.size_t
	var weightsPtr *C.gsl_vector
	var rPtr *C.gsl_vector
	res := &GslMultifitRobustStatistics{}
	C.get_robust_statistics((*C.gsl_multifit_robust_workspace)(unsafe.Pointer(rw.Ptr())),
		(*C.double)(&res.SigmaOls), (*C.double)(&res.SigmaMad), (*C.double)(&res.SigmaRob), (*C.double)(&res.Sigma),
		(*C.double)(&res.Rsq), (*C.double)(&res.AdjRsq), (*C.double)(&res.Rmse), (*C.double)(&res.Sse),
		&dof, &numit, &weightsPtr, &rPtr)
	res.Dof = int(dof)
	res.Numit = int(numit)
	weights := &vector.GslVector{}
	gogsl.InitializeGslStruct(weights, uintptr(unsafe.Pointer(weightsPtr)))
	r := &vector.GslVector{}
	gogsl.InitializeGslStruct(r, uintptr(unsafe.Pointer(rPtr)))
	res.Weights = vector.VectorAlloc(weights.Length())
	res.R = vector.VectorAlloc(r.Length())
	vector.Memcpy(res.Weights, weights)
	vector.Memcpy(res.R, r)
	return res
}
Exemplo n.º 4
0
func TestLinalg(t *testing.T) {

	aData := []float64{0.18, 0.60, 0.57, 0.96,
		0.41, 0.24, 0.99, 0.58,
		0.14, 0.30, 0.97, 0.66,
		0.51, 0.13, 0.19, 0.85}
	bData := []float64{1.0, 2.0, 3.0, 4.0}
	m := matrix.ViewArray(aData, 4, 4)
	b := vector.ViewArray(bData, 4)
	x := vector.VectorAlloc(4)
	p := permutation.PermutationAlloc(4)
	linalg.LUDecomp(m.Matrix(), p)
	linalg.LUSolve(m.Matrix(), p, b.Vector(), x)
	fmt.Printf("x = \n")
	vector.Fprintf(os.Stdout, x, "%g")
}
Exemplo n.º 5
0
func TestMultirootFdf(t *testing.T) {

	var n int = 2

	rp := &Rparams{
		A: 1.0,
		B: 10.0,
	}

	f := &multiroot.GslMultirootFunctionFdf{
		Function:   Rosenbrock,
		Derivative: RosenbrockDf,
		Fdf:        RosenbrockFdf,
		Dimension:  n,
		Params:     rp,
	}
	multiroot.InitializeGslMultirootFunctionFdf(f)

	xInit := []float64{-10.0, -5.0}
	x := vector.VectorAlloc(n)

	vector.Set(x, 0, xInit[0])
	vector.Set(x, 1, xInit[1])

	T := multiroot.GSL_MULTIROOT_FDFSOLVER_GNEWTON
	s := multiroot.FdfsolverAlloc(T, 2)
	multiroot.FdfsolverSet(s, f, x)

	var iter int

	PrintStateFdf(iter, s)

	for {
		iter += 1
		status := gogsl.GslError(multiroot.FdfsolverIterate(s))
		PrintStateFdf(iter, s)
		if status != gogsl.GSL_SUCCESS {
			break
		}
		status = gogsl.GslError(multiroot.TestResidual(s.F_(), 1e-7))
		if status != gogsl.GSL_CONTINUE || iter >= 1000 {
			break
		}
		fmt.Printf("status = %s\n", status.String())
	}
}
Exemplo n.º 6
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)
	}
}
Exemplo n.º 7
0
func TestVector(t *testing.T) {
	var stopDoingIt bool
	gogsl.SetErrorHandler(&gogsl.GslErrorHandler{
		Handler: func(reason string, file string, line int, gslError gogsl.GslError) {
			fmt.Println("Caught error: " + reason)
			stopDoingIt = true
		},
	})
	v := vector.VectorAlloc(3)
	for i := 0; i < 3; i++ {
		vector.Set(v, i, float64(1.23)+float64(i))
	}
	for i := 0; i < 100; i++ { // OUT OF RANGE ERROR
		fmt.Printf("v_%d = %g\n", i, vector.Get(v, i))
		if stopDoingIt {
			break
		}
	}
}
Exemplo n.º 8
0
func TestEigen(t *testing.T) {

	data := []float64{1.0, 1 / 2.0, 1 / 3.0, 1 / 4.0,
		1 / 2.0, 1 / 3.0, 1 / 4.0, 1 / 5.0,
		1 / 3.0, 1 / 4.0, 1 / 5.0, 1 / 6.0,
		1 / 4.0, 1 / 5.0, 1 / 6.0, 1 / 7.0}
	m := matrix.ViewArray(data, 4, 4)
	eval := vector.VectorAlloc(4)
	evec := matrix.MatrixAlloc(4, 4)
	w := eigen.SymmvAlloc(4)
	eigen.Symmv(m.Matrix(), eval, evec, w)
	eigen.SymmvSort(eval, evec, eigen.EIGEN_SORT_ABS_ASC)
	for i := 0; i < 4; i++ {
		evalI := vector.Get(eval, i)
		evecI := matrix.Column(evec, i)
		fmt.Printf("eigenvalue = %g\n", evalI)
		fmt.Printf("eigenvector = \n")
		vector.Fprintf(os.Stdout, evecI.Vector(), "%g")
	}
}
Exemplo n.º 9
0
func TestMultiminFdf(t *testing.T) {

	par := []float64{1.0, 2.0, 10.0, 20.0, 30.0}
	f := &multimin.GslMultiminFunctionFdf{
		Function:   MyF,
		Derivative: MyDf,
		Fdf:        MyFdf,
		Dimension:  2,
		Params:     par,
	}
	multimin.InitializeGslMultiminFunctionFdf(f)

	/* Starting point */
	x := vector.VectorAlloc(2)
	vector.Set(x, 0, 5.0)
	vector.Set(x, 1, 7.0)

	T := multimin.GSL_MULTIMIN_FDFMINIMIZER_CONJUGATE_FR
	s := multimin.FdfminimizerAlloc(T, 2)
	multimin.FdfminimizerSet(s, f, x, 0.01, 1e-4)

	iter := 0

	for {
		iter += 1
		status := gogsl.GslError(multimin.FdfminimizerIterate(s))
		if status != gogsl.GSL_SUCCESS {
			break
		}
		status = gogsl.GslError(multimin.TestGradient(s.Gradient_(), 1e-3))
		if status == gogsl.GSL_SUCCESS {
			fmt.Printf("Minimum found at:\n")
		}
		fmt.Printf("%5d %10.3e %10.3e f() = %7.3f\n",
			iter, vector.Get(s.X_(), 0), vector.Get(s.X_(), 1), s.Fval())
		if status != gogsl.GSL_CONTINUE || iter >= 100 {
			break
		}
	}
}
Exemplo n.º 10
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))
}
Exemplo n.º 11
0
func TestBspline(t *testing.T) {

	var n int = 200
	var ncoeffs int = 12
	var nbreak int = ncoeffs - 2

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

	// allocate a cubic bspline workspace (k = 4)
	bw := bspline.Alloc(4, nbreak)
	B := vector.VectorAlloc(ncoeffs)

	x := vector.VectorAlloc(n)
	y := vector.VectorAlloc(n)
	X := matrix.MatrixAlloc(n, ncoeffs)
	c := vector.VectorAlloc(ncoeffs)
	w := vector.VectorAlloc(n)
	cov := matrix.MatrixAlloc(ncoeffs, ncoeffs)
	mw := multifit.LinearAlloc(n, ncoeffs)

	fmt.Printf("#m=0,S=0\n")
	// this is the data to be fitted
	for i := 0; i < n; i++ {
		xi := (15.0 / (float64(n) - 1)) * float64(i)
		yi := math.Cos(xi) * math.Exp(-0.1*xi)
		sigma := 0.1 * yi
		dy := randist.Gaussian(r, sigma)
		vector.Set(x, i, xi)
		vector.Set(y, i, yi+dy)
		vector.Set(w, i, 1.0/(sigma*sigma))
		fmt.Printf("%f %f\n", xi, yi+dy)
	}

	// use uniform breakpoints on [0, 15]
	bspline.KnotsUniform(0.0, 15.0, bw)

	// construct the fit matrix X
	for i := 0; i < n; i++ {
		xi := vector.Get(x, i)

		// compute B_j(xi) for all j
		bspline.Eval(xi, B, bw)

		// fill in row i of X
		for j := 0; j < ncoeffs; j++ {
			matrix.Set(X, i, j, vector.Get(B, j))
		}
	}

	// do the fit
	_, chisq := multifit.Wlinear(X, w, y, c, cov, mw)
	dof := float64(n - ncoeffs)
	tss := stats.Wtss(w.Data_(), w.Stride(), y.Data_(), y.Stride(), n)
	rsq := 1.0 - chisq/tss
	fmt.Printf("chisq/dof = %e, Rsq = %f\n", chisq/dof, rsq)

	fmt.Printf("#m=1,S=0\n")
	for xi := 0.0; xi < 15.0; xi += 0.1 {
		bspline.Eval(xi, B, bw)
		_, yi, _ := multifit.LinearEst(B, c, cov)
		fmt.Printf("%f %f\n", xi, yi)
	}
}