Ejemplo n.º 1
0
func q3() {
	fns := []linreg.TransformFunc{phi0, phi1, phi2, phi3, phi4, phi5, phi6, phi7}

	ks := []int{3, 4, 5, 6, 7}

	data := getData("data/in.dta")
	for _, k := range ks {
		linreg := linreg.NewLinearRegression()

		linreg.InitializeFromData(data[25:])
		linreg.InitializeValidationFromData(data[:25])

		linreg.TransformFunction = fns[k]

		linreg.ApplyTransformation()
		linreg.ApplyTransformationOnValidation()
		linreg.Learn()
		eIn := linreg.Ein()
		eVal := linreg.EValIn()
		eOut, _ := linreg.EoutFromFile("data/out.dta")

		fmt.Printf("EVal = %f, for k = %d\n", eVal, k)
		fmt.Printf("EIn = %f, for k = %d\n", eIn, k)
		fmt.Printf("EOut = %f, for k = %d\n", eOut, k)
		fmt.Println()
	}
}
Ejemplo n.º 2
0
func q5() {
	runs := 1000 // number of times we repeat the experiment
	linreg := linreg.NewLinearRegression()
	linreg.N = 100
	var avgEin, avgEout float64

	for run := 0; run < runs; run++ {
		linreg.Initialize()

		linreg.Learn()
		avgEin += linreg.Ein()
		avgEout += linreg.Eout()
	}
	avgEin = avgEin / float64(runs)
	avgEout = avgEout / float64(runs)
	fmt.Printf("average of In sample error 'Ein' for Linear regresion for N = 100 is %4.2f\n", avgEin)
	fmt.Printf("average of Out of sample error 'Eout' for Linear regresion for N = 100 is %4.2f\n", avgEout)
}
Ejemplo n.º 3
0
func q7() {
	runs := 1000 // number of times we repeat the experiment
	linreg := linreg.NewLinearRegression()
	linreg.N = 10
	iterations := 0
	for run := 0; run < runs; run++ {
		linreg.Initialize()

		linreg.Learn()
		pla := pla.NewPLA()
		pla.Initialize()
		for i := 0; i < len(pla.Wn); i++ {
			pla.Wn[i] = linreg.Wn[i]
		}
		iterations += pla.Converge()
	}
	avgIterations := float64(iterations) / float64(runs)
	fmt.Printf("average for PLA to converge for N = %v with initial weight computed by linear regression is %4.2f\n", linreg.N, avgIterations)
}
Ejemplo n.º 4
0
func q2() {
	linreg := linreg.NewLinearRegression()
	linreg.InitializeFromFile("data/in.dta")
	linreg.TransformFunction = nonLinearFeature
	linreg.ApplyTransformation()
	linreg.Learn()
	ein := linreg.Ein()
	eout, _ := linreg.EoutFromFile("data/out.dta")
	fmt.Printf("Ein = %f, Eout = %f\n", ein, eout)
	linreg.K = -3
	linreg.LearnWeightDecay()
	eAugIn := linreg.EAugIn()
	eAugOut, _ := linreg.EAugOutFromFile("data/out.dta")
	fmt.Printf("Ein = %f, Eout = %f for k = %d\n", eAugIn, eAugOut, linreg.K)

	linreg.K = 3
	linreg.LearnWeightDecay()
	eAugIn = linreg.EAugIn()
	eAugOut, _ = linreg.EAugOutFromFile("data/out.dta")
	fmt.Printf("Ein = %f, Eout = %f for k = %d\n", eAugIn, eAugOut, linreg.K)

	k := []int{2, 1, 0, -1, -2}
	for _, ki := range k {
		linreg.K = ki
		linreg.LearnWeightDecay()
		eAugIn = linreg.EAugIn()
		eAugOut, _ = linreg.EAugOutFromFile("data/out.dta")
		fmt.Printf("Ein = %f, Eout = %f for k = %d\n", eAugIn, eAugOut, ki)
	}

	minK := 0
	minEAug := float64(1000)
	for i := -10; i < 10; i++ {
		linreg.K = i
		linreg.LearnWeightDecay()
		eAugOut, _ = linreg.EAugOutFromFile("data/out.dta")
		if minEAug > eAugOut {
			minK = i
			minEAug = eAugOut
		}
	}
	fmt.Printf("mininum Eout = %f with k = %d for k in {-10; 10}\n", minEAug, minK)
}
Ejemplo n.º 5
0
func q8() {

	runs := 1000 // number of times we repeat the experiment
	linreg := linreg.NewLinearRegression()
	linreg.N = 1000
	linreg.RandomTargetFunction = false
	linreg.TwoParams = true
	linreg.Noise = 0.1
	var avgEin float64

	for run := 0; run < runs; run++ {

		linreg.TargetFunction = f //non linear function
		linreg.Initialize()
		linreg.Learn()
		avgEin += linreg.Ein()
	}

	avgEin = avgEin / float64(runs)
	fmt.Printf("average of In sample error 'Ein' for Linear regresion for N = 100 is %4.2f\n", avgEin)
}
Ejemplo n.º 6
0
func q9() {

	runs := 1000 // number of times we repeat the experiment
	linreg := linreg.NewLinearRegression()
	linreg.N = 1000
	linreg.RandomTargetFunction = false
	linreg.TwoParams = true
	linreg.Noise = 0.1
	var aAvgIn, bAvgIn, cAvgIn, dAvgIn, eAvgIn float64
	var aAvgOut, bAvgOut, cAvgOut, dAvgOut, eAvgOut float64

	var AvgEout float64
	for run := 0; run < runs; run++ {

		linreg.TargetFunction = f //non linear function
		linreg.Initialize()
		linreg.TransformDataSet(nonLinearFeature, 6)
		linreg.Learn()

		aAvgIn += linreg.CompareInSample(a, 2)
		bAvgIn += linreg.CompareInSample(b, 2)
		cAvgIn += linreg.CompareInSample(c, 2)
		dAvgIn += linreg.CompareInSample(d, 2)
		eAvgIn += linreg.CompareInSample(e, 2)

		aAvgOut += linreg.CompareOutOfSample(a, 2)
		bAvgOut += linreg.CompareOutOfSample(b, 2)
		cAvgOut += linreg.CompareOutOfSample(c, 2)
		dAvgOut += linreg.CompareOutOfSample(d, 2)
		eAvgOut += linreg.CompareOutOfSample(e, 2)

		AvgEout += linreg.Eout()
	}

	aAvgIn = aAvgIn / float64(runs)
	bAvgIn = bAvgIn / float64(runs)
	cAvgIn = cAvgIn / float64(runs)
	dAvgIn = dAvgIn / float64(runs)
	eAvgIn = eAvgIn / float64(runs)

	aAvgOut = aAvgOut / float64(runs)
	bAvgOut = bAvgOut / float64(runs)
	cAvgOut = cAvgOut / float64(runs)
	dAvgOut = dAvgOut / float64(runs)
	eAvgOut = eAvgOut / float64(runs)

	AvgEout = AvgEout / float64(runs)

	fmt.Printf("average of difference in sample between a() and the linear regression hypothesis is %5.3f\n", aAvgIn)
	fmt.Printf("average of difference in sample between b() and the linear regression hypothesis is %5.3f\n", bAvgIn)
	fmt.Printf("average of difference in sample between c() and the linear regression hypothesis is %5.3f\n", cAvgIn)
	fmt.Printf("average of difference in sample between d() and the linear regression hypothesis is %5.3f\n", dAvgIn)
	fmt.Printf("average of difference in sample between e() and the linear regression hypothesis is %5.3f\n", eAvgIn)

	fmt.Printf("average of difference out of sample between a() and the linear regression hypothesis is %7.5f\n", aAvgOut)
	fmt.Printf("average of difference out of sample between b() and the linear regression hypothesis is %7.5f\n", bAvgOut)
	fmt.Printf("average of difference out of sampleb etween c() and the linear regression hypothesis is %7.5f\n", cAvgOut)
	fmt.Printf("average of difference out of sample between d() and the linear regression hypothesis is %7.5f\n", dAvgOut)
	fmt.Printf("average of difference out of sample between e() and the linear regression hypothesis is %7.5f\n", eAvgOut)

	fmt.Printf("average Eout of the hypothesis learn through linear regression with noise of %4.2f is %4.3f\n", linreg.Noise, AvgEout)
}