Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhuber committed Apr 11, 2023
1 parent 246ba45 commit 1a25066
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

public class OperationUtils {

private OperationUtils() {
}


public static double logSum(double first, double second) {
final double min, max;
if (first < second) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ch.idsia.crema.factor.algebra.bayesian;

import ch.idsia.crema.factor.algebra.OperationUtils;
import ch.idsia.crema.factor.bayesian.BayesianFactor;

import java.util.Arrays;

import org.apache.commons.math3.util.FastMath;

/**
Expand All @@ -23,10 +27,15 @@ public LogBayesianMarginal(int size, int stride) {

@Override
public final double collect(BayesianFactor factor, final int source) {
double value = 0;

return Arrays.stream(offsets)
.mapToDouble(factor::getLogValueAt)
.reduce(0, OperationUtils::logSum);

/** double value = 0;
for (int i = 0; i < size; ++i) {
value += factor.getValueAt(source + offsets[i]); // TODO: try with ch.idsia.crema.factor.algebra.OperationUtils.logSum()
}
return FastMath.log(value);
return FastMath.log(value);*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class BayesianAbstractFactor implements BayesianFactor {

protected Strides domain;

public BayesianAbstractFactor(Domain domain) {
protected BayesianAbstractFactor(Domain domain) {
this.domain = Strides.fromDomain(domain);
}

Expand Down
26 changes: 26 additions & 0 deletions src/test/java/ch/idsia/crema/model/BayesianFactorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ch.idsia.crema.core.Strides;
import ch.idsia.crema.factor.bayesian.BayesianFactor;
import ch.idsia.crema.factor.bayesian.BayesianFactorFactory;
import ch.idsia.crema.factor.bayesian.BayesianLogFactor;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
Expand Down Expand Up @@ -240,4 +242,28 @@ public void testLogCombineDivision() {
assertEquals(ibf, r);
}

@Test
public void testtime() {
int a = 0;
int b = 1;
int c = 2;

Strides d1 = new Strides(new int[]{a,b,c}, new int[]{2,3,2});


Strides d2 = new Strides(new int[]{b,c}, new int[]{3,2});

long nt = System.nanoTime();

double x = 0;
BayesianLogFactor pa = BayesianFactorFactory.factory().domain(d1).data(new double[]{0.15, 0.85, 0.25, 0.75, 0.95, 0.05, 0.5, 0.5, 0.4, 0.6, 0.7,0.3}).log();
BayesianLogFactor pb = BayesianFactorFactory.factory().domain(d2).data(new double[]{0.15, 0.05, 0.25,0.20, 0.25, 0.1}).log();
for (int i= 0; i < 100000; ++i) {
double[] data = pa.combine(pb).marginalize(b).marginalize(c).getData();
x += data[0];
}

double time = (System.nanoTime() - nt) / 1000000000.0;
System.out.println(time + " " + x);
}
}

0 comments on commit 1a25066

Please sign in to comment.