/* (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); } }
/* (non-Javadoc) * @see net.finmath.stochastic.RandomVariableInterface#addProduct(net.finmath.stochastic.RandomVariableInterface, double) */ public RandomVariableInterface addProduct(RandomVariableInterface factor1, double factor2) { // Set time of this random variable to maximum of time with respect to which measurability is // known. double newTime = Math.max(time, factor1.getFiltrationTime()); if (isDeterministic() && factor1.isDeterministic()) { double newValueIfNonStochastic = valueIfNonStochastic + (factor1.get(0) * factor2); return new RandomVariable(newTime, newValueIfNonStochastic); } else if (isDeterministic() && !factor1.isDeterministic()) { double[] factor1Realizations = factor1.getRealizations(); double[] newRealizations = new double[Math.max(size(), factor1.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = valueIfNonStochastic + factor1Realizations[i] * factor2; return new RandomVariable(newTime, newRealizations); } else if (!isDeterministic() && factor1.isDeterministic()) { double factor1Value = factor1.get(0); double[] newRealizations = new double[Math.max(size(), factor1.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = realizations[i] + factor1Value * factor2; return new RandomVariable(newTime, newRealizations); } else { double[] factor1Realizations = factor1.getRealizations(); double[] newRealizations = new double[Math.max(size(), factor1.size())]; for (int i = 0; i < newRealizations.length; i++) newRealizations[i] = realizations[i] + factor1Realizations[i] * factor2; 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; }