/* (non-Javadoc) * @see net.finmath.stochastic.RandomVariableInterface#addProduct(net.finmath.stochastic.RandomVariableInterface, net.finmath.stochastic.RandomVariableInterface) */ public RandomVariableInterface addProduct( RandomVariableInterface factor1, RandomVariableInterface factor2) { // Set time of this random variable to maximum of time with respect to which measurability is // known. double newTime = Math.max(Math.max(time, factor1.getFiltrationTime()), factor2.getFiltrationTime()); if (isDeterministic() && factor1.isDeterministic() && factor2.isDeterministic()) { double newValueIfNonStochastic = valueIfNonStochastic + (factor1.get(0) * factor2.get(0)); return new RandomVariable(newTime, newValueIfNonStochastic); } else if (isDeterministic() && !factor1.isDeterministic() && !factor2.isDeterministic()) { double[] factor1Realizations = factor1.getRealizations(); double[] factor2Realizations = factor2.getRealizations(); double[] newRealizations = new double[Math.max(size(), factor1.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = valueIfNonStochastic + factor1Realizations[i] * factor2Realizations[i]; return new RandomVariable(newTime, newRealizations); } else if (!isDeterministic() && !factor1.isDeterministic() && !factor2.isDeterministic()) { double[] factor1Realizations = factor1.getRealizations(); double[] factor2Realizations = factor2.getRealizations(); double[] newRealizations = new double[Math.max(size(), factor1.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = realizations[i] + factor1Realizations[i] * factor2Realizations[i]; return new RandomVariable(newTime, newRealizations); } else { double[] newRealizations = new double[Math.max(Math.max(size(), factor1.size()), factor2.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = get(i) + factor1.get(i) * factor2.get(i); return new RandomVariable(newTime, newRealizations); } }
/* (non-Javadoc) * @see net.finmath.stochastic.RandomVariableInterface#barrier(net.finmath.stochastic.RandomVariableInterface, net.finmath.stochastic.RandomVariableInterface, net.finmath.stochastic.RandomVariableInterface) */ public RandomVariableInterface barrier( RandomVariableInterface trigger, RandomVariableInterface valueIfTriggerNonNegative, RandomVariableInterface valueIfTriggerNegative) { // Set time of this random variable to maximum of time with respect to which measurability is // known. double newTime = Math.max(time, trigger.getFiltrationTime()); newTime = Math.max(newTime, valueIfTriggerNonNegative.getFiltrationTime()); newTime = Math.max(newTime, valueIfTriggerNegative.getFiltrationTime()); if (isDeterministic() && trigger.isDeterministic() && valueIfTriggerNonNegative.isDeterministic() && valueIfTriggerNegative.isDeterministic()) { double newValueIfNonStochastic = trigger.get(0) >= 0 ? valueIfTriggerNonNegative.get(0) : valueIfTriggerNegative.get(0); return new RandomVariable(newTime, newValueIfNonStochastic); } else { int numberOfPaths = Math.max( Math.max(trigger.size(), valueIfTriggerNonNegative.size()), valueIfTriggerNegative.size()); double[] newRealizations = new double[numberOfPaths]; for (int i = 0; i < newRealizations.length; i++) { newRealizations[i] = trigger.get(i) >= 0.0 ? valueIfTriggerNonNegative.get(i) : valueIfTriggerNegative.get(i); } return new RandomVariable(newTime, newRealizations); } }
/* (non-Javadoc) * @see net.finmath.stochastic.RandomVariableInterface#discount(net.finmath.stochastic.RandomVariableInterface, double) */ public RandomVariableInterface discount(RandomVariableInterface rate, double periodLength) { // Set time of this random variable to maximum of time with respect to which measurability is // known. double newTime = Math.max(time, rate.getFiltrationTime()); if (isDeterministic() && rate.isDeterministic()) { double newValueIfNonStochastic = valueIfNonStochastic / (1 + rate.get(0) * periodLength); return new RandomVariable(newTime, newValueIfNonStochastic); } else if (isDeterministic() && !rate.isDeterministic()) { double[] rateRealizations = rate.getRealizations(); double[] newRealizations = new double[Math.max(size(), rate.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = valueIfNonStochastic / (1.0 + rateRealizations[i] * periodLength); return new RandomVariable(newTime, newRealizations); } else if (!isDeterministic() && rate.isDeterministic()) { double rateValue = rate.get(0); double[] newRealizations = new double[Math.max(size(), rate.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = realizations[i] / (1.0 + rateValue * periodLength); return new RandomVariable(newTime, newRealizations); } else { double[] rateRealizations = rate.getRealizations(); double[] newRealizations = new double[Math.max(size(), rate.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = realizations[i] / (1.0 + rateRealizations[i] * periodLength); return new RandomVariable(newTime, newRealizations); } }
public RandomVariableInterface barrier( RandomVariableInterface trigger, RandomVariableInterface valueIfTriggerNonNegative, double valueIfTriggerNegative) { return this.barrier( trigger, valueIfTriggerNonNegative, new RandomVariable(valueIfTriggerNonNegative.getFiltrationTime(), valueIfTriggerNegative)); }
/* (non-Javadoc) * @see net.finmath.stochastic.RandomVariableInterface#subRatio(net.finmath.stochastic.RandomVariableInterface, net.finmath.stochastic.RandomVariableInterface) */ public RandomVariableInterface subRatio( RandomVariableInterface numerator, RandomVariableInterface denominator) { // Set time of this random variable to maximum of time with respect to which measurability is // known. double newTime = Math.max(Math.max(time, numerator.getFiltrationTime()), denominator.getFiltrationTime()); if (isDeterministic() && numerator.isDeterministic() && denominator.isDeterministic()) { double newValueIfNonStochastic = valueIfNonStochastic - (numerator.get(0) / denominator.get(0)); return new RandomVariable(newTime, newValueIfNonStochastic); } else { double[] newRealizations = new double[Math.max(Math.max(size(), numerator.size()), denominator.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = get(i) - numerator.get(i) / denominator.get(i); return new RandomVariable(newTime, newRealizations); } }
/* (non-Javadoc) * @see net.finmath.stochastic.RandomVariableInterface#equals(net.finmath.montecarlo.RandomVariable) */ @Override public boolean equals(RandomVariableInterface randomVariable) { if (this.time != randomVariable.getFiltrationTime()) return false; if (this.isDeterministic() && randomVariable.isDeterministic()) { return this.valueIfNonStochastic == randomVariable.get(0); } if (this.isDeterministic() != randomVariable.isDeterministic()) return false; for (int i = 0; i < realizations.length; i++) if (realizations[i] != randomVariable.get(i)) return false; return true; }
/* (non-Javadoc) * @see net.finmath.stochastic.RandomVariableInterface#floor(net.finmath.stochastic.RandomVariableInterface) */ public RandomVariableInterface floor(RandomVariableInterface randomVariable) { // Set time of this random variable to maximum of time with respect to which measurability is // known. double newTime = Math.max(time, randomVariable.getFiltrationTime()); if (isDeterministic() && randomVariable.isDeterministic()) { double newValueIfNonStochastic = FastMath.max(valueIfNonStochastic, randomVariable.get(0)); return new RandomVariable(newTime, newValueIfNonStochastic); } else if (isDeterministic()) return randomVariable.floor(this); else { double[] newRealizations = new double[Math.max(size(), randomVariable.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = FastMath.max(realizations[i], randomVariable.get(i)); return new RandomVariable(newTime, newRealizations); } }
/** * Create a random variable from a given other implementation of <code>RandomVariableInterface * </code>. * * @param value Object implementing <code>RandomVariableInterface</code>. */ public RandomVariable(RandomVariableInterface value) { super(); this.time = value.getFiltrationTime(); this.realizations = value.isDeterministic() ? null : value.getRealizations(); this.valueIfNonStochastic = value.isDeterministic() ? value.get(0) : Double.NaN; }