Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.apache.mahout.math.random;
Samples from a multi-variate normal distribution.

This is done by sampling from several independent unit normal distributions to get a vector u. The sample value that is returned is then A u + m where A is derived from the covariance matrix and m is the mean of the result.

If \Sigma is the desired covariance matrix, then you can use any value of A such that A' A = \Sigma. The Cholesky decomposition can be used to compute A if \Sigma is positive definite. Slightly more expensive is to use the SVD U S V' = \Sigma and then set A = U \sqrt{S}. Useful special cases occur when \Sigma is diagonal so that A = \sqrt(\Sigma) or where \Sigma = r I. Another special case is where m = 0.

 public class MultiNormal implements Sampler<Vector> {
   private final Random gen;
   private final int dimension;
   private final Matrix scale;
   private final Vector mean;

Constructs a sampler with diagonal scale matrix.

diagonal The diagonal elements of the scale matrix.
   public MultiNormal(Vector diagonal) {
     this(new DiagonalMatrix(diagonal), null);

Constructs a sampler with diagonal scale matrix and (potentially) non-zero mean.

diagonal The scale matrix's principal diagonal.
mean The desired mean. Set to null if zero mean is desired.
   public MultiNormal(Vector diagonalVector mean) {
     this(new DiagonalMatrix(diagonal), mean);

Constructs a sampler with non-trivial scale matrix and mean.
   public MultiNormal(Matrix aVector mean) {
   public MultiNormal(int dimension) {
   public MultiNormal(double radiusVector mean) {
     this(new DiagonalMatrix(radiusmean.size()), mean);
   private MultiNormal(Matrix scaleVector meanint dimension) {
      = RandomUtils.getRandom();
     this. = dimension;
     this. = scale;
     this. = mean;
   public Vector sample() {
     Vector v = new DenseVector().assign(
       new DoubleFunction() {
         public double apply(double ignored) {
           return .nextGaussian();
    if ( != null) {
      if ( != null) {
        return .times(v).plus();
      } else {
    } else {
      if ( != null) {
        return .times(v);
      } else {
        return v;
  public Vector getScale() {
    return ;
New to GrepCode? Check out our FAQ X