|
14 | 14 | import org.junit.runners.Parameterized;
|
15 | 15 | import org.junit.runners.Parameterized.Parameters;
|
16 | 16 |
|
17 |
| -import net.finmath.montecarlo.AbstractRandomVariableFactory; |
| 17 | +import net.finmath.montecarlo.BrownianMotion; |
| 18 | +import net.finmath.montecarlo.BrownianMotionInterface; |
18 | 19 | import net.finmath.montecarlo.RandomVariable;
|
19 | 20 | import net.finmath.montecarlo.RandomVariableFactory;
|
20 | 21 | import net.finmath.montecarlo.automaticdifferentiation.AbstractRandomVariableDifferentiableFactory;
|
21 | 22 | import net.finmath.montecarlo.automaticdifferentiation.RandomVariableDifferentiableInterface;
|
22 | 23 | import net.finmath.montecarlo.automaticdifferentiation.backward.alternative.RandomVariableAADv2Factory;
|
23 | 24 | import net.finmath.stochastic.RandomVariableInterface;
|
| 25 | +import net.finmath.time.TimeDiscretization; |
24 | 26 |
|
25 | 27 | /**
|
26 | 28 | * Unit test for random variables implementing <code>RandomVariableDifferentiableInterface</code>.
|
@@ -346,6 +348,33 @@ public void testRandomVariableGradientBigSum2(){
|
346 | 348 |
|
347 | 349 | }
|
348 | 350 |
|
| 351 | + @Test |
| 352 | + public void testRandomVariableExpectation(){ |
| 353 | + |
| 354 | + int numberOfPaths = 100000; |
| 355 | + int seed = 3141; |
| 356 | + BrownianMotionInterface brownianMotion = new BrownianMotion(new TimeDiscretization(0.0, 1.0), 1 /* numberOfFactors */, numberOfPaths, seed); |
| 357 | + RandomVariableInterface brownianIncrement = brownianMotion.getIncrement(0, 0); |
| 358 | + |
| 359 | + RandomVariableDifferentiableInterface x = randomVariableFactory.createRandomVariable(1.0); |
| 360 | + |
| 361 | + RandomVariableInterface y = x.mult(brownianIncrement.sub(brownianIncrement.average())).average().mult(brownianIncrement); |
| 362 | + |
| 363 | + Map<Long, RandomVariableInterface> aadGradient = ((RandomVariableDifferentiableInterface) y).getGradient(); |
| 364 | + |
| 365 | + RandomVariableInterface derivative = aadGradient.get(x.getID()); |
| 366 | + |
| 367 | + System.out.println(randomVariableFactory.toString()); |
| 368 | + System.out.println(y.getAverage()); |
| 369 | + System.out.println(brownianIncrement.squared().getAverage()); |
| 370 | + System.out.println((aadGradient.get(x.getID())).getAverage()); |
| 371 | + |
| 372 | + Assert.assertEquals(0.0, y.getAverage(), 1E-8); |
| 373 | + |
| 374 | + // Test RandomVariableDifferentiableAADFactory (the others currently fail) |
| 375 | + if(randomVariableFactory instanceof RandomVariableDifferentiableAADFactory) Assert.assertEquals(0.0, derivative.getAverage(), 1E-8); |
| 376 | + } |
| 377 | + |
349 | 378 | @Test
|
350 | 379 | public void testRandomVariableGradientBigSumWithConstants(){
|
351 | 380 |
|
|
0 commit comments