sampleshape into field
@@ -9,28 +9,26 @@ import io.improbable.keanu.tensor.dbl.DoubleTensor;
import java.util.List;
public class DoubleVertexSamples extends VertexSamples<DoubleTensor> {
+ private final long sampleShape[];
public DoubleVertexSamples(List<DoubleTensor> samples) {
super(samples);
+ sampleShape = samples.iterator().next().getShape();
}
public DoubleTensor getAverages() {
Preconditions.checkState(!samples.isEmpty(), "No samples exist for averaging.");
- long[] shape = samples.iterator().next().getShape();
-
return this.samples.stream()
- .reduce(DoubleTensor.zeros(shape), DoubleTensor::plusInPlace)
+ .reduce(DoubleTensor.zeros(sampleShape), DoubleTensor::plusInPlace)
.divInPlace(samples.size());
}
public DoubleTensor getVariances() {
Preconditions.checkState(!samples.isEmpty(), "No samples exist for averaging.");
- long[] shape = samples.iterator().next().getShape();
-
DoubleTensor sumOfSquares = this.samples.stream()
- .reduce(DoubleTensor.zeros(shape), (l, r) -> l.plusInPlace(r.pow(2)));
+ .reduce(DoubleTensor.zeros(sampleShape), (l, r) -> l.plusInPlace(r.pow(2)));
return sumOfSquares
.divInPlace(samples.size())
.minusInPlace(getAverages().pow(2))
@@ -40,14 +38,13 @@ public class DoubleVertexSamples extends VertexSamples<DoubleTensor> {
/**
* Calculates the autocorrelation or samples across a specified tensor index.
+ *
* @param index The tensor index to calculate autocorrelation across.
* @return A tensor of autocorrelation at different lags.
*/
public DoubleTensor getAutocorrelation(long... index) {
Preconditions.checkState(!samples.isEmpty(), "No samples exist for averaging.");
-
- long[] shape = samples.iterator().next().getShape();
- TensorShapeValidation.checkIndexIsValid(shape, index);
+ TensorShapeValidation.checkIndexIsValid(sampleShape, index);
double[] sampleValuesAtIndex = samples.stream()
.mapToDouble(x -> x.getValue(index)).toArray();
