//------------------------------------------------------------------------- /// <summary> /// Combines two or more instances to form a single sensitivity instance. /// <para> /// The result will store information about the separate instances allowing it to be <seealso cref="#split()"/> later. /// /// </para> /// </summary> /// <param name="marketDataName"> the combined name of the market data that the sensitivity refers to </param> /// <param name="sensitivities"> the sensitivity instances to combine, two or more </param> /// <returns> the combined sensitivity object </returns> public static UnitParameterSensitivity combine <T1>(MarketDataName <T1> marketDataName, params UnitParameterSensitivity[] sensitivities) { ArgChecker.notEmpty(sensitivities, "sensitivities"); if (sensitivities.Length < 2) { throw new System.ArgumentException("At least two sensitivity instances must be specified"); } int size = Stream.of(sensitivities).mapToInt(s => s.ParameterCount).sum(); double[] combinedSensitivities = new double[size]; ImmutableList.Builder <ParameterMetadata> combinedMeta = ImmutableList.builder(); ImmutableList.Builder <ParameterSize> split = ImmutableList.builder(); int count = 0; for (int i = 0; i < sensitivities.Length; i++) { UnitParameterSensitivity sens = sensitivities[i]; Array.Copy(sens.Sensitivity.toArrayUnsafe(), 0, combinedSensitivities, count, sens.ParameterCount); combinedMeta.addAll(sens.ParameterMetadata); split.add(ParameterSize.of(sens.MarketDataName, sens.ParameterCount)); count += sens.ParameterCount; } return(new UnitParameterSensitivity(marketDataName, combinedMeta.build(), DoubleArray.ofUnsafe(combinedSensitivities), split.build())); }
/// <summary> /// Obtains an instance from a set of targets, columns and rules, resolving the targets. /// <para> /// The targets will typically be trades and positions. /// The columns represent the measures to calculate. /// </para> /// <para> /// The targets will be resolved if they implement <seealso cref="ResolvableCalculationTarget"/>. /// /// </para> /// </summary> /// <param name="rules"> the rules defining how the calculation is performed </param> /// <param name="targets"> the targets for which values of the measures will be calculated </param> /// <param name="columns"> the columns that will be calculated </param> /// <param name="refData"> the reference data to use to resolve the targets </param> /// <returns> the calculation tasks </returns> public static CalculationTasks of <T1>(CalculationRules rules, IList <T1> targets, IList <Column> columns, ReferenceData refData) where T1 : com.opengamma.strata.basics.CalculationTarget { // create columns that are a combination of the column overrides and the defaults // this is done once as it is the same for all targets //JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter: IList <Column> effectiveColumns = columns.Select(column => column.combineWithDefaults(rules.ReportingCurrency, rules.Parameters)).collect(toImmutableList()); // loop around the targets, then the columns, to build the tasks ImmutableList.Builder <CalculationTask> taskBuilder = ImmutableList.builder(); for (int rowIndex = 0; rowIndex < targets.Count; rowIndex++) { CalculationTarget target = resolveTarget(targets[rowIndex], refData); // find the applicable function, resolving the target if necessary //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: CalculationFunction<?> fn = target instanceof UnresolvableTarget ? UnresolvableTargetCalculationFunction.INSTANCE : rules.getFunctions().getFunction(target); CalculationFunction <object> fn = target is UnresolvableTarget ? UnresolvableTargetCalculationFunction.INSTANCE : rules.Functions.getFunction(target); // create the tasks IList <CalculationTask> targetTasks = createTargetTasks(target, rowIndex, fn, effectiveColumns); taskBuilder.addAll(targetTasks); } // calculation tasks holds the original user-specified columns, not the derived ones return(new CalculationTasks(taskBuilder.build(), columns)); }
/// <summary> /// Returns a list created by repeating the items in the input list multiple times, with each item repeated /// in groups. /// <para> /// For example, given a list [1, 2, 3, 4], total count 12, group size 3 the result is /// [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]. /// </para> /// <para> /// This is used when creating scenarios from every possible combination of a set of perturbations. /// /// </para> /// </summary> /// <param name="inputs"> an input list whose elements are repeated in the output </param> /// <param name="totalCount"> the number of elements in the output list </param> /// <param name="groupSize"> the number of times each element should be repeated in each group </param> /// @param <T> the type of the elements </param> /// <returns> a list created by repeating the elements of the input list </returns> internal static IList <T> repeatItems <T>(IList <T> inputs, int totalCount, int groupSize) { ImmutableList.Builder <T> builder = ImmutableList.builder(); for (int i = 0; i < (totalCount / groupSize / inputs.Count); i++) { foreach (T input in inputs) { builder.addAll(Collections.nCopies(groupSize, input)); } } return(builder.build()); }
//------------------------------------------------------------------------- /// <summary> /// Resolves the sequence to a list of steps. /// </summary> /// <param name="existingSteps"> the existing list of steps </param> /// <param name="rollConv"> the roll convention </param> /// <returns> the steps </returns> internal IList <ValueStep> resolve(IList <ValueStep> existingSteps, RollConvention rollConv) { ImmutableList.Builder <ValueStep> steps = ImmutableList.builder(); steps.addAll(existingSteps); LocalDate prev = firstStepDate; LocalDate date = firstStepDate; while (!date.isAfter(lastStepDate)) { steps.add(ValueStep.of(date, adjustment)); prev = date; date = rollConv.next(date, frequency); } if (!prev.Equals(lastStepDate)) { throw new System.ArgumentException(Messages.format("ValueStepSequence lastStepDate did not match frequency '{}' using roll convention '{}', {} != {}", frequency, rollConv, lastStepDate, prev)); } return(steps.build()); }
/// <summary> /// Adds a list of failures to the list. /// </summary> /// <param name="failures"> the failures to add </param> /// <returns> this, for chaining </returns> public FailureItemsBuilder addAllFailures(IList <FailureItem> failures) { listBuilder.addAll(failures); return(this); }