Math-Business-BlackScholes-Binaries-Greeks

 view release on metacpan or  search on metacpan

lib/Math/Business/BlackScholes/Binaries/Greeks/Delta.pm  view on Meta::CPAN

        my $phi = ($vol * $vol) / ($h * $h * $h) * exp(-$lambda_k_dash * $t) * $k * $k / $lambda_k_dash;

        $series_part += $phi * $pi * $pi * cos($k * $pi * ($h - $x) / $h);

        #
        # For delta, the stability function is $phi/$S, for gamma it is different,
        # but we shall ignore for now.
        #
        if ($k == 1 and (not(abs($phi / $S) < $stability_constant))) {
            die
                "$0: PELSSER DELTA formula for S=$S, U=$U, D=$D, t=$t, r_q=$r_q, mu=$mu, vol=$vol, w=$w, eta=$eta cannot be evaluated because PELSSER DELTA stability conditions ($phi / $S less than $stability_constant) not met. This could be due to b...
        }
    }

    # Need to take care when $mu goes to zero
    if (abs($mu_new) < $Math::Business::BlackScholesMerton::Binaries::SMALL_VALUE_MU) {
        my $sign = ($mu_new >= 0) ? 1 : -1;
        $mu_new = $sign * $Math::Business::BlackScholesMerton::Binaries::SMALL_VALUE_MU;
        $mu_dash = sqrt(($mu_new * $mu_new) + (2 * $vol * $vol * $r_q * (1 - $w)));
    }

lib/Math/Business/BlackScholes/Binaries/Greeks/Gamma.pm  view on Meta::CPAN

        my $lambda_k_dash = (0.5 * (($mu_dash * $mu_dash) / ($vol * $vol) + ($k * $k * $pi * $pi * $vol * $vol) / ($h * $h)));

        my $phi = ($vol * $vol) / ($h**4) * exp(-$lambda_k_dash * $t) * ($k**3) / $lambda_k_dash;

        $series_part += $phi * ($pi**3) * sin($k * $pi * ($h - $x) / $h);

        if ($k == 1
            and (not(abs($phi / ($S**2)) < $stability_constant)))
        {
            die
                "$0: PELSSER GAMMA formula for S=$S, U=$U, D=$D, t=$t, r=$r_q, mu=$mu, vol=$vol, w=$w, eta=$eta cannot be evaluated because PELSSER GAMMA stability conditions ($phi / ($S * $S) less than $stability_constant) not met. This could be due...
        }
    }

    # Need to take care when $mu goes to zero
    if (abs($mu_) < $Math::Business::BlackScholesMerton::Binaries::SMALL_VALUE_MU) {
        my $sign = ($mu_ >= 0) ? 1 : -1;
        $mu_ = $sign * $Math::Business::BlackScholesMerton::Binaries::SMALL_VALUE_MU;
        $mu_dash =
            sqrt(max($Math::Business::BlackScholesMerton::Binaries::SMALL_VALUE_MU, ($mu_ * $mu_) + (2 * $vol * $vol * $r_q * (1 - $w))));
    }

lib/Math/Business/BlackScholes/Binaries/Greeks/Theta.pm  view on Meta::CPAN


    for (my $k = 1; $k < $iterations_required; $k++) {
        my $lambda_k_dash = (0.5 * (($mu_dash * $mu_dash) / ($vol * $vol) + ($k * $k * $pi * $pi * $vol * $vol) / ($h * $h)));

        my $phi = ($vol * $vol) / ($h * $h) * (1 + ($r_q * $w / $lambda_k_dash)) * exp(-($r_q * $w + $lambda_k_dash) * $t) * $k;

        $series_part += $phi * $pi * sin($k * $pi * ($h - $x) / $h);

        if ($k == 1 and (not(abs($phi) < $stability_constant))) {
            die
                "$0: PELSSER THETA formula for S=$S, U=$U, D=$D, t=$t, r=$r_q, mu=$mu, vol=$vol, w=$w, eta=$eta cannot be evaluated because PELSSER THETA stability conditions ($phi less than $stability_constant) not met. This could be due to barriers...
        }
    }

# We have to handle the special case where the denominator approaches 0, see our documentation in
# quant/Documents/Breakout_bet.tex under the SVN "quant" module.
    if ((Math::Trig::sinh($mu_dash * $h / ($vol * $vol))) == 0) {
        $hyp_part = -($r_q * $w) * exp(-$r_q * $w * $t) * ($x / $h);
    } else {
        $hyp_part =
            -($r_q * $w) * exp(-$r_q * $w * $t) * Math::Trig::sinh($mu_dash * $x / ($vol * $vol)) / Math::Trig::sinh($mu_dash * $h / ($vol * $vol));

lib/Math/Business/BlackScholes/Binaries/Greeks/Vanna.pm  view on Meta::CPAN

        my $dbetak_domega = $dlambdak_domega * $dbetak_dlambdak;

        my $phi = (1.0 / ($h * $h * $h)) * ($omega * $dbetak_domega + $beta_k) * $k * $k;

        $series_part += $phi * $pi * $pi * cos($k * $pi * ($h - $x) / $h);

        if ($k == 1
            and (not(abs(2 * $vol * $phi / $S) < $stability_constant)))
        {
            die
                "$0: PELSSER VANNA formula for S=$S, U=$U, D=$D, t=$t, r_q=$r_q, mu=$mu, vol=$vol, w=$w, eta=$eta cannot be evaluated because PELSSER VANNA stability conditions (2 * $vol * $phi / $S less than $stability_constant) not met. This could ...
        }
    }

    my $alpha = $mu_dash / ($vol * $vol);
    my $dalpha_domega = -(($mu_new * $omega) + (2 * $mu_new * $mu_new) + (2 * $r_dash * $omega)) / (2 * $alpha * $omega * $omega * $omega);

# We have to handle the special case where the denominator approaches 0, see our documentation in
# quant/Documents/Breakout_bet.tex under the SVN "quant" module.
    if ((Math::Trig::sinh($alpha * $h)**2) == 0) {
        $hyp_part = 0;

lib/Math/Business/BlackScholes/Binaries/Greeks/Vega.pm  view on Meta::CPAN

        $series_part += $phi * $pi * sin($k * $pi * ($h - $x) / $h);

#
# For vega, the stability function is 2* $vol * $phi, for volga/vanna it is different,
# but we shall ignore for now.
#
        if ($k == 1
            and (not(abs(2 * $vol * $phi) < $stability_constant)))
        {
            die
                "$0: PELSSER VEGA formula for S=$S, U=$U, D=$D, t=$t, r_q=$r_q, mu=$mu, vol=$vol, w=$w, eta=$eta cannot be evaluated because PELSSER VEGA stability conditions (2 * $vol * $phi less than $stability_constant) not met. This could be due ...
        }
    }

    my $alpha = $mu_dash / ($vol * $vol);
    my $dalpha_domega = -(($mu_new * $omega) + (2 * $mu_new * $mu_new) + (2 * $r_dash * $omega)) / (2 * $alpha * $omega * $omega * $omega);

# We have to handle the special case where the denominator approaches 0, see our documentation in
# quant/Documents/Breakout_bet.tex under the SVN "quant" module.
    if ((Math::Trig::sinh($alpha * $h)**2) == 0) {
        $hyp_part = 0;

lib/Math/Business/BlackScholes/Binaries/Greeks/Volga.pm  view on Meta::CPAN

        # d{beta_k}/dw
        my $dbetak_domega = $dlambdak_domega * $dbetak_dlambdak;
        my $d2betak_domega2 = ($dlambdak_domega * $dlambdak_domega * $d2betak_dlambdak2) + ($dbetak_dlambdak * $d2lambdak_domega2);

        my $phi = (1.0 / ($h * $h)) * ($omega * $d2betak_domega2 + 2 * $dbetak_domega) * $k;

        $series_part += $phi * $pi * sin($k * $pi * ($h - $x) / $h);

        if ($k == 1 and (not(abs(4 * $vol * $vol * $phi) < $stability_constant))) {
            die
                "$0: PELSSER VOLGA formula for S=$S, U=$U, D=$D, t=$t, r_q=$r_q, mu=$mu, vol=$vol, w=$w, eta=$eta cannot be evaluated because PELSSER VOLGA stability conditions (4 * $vol * $vol * $phi less than $stability_constant) not met. This coul...
        }
    }

    my $alpha = $mu_dash / ($vol * $vol);
    my $dalpha_domega = -(($mu_new * $omega) + (2 * $mu_new * $mu_new) + (2 * $r_dash * $omega)) / (2 * $alpha * $omega * $omega * $omega);

    my $d2alpha_domega2 = $alpha * ($omega**3) * (2 * $mu_new + $omega - 4 * $r_dash);
    $d2alpha_domega2 +=
        (($mu_new * $omega) + (2 * $mu_new * $mu_new) + (2 * $r_dash * $omega)) *
        ((6 * $alpha * $omega * $omega) + (2 * $omega * $omega * $omega * $dalpha_domega));

t/Greeks.t  view on Meta::CPAN

        . $bet_type . ' '
        . 'sigma: ['
        . $case->{sigma} . '] '
        . 'barrier:['
        . $case->{barriers}[0]
        . ($case->{barriers}[1] ? ', ' . $case->{barriers}[1] : '') . '] '
        . 's,r,q:['
        . join(', ', map { $case->{$_} } ('s', 'r', 'q')) . ']';
    subtest $case_descrip => sub {
        foreach my $greek (qw(delta gamma theta vanna vega volga)) {
            my $formula_name   = 'Math::Business::BlackScholes::Binaries::Greeks::' . ucfirst($greek) . '::' . lc($bet_type);
            my $formula        = \&$formula_name;
            my $computed_value = $formula->($case->{s}, @{$case->{barriers}}, $case->{t} / 365, $case->{r}, $case->{r} - $case->{q}, $case->{sigma});
            is(roundnear(1e-4, $computed_value), $case->{$greek}, $greek);
        }
    };
}



( run in 0.744 second using v1.01-cache-2.11-cpan-26ccb49234f )