1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use num_complex::Complex;
use std::f64::consts::PI;

fn compute_stable_phi(alpha: f64) -> f64 {
    (alpha * 0.5 * PI).tan()
}

fn stable_cf_memoize(
    u: &Complex<f64>,
    alpha: f64,
    mu: f64,
    beta: f64,
    c: f64,
    phi: f64,
) -> Complex<f64> {
    (u * mu - (u * Complex::new(0.0, -1.0) * c).powf(alpha) * Complex::new(1.0, -beta * phi)).exp()
}
/// Returns characteristic function of a stable distribution.
/// # Examples
///
/// ```
/// extern crate num_complex;
/// use num_complex::Complex;
/// extern crate cf_functions;
/// # fn main() {
/// let u = Complex::new(1.0, 1.0);
/// let alpha = 0.5;
/// let mu = 0.5;
/// let beta = 0.3;
/// let c = 0.3;
/// let cf = cf_functions::stable::stable_cf(
///     &u, alpha, mu, beta, c
/// );
/// # }
/// ```
pub fn stable_cf(u: &Complex<f64>, alpha: f64, mu: f64, beta: f64, c: f64) -> Complex<f64> {
    let phi = compute_stable_phi(alpha);
    stable_cf_memoize(&u, alpha, mu, beta, c, phi)
}

const BETA_STABLE: f64 = 1.0; //to stay on the positive reals

//for stable distribution
fn alpha_stable_log(
    u: &Complex<f64>,
    t: f64,
    v0: f64,
    a: f64,
    sigma: f64,
    lambda: f64,
    correlation: f64,
    alpha: f64,
    mu: f64,
    c: f64,
    phi: f64,
    num_steps: usize,
) -> Complex<f64> {
    crate::cir::cir_leverage_jump(
        u,
        &|u| stable_cf_memoize(u, alpha, mu, BETA_STABLE, c, phi),
        t,
        v0,
        correlation,
        mu,
        a,
        sigma,
        lambda,
        num_steps,
    )
}

/// Returns log CF of an alpha stable process when transformed by an affine process
/// and the process is correlated with the jump component of the Levy process.
///
/// # Remarks
/// The time change is assumed to be a single-dimensional CIR process with a
/// jump component with mean 1.
/// The correlation between the Levy process and the affine process is due
/// to sharing the same jumps (both the Levy process and the affine process
/// jump at the same time).
///
/// # Examples
///
/// ```
/// extern crate num_complex;
/// use num_complex::Complex;
/// extern crate cf_functions;
/// # fn main() {
/// let mu=1300.0;
/// let c=100.0;
/// let alpha=1.1;
/// let lambda=100.0;
/// let correlation=0.9;
/// let a=0.4;
/// let sigma=0.4;
/// let t=1.0;
/// let v0=1.0;
/// let num_steps:usize=1024;
/// let cf=|u:&Complex<f64>|u.exp();
/// let cf=cf_functions::stable::alpha_stable_leverage(
///     t, v0, a, sigma, lambda,
///     correlation, alpha, mu, c, num_steps
/// );
/// let u=Complex::new(0.05, -0.5);
/// let value_of_cf=cf(&u);
/// # }
/// ```
pub fn alpha_stable_leverage(
    t: f64,
    v0: f64,
    a: f64,
    sigma: f64,
    lambda: f64,
    correlation: f64,
    alpha: f64,
    mu: f64,
    c: f64,
    num_steps: usize,
) -> impl Fn(&Complex<f64>) -> Complex<f64> {
    let phi = compute_stable_phi(alpha);
    move |u| {
        alpha_stable_log(
            u,
            t,
            v0,
            a,
            sigma,
            lambda,
            correlation,
            alpha,
            mu,
            c,
            phi,
            num_steps,
        )
        .exp()
    }
}