Пример #1
0
        def loss_function(x):
            """Loss function for the optimization."""
            x_mr = _to_constrained(x[:num_mean_reversion], mr_lb, mr_ub)
            x_vol = _to_constrained(x[num_mean_reversion:], vol_lb, vol_ub)

            mean_reversion_param = piecewise.PiecewiseConstantFunc(
                jump_locations=[], values=x_mr, dtype=dtype)
            volatility_param = piecewise.PiecewiseConstantFunc(
                jump_locations=volatility.jump_locations(),
                values=x_vol,
                dtype=dtype)

            model_values = swaption.swaption_price(
                expiries=expiries,
                floating_leg_start_times=float_leg_start_times,
                floating_leg_end_times=float_leg_end_times,
                fixed_leg_payment_times=fixed_leg_payment_times,
                floating_leg_daycount_fractions=floating_leg_daycount_fractions,
                fixed_leg_daycount_fractions=fixed_leg_daycount_fractions,
                fixed_leg_coupon=fixed_leg_coupon,
                reference_rate_fn=reference_rate_fn,
                dim=1,
                mean_reversion=mean_reversion_param,
                volatility=volatility_param,
                notional=notional,
                is_payer_swaption=is_payer_swaption,
                use_analytic_pricing=use_analytic_pricing,
                num_samples=num_samples,
                random_type=random_type,
                seed=seed,
                skip=skip,
                time_step=time_step,
                dtype=dtype)[:, 0]

            if volatility_based_calibration:
                model_values = implied_vol(
                    prices=model_values / annuity / notional,
                    strikes=fixed_leg_coupon[..., 0],
                    expiries=expiries,
                    forwards=swap_rate,
                    is_call_options=is_payer_swaption,
                    underlying_distribution=UnderlyingDistribution.NORMAL,
                    dtype=dtype)
                model_values = tf.where(tf.math.is_nan(model_values),
                                        tf.zeros_like(model_values),
                                        model_values)
                print(x_mr, x_vol, model_values)

            value = tf.math.reduce_sum(
                (_scale(model_values, target_lb, target_ub) -
                 scaled_target)**2)
            return value
Пример #2
0
        def loss_function(x):
            """Loss function for the optimization."""
            x_mr = _to_constrained(x[:num_mean_reversion], mr_lb, mr_ub)
            x_vol = _to_constrained(x[num_mean_reversion:], vol_lb, vol_ub)

            mean_reversion_param = piecewise.PiecewiseConstantFunc(
                jump_locations=[], values=x_mr, dtype=dtype)
            volatility_param = piecewise.PiecewiseConstantFunc(
                jump_locations=volatility.jump_locations(),
                values=x_vol,
                dtype=dtype)

            model_prices = swaption.swaption_price(
                expiries=expiries,
                floating_leg_start_times=float_leg_start_times,
                floating_leg_end_times=float_leg_end_times,
                fixed_leg_payment_times=fixed_leg_payment_times,
                floating_leg_daycount_fractions=floating_leg_daycount_fractions,
                fixed_leg_daycount_fractions=fixed_leg_daycount_fractions,
                fixed_leg_coupon=fixed_leg_coupon,
                reference_rate_fn=reference_rate_fn,
                dim=1,
                mean_reversion=mean_reversion_param,
                volatility=volatility_param,
                notional=notional,
                is_payer_swaption=is_payer_swaption,
                use_analytic_pricing=use_analytic_pricing,
                num_samples=num_samples,
                random_type=random_type,
                seed=seed,
                skip=skip,
                time_step=time_step,
                dtype=dtype)[:, 0]

            value = tf.math.reduce_sum(
                (_scale(model_prices, price_lb, price_ub) - scaled_prices)**2)
            return value