adversarial-robustness-toolbox
adversarial-robustness-toolbox copied to clipboard
Create MarketBackFinal 2.0
Backdoor attack via jumps-Diffusion and stock market and Bayesian Optimization : MarketBackFinal 2.0
Description
Hi guys @beat-buesser !, I am attaching the latest backdoor attack method based on Dynamic Hedging and rough volatility paths, the attack method is called MarketBackFinal 2.0 and it focuses explicitly on Bayesian optimization (BO) of trading processes. jump via a stochastic diffusion method.
Testing
MarketBackFinal 2.0 : For Bayesian optimization, an advanced black-box objective function is used in order to be able to use quadratic terms introducing non-linearity, sinusoidal components which add periodic fluctuations, making the oscillatory function more complex, thus introducing multiple local minima and maxima:
$$f(x, y) = -(x^{2} + y^{2} + 0.1xy + \sin{x} + \cos{y})$$
Update September 12rd, 2024 , last notebook,link
This is the last notebook, the next step will be to incorporate methods into the ART.1.19 framework.
Last update September 20, 2024 , last notebook,link
[update notebook MarketBackFinal 2.0 ( best version for fluid comprehension ) link..., notebook link(This notebook corresponds to the two figures you are observing )](https://github.com/OrsonTyphanel93/adversarial-robustness-toolbox/blob/dev_1.14.0/Udpdate_ART_1_18_MarketBackFinal_2_0_adversarial_machine_learning.ipynb
Reference 0 Reference 1 Reference 2 Reference 3 Reference 4 Reference 5 Reference 6
Reference 7 Reference 8 Reference 9 Reference 10 Reference 11 Reference 12 Reference 13 Reference 14 Reference 15 Reference 16 Reference 17
Reference 18
Reference 19
Reference 20
Reference 21
Reference 22
Reference 23
Reference 24
Reference 25
Reference 26
Reference 26
Reference 27
Reference 28
Reference 29
Reference 34 Reference 35 Reference 36 Reference 37 Reference 38 Reference 39 Reference 40 Reference 41 END.
The pathwise estimator of the option delta is $$\frac{dY}{dS(0)} = e^{-rT} 1_{\bar{S} > K} \frac{\bar{S}}{S(0)}.$$
$$S(t_i) = S(t_{i-1})e^{(r - \frac{1}{2}\sigma^2)(t_i - t_{i-1})} + \sigma\sqrt{t_i - t_{i-1}}Z_i, \quad i = 1, \dots, m,$$
with $Z_1,\ldots,Z_m$ independent standard normal random variables. The parameter $\sigma$ affects $S(t_i)$ explicitly through this functional relation but also implicitly through the dependence of $S(t_{i-1})$ on $\sigma$. By differentiating both sides, we get a recursion for the derivatives along the path:
$$\frac{dS(t_i)}{d\sigma} = \frac{dS(t_{i-1})}{d\sigma} \frac{S(t_i)}{S(t_{i-1})} + S(t_i)\left[-\sigma(t_i - t_{i-1}) + \sqrt{t_i - t_{i-1}}Z_i\right],$$
With initial condition $\frac{dS(0)}{d\sigma}=0$, this recursion is solved by
$$\frac{dS(t_i)}{d\sigma} = S(t_i)\left[\log\left(\frac{S(t_i)}{S(0)}\right) - (r + \frac{1}{2}\sigma^2)t_i\right]/\sigma.$$
The pathwise estimator of the Asian option vega is
$$e^{-rT} \frac{1}{m} \sum_{i=1}^{m} \frac{dS(t_i)}{d\sigma} 1_{\bar{S} > K}.$$
$$\begin{aligned} & \mathbb{E}\left[e^{-r T} V_T-e^{-r T}\left(S_T-K\right)_{+}\right]^2 \ &= {\left[V_0-v\left(0, S_0\right)\right]^2+\mathbb{E}\left(\int_0^T e^{-2 r s} S_{s-}^2 \sigma^2\left[v_x^{\prime}\left(s, S_{s-}\right)-\delta(s)\right]^2 d s\right.} \ &\left.\quad+\int_0^T \lambda \int_{\mathbb{R}} e^{-2 r s}\left[v\left(s, S_{s-}(1+y)\right)-v\left(s, S_{s-}\right)-\delta(s) y S_{s-}\right]^2 \nu(d y) d s\right) \end{aligned}$$
$$\begin{aligned} & e^{-r T}\left(S_T-K\right)_{+} \ & =v\left(0, S_0\right)+\int_0^T e^{-r t}\left(v_t^{\prime}\left(t, S_{t-}\right)+\mu S_{t-} v_x^{\prime}\left(t, S_{t-}\right)+\frac{1}{2} \sigma^2 S_{t-}^2 v_{x x}^{\prime \prime}\left(t, S_{t-}\right)-r v\left(t, S_{t-}\right)\right) d t \ & +\int_0^T e^{-r t} \sigma S_{t-} v_x^{\prime}\left(t, S_{t-}\right) d W_t+\int_{10, T]} e^{-r t}\left(v\left(t, S_{t-}\left(1+Y_t\right)\right)-v\left(t, S_{t-}\right)\right) d N_t \ & =v\left(0, S_0\right)+\int_0^T e^{-r t} \sigma S_{t-} v_x^{\prime}\left(t, S_{t-}\right) d W_t+\int_{\mid 0, T]} e^{-r t}\left(v\left(t, S_{t-}\left(1+Y_t\right)\right)-v\left(t, S_{t-}\right)\right) d N_t \ & \quad-\int_0^T e^{-r t} \lambda \int_{\mathbb{R}}\left(v\left(t, S_{t-}(1+y)\right)-v\left(t, S_{t-}\right)\right) \nu(d y) d t . \end{aligned}$$
$$\begin{aligned} e^{-r T} V_T & =V_0+\int_0^T \delta(t) e^{-r t} S_{t-}\left((\mu-r) d t+\sigma d W_t\right)+\int_{[0, T]} \delta(t) e^{-r t} S_{t-} Y_t d N_t \ & =V_0+\int_0^T e^{-r t} \sigma S_{t-} \delta(t) d W_t \ & +\int_{10, T]} \delta(t) e^{-r t} S_{t-} Y_t d N_t-\int_0^T \lambda \delta(t) e^{-r t} S_{t-} \int_{\mathbb{R}} y \nu(dy) dt \end{aligned}$$
Let $V_t^C$ be the value of a Call option at time $t$ whose payoff is $\Lambda^C$ on the underlying $S(t)$
$$V_t^C=\mathbb{E}^{\mathbb{Q}}\left[e^{-r(T-t)} \Lambda^C \mid \mathcal{F}_t\right]$$
$$ \begin{aligned} V_t^C= & S(t) \Phi\left(\frac{\ln \frac{S(t)}{K}+\left(r+\sigma^2 / 2\right)(T-t)}{\sigma \sqrt{T-t}}\right) \ & -K e^{-r(T-t)} \Phi\left(\frac{\ln \frac{S(t)}{K}+\left(r-\sigma^2 / 2\right)(T-t)}{\sigma \sqrt{T-t}}\right) \ V_t^C= & S(t) \Phi\left(d_{1, t}\right)-K e^{-r(T-t)} \Phi\left(d_{2, t}\right) \end{aligned} $$
where $\Phi$ is the cumulative distribution function of a standard normal random variable, and $d_{1, t}$ and $d_{2, t}$ are given as follows:
$$ d_{1, t}=\frac{\ln \frac{S(t)}{K}+\left(r+\sigma^2 / 2\right)(T-t)}{\sigma \sqrt{T-t}} $$
and
$$ \begin{aligned} d_{2, t} & =\frac{\ln \frac{S(t)}{K}+\left(r-\sigma^2 / 2\right)(T-t)}{\sigma \sqrt{T-t}} \ & =d_{1, t}-\sigma \sqrt{T-t} \end{aligned} $$
Then the unique rational price at time 0 of the Call option can be found betting $t=0$ in the equations of $V_t^C, d_{1, t}$ and $d_{2, t}$ to get the famous Black-Scholes pricing formula:
$$V_0^C=S(0) \Phi\left(d_{1,0}\right)-K e^{-r T} \Phi\left(d_{2,0}\right)$$
Fourier transform method for price calculation,sources
Reference 1 Reference 2 Reference 3
FAQ
to quantify the measure of financial risk, you can apply in the code of this notebook dating from August 2024, this code attached in this notebook.
We quantify the simulations by determining the potential financial risks that may arise over a given time horizon.
A zero coupon bond price subject to default risk (at time $t$ ) with a tenor of $T$ years can be defined as follows ,
$$\tilde{\delta}(t, T)=\underbrace{\mathbb{E}_t^{\mathbb{Q}}\left(e^{-\int_t^T r(u) d u} \mathbb{I}_{\tau>T}\right)}_{}+\underbrace{\mathbb{E}_t^{\mathbb{Q}}\left(e^{-\int_t^T r(u) d u} R \mathbb{I}_{\tau \leq T}\right)}_{}$$
where $R$ denotes the recovery rate and $r(t)$ is the instantaneous short-rate process. There is a single unit cash-flow at time $T$, the pay-off is $R \in[0,1]$.
$$\begin{aligned} \tilde{\delta}(t, T) & =\mathbb{E}_t^{\mathbb{Q}}\left(e^{-\int_t^T r(u) d u}\right) \mathbb{E}_t^{\mathbb{Q}}\left(\mathbb{I}_{\tau>T}\right)+\mathbb{E}_t^{\mathbb{Q}}\left(e^{-\int_t^T r(u) d u}\right) R \mathbb{E}_t^{\mathbb{Q}}\left(\mathbb{I}_{\tau \leq T}\right) \ & =\delta(t, T) \mathbb{Q}(\tau>T)+\delta(t, T) R \mathbb{Q}(\tau \leq T) \ & =\delta(t, T)(\mathbb{Q}(\tau>T)+R \mathbb{Q}(\tau \leq T)) \end{aligned}$$
where $\delta(t, T)$ and $\tilde{\delta}(t, T)$ represent risk-free and default-risky $T$ period zero-coupon bonds, respectively. The terms $\mathbb{Q}(\tau>T)$ and $\mathbb{Q}(\tau \leq T)$ .
Usage/Examples mathematics concept see
def tail_probabilities_and_shortfall_integrals(self, S, K, T, r, sigma, threshold=5):
"""
Calculate tail probabilities and shortfall integrals for a call option.
Parameters:
S (float): Underlying asset price
K (float): Strike price
T (float): Time to expiration
r (float): Risk-free interest rate
sigma (float): Volatility of the underlying asset
threshold (float): Threshold value for shortfall calculation
Returns:
tuple: (tail_probability, shortfall_integral)
"""
# Calculate d1 and d2
d1 = (np.log(S / K) + (r + 0.5 * sigma**2) * T) / (sigma * np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
# Calculate tail probability
tail_probability = 1 - norm.cdf(d2)
# Calculate shortfall integral
shortfall_integral = (K * np.exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1)) - threshold
# Handle potential numerical issues
tail_probability = np.clip(tail_probability, 0, 1)
shortfall_integral = np.clip(shortfall_integral, 0, None)
# Calculate VaR for 95% and 99% confidence levels
var_95 = norm.ppf(0.05, d1, sigma * np.sqrt(T))
var_99 = norm.ppf(0.01, d1, sigma * np.sqrt(T))
# Convert VaR values to percentage loss
var_95_percent = (1 - np.exp(var_95)) * 100
var_99_percent = (1 - np.exp(var_99)) * 100
return tail_probability, shortfall_integral, var_95_percent, var_99_percent
# Then apply exactly this in the Bayesian function only, and you'll get all the numerical values
tail_prob, shortfall_int,var_95_percent, var_99_percent = self.tail_probabilities_and_shortfall_integrals(S, K, T, r,sigma, threshold=5)
print(f"Tail Probability: {tail_prob}")
print(f"Shortfall Integral: {shortfall_int}")
print(f"95% VaR: {var_95_percent}")
print(f"99% VaR: {var_99_percent}")
Usage/Examples McKean-Vlasov process
def mean_field_interaction(self,x):
"""
Compute the mean field interaction for the McKean-Vlasov process.
Parameters:
x (numpy array): Current state of the process
Returns:
float: Mean field interaction value
"""
# Use np.mean() for efficiency
mean_field_effect = np.mean(x)
# Add a small constant to avoid division by zero
epsilon = 1e-8
# Compute the squared difference between each element and the mean
squared_diff = (x - mean_field_effect)**2
# Compute the mean of the squared differences
var_x = np.mean(squared_diff)
# Compute the second-order correction term
second_order_correction = 0.5 * var_x
# Combine terms
drift_term = mean_field_effect + second_order_correction
return drift_term
def mckean_vlasov_process(self,P0, mu, sigma, t_max, dt, num_steps):
"""
Simulate the McKean-Vlasov process.
Parameters:
S0 (float): Initial value of the process
mu (callable): Drift term function
sigma (float): Volatility
t_max (float): Maximum time
dt (float): Time step size
num_steps (int): Number of steps
Returns:
numpy array: Array of simulated values at each time step
"""
n = int(t_max / dt)
t = np.linspace(0, t_max, n+1)
# Initialize the process
P = np.zeros(n+1)
P[0] = P0
# Generate random numbers for the Brownian motion
# Simulate the process
for i in range(1, n+1):
dW = norm.rvs(size=1)
P[i] = P[i-1] * np.exp((mu(P[i-1]) - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * dW)
return t, P
t, P = self.mckean_vlasov_process(P0=1.0, mu=self.mean_field_interaction, sigma=0.5, t_max=1.0, dt=0.01, num_steps=10000)
print(f"Mckean-Vlasov Process: Time={t}, Value={S}")
plt.figure(figsize=(12, 6))
plt.plot(t, P)
plt.title("McKean-Vlasov Process Simulation")
plt.xlabel("Time")
plt.ylabel("Value")
plt.grid(True)
plt.savefig('mckean_vlasov_process_simulation.png', dpi=300, bbox_inches='tight')
plt.show()