|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Neo4j is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +package org.neo4j.gds.embeddings.node2vec; |
| 21 | + |
| 22 | +import org.assertj.core.data.Offset; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.neo4j.gds.core.concurrency.Concurrency; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | + class RandomWalkProbabilitiesTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + void shouldProduceSamplesAccordingToNodeDistribution() { |
| 32 | + double positiveSamplingFactor = 0.001; |
| 33 | + double negativeSamplingExponent = 0.75; |
| 34 | + var builder = new RandomWalkProbabilities.Builder( |
| 35 | + 2, |
| 36 | + new Concurrency(4), |
| 37 | + positiveSamplingFactor, |
| 38 | + negativeSamplingExponent |
| 39 | + ); |
| 40 | + |
| 41 | + builder |
| 42 | + .registerWalk(new long[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); |
| 43 | + |
| 44 | + builder.registerWalk(new long[]{1}); |
| 45 | + |
| 46 | + RandomWalkProbabilities probabilityComputer = builder.build(); |
| 47 | + |
| 48 | + var negSampling = probabilityComputer.negativeSamplingDistribution(); |
| 49 | + var posSampling = probabilityComputer.positiveSamplingProbabilities(); |
| 50 | + |
| 51 | + double app0 = 16; |
| 52 | + double app1 = 1; |
| 53 | + double sum = 17; |
| 54 | + double freq0 = app0/sum; |
| 55 | + double freq1 = app1/sum; |
| 56 | + |
| 57 | + var expectedPos0 = (Math.sqrt(freq0/positiveSamplingFactor) + 1) * (positiveSamplingFactor/freq0); |
| 58 | + var expectedPos1 = (Math.sqrt(freq1/positiveSamplingFactor) + 1) * (positiveSamplingFactor/freq1); |
| 59 | + |
| 60 | + assertThat(posSampling.get(0)).isCloseTo(expectedPos0, Offset.offset(1e-6)); |
| 61 | + assertThat(posSampling.get(1)).isCloseTo(expectedPos1, Offset.offset(1e-6)); |
| 62 | + |
| 63 | + //neg[i] = 2*pow(16,negativeSamplingExponent) + neg[i-1] |
| 64 | + long expectedNeg0 = 2 * (long) Math.pow(app0, negativeSamplingExponent); |
| 65 | + long expectedNeg1 = 2 * (long) Math.pow(app1, negativeSamplingExponent) + expectedNeg0; |
| 66 | + |
| 67 | + assertThat(negSampling.get(0)).isEqualTo(expectedNeg0); |
| 68 | + assertThat(negSampling.get(1)).isEqualTo(expectedNeg1); |
| 69 | + |
| 70 | + } |
| 71 | +} |
0 commit comments