Пример #1
0
func calculate_even_fib_until(limit int64) (m []float64) {
	if limit <= 2 { // base case: This cannot be determined by the recurrence relation
		m = []float64{2, 0}
		return
	}
	done := false
	var a, b, c *matrix.DenseMatrix
	a = relation_mat // a holds older b before multiplication
	b = relation_mat // b holds result of matrix multiplication
	c = relation_mat // c holds matrix that is to be multiplied in current iteration
	dict[float64(1)] = relation_mat

	for !done {
		a = b
		b, _ = b.TimesDense(c)
		t, _ := b.TimesDense(init_mat) // calculate resultant vector for a certain power of a matrix
		if t.ColCopy(0)[1] < float64(limit) {
			if pow[1] == -1 { // pow[1] = -1 means we are exponentiating the matrix (first mode).
				pow[0] = 2 * pow[0]
				c = b
				dict[pow[0]] = b
			} else {
				pow[0] = math.Ceil((pow[1] + pow[0]) / float64(2))
				c = dict[math.Ceil((pow[1]-pow[0])/float64(2))]
			}
		} else {
			if pow[1] == -1 {
				pow[1] = pow[0]
				pow[0] = pow[0] / 2
				dict[pow[1]] = b
				if pow[1]-pow[0] < 2 {
					c = dict[float64(1)]
				} else {
					c = dict[math.Ceil((pow[1]-pow[0])/float64(2))]
				}
				b = a
			} else {
				pow[1] = math.Ceil((pow[1] + pow[0]) / float64(2))
				c = dict[math.Ceil((pow[1]-pow[0])/float64(2))]
				b = a
			}
		}
		t, _ = b.TimesDense(init_mat)
		if pow[1] != -1 && t.ColCopy(0)[1] < float64(limit) && t.ColCopy(0)[0] >= float64(limit) {
			done = true
			m = t.ColCopy(0)
		}
	}
	return
}
Пример #2
0
func (this *KnownVarianceLRPosterior) Remove(x, y *mx.DenseMatrix) {
	xxt, _ := x.TimesDense(x.Transpose())
	this.XXt.Subtract(xxt)
	yxt, _ := y.TimesDense(x.Transpose())
	this.YXt.Subtract(yxt)
}
Пример #3
0
func (this *KnownVarianceLRPosterior) Insert(x, y *mx.DenseMatrix) {
	xxt, _ := x.TimesDense(x.Transpose())
	this.XXt.Add(xxt)
	yxt, _ := y.TimesDense(x.Transpose())
	this.YXt.Add(yxt)
}
Пример #4
0
/*
	If Y ~ N(AX, Sigma, I)
	and A ~ N(M, Sigma, Phi)
	this returns a sampler for P(A|X,Y,Sigma,M,Phi)
*/
func KnownVariancePosterior(Y, X, Sigma, M, Phi *mx.DenseMatrix) func() (A *mx.DenseMatrix) {
	o := Y.Rows()
	i := X.Rows()
	n := Y.Cols()
	if n != X.Cols() {
		panic("X and Y don't have the same number of columns")
	}
	if o != M.Rows() {
		panic("Y.Rows != M.Rows")
	}
	if i != M.Cols() {
		panic("Y.Rows != M.Cols")
	}
	if o != Sigma.Rows() {
		panic("Y.Rows != Sigma.Rows")
	}
	if Sigma.Cols() != Sigma.Rows() {
		panic("Sigma is not square")
	}
	if i != Phi.Rows() {
		panic("X.Rows != Phi.Rows")
	}
	if Phi.Cols() != Phi.Rows() {
		panic("Phi is not square")
	}

	Xt := X.Transpose()

	PhiInv, err := Phi.Inverse()
	if err != nil {
		panic(err)
	}

	XXt, err := X.TimesDense(Xt)
	if err != nil {
		panic(err)
	}

	XXtpPhiInv, err := XXt.PlusDense(PhiInv)
	if err != nil {
		panic(err)
	}

	Omega, err := XXtpPhiInv.Inverse()
	if err != nil {
		panic(err)
	}

	YXtpMPhiInv, err := Y.TimesDense(Xt)
	if err != nil {
		panic(err)
	}

	MPhiInv, err := M.TimesDense(PhiInv)
	if err != nil {
		panic(err)
	}

	err = YXtpMPhiInv.AddDense(MPhiInv)
	if err != nil {
		panic(err)
	}

	Mxy, err := YXtpMPhiInv.TimesDense(Omega)
	if err != nil {
		panic(err)
	}

	if false {
		fmt.Printf("Mxy:\n%v\n", Mxy)
		fmt.Printf("Sigma:\n%v\n", Sigma)
		fmt.Printf("Omega:\n%v\n", Omega)
	}

	return dst.MatrixNormal(Mxy, Sigma, Omega)
}