Esempio n. 1
0
func NewScatter(p int) (this *Scatter) {
	this = new(Scatter)
	this.S = matrix.Zeros(p, p)
	this.Mean = matrix.Zeros(p, 1)
	this.Count = 0
	return
}
Esempio n. 2
0
func (this *Scatter) RemoveScatter(other *Scatter) {
	/*
		int newCount = count-s.getCount();

		Matrix newMean = mean.times(count).minus(s.getMean().times(s.getCount())).times(1.0/newCount);

		Matrix newScatter = scatter.minus(s.getScatter());
		newScatter = newScatter.minus(s.getMean().times(s.getMean().transpose()).times(s.getCount()));
		newScatter = newScatter.minus(newMean.times(newMean.transpose()).times(newCount));
		newScatter = newScatter.plus(mean.times(mean.transpose()).times(count));

		count = newCount;
		mean = newMean;
		scatter = newScatter;
	*/
	if other.Count == 0 {
		return
	}

	newCount := this.Count - other.Count

	if newCount == 0 {
		p := this.S.Rows()
		this.S = matrix.Zeros(p, p)
		this.Mean = matrix.Zeros(p, 1)
		this.Count = newCount
		return
	}

	newMean := this.Mean.Copy()
	newMean.Scale(float64(this.Count))
	otherMean := other.Mean.Copy()
	otherMean.Scale(float64(other.Count))
	newMean.Subtract(otherMean)
	newMean.Scale(1 / float64(newCount))

	this.S.Subtract(other.S)
	thisCross, _ := this.Mean.Times(this.Mean.Transpose())
	thisCross.Scale(float64(this.Count))
	otherCross, _ := other.Mean.Times(other.Mean.Transpose())
	otherCross.Scale(float64(other.Count))
	newCross, _ := newMean.Times(newMean.Transpose())
	newCross.Scale(float64(newCount))

	this.S.Add(thisCross)
	this.S.Subtract(otherCross)
	this.S.Subtract(newCross)

	this.Count = newCount
	this.Mean = newMean
}
Esempio n. 3
0
func (this *Scatter) Remove(x *matrix.DenseMatrix) {
	/*
	   int new_count = count-1;
	   Matrix new_mu = mean;
	   if (useSampleMean)
	   	new_mu = mean.plus(mean.minus(x).times(1.0/new_count));
	   scatter = scatter.minus((x.minus(new_mu)).times(x.minus(mean).transpose()));
	   count = new_count;
	   mean = new_mu;
	   if (count == 0) {
	   	if (useSampleMean)
	   		mean = new Matrix(p, 1, 0);
	       scatter = new Matrix(p, p, 0);
	   }
	*/
	newCount := this.Count - 1
	if newCount == 0 {
		p := this.S.Rows()
		this.S = matrix.Zeros(p, p)
		this.Mean = matrix.Zeros(p, 1)
		this.Count = newCount
		return
	}

	newMean, _ := this.Mean.MinusDense(x)
	newMean.Scale(1 / float64(newCount))
	newMean.Add(this.Mean)

	xMinusNewMean, _ := x.MinusDense(newMean)
	xMinusMean, _ := x.MinusDense(this.Mean)
	xMinusMeanT := xMinusMean.Transpose()
	xCross, _ := xMinusNewMean.TimesDense(xMinusMeanT)

	this.S.Subtract(xCross)

	this.Count = newCount
	this.Mean = newMean
}
Esempio n. 4
0
/*
 M is r x c, o x i
 Sigma is r x r, o x o
 Phi is c x c, i x i

 Sigma matches Y o x 1 output dimension
 Phi matches X i x 1 input dimension
*/
func NewKnownVarianceLRPosterior(M, Sigma, Phi *mx.DenseMatrix) (this *KnownVarianceLRPosterior) {
	if M.Rows() != Sigma.Rows() {
		panic("M.Rows != Sigma.Rows")
	}
	if M.Cols() != Phi.Cols() {
		panic("M.Cols != Phi.Cols")
	}
	if Sigma.Rows() != Sigma.Cols() {
		panic("Sigma is not square")
	}
	if Phi.Rows() != Phi.Cols() {
		panic("Phi is not square")
	}
	this = &KnownVarianceLRPosterior{
		M:     M,
		Sigma: Sigma,
		Phi:   Phi,
		XXt:   mx.Zeros(Phi.Cols(), Phi.Cols()),
		YXt:   mx.Zeros(Sigma.Cols(), Phi.Cols()),
	}

	return
}