Removed dependency on implementatoin of RandomVariable
*/
package net.finmath.montecarlo.interestrate.modelplugins;
-import net.finmath.montecarlo.RandomVariable;
import net.finmath.stochastic.RandomVariableInterface;
import net.finmath.time.TimeDiscretizationInterface;
@@ -133,11 +132,15 @@ public abstract class AbstractLIBORCovarianceModel {
* @return The instantaneous covariance between component <i>i</i> and <i>j</i>.
*/
public RandomVariableInterface getCovariance(int timeIndex, int component1, int component2, RandomVariableInterface[] realizationAtTimeIndex) {
- RandomVariableInterface covariance = new RandomVariable(0.0, 0.0);
RandomVariableInterface[] factorLoadingOfComponent1 = getFactorLoading(timeIndex, component1, realizationAtTimeIndex);
RandomVariableInterface[] factorLoadingOfComponent2 = getFactorLoading(timeIndex, component2, realizationAtTimeIndex);
- for(int factorIndex=0; factorIndex<this.getNumberOfFactors(); factorIndex++) {
+
+ // Multiply first factor loading (this avoids that we have to init covariance to 0).
+ RandomVariableInterface covariance = factorLoadingOfComponent1[0].mult(factorLoadingOfComponent2[0]);
+
+ // Add others, if any
+ for(int factorIndex=1; factorIndex<this.getNumberOfFactors(); factorIndex++) {
covariance = covariance.addProduct(factorLoadingOfComponent1[factorIndex],factorLoadingOfComponent2[factorIndex]);
}
