// corrToCov converts a correlation matrix to a covariance matrix. // The input sigma should be vector of standard deviations corresponding // to the covariance. It will panic if len(sigma) is not equal to the // number of rows in the correlation matrix. func corrToCov(c *mat64.SymDense, sigma []float64) { r, _ := c.Dims() if r != len(sigma) { panic(matrix.ErrShape) } for i, sx := range sigma { // Ensure that the diagonal has exactly sigma squared. c.SetSym(i, i, sx*sx) for j := i + 1; j < r; j++ { v := c.At(i, j) c.SetSym(i, j, v*sx*sigma[j]) } } }
func (BrownBadlyScaled) Hess(x []float64, hess *mat64.SymDense) { if len(x) != 2 { panic("dimension of the problem must be 2") } if len(x) != hess.Symmetric() { panic("incorrect size of the Hessian") } h00 := 2 + 2*x[1]*x[1] h01 := 4*x[0]*x[1] - 4 h11 := 2 + 2*x[0]*x[0] hess.SetSym(0, 0, h00) hess.SetSym(0, 1, h01) hess.SetSym(1, 1, h11) }
func (gp *GP) setKernelMat(s *mat64.SymDense, noise float64) { n := s.Symmetric() for i := 0; i < n; i++ { for j := i; j < n; j++ { v := gp.kernel.Distance( gp.inputs.RawRowView(i), gp.inputs.RawRowView(j), ) if i == j { v += noise } s.SetSym(i, j, v) } } }
// covToCorr converts a covariance matrix to a correlation matrix. func covToCorr(c *mat64.SymDense) { r := c.Symmetric() s := make([]float64, r) for i := 0; i < r; i++ { s[i] = 1 / math.Sqrt(c.At(i, i)) } for i, sx := range s { // Ensure that the diagonal has exactly ones. c.SetSym(i, i, 1) for j := i + 1; j < r; j++ { v := c.At(i, j) c.SetSym(i, j, v*sx*s[j]) } } }
func (Watson) Hess(x []float64, hess *mat64.SymDense) { dim := len(x) if dim != hess.Symmetric() { panic("incorrect size of the Hessian") } for j := 0; j < dim; j++ { for k := j; k < dim; k++ { hess.SetSym(j, k, 0) } } for i := 1; i <= 29; i++ { d1 := float64(i) / 29 d2 := 1.0 var s1 float64 for j := 1; j < dim; j++ { s1 += float64(j) * d2 * x[j] d2 *= d1 } d2 = 1.0 var s2 float64 for _, v := range x { s2 += d2 * v d2 *= d1 } t := s1 - s2*s2 - 1 s3 := 2 * d1 * s2 d2 = 2 / d1 th := 2 * d1 * d1 * t for j := 0; j < dim; j++ { v := float64(j) - s3 d3 := 1 / d1 for k := 0; k <= j; k++ { hess.SetSym(k, j, hess.At(k, j)+d2*d3*(v*(float64(k)-s3)-th)) d3 *= d1 } d2 *= d1 } } t1 := x[1] - x[0]*x[0] - 1 hess.SetSym(0, 0, hess.At(0, 0)+8*x[0]*x[0]+2-4*t1) hess.SetSym(0, 1, hess.At(0, 1)-4*x[0]) hess.SetSym(1, 1, hess.At(1, 1)+2) }
func (PowellBadlyScaled) Hess(x []float64, hess *mat64.SymDense) { if len(x) != 2 { panic("dimension of the problem must be 2") } if len(x) != hess.Symmetric() { panic("incorrect size of the Hessian") } t1 := 1e4*x[0]*x[1] - 1 s1 := math.Exp(-x[0]) s2 := math.Exp(-x[1]) t2 := s1 + s2 - 1.0001 h00 := 2 * (1e8*x[1]*x[1] + s1*(s1+t2)) h01 := 2 * (1e4*(1+2*t1) + s1*s2) h11 := 2 * (1e8*x[0]*x[0] + s2*(s2+t2)) hess.SetSym(0, 0, h00) hess.SetSym(0, 1, h01) hess.SetSym(1, 1, h11) }
func (Beale) Hess(x []float64, hess *mat64.SymDense) { if len(x) != 2 { panic("dimension of the problem must be 2") } if len(x) != hess.Symmetric() { panic("incorrect size of the Hessian") } t1 := 1 - x[1] t2 := 1 - x[1]*x[1] t3 := 1 - x[1]*x[1]*x[1] f1 := 1.5 - x[1]*t1 f2 := 2.25 - x[1]*t2 f3 := 2.625 - x[1]*t3 h00 := 2 * (t1*t1 + t2*t2 + t3*t3) h01 := 2 * (f1 + x[1]*(2*f2+3*x[1]*f3) - x[0]*(t1+x[1]*(2*t2+3*x[1]*t3))) h11 := 2 * x[0] * (x[0] + 2*f2 + x[1]*(6*f3+x[0]*x[1]*(4+9*x[1]*x[1]))) hess.SetSym(0, 0, h00) hess.SetSym(0, 1, h01) hess.SetSym(1, 1, h11) }
// ConditionNormal returns the Normal distribution that is the receiver conditioned // on the input evidence. The returned multivariate normal has dimension // n - len(observed), where n is the dimension of the original receiver. The updated // mean and covariance are // mu = mu_un + sigma_{ob,un}^T * sigma_{ob,ob}^-1 (v - mu_ob) // sigma = sigma_{un,un} - sigma_{ob,un}^T * sigma_{ob,ob}^-1 * sigma_{ob,un} // where mu_un and mu_ob are the original means of the unobserved and observed // variables respectively, sigma_{un,un} is the unobserved subset of the covariance // matrix, sigma_{ob,ob} is the observed subset of the covariance matrix, and // sigma_{un,ob} are the cross terms. The elements of x_2 have been observed with // values v. The dimension order is preserved during conditioning, so if the value // of dimension 1 is observed, the returned normal represents dimensions {0, 2, ...} // of the original Normal distribution. // // ConditionNormal returns {nil, false} if there is a failure during the update. // Mathematically this is impossible, but can occur with finite precision arithmetic. func (n *Normal) ConditionNormal(observed []int, values []float64, src *rand.Rand) (*Normal, bool) { if len(observed) == 0 { panic("normal: no observed value") } if len(observed) != len(values) { panic("normal: input slice length mismatch") } for _, v := range observed { if v < 0 || v >= n.Dim() { panic("normal: observed value out of bounds") } } ob := len(observed) unob := n.Dim() - ob obMap := make(map[int]struct{}) for _, v := range observed { if _, ok := obMap[v]; ok { panic("normal: observed dimension occurs twice") } obMap[v] = struct{}{} } if len(observed) == n.Dim() { panic("normal: all dimensions observed") } unobserved := make([]int, 0, unob) for i := 0; i < n.Dim(); i++ { if _, ok := obMap[i]; !ok { unobserved = append(unobserved, i) } } mu1 := make([]float64, unob) for i, v := range unobserved { mu1[i] = n.mu[v] } mu2 := make([]float64, ob) // really v - mu2 for i, v := range observed { mu2[i] = values[i] - n.mu[v] } n.setSigma() var sigma11, sigma22 mat64.SymDense sigma11.SubsetSym(n.sigma, unobserved) sigma22.SubsetSym(n.sigma, observed) sigma21 := mat64.NewDense(ob, unob, nil) for i, r := range observed { for j, c := range unobserved { v := n.sigma.At(r, c) sigma21.Set(i, j, v) } } var chol mat64.Cholesky ok := chol.Factorize(&sigma22) if !ok { return nil, ok } // Compute sigma_{2,1}^T * sigma_{2,2}^-1 (v - mu_2). v := mat64.NewVector(ob, mu2) var tmp, tmp2 mat64.Vector err := tmp.SolveCholeskyVec(&chol, v) if err != nil { return nil, false } tmp2.MulVec(sigma21.T(), &tmp) // Compute sigma_{2,1}^T * sigma_{2,2}^-1 * sigma_{2,1}. // TODO(btracey): Should this be a method of SymDense? var tmp3, tmp4 mat64.Dense err = tmp3.SolveCholesky(&chol, sigma21) if err != nil { return nil, false } tmp4.Mul(sigma21.T(), &tmp3) for i := range mu1 { mu1[i] += tmp2.At(i, 0) } // TODO(btracey): If tmp2 can constructed with a method, then this can be // replaced with SubSym. for i := 0; i < len(unobserved); i++ { for j := i; j < len(unobserved); j++ { v := sigma11.At(i, j) sigma11.SetSym(i, j, v-tmp4.At(i, j)) } } return NewNormal(mu1, &sigma11, src) }
func (BrownAndDennis) Hess(x []float64, hess *mat64.SymDense) { if len(x) != 4 { panic("dimension of the problem must be 4") } if len(x) != hess.Symmetric() { panic("incorrect size of the Hessian") } for i := 0; i < 4; i++ { for j := i; j < 4; j++ { hess.SetSym(i, j, 0) } } for i := 1; i <= 20; i++ { d1 := float64(i) / 5 d2 := math.Sin(d1) t1 := x[0] + d1*x[1] - math.Exp(d1) t2 := x[2] + d2*x[3] - math.Cos(d1) t := t1*t1 + t2*t2 s3 := 2 * t1 * t2 r1 := t + 2*t1*t1 r2 := t + 2*t2*t2 hess.SetSym(0, 0, hess.At(0, 0)+r1) hess.SetSym(0, 1, hess.At(0, 1)+d1*r1) hess.SetSym(1, 1, hess.At(1, 1)+d1*d1*r1) hess.SetSym(0, 2, hess.At(0, 2)+s3) hess.SetSym(1, 2, hess.At(1, 2)+d1*s3) hess.SetSym(2, 2, hess.At(2, 2)+r2) hess.SetSym(0, 3, hess.At(0, 3)+d2*s3) hess.SetSym(1, 3, hess.At(1, 3)+d1*d2*s3) hess.SetSym(2, 3, hess.At(2, 3)+d2*r2) hess.SetSym(3, 3, hess.At(3, 3)+d2*d2*r2) } for i := 0; i < 4; i++ { for j := i; j < 4; j++ { hess.SetSym(i, j, 4*hess.At(i, j)) } } }
func (Wood) Hess(x []float64, hess *mat64.SymDense) { if len(x) != 4 { panic("dimension of the problem must be 4") } if len(x) != hess.Symmetric() { panic("incorrect size of the Hessian") } hess.SetSym(0, 0, 400*(3*x[0]*x[0]-x[1])+2) hess.SetSym(0, 1, -400*x[0]) hess.SetSym(1, 1, 220.2) hess.SetSym(0, 2, 0) hess.SetSym(1, 2, 0) hess.SetSym(2, 2, 360*(3*x[2]*x[2]-x[3])+2) hess.SetSym(0, 3, 0) hess.SetSym(1, 3, 19.8) hess.SetSym(2, 3, -360*x[2]) hess.SetSym(3, 3, 200.2) }