Repository: bigtop Updated Branches: refs/heads/master 3bbbb557a -> 15af83ebd
http://git-wip-us.apache.org/repos/asf/bigtop/blob/15af83eb/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/markovmodels/TestMarkovModelBuilder.java ---------------------------------------------------------------------- diff --git a/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/markovmodels/TestMarkovModelBuilder.java b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/markovmodels/TestMarkovModelBuilder.java new file mode 100644 index 0000000..6aa6a6a --- /dev/null +++ b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/markovmodels/TestMarkovModelBuilder.java @@ -0,0 +1,76 @@ +/** + * 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 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.bigtop.datagenerators.samplers.markovmodels; + +import org.apache.bigtop.datagenerators.samplers.markovmodels.MarkovModel; +import org.apache.bigtop.datagenerators.samplers.markovmodels.MarkovModelBuilder; +import org.junit.Test; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.matchers.JUnitMatchers.*; + +public class TestMarkovModelBuilder +{ + + @Test + public void testAddStateState() + { + MarkovModelBuilder<String> builder = MarkovModelBuilder.create(); + + builder.addStartState("a", 1.0); + + MarkovModel<String> msm = builder.build(); + + assertThat(msm.getStartWeights().keySet(), hasItem("a")); + assertEquals((double) msm.getStartWeights().get("a"), (double) 1.0, 0.000001); + + } + + @Test + public void testAddEdgeTransition() + { + MarkovModelBuilder<String> builder = MarkovModelBuilder.create(); + + builder.addTransition("a", "b", 1.0); + + MarkovModel<String> msm = builder.build(); + + assertThat(msm.getTransitionWeights().keySet(), hasItem("a")); + assertThat(msm.getTransitionWeights().get("a").keySet(), hasItem("b")); + assertEquals((double) msm.getTransitionWeights().get("a").get("b"), (double) 1.0, 0.000001); + } + + @Test + public void testBuildMSM() + { + MarkovModelBuilder<String> builder = MarkovModelBuilder.create(); + + builder.addStartState("a", 1.0); + builder.addTransition("a", "b", 1.0); + builder.addTransition("a", "c", 1.0); + + MarkovModel<String> msm = builder.build(); + + assertThat(msm.getStartWeights().keySet(), hasItem("a")); + assertThat(msm.getTransitionWeights().keySet(), hasItem("a")); + assertThat(msm.getTransitionWeights().get("a").keySet(), hasItem("b")); + assertThat(msm.getTransitionWeights().get("a").keySet(), hasItem("c")); + assertEquals((double) msm.getTransitionWeights().get("a").get("b"), (double) 1.0, 0.000001); + assertEquals((double) msm.getTransitionWeights().get("a").get("c"), (double) 1.0, 0.000001); + } + +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/15af83eb/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/markovmodels/TestMarkovProcess.java ---------------------------------------------------------------------- diff --git a/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/markovmodels/TestMarkovProcess.java b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/markovmodels/TestMarkovProcess.java new file mode 100644 index 0000000..a26f31c --- /dev/null +++ b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/markovmodels/TestMarkovProcess.java @@ -0,0 +1,53 @@ +/** + * 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 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.bigtop.datagenerators.samplers.markovmodels; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.matchers.JUnitMatchers.hasItem; + +import java.util.Arrays; + +import org.apache.bigtop.datagenerators.samplers.SeedFactory; +import org.apache.bigtop.datagenerators.samplers.markovmodels.MarkovModel; +import org.apache.bigtop.datagenerators.samplers.markovmodels.MarkovModelBuilder; +import org.apache.bigtop.datagenerators.samplers.markovmodels.MarkovProcess; +import org.junit.Test; + +public class TestMarkovProcess +{ + + @Test + public void test() throws Exception + { + SeedFactory factory = new SeedFactory(1245); + MarkovModelBuilder<String> builder = MarkovModelBuilder.create(); + + builder.addStartState("a", 1.0); + builder.addTransition("a", "b", 1.0); + builder.addTransition("a", "c", 1.0); + + MarkovModel<String> msm = builder.build(); + MarkovProcess<String> process = MarkovProcess.create(msm, factory); + + String firstState = process.sample(); + assertEquals(firstState, "a"); + + String secondState = process.sample(); + assertThat(Arrays.asList("b", "c"), hasItem(secondState)); + } + +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/15af83eb/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/pdfs/TestMultinomialPDF.java ---------------------------------------------------------------------- diff --git a/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/pdfs/TestMultinomialPDF.java b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/pdfs/TestMultinomialPDF.java new file mode 100644 index 0000000..17e1d5e --- /dev/null +++ b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/pdfs/TestMultinomialPDF.java @@ -0,0 +1,42 @@ +/** + * 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 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.bigtop.datagenerators.samplers.pdfs; + +import java.util.Map; +import java.util.Set; + +import org.apache.bigtop.datagenerators.samplers.pdfs.MultinomialPDF; +import org.junit.Assert; +import org.junit.Test; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Sets; + +public class TestMultinomialPDF +{ + + @Test + public void testToString() + { + Map<String, Double> objects = ImmutableMap.of("A", 0.1, "B", 0.3, "C", 0.5); + MultinomialPDF<String> pdf = new MultinomialPDF<String>(objects); + String string = pdf.toString(); + Set<String> observed = Sets.newHashSet(string.split("\n")); + Set<String> expected = Sets.newHashSet("0.1,A", "0.3,B", "0.5,C"); + + Assert.assertEquals(expected, observed); + } +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/15af83eb/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestBoundedMultiModalGaussianSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestBoundedMultiModalGaussianSampler.java b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestBoundedMultiModalGaussianSampler.java new file mode 100644 index 0000000..54d9e41 --- /dev/null +++ b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestBoundedMultiModalGaussianSampler.java @@ -0,0 +1,50 @@ +/** + * 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 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.bigtop.datagenerators.samplers.samplers; + +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.apache.bigtop.datagenerators.samplers.SeedFactory; +import org.apache.bigtop.datagenerators.samplers.samplers.BoundedMultiModalGaussianSampler; +import org.apache.bigtop.datagenerators.samplers.samplers.Sampler; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Test; + +import com.google.common.collect.Lists; + +public class TestBoundedMultiModalGaussianSampler +{ + + @Test + public void testSample() throws Exception + { + double upperbound = 10.0; + double lowerbound = 1.0; + + List<Pair<Double, Double>> distributions = Lists.newArrayList(Pair.of(2.0, 2.0), Pair.of(7.5, 2.0)); + + SeedFactory seedFactory = new SeedFactory(1234); + + Sampler<Double> sampler = new BoundedMultiModalGaussianSampler(distributions, lowerbound, upperbound, seedFactory); + + Double result = sampler.sample(); + + assertTrue(result >= lowerbound); + assertTrue(result <= upperbound); + } +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/15af83eb/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestExponentialSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestExponentialSampler.java b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestExponentialSampler.java new file mode 100644 index 0000000..b04015a --- /dev/null +++ b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestExponentialSampler.java @@ -0,0 +1,41 @@ +/** + * 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 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.bigtop.datagenerators.samplers.samplers; + +import static org.junit.Assert.assertTrue; + +import org.apache.bigtop.datagenerators.samplers.SeedFactory; +import org.apache.bigtop.datagenerators.samplers.samplers.ExponentialSampler; +import org.apache.bigtop.datagenerators.samplers.samplers.Sampler; +import org.junit.Test; + +public class TestExponentialSampler +{ + + @Test + public void testSample() throws Exception + { + double lambda = 1.0 / 2.0; + + SeedFactory seedFactory = new SeedFactory(1234); + + Sampler<Double> sampler = new ExponentialSampler(lambda, seedFactory); + + Double result = sampler.sample(); + + assertTrue(result >= 0.0); + } +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/15af83eb/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestGaussianSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestGaussianSampler.java b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestGaussianSampler.java new file mode 100644 index 0000000..9c816b5 --- /dev/null +++ b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestGaussianSampler.java @@ -0,0 +1,43 @@ +/** + * 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 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.bigtop.datagenerators.samplers.samplers; + +import static org.junit.Assert.assertTrue; + +import org.apache.bigtop.datagenerators.samplers.SeedFactory; +import org.apache.bigtop.datagenerators.samplers.samplers.GaussianSampler; +import org.apache.bigtop.datagenerators.samplers.samplers.Sampler; +import org.junit.Test; + +public class TestGaussianSampler +{ + + @Test + public void testSample() throws Exception + { + double mean = 2.0; + double var = 1.0; + + SeedFactory seedFactory = new SeedFactory(1234); + + Sampler<Double> sampler = new GaussianSampler(mean, var, seedFactory); + + Double result = sampler.sample(); + + assertTrue(result >= -10); + assertTrue(result <= 10); + } +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/15af83eb/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestRouletteWheelSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestRouletteWheelSampler.java b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestRouletteWheelSampler.java new file mode 100644 index 0000000..0c4cc05 --- /dev/null +++ b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestRouletteWheelSampler.java @@ -0,0 +1,71 @@ +/** + * 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 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.bigtop.datagenerators.samplers.samplers; + +import static org.junit.Assert.assertThat; +import static org.junit.matchers.JUnitMatchers.hasItem; + +import java.util.Map; + +import org.apache.bigtop.datagenerators.samplers.SeedFactory; +import org.apache.bigtop.datagenerators.samplers.samplers.RouletteWheelSampler; +import org.apache.bigtop.datagenerators.samplers.samplers.Sampler; +import org.junit.Test; + +import com.google.common.collect.ImmutableMap; + +public class TestRouletteWheelSampler +{ + + @Test + public void testSample() throws Exception + { + Map<String, Double> dataPoints = ImmutableMap.of( + "a", 0.25, + "b", 0.25, + "c", 0.25, + "d", 0.25 + ); + + SeedFactory seedFactory = new SeedFactory(1234); + + Sampler<String> sampler = new RouletteWheelSampler<String>(dataPoints, seedFactory); + + String result = sampler.sample(); + + assertThat(dataPoints.keySet(), hasItem(result)); + } + + @Test + public void testSampleUnnormalized() throws Exception + { + Map<String, Double> dataPoints = ImmutableMap.of( + "a", 1.0, + "b", 1.0, + "c", 1.0, + "d", 1.0 + ); + + SeedFactory seedFactory = new SeedFactory(1234); + + Sampler<String> sampler = new RouletteWheelSampler<String>(dataPoints, seedFactory); + + String result = sampler.sample(); + + assertThat(dataPoints.keySet(), hasItem(result)); + } + +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/15af83eb/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestSequenceSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestSequenceSampler.java b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestSequenceSampler.java new file mode 100644 index 0000000..84e9ab8 --- /dev/null +++ b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestSequenceSampler.java @@ -0,0 +1,38 @@ +/** + * 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 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.bigtop.datagenerators.samplers.samplers; + +import static org.junit.Assert.assertEquals; + +import org.apache.bigtop.datagenerators.samplers.samplers.Sampler; +import org.apache.bigtop.datagenerators.samplers.samplers.SequenceSampler; +import org.junit.Test; + +public class TestSequenceSampler +{ + + @Test + public void testSample() throws Exception + { + Sampler<Integer> sampler = new SequenceSampler(0, 10, 1); + + for(int i = 0; i < 10; i++) + { + Integer value = sampler.sample(); + assertEquals( (int) value, i); + } + } +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/15af83eb/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestUniformIntSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestUniformIntSampler.java b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestUniformIntSampler.java new file mode 100644 index 0000000..6ab51b8 --- /dev/null +++ b/bigtop-data-generators/bigtop-samplers/src/test/java/org/apache/bigtop/datagenerators/samplers/samplers/TestUniformIntSampler.java @@ -0,0 +1,60 @@ +/** + * 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 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.bigtop.datagenerators.samplers.samplers; + +import static org.junit.Assert.assertTrue; + +import org.apache.bigtop.datagenerators.samplers.SeedFactory; +import org.apache.bigtop.datagenerators.samplers.samplers.Sampler; +import org.apache.bigtop.datagenerators.samplers.samplers.UniformIntSampler; +import org.junit.Test; + +public class TestUniformIntSampler +{ + + @Test + public void testSample() throws Exception + { + int upperbound = 10; + int lowerbound = 1; + + SeedFactory seedFactory = new SeedFactory(1234); + + Sampler<Integer> sampler = new UniformIntSampler(lowerbound, upperbound, seedFactory); + + Integer result = sampler.sample(); + + assertTrue(result >= lowerbound); + assertTrue(result <= upperbound); + } + + @Test + public void testSampleInclusive() throws Exception + { + int upperbound = 2; + int lowerbound = 1; + + SeedFactory seedFactory = new SeedFactory(1234); + + Sampler<Integer> sampler = new UniformIntSampler(lowerbound, upperbound, seedFactory); + + Integer result = sampler.sample(); + + assertTrue(result >= lowerbound); + assertTrue(result <= upperbound); + } + +}
