```python
import sympy as sy
from sympy import pi, cos, sin, tan
from IPython.display import display

x, y, z, alpha, beta, gamma = sy.symbols("x, y, z, alpha, beta, gamma")
Y = sy.Matrix([[x, y, z, alpha, beta, gamma]]).T  # 状態ベクトル

x_d, y_d, z_d, alpha_d, beta_d, gamma_d = sy.symbols("x_d, y_d, z_d, alpha_d, beta_d, gamma_d")
Y_d = sy.Matrix([[x_d, y_d, z_d, alpha_d, beta_d, gamma_d]]).T  # 目標位置ベクトル

x_e, y_e, z_e, alpha_e, beta_e, gamma_e = sy.symbols("x_e, y_e, z_e, alpha_e, beta_e, gamma_e")
Y_e = sy.Matrix([[x_e, y_e, z_e, alpha_e, beta_e, gamma_e]]).T  # 目標位置ベクトル

eta, a = sy.symbols("eta, a")

def sigmoid(x):
    return 1 / (1 + sy.exp(-a*x))

def pot(x):
    x_norm = x.norm()
    return 1/eta * sy.log(sy.exp(eta*x_norm) + sy.exp(-eta*x_norm))

def pot_sice(y, y_d, y_e):
    e_d = y - y_d
    e_e = y - y_e

    alpha = sigmoid(e_d.norm())

    return (1-alpha)*pot(e_d) + alpha*pot(e_e)

p = pot_sice(Y, Y_d, Y_e)

grad_p = sy.tensor.array.derive_by_array(p, Y)
grad_p
```

$\displaystyle \left[\begin{matrix}- \frac{a \left(\left(\operatorname{re}{\left(x\right)} - \operatorname{re}{\left(x_{d}\right)}\right) \frac{d}{d x} \operatorname{re}{\left(x\right)} + \left(\operatorname{im}{\left(x\right)} - \operatorname{im}{\left(x_{d}\right)}\right) \frac{d}{d x} \operatorname{im}{\left(x\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \right)} \left|{x - x_{d}}\right| \operatorname{sign}{\left(x - x_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(x - x_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{a \left(\left(\operatorname{re}{\left(x\right)} - \operatorname{re}{\left(x_{d}\right)}\right) \frac{d}{d x} \operatorname{re}{\left(x\right)} + \left(\operatorname{im}{\left(x\right)} - \operatorname{im}{\left(x_{d}\right)}\right) \frac{d}{d x} \operatorname{im}{\left(x\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \right)} \left|{x - x_{d}}\right| \operatorname{sign}{\left(x - x_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(x - x_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{\left(1 - \frac{1}{1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}}\right) \left(\frac{\eta \left(\left(\operatorname{re}{\left(x\right)} - \operatorname{re}{\left(x_{d}\right)}\right) \frac{d}{d x} \operatorname{re}{\left(x\right)} + \left(\operatorname{im}{\left(x\right)} - \operatorname{im}{\left(x_{d}\right)}\right) \frac{d}{d x} \operatorname{im}{\left(x\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{x - x_{d}}\right| \operatorname{sign}{\left(x - x_{d} \right)}}{\left(x - x_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(x\right)} - \operatorname{re}{\left(x_{d}\right)}\right) \frac{d}{d x} \operatorname{re}{\left(x\right)} + \left(\operatorname{im}{\left(x\right)} - \operatorname{im}{\left(x_{d}\right)}\right) \frac{d}{d x} \operatorname{im}{\left(x\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{x - x_{d}}\right| \operatorname{sign}{\left(x - x_{d} \right)}}{\left(x - x_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)}{\eta \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)} + \frac{\frac{\eta \left(\left(\operatorname{re}{\left(x\right)} - \operatorname{re}{\left(x_{e}\right)}\right) \frac{d}{d x} \operatorname{re}{\left(x\right)} + \left(\operatorname{im}{\left(x\right)} - \operatorname{im}{\left(x_{e}\right)}\right) \frac{d}{d x} \operatorname{im}{\left(x\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{x - x_{e}}\right| \operatorname{sign}{\left(x - x_{e} \right)}}{\left(x - x_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(x\right)} - \operatorname{re}{\left(x_{e}\right)}\right) \frac{d}{d x} \operatorname{re}{\left(x\right)} + \left(\operatorname{im}{\left(x\right)} - \operatorname{im}{\left(x_{e}\right)}\right) \frac{d}{d x} \operatorname{im}{\left(x\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{x - x_{e}}\right| \operatorname{sign}{\left(x - x_{e} \right)}}{\left(x - x_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right) \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}\right)}\\- \frac{a \left(\left(\operatorname{re}{\left(y\right)} - \operatorname{re}{\left(y_{d}\right)}\right) \frac{d}{d y} \operatorname{re}{\left(y\right)} + \left(\operatorname{im}{\left(y\right)} - \operatorname{im}{\left(y_{d}\right)}\right) \frac{d}{d y} \operatorname{im}{\left(y\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \right)} \left|{y - y_{d}}\right| \operatorname{sign}{\left(y - y_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(y - y_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{a \left(\left(\operatorname{re}{\left(y\right)} - \operatorname{re}{\left(y_{d}\right)}\right) \frac{d}{d y} \operatorname{re}{\left(y\right)} + \left(\operatorname{im}{\left(y\right)} - \operatorname{im}{\left(y_{d}\right)}\right) \frac{d}{d y} \operatorname{im}{\left(y\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \right)} \left|{y - y_{d}}\right| \operatorname{sign}{\left(y - y_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(y - y_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{\left(1 - \frac{1}{1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}}\right) \left(\frac{\eta \left(\left(\operatorname{re}{\left(y\right)} - \operatorname{re}{\left(y_{d}\right)}\right) \frac{d}{d y} \operatorname{re}{\left(y\right)} + \left(\operatorname{im}{\left(y\right)} - \operatorname{im}{\left(y_{d}\right)}\right) \frac{d}{d y} \operatorname{im}{\left(y\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{y - y_{d}}\right| \operatorname{sign}{\left(y - y_{d} \right)}}{\left(y - y_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(y\right)} - \operatorname{re}{\left(y_{d}\right)}\right) \frac{d}{d y} \operatorname{re}{\left(y\right)} + \left(\operatorname{im}{\left(y\right)} - \operatorname{im}{\left(y_{d}\right)}\right) \frac{d}{d y} \operatorname{im}{\left(y\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{y - y_{d}}\right| \operatorname{sign}{\left(y - y_{d} \right)}}{\left(y - y_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)}{\eta \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)} + \frac{\frac{\eta \left(\left(\operatorname{re}{\left(y\right)} - \operatorname{re}{\left(y_{e}\right)}\right) \frac{d}{d y} \operatorname{re}{\left(y\right)} + \left(\operatorname{im}{\left(y\right)} - \operatorname{im}{\left(y_{e}\right)}\right) \frac{d}{d y} \operatorname{im}{\left(y\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{y - y_{e}}\right| \operatorname{sign}{\left(y - y_{e} \right)}}{\left(y - y_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(y\right)} - \operatorname{re}{\left(y_{e}\right)}\right) \frac{d}{d y} \operatorname{re}{\left(y\right)} + \left(\operatorname{im}{\left(y\right)} - \operatorname{im}{\left(y_{e}\right)}\right) \frac{d}{d y} \operatorname{im}{\left(y\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{y - y_{e}}\right| \operatorname{sign}{\left(y - y_{e} \right)}}{\left(y - y_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right) \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}\right)}\\- \frac{a \left(\left(\operatorname{re}{\left(z\right)} - \operatorname{re}{\left(z_{d}\right)}\right) \frac{d}{d z} \operatorname{re}{\left(z\right)} + \left(\operatorname{im}{\left(z\right)} - \operatorname{im}{\left(z_{d}\right)}\right) \frac{d}{d z} \operatorname{im}{\left(z\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \right)} \left|{z - z_{d}}\right| \operatorname{sign}{\left(z - z_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(z - z_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{a \left(\left(\operatorname{re}{\left(z\right)} - \operatorname{re}{\left(z_{d}\right)}\right) \frac{d}{d z} \operatorname{re}{\left(z\right)} + \left(\operatorname{im}{\left(z\right)} - \operatorname{im}{\left(z_{d}\right)}\right) \frac{d}{d z} \operatorname{im}{\left(z\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \right)} \left|{z - z_{d}}\right| \operatorname{sign}{\left(z - z_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(z - z_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{\left(1 - \frac{1}{1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}}\right) \left(\frac{\eta \left(\left(\operatorname{re}{\left(z\right)} - \operatorname{re}{\left(z_{d}\right)}\right) \frac{d}{d z} \operatorname{re}{\left(z\right)} + \left(\operatorname{im}{\left(z\right)} - \operatorname{im}{\left(z_{d}\right)}\right) \frac{d}{d z} \operatorname{im}{\left(z\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{z - z_{d}}\right| \operatorname{sign}{\left(z - z_{d} \right)}}{\left(z - z_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(z\right)} - \operatorname{re}{\left(z_{d}\right)}\right) \frac{d}{d z} \operatorname{re}{\left(z\right)} + \left(\operatorname{im}{\left(z\right)} - \operatorname{im}{\left(z_{d}\right)}\right) \frac{d}{d z} \operatorname{im}{\left(z\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{z - z_{d}}\right| \operatorname{sign}{\left(z - z_{d} \right)}}{\left(z - z_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)}{\eta \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)} + \frac{\frac{\eta \left(\left(\operatorname{re}{\left(z\right)} - \operatorname{re}{\left(z_{e}\right)}\right) \frac{d}{d z} \operatorname{re}{\left(z\right)} + \left(\operatorname{im}{\left(z\right)} - \operatorname{im}{\left(z_{e}\right)}\right) \frac{d}{d z} \operatorname{im}{\left(z\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{z - z_{e}}\right| \operatorname{sign}{\left(z - z_{e} \right)}}{\left(z - z_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(z\right)} - \operatorname{re}{\left(z_{e}\right)}\right) \frac{d}{d z} \operatorname{re}{\left(z\right)} + \left(\operatorname{im}{\left(z\right)} - \operatorname{im}{\left(z_{e}\right)}\right) \frac{d}{d z} \operatorname{im}{\left(z\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{z - z_{e}}\right| \operatorname{sign}{\left(z - z_{e} \right)}}{\left(z - z_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right) \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}\right)}\\- \frac{a \left(\left(\operatorname{re}{\left(\alpha\right)} - \operatorname{re}{\left(\alpha_{d}\right)}\right) \frac{d}{d \alpha} \operatorname{re}{\left(\alpha\right)} + \left(\operatorname{im}{\left(\alpha\right)} - \operatorname{im}{\left(\alpha_{d}\right)}\right) \frac{d}{d \alpha} \operatorname{im}{\left(\alpha\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \right)} \left|{\alpha - \alpha_{d}}\right| \operatorname{sign}{\left(\alpha - \alpha_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(\alpha - \alpha_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{a \left(\left(\operatorname{re}{\left(\alpha\right)} - \operatorname{re}{\left(\alpha_{d}\right)}\right) \frac{d}{d \alpha} \operatorname{re}{\left(\alpha\right)} + \left(\operatorname{im}{\left(\alpha\right)} - \operatorname{im}{\left(\alpha_{d}\right)}\right) \frac{d}{d \alpha} \operatorname{im}{\left(\alpha\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \right)} \left|{\alpha - \alpha_{d}}\right| \operatorname{sign}{\left(\alpha - \alpha_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(\alpha - \alpha_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{\left(1 - \frac{1}{1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}}\right) \left(\frac{\eta \left(\left(\operatorname{re}{\left(\alpha\right)} - \operatorname{re}{\left(\alpha_{d}\right)}\right) \frac{d}{d \alpha} \operatorname{re}{\left(\alpha\right)} + \left(\operatorname{im}{\left(\alpha\right)} - \operatorname{im}{\left(\alpha_{d}\right)}\right) \frac{d}{d \alpha} \operatorname{im}{\left(\alpha\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{\alpha - \alpha_{d}}\right| \operatorname{sign}{\left(\alpha - \alpha_{d} \right)}}{\left(\alpha - \alpha_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(\alpha\right)} - \operatorname{re}{\left(\alpha_{d}\right)}\right) \frac{d}{d \alpha} \operatorname{re}{\left(\alpha\right)} + \left(\operatorname{im}{\left(\alpha\right)} - \operatorname{im}{\left(\alpha_{d}\right)}\right) \frac{d}{d \alpha} \operatorname{im}{\left(\alpha\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{\alpha - \alpha_{d}}\right| \operatorname{sign}{\left(\alpha - \alpha_{d} \right)}}{\left(\alpha - \alpha_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)}{\eta \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)} + \frac{\frac{\eta \left(\left(\operatorname{re}{\left(\alpha\right)} - \operatorname{re}{\left(\alpha_{e}\right)}\right) \frac{d}{d \alpha} \operatorname{re}{\left(\alpha\right)} + \left(\operatorname{im}{\left(\alpha\right)} - \operatorname{im}{\left(\alpha_{e}\right)}\right) \frac{d}{d \alpha} \operatorname{im}{\left(\alpha\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{\alpha - \alpha_{e}}\right| \operatorname{sign}{\left(\alpha - \alpha_{e} \right)}}{\left(\alpha - \alpha_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(\alpha\right)} - \operatorname{re}{\left(\alpha_{e}\right)}\right) \frac{d}{d \alpha} \operatorname{re}{\left(\alpha\right)} + \left(\operatorname{im}{\left(\alpha\right)} - \operatorname{im}{\left(\alpha_{e}\right)}\right) \frac{d}{d \alpha} \operatorname{im}{\left(\alpha\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{\alpha - \alpha_{e}}\right| \operatorname{sign}{\left(\alpha - \alpha_{e} \right)}}{\left(\alpha - \alpha_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right) \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}\right)}\\- \frac{a \left(\left(\operatorname{re}{\left(\beta\right)} - \operatorname{re}{\left(\beta_{d}\right)}\right) \frac{d}{d \beta} \operatorname{re}{\left(\beta\right)} + \left(\operatorname{im}{\left(\beta\right)} - \operatorname{im}{\left(\beta_{d}\right)}\right) \frac{d}{d \beta} \operatorname{im}{\left(\beta\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \right)} \left|{\beta - \beta_{d}}\right| \operatorname{sign}{\left(\beta - \beta_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(\beta - \beta_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{a \left(\left(\operatorname{re}{\left(\beta\right)} - \operatorname{re}{\left(\beta_{d}\right)}\right) \frac{d}{d \beta} \operatorname{re}{\left(\beta\right)} + \left(\operatorname{im}{\left(\beta\right)} - \operatorname{im}{\left(\beta_{d}\right)}\right) \frac{d}{d \beta} \operatorname{im}{\left(\beta\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \right)} \left|{\beta - \beta_{d}}\right| \operatorname{sign}{\left(\beta - \beta_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(\beta - \beta_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{\left(1 - \frac{1}{1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}}\right) \left(\frac{\eta \left(\left(\operatorname{re}{\left(\beta\right)} - \operatorname{re}{\left(\beta_{d}\right)}\right) \frac{d}{d \beta} \operatorname{re}{\left(\beta\right)} + \left(\operatorname{im}{\left(\beta\right)} - \operatorname{im}{\left(\beta_{d}\right)}\right) \frac{d}{d \beta} \operatorname{im}{\left(\beta\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{\beta - \beta_{d}}\right| \operatorname{sign}{\left(\beta - \beta_{d} \right)}}{\left(\beta - \beta_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(\beta\right)} - \operatorname{re}{\left(\beta_{d}\right)}\right) \frac{d}{d \beta} \operatorname{re}{\left(\beta\right)} + \left(\operatorname{im}{\left(\beta\right)} - \operatorname{im}{\left(\beta_{d}\right)}\right) \frac{d}{d \beta} \operatorname{im}{\left(\beta\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{\beta - \beta_{d}}\right| \operatorname{sign}{\left(\beta - \beta_{d} \right)}}{\left(\beta - \beta_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)}{\eta \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)} + \frac{\frac{\eta \left(\left(\operatorname{re}{\left(\beta\right)} - \operatorname{re}{\left(\beta_{e}\right)}\right) \frac{d}{d \beta} \operatorname{re}{\left(\beta\right)} + \left(\operatorname{im}{\left(\beta\right)} - \operatorname{im}{\left(\beta_{e}\right)}\right) \frac{d}{d \beta} \operatorname{im}{\left(\beta\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{\beta - \beta_{e}}\right| \operatorname{sign}{\left(\beta - \beta_{e} \right)}}{\left(\beta - \beta_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(\beta\right)} - \operatorname{re}{\left(\beta_{e}\right)}\right) \frac{d}{d \beta} \operatorname{re}{\left(\beta\right)} + \left(\operatorname{im}{\left(\beta\right)} - \operatorname{im}{\left(\beta_{e}\right)}\right) \frac{d}{d \beta} \operatorname{im}{\left(\beta\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{\beta - \beta_{e}}\right| \operatorname{sign}{\left(\beta - \beta_{e} \right)}}{\left(\beta - \beta_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right) \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}\right)}\\- \frac{a \left(\left(\operatorname{re}{\left(\gamma\right)} - \operatorname{re}{\left(\gamma_{d}\right)}\right) \frac{d}{d \gamma} \operatorname{re}{\left(\gamma\right)} + \left(\operatorname{im}{\left(\gamma\right)} - \operatorname{im}{\left(\gamma_{d}\right)}\right) \frac{d}{d \gamma} \operatorname{im}{\left(\gamma\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \right)} \left|{\gamma - \gamma_{d}}\right| \operatorname{sign}{\left(\gamma - \gamma_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(\gamma - \gamma_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{a \left(\left(\operatorname{re}{\left(\gamma\right)} - \operatorname{re}{\left(\gamma_{d}\right)}\right) \frac{d}{d \gamma} \operatorname{re}{\left(\gamma\right)} + \left(\operatorname{im}{\left(\gamma\right)} - \operatorname{im}{\left(\gamma_{d}\right)}\right) \frac{d}{d \gamma} \operatorname{im}{\left(\gamma\right)}\right) e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \log{\left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \right)} \left|{\gamma - \gamma_{d}}\right| \operatorname{sign}{\left(\gamma - \gamma_{d} \right)}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)^{2} \left(\gamma - \gamma_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + \frac{\left(1 - \frac{1}{1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}}\right) \left(\frac{\eta \left(\left(\operatorname{re}{\left(\gamma\right)} - \operatorname{re}{\left(\gamma_{d}\right)}\right) \frac{d}{d \gamma} \operatorname{re}{\left(\gamma\right)} + \left(\operatorname{im}{\left(\gamma\right)} - \operatorname{im}{\left(\gamma_{d}\right)}\right) \frac{d}{d \gamma} \operatorname{im}{\left(\gamma\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{\gamma - \gamma_{d}}\right| \operatorname{sign}{\left(\gamma - \gamma_{d} \right)}}{\left(\gamma - \gamma_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(\gamma\right)} - \operatorname{re}{\left(\gamma_{d}\right)}\right) \frac{d}{d \gamma} \operatorname{re}{\left(\gamma\right)} + \left(\operatorname{im}{\left(\gamma\right)} - \operatorname{im}{\left(\gamma_{d}\right)}\right) \frac{d}{d \gamma} \operatorname{im}{\left(\gamma\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} \left|{\gamma - \gamma_{d}}\right| \operatorname{sign}{\left(\gamma - \gamma_{d} \right)}}{\left(\gamma - \gamma_{d}\right) \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)}{\eta \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right)} + \frac{\frac{\eta \left(\left(\operatorname{re}{\left(\gamma\right)} - \operatorname{re}{\left(\gamma_{e}\right)}\right) \frac{d}{d \gamma} \operatorname{re}{\left(\gamma\right)} + \left(\operatorname{im}{\left(\gamma\right)} - \operatorname{im}{\left(\gamma_{e}\right)}\right) \frac{d}{d \gamma} \operatorname{im}{\left(\gamma\right)}\right) e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{\gamma - \gamma_{e}}\right| \operatorname{sign}{\left(\gamma - \gamma_{e} \right)}}{\left(\gamma - \gamma_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} - \frac{\eta \left(\left(\operatorname{re}{\left(\gamma\right)} - \operatorname{re}{\left(\gamma_{e}\right)}\right) \frac{d}{d \gamma} \operatorname{re}{\left(\gamma\right)} + \left(\operatorname{im}{\left(\gamma\right)} - \operatorname{im}{\left(\gamma_{e}\right)}\right) \frac{d}{d \gamma} \operatorname{im}{\left(\gamma\right)}\right) e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} \left|{\gamma - \gamma_{e}}\right| \operatorname{sign}{\left(\gamma - \gamma_{e} \right)}}{\left(\gamma - \gamma_{e}\right) \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}}{\eta \left(1 + e^{- a \sqrt{\left|{\alpha - \alpha_{d}}\right|^{2} + \left|{\beta - \beta_{d}}\right|^{2} + \left|{\gamma - \gamma_{d}}\right|^{2} + \left|{x - x_{d}}\right|^{2} + \left|{y - y_{d}}\right|^{2} + \left|{z - z_{d}}\right|^{2}}}\right) \left(e^{\eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}} + e^{- \eta \sqrt{\left|{\alpha - \alpha_{e}}\right|^{2} + \left|{\beta - \beta_{e}}\right|^{2} + \left|{\gamma - \gamma_{e}}\right|^{2} + \left|{x - x_{e}}\right|^{2} + \left|{y - y_{e}}\right|^{2} + \left|{z - z_{e}}\right|^{2}}}\right)}\end{matrix}\right]$


# Cross-validated MANOVA in Python
I've worked quite a bit on 'decoding (machine-learning) analyses' of fMRI data. A while back, I came across the article by [Allefeld & Haynes (2014)](http://www.sciencedirect.com/science/article/pii/S1053811913011920) on 'cross-validated MANOVA', an multivariate encoding-type of analysis. In this notebook, I've tried to implement this analysis and annotated some of the steps.

# MGLM
The MANOVA is a test derived from the multivariate general linear model (MGLM). Conceptually, a MANOVA tests how much variance in a set of target variables (dependent variables; $\mathbf{y}$) by a (set of) predictor(s) ($\mathbf{X}$); in other words, it's a "multivariate analysis of variance", as the name implies. In my understanding (which is often more conceptual than mathematical), the first step in the analysis described by Allefeld & Haynes is to simply calculate the parameters of the GLM ($\beta$) for all target variables. The GLM, here, is defined as:

\begin{align}
y = \beta\mathbf{X} + \epsilon
\end{align}

in which $\beta$ represents the GLM's parameter(s) and $\epsilon$ the model's errors. Now, the parameter(s) $\beta$ can be found by the GLM's analytical solution:

\begin{align}
\hat{\beta} = (\mathbf{X}'\mathbf{X})^{-1}y
\end{align}

In which $X$ represents the design-matrix (predictors), a $N\ (samples) \times P\ (predictors)$ matrix, and $y$ represents a $N \times 1$ column-vector with the target variable. I recently found at that you can estimate the parameters for different target variables (so different $y$ variables) corresponding to the model:

\begin{align}
\mathbf{y} = \beta\mathbf{X} + \Xi
\end{align}

in which $\mathbf{y}$ is now a $N \times K$ (number of target variables) matrix and $\Xi$ a $N \times K$ matrix with the model errors. As such, the parameters of this model can be found by vectorizing the previously outlined formula for the (univariate) GLM as follows:

\begin{align}
\hat{\mathbf{\beta}} = (\mathbf{X}'\mathbf{X})^{-1}\mathbf{y}
\end{align}

in which, now, $\mathbf{y}$ is an $N \times K$ (target-variables) matrix and $\beta$ is an $P \times K$ matrix.

Let's check out an example. We'll use the 'canonical' Iris-dataset, which contains data of three types of Iris flowers (let's say, class C1, C2, and C3) associated with four types of variables (let's call them V1, V2, V3, and V4). In the context of a MANOVA, we could investigate whether the factor 'flower type' has a multivariate effect on the four measured variables. Thus, here flower type represents the design-matrix ($\mathbf{X}$) and the measured variables represent the target variables ($\mathbf{y}$). Note, this may seem counterintuitive for people that are familiar with this dataset in the context of machine learning, in which it is used to predict flower type ($y$) as a function of the flower features ($\mathbf{X}$). This is, essentially, just a manifestation of a different question about the data. In neuroimaging, however, this difference is quite important (check out the awesome article by [Naselaris and colleagues](http://www.sciencedirect.com/science/article/pii/S1053811910010657) on this topic).

Anyway, let's check out how the parameters are calculated in the MGLM. We'll first load the data:

```python
from sklearn.datasets import load_iris
import numpy as np

y, flower_types = load_iris(return_X_y=True)
print("Shape of y (flower properties): %s" % (y.shape,))
print("Shape of flower types variable: %s" % (flower_types.shape,), end='\n\n')
print(flower_types)
```

    Shape of y (flower properties): (150, 4)
    Shape of flower types variable: (150,)

    [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
     2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
     2 2]

To investigate the effect of flower type on the target variables in the context of the GLM, we need to create a separate predictor for each class (C1, C2, C3), which contains all zeros except for the samples that correspond to that particular class. This process is known (albeit in a different context) as 'one-hot encoding'. Let's do this:

```python
from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder(sparse=False)
X = ohe.fit_transform(flower_types[:, np.newaxis]).astype(float)
print("Shape of X (flower type): %s" % (X.shape,))
print(X)
```

    Shape of X (flower type): (150, 3)
    [[ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 1.  0.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  1.  0.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]
     [ 0.  0.  1.]]

Sweet! Exactly what we wanted. Now, let's solve the parameters of this GLM-model:

```python
betas = np.linalg.pinv(X.T.dot(X)).dot(X.T).dot(y)
print("Shape of betas: %s" % (betas.shape,))
print(betas)
```

    Shape of betas: (3, 4)
    [[ 5.006  3.418  1.464  0.244]
     [ 5.936  2.77   4.26   1.326]
     [ 6.588  2.974  5.552  2.026]]

In fact, the betas (given this design-matrix) represent the mean of each class (rows) for each variable (columns).

## The MANOVA
Now, the Allefeld & Haynes paper define a 'pattern-distinctness' statistic $\hat{D}$, which is based on the Barlett-Lawley-Hotelling Trace $T_{BLH}$, which represents the trace of the product of the between-class covariance (I call it $B$) and the inverse of the within-class covariance (they call it $\Sigma$). Thus, the formula for the Barlett-Lawley-Hotelling trace become:

\begin{align}
T_{BLH} = \mathrm{trace}(B\Sigma^{-1})
\end{align}

In the paper, $B$ is calculated using the following formula (this becomes a bit hairy):

\begin{align}
B = \hat{\beta}_{\Delta}'\mathbf{X}'\mathbf{X}\hat{\beta}_{\Delta}
\end{align}

in which $\hat{\beta}_{\Delta}$ is calculated using the contrast-matrix $C$ as follows:

\begin{align}
\hat{\beta}_{\Delta} = (C'C)^{-1}C\hat{\beta}
\end{align}

Fortunately, $\Sigma$ is simply the inner product of the residuals from the model:

\begin{align}
\Sigma = (\mathbf{y} - \hat{\beta}\mathbf{X})'(\mathbf{y} - \hat{\beta}\mathbf{X}) = \hat{\Xi}'\hat{\Xi}
\end{align}

Alright, but what contrast should we use? Well, that of course depends on your question. In our case, we said we were interested in the effect of flower type on the target variables. Given that we have three flower types (C1, C2, C3), an F-test of $K - 1$ pairwise difference contrasts (e.g. C1 - C2 and C1 - C3) should do the trick. As such, we could define $C$ as:

\begin{align}
C =
    \begin{bmatrix}
    1 & -1 & 0\\1 & 0 & -1
    \end{bmatrix}
\end{align}

Let's define it below

```python
C = np.array([
    [1, 0, -1],
    [0, 1, -1]
])
```

Now, let's calculate $\hat{\beta}_{\Delta}$ ...

```python
beta_delta = C.T.dot(np.linalg.pinv(C.T.dot(C)).dot(C.T).T).dot(betas)
print("Shape B-delta: %s" % (beta_delta.shape,))
```

    Shape B-delta: (3, 4)

... and $B$:

```python
B = beta_delta.T.dot(X.T.dot(X)).dot(beta_delta)
```

... $\Sigma$:

```python
sigma = (y - X.dot(betas)).T.dot((y - X.dot(betas)))
```

... and finally $T_{BLH}$!

```python
T_blh = np.trace(B.dot(np.linalg.inv(sigma)))
print(T_blh)
```

    32.5495246636

I always check whether I got the right result by checking it against some existing implementation. Fortunately, there is a MANOVA implementation in the master branch (not yet op PyPI) of the statsmodels package. So, let's check whether it matches:

```python
from statsmodels.multivariate.manova import MANOVA

mglm = MANOVA(endog=y, exog=X)
fitted_model = mglm.mv_test([
    ('One-way MANOVA', C)
])

fitted_model.summary()
```

  	     One-way MANOVA	      Value	  Num DF	  Den DF	   F Value	  Pr > F

  	      Wilks' lambda	     0.0235	  8.0000	 288.0000	 198.7110	  0.0000

  	     Pillai's trace	     1.1872	  8.0000	 290.0000	  52.9486	  0.0000

  	 Hotelling-Lawley trace	 32.5495	 8.0000	 203.4024	 583.4914	  0.0000

  	   Roy's greatest root	  32.2720	 4.0000	 145.0000	 1169.8585	 0.0000

Nice! Exactly the same (the value in statsmodels is called *Hotelling-Layley trace*).

## The *cross-validated* MANOVA
Importantly, the paper suggests to cross-validate the MANOVA procedure (and thus the calculation of the statistic of interest, $\hat{D}$), because $\hat{D}$ is a distance-measure -- essentially an extension of the mahalanobis distance for more than two groups (levels) -- which are inherently positively biased because distances cannot be negative. Cross-validated makes sure the estimate becomes unbiased.

```python
from sklearn.model_selection import StratifiedKFold

M = 5
cv = StratifiedKFold(n_splits=5)
T_blh_cv = np.zeros(5)

for i, (train_idx, test_idx) in enumerate(cv.split(X=y, y=flower_types)):

    X_train, X_test = X[train_idx], X[test_idx]
    y_train, y_test = y[train_idx], y[test_idx]
    betas_train = np.linalg.pinv(X_train.T.dot(X_train)).dot(X_train.T).dot(y_train)
    betas_test = np.linalg.pinv(X_test.T.dot(X_test)).dot(X_test.T).dot(y_test)
    beta_delta_train = C.T.dot(np.linalg.pinv(C.T.dot(C)).dot(C.T).T).dot(betas_train)
    beta_delta_test = C.T.dot(np.linalg.pinv(C.T.dot(C)).dot(C.T).T).dot(betas_test)
    B_cv = beta_delta_train.T.dot(X_test.T.dot(X_test)).dot(beta_delta_test)
    sigma_cv = (y_train - X_train.dot(betas_train)).T.dot((y_train - X_train.dot(betas_train)))
    T_blh_cv[i] = np.trace(B_cv.dot(np.linalg.inv(sigma_cv)))

print(T_blh_cv.mean())
N = X.shape[0]
((M - 1)*(N - np.linalg.matrix_rank(X))-y.shape[1]-1) / ((M - 1) * N)
```

    8.26461949482

    0.97166666666666668

Hmm, this is much lower than the uncrossvalidated statistic. Need to check up on this.


## Risk Parity Portfolio Selection using MOSEK Optimizer API

Portfolio selection (or portfolio management) is the art and science of making decisions about investment mix and policy, matching investments to objectives, asset allocation for individuals and institutions, and balancing risk against performance.

This tutorial demonstrates the use of exponential cones in modeling logarithmic terms using Python Fusion. It is largerly based on Bai, X., Scheinberg, K., Tutuncu, R. (2013), Least-squares approach to risk parity in portfolio selection. We assume basic familiarity with the Markowitz risk-return model.

# Risk Parity

Consider the problem of investing in $n$ different assets. Let $x_i$ be the percentage of money invested in assets $i$, and $\mu_i$ its expected return. Let assume we know the covariance matrix $\Sigma$ that link the assets together. We can then define two global measures of the portfolio performance, expected return:

$$
\mu =\sum_{i=1}^n \mu_i x_i,
$$

and risk:

$$
\sigma^2 = \sum_{i=1}^n \sum_{j=1}^n \Sigma_{i,j}x_i x_j = x^T \Sigma x.
$$

For different choices of $x_i$ the investor will get different combinations of $\mu$ and $\sigma^2$. The set of all possible ($\sigma^2$, $\mu$) combinations is called the $\textit{attainable set}$. The theory assumes that investors prefer to minimize risk: given the choice of two portfolios with equal returns, investors will choose the one with the least risk.

In a fully-invested portfolio, every asset $i$ has a contribution in terms of risk. Let $RC_i(x)$ be this contribution, we have that the total risk of the invested portfolio is:

$$
\mathcal{R}(x) = \sum_{i=1}^n RC_i(x)
$$

where

$$
RC_i(x) = x_i \frac{\partial \mathcal{R}(x)}{\partial x_i}
$$

Let $b=(b_1,..,b_n)$ be a vector of $\textbf{budgets}$ such as $b_i > 0$ and $\sum_{i=1}^n b_i = 1$. We define a **risk parity** (or risk budgeting) portfolio as the solution of the following set of equations:

$$
\begin{aligned}
&\begin{cases}
RC_1(x) = b_1 \mathcal{R}(x)\\
..\\
RC_i(x) = b_i \mathcal{R}(x)\\
..\\
RC_n(x) = b_n \mathcal{R}(x)
\end{cases}
\end{aligned}
$$

From the above equations, we can write the risk parity constraint as:

$$
\frac{x_i}{b_i} \frac{\partial \mathcal{R}(x)}{\partial x_i} = \frac{x_j}{b_j} \frac{\partial \mathcal{R}(x)}{\partial x_j} \hspace{1em} \forall i,j
$$

The Risk Parity portfolio requires that each asset has the same total contribution to risk, that is:

$$
\begin{aligned}
&\frac{x_i}{b_i} \frac{\partial \mathcal{R}(x)}{\partial x_i} = \frac{x_j}{b_j} \frac{\partial \mathcal{R}(x)}{\partial x_j} && \forall i,j\\
&x_i \geq 0,  &&b_i > 0 \\
&\sum_{i=1}^n x_i = 1,  &&\sum_{i=1}^n b_i = 1
\end{aligned}
$$

If we set

$$
\mathcal{R}(x) = \sqrt{x^T\Sigma x}
$$

solving the following problem that incorporates a logarithmic barrier in the objective function is equivalent to find a Risk Parity solution:

$$
\begin{equation}
\label{eq:11}
\begin{array}{l}
    \min_x \frac{1}{2} x^T \Sigma x - c \sum_{i=1}^{n} b_i ln(x_i)\\
    \text{s.t.}\\
    x > 0
        \end{array}
\end{equation}
$$

where $b_i > 0$, $\sum_i b_i = 1$ and $c$ is a positive constant.

### Proof

Since $\Sigma$ is positive semidefinite and the logarithm function is strictly concave, the objective function is $\textbf{strictly convex}$. From the first order condition, the unique solution is in corrispondence of the point where the gradient of the objective function is zero:

$$
\Sigma x - c b_i x^{-1} = 0
$$

Hence, at optimality we have

$$
(\Sigma x)_i = \frac{c b_i}{x_i} \Rightarrow \frac{x_i(\Sigma x)_i}{b_i} = \frac{x_j(\Sigma x)_j}{b_j}, \quad \forall i,j,
$$

since

$$
\frac{\partial \mathcal{R}(x)}{\partial x_i} = (\Sigma x)_i.
$$

# MOSEK Implementation

We begin by translating the problem in conic form. Assume that we have a factorization $\Sigma=F^TF$, then $x^T\Sigma x=\|Fx\|_2^2$ and we can write an equivalent model as:

\begin{equation}
\begin{array}{ll}
    \min_x & r - c b^Tt \\
    \text{s.t.}& (1,r,Fx)\in \mathcal{Q}_r\\
     & t_i\leq \mathrm{ln} x_i,\ i=1,\ldots,n \\
     & x \geq 0
\end{array}
\end{equation}

Indeed, the first constraint involving the rotated quadratic cone $\mathcal{Q}_r$ means

$$ 2\cdot 1\cdot r\geq \|Fx\|^2 = x^T \Sigma x $$

and moreover each inequality $t_i\leq \mathrm{ln} x_i$ can be expressed with an exponential cone as

$$ (x_i, 1, t_i)\in K_{\mathrm{exp}}. $$

We proceed with implementation of this model in Fusion:

```python
from mosek.fusion import *

def parityModel(n, F, c, b):
    M = Model('parity')

    # Define all variables appearing in the model
    x = M.variable('x', n)
    t = M.variable(n)
    r = M.variable()

    # Objective r-cb^Tt
    M.objective(ObjectiveSense.Minimize, Expr.sub(r, Expr.mul(c, Expr.dot(b,t))))

    # Bound on risk - construct the vector (1,r,Fx)
    M.constraint(Expr.vstack(1, r, Expr.mul(F,x)), Domain.inRotatedQCone())

    #Logarithmic bounds - all together in matrix form
    M.constraint(Expr.hstack(x, Expr.constTerm(n,1.0), t), Domain.inPExpCone())

    # That's all
    return M
```

Now, we create some sample data.

```python
import numpy as np

n = 20
k = 8

F = np.random.sample([k,n])
Sigma = np.dot(F.transpose(), F)   # Not used

b = np.random.sample(n)
b = b/np.sum(b)

c = 1.0

PM = parityModel(n, F, c, b)
import sys
PM.setLogHandler(sys.stdout)
PM.solve()
```

    Problem
      Name                   : parity
      Objective sense        : min
      Type                   : CONIC (conic optimization problem)
      Constraints            : 70
      Cones                  : 21
      Scalar variables       : 111
      Matrix variables       : 0
      Integer variables      : 0

    Optimizer started.
    Presolve started.
    Linear dependency checker started.
    Linear dependency checker terminated.
    Eliminator started.
    Freed constraints in eliminator : 21
    Eliminator terminated.
    Eliminator started.
    Freed constraints in eliminator : 0
    Eliminator terminated.
    Eliminator - tries                  : 2                 time                   : 0.00
    Lin. dep.  - tries                  : 1                 time                   : 0.00
    Lin. dep.  - number                 : 0
    Presolve terminated. Time: 0.01
    Problem
      Name                   : parity
      Objective sense        : min
      Type                   : CONIC (conic optimization problem)
      Constraints            : 70
      Cones                  : 21
      Scalar variables       : 111
      Matrix variables       : 0
      Integer variables      : 0

    Optimizer  - threads                : 20
    Optimizer  - solved problem         : the primal
    Optimizer  - Constraints            : 8
    Optimizer  - Cones                  : 21
    Optimizer  - Scalar variables       : 70                conic                  : 70
    Optimizer  - Semi-definite variables: 0                 scalarized             : 0
    Factor     - setup time             : 0.00              dense det. time        : 0.00
    Factor     - ML order time          : 0.00              GP order time          : 0.00
    Factor     - nonzeros before factor : 36                after factor           : 36
    Factor     - dense dim.             : 0                 flops                  : 3.11e+03
    ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME
    0   1.5e+01  1.3e+00  2.5e+00  0.00e+00   1.534945180e+00   0.000000000e+00   1.0e+00  0.02
    1   9.5e-01  8.0e-02  1.6e-01  6.74e-02   3.799364477e+00   4.071561966e+00   6.2e-02  0.03
    2   1.6e-01  1.3e-02  2.6e-02  8.65e-01   3.913169811e+00   3.949278388e+00   1.0e-02  0.03
    3   6.1e-02  5.1e-03  1.0e-02  1.08e+00   3.709041715e+00   3.717025112e+00   4.0e-03  0.03
    4   1.1e-02  9.2e-04  1.8e-03  1.03e+00   3.639500611e+00   3.639166289e+00   7.1e-04  0.03
    5   1.0e-03  8.5e-05  1.7e-04  1.00e+00   3.638115939e+00   3.638070284e+00   6.6e-05  0.03
    6   7.0e-05  5.9e-06  1.2e-05  1.00e+00   3.638231209e+00   3.638228036e+00   4.5e-06  0.03
    7   7.4e-06  6.2e-07  1.2e-06  1.00e+00   3.638236463e+00   3.638236125e+00   4.8e-07  0.03
    8   9.8e-07  8.2e-08  1.6e-07  1.00e+00   3.638236859e+00   3.638236815e+00   6.3e-08  0.03
    9   1.3e-07  1.1e-08  2.2e-08  1.00e+00   3.638236836e+00   3.638236830e+00   8.5e-09  0.03
    10  2.8e-08  9.4e-09  8.9e-10  1.00e+00   3.638236825e+00   3.638236824e+00   3.5e-10  0.04
    11  4.6e-09  1.6e-09  1.5e-10  1.00e+00   3.638236816e+00   3.638236816e+00   5.9e-11  0.04
    Optimizer terminated. Time: 0.04

    Interior-point solution summary
      Problem status  : PRIMAL_AND_DUAL_FEASIBLE
      Solution status : OPTIMAL
      Primal.  obj: 3.6382368157e+00    nrm: 6e+00    Viol.  con: 2e-09    var: 0e+00    cones: 8e-10
      Dual.    obj: 3.6382368138e+00    nrm: 2e+00    Viol.  con: 0e+00    var: 3e-09    cones: 0e+00

We can access, normalize and plot the optimal solution

```python
xx = PM.getVariable('x').level()
xx = xx/sum(xx)

%matplotlib inline
import matplotlib.pyplot as plt

plt.bar(np.arange(n), xx)
plt.show()
```

We can also verify that the total contribution of each assed towards total risk is proportional to the budgeting vector $b$, as required:

```python
RC = xx * (np.dot(Sigma,xx))
plt.bar(np.arange(n), RC/b)
plt.show()
```

This work is licensed under a Creative Commons Attribution 4.0 International License. The **MOSEK** logo and name are trademarks of Mosek ApS. The code is provided as-is. Compatibility with future release of **MOSEK** or the `Fusion API` are not guaranteed. For more information contact our [support](mailto:support@mosek.com).


```python
%matplotlib inline
import matplotlib
from matplotlib import pyplot as plt
import seaborn as sns
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('retina')
sns.set(rc={'figure.figsize':(11.7,8.27)})
sns.set_palette(sns.color_palette())

import pandas as pd
import pickle
from tqdm.notebook import tqdm
from plotnine import *
from mizani.palettes import brewer_pal
from palettable import colorbrewer
palette=brewer_pal(type='qual', palette='Set1')

from mcmcmd.samplers import *
from mcmcmd.tests import *

from statsmodels.tsa.stattools import acf
import joblib

```

```python
class gandy_scott_sampler(model_sampler):
    def __init__(self, **kwargs):
        self._N = 1
        self._D = 1
        self._sigma = 10
        self._sigma_epsilon = onp.sqrt(0.1)
        super().__init__(**kwargs)

        assert self._sigma > 0
        assert self._sigma_epsilon > 0

        pass

    @property
    def sample_dim(self):
        return self._N + 2

    @property
    def theta_indices(self):
        return onp.arange(self._N, self.sample_dim)

    def drawPrior(self, rng=None):
        if rng is None:
            rng = self._rng_s

        self._theta_1 = rng.normal(loc=0, scale=self._sigma)
        self._theta_2 = rng.normal(loc=0, scale=self._sigma)

        return onp.array([self._theta_1, self._theta_2])

    def drawLikelihood(self, rng=None):
        if rng is None:
            rng = self._rng_s

        self._y = rng.normal(loc=self._theta_1 + self._theta_2,
                           scale=self._sigma_epsilon, size = (self._N, 1))

        return self._y.flatten()

    def drawPosterior(self, rng=None):
        if rng is None:
            rng = self._rng_s

        var_post = 1/(1/self._sigma**2 + self._N/self._sigma_epsilon**2)

        # Random scan for reversibility
        if rng.choice(2) == 1:
            exp_post_1 = var_post * ((self._y-self._theta_2).sum()/self._sigma_epsilon**2)
            self._theta_1 = rng.normal(loc=exp_post_1, scale=onp.sqrt(var_post))
            exp_post_2 = var_post * ((self._y-self._theta_1).sum()/self._sigma_epsilon**2)
            self._theta_2 = rng.normal(loc=exp_post_2, scale=onp.sqrt(var_post))
        else:
            exp_post_2 = var_post * ((self._y-self._theta_1).sum()/self._sigma_epsilon**2)
            self._theta_2 = rng.normal(loc=exp_post_2, scale=onp.sqrt(var_post))
            exp_post_1 = var_post * ((self._y-self._theta_2).sum()/self._sigma_epsilon**2)
            self._theta_1 = rng.normal(loc=exp_post_1, scale=onp.sqrt(var_post))

        return onp.array([self._theta_1, self._theta_2])

    def test_functions(self, samples):
        assert samples.shape[1] == self.sample_dim

        g = onp.hstack([samples[:, self.theta_indices[0]].reshape(-1,1),
                       (samples[:, self.theta_indices[0]]**2).reshape(-1,1),
                       (samples[:, self.theta_indices[0]]*samples[:, self.theta_indices[1]]).reshape(-1,1),
                       onp.exp(scipy.stats.norm.logpdf(x=samples[:, self.theta_indices], scale=self._sigma).sum(1)).reshape(-1,1),
                       onp.exp(scipy.stats.norm.logpdf(x=(samples[:, :self._N]).reshape(-1,1), loc=(samples[:, self.theta_indices[0]]+samples[:, self.theta_indices[1]]).reshape(-1,1), scale=self._sigma_epsilon).sum(1)).reshape(-1,1)
                      ])
        return g

# Incorrect expectation
class gandy_scott_sampler_error_1(gandy_scott_sampler):
    def drawPosterior(self, rng=None):
        if rng is None:
            rng = self._rng_s

        var_post = 1/(1/self._sigma**2 + self._N/self._sigma_epsilon**2)

        # Random scan for reversibility
        if rng.choice(2) == 1:
            exp_post_1 = var_post * ((self._y+self._theta_2).sum()/self._sigma_epsilon**2)
            self._theta_1 = rng.normal(loc=exp_post_1, scale=onp.sqrt(var_post))
            exp_post_2 = var_post * ((self._y+self._theta_1).sum()/self._sigma_epsilon**2)
            self._theta_2 = rng.normal(loc=exp_post_2, scale=onp.sqrt(var_post))
        else:
            exp_post_2 = var_post * ((self._y+self._theta_1).sum()/self._sigma_epsilon**2)
            self._theta_2 = rng.normal(loc=exp_post_2, scale=onp.sqrt(var_post))
            exp_post_1 = var_post * ((self._y+self._theta_2).sum()/self._sigma_epsilon**2)
            self._theta_1 = rng.normal(loc=exp_post_1, scale=onp.sqrt(var_post))

        return onp.array([self._theta_1, self._theta_2])

# Incorrect variance
class gandy_scott_sampler_error_2(gandy_scott_sampler):
    def drawPosterior(self, rng=None):
        if rng is None:
            rng = self._rng_s

        var_post = 1/(1/self._sigma + self._N/self._sigma_epsilon)

        # Random scan for reversibility
        if rng.choice(2) == 1:
            exp_post_1 = var_post * ((self._y-self._theta_2).sum()/self._sigma_epsilon**2)
            self._theta_1 = rng.normal(loc=exp_post_1, scale=onp.sqrt(var_post))
            exp_post_2 = var_post * ((self._y-self._theta_1).sum()/self._sigma_epsilon**2)
            self._theta_2 = rng.normal(loc=exp_post_2, scale=onp.sqrt(var_post))
        else:
            exp_post_2 = var_post * ((self._y-self._theta_1).sum()/self._sigma_epsilon**2)
            self._theta_2 = rng.normal(loc=exp_post_2, scale=onp.sqrt(var_post))
            exp_post_1 = var_post * ((self._y-self._theta_2).sum()/self._sigma_epsilon**2)
            self._theta_1 = rng.normal(loc=exp_post_1, scale=onp.sqrt(var_post))

        return onp.array([self._theta_1, self._theta_2])

# Swap posterior means
class gandy_scott_sampler_error_3(gandy_scott_sampler):
    def drawPosterior(self, rng=None):
        if rng is None:
            rng = self._rng_s

        var_post = 1/(1/self._sigma**2 + self._N/self._sigma_epsilon**2)

        # Random scan for reversibility
        if rng.choice(2) == 1:
            exp_post_1 = var_post * ((self._y-self._theta_2).sum()/self._sigma_epsilon**2)
            exp_post_2 = var_post * ((self._y-self._theta_1).sum()/self._sigma_epsilon**2)
            self._theta_1 = rng.normal(loc=exp_post_2, scale=onp.sqrt(var_post))
            self._theta_2 = rng.normal(loc=exp_post_1, scale=onp.sqrt(var_post))
        else:
            exp_post_2 = var_post * ((self._y-self._theta_1).sum()/self._sigma_epsilon**2)
            exp_post_1 = var_post * ((self._y-self._theta_2).sum()/self._sigma_epsilon**2)
            self._theta_2 = rng.normal(loc=exp_post_1, scale=onp.sqrt(var_post))
            self._theta_1 = rng.normal(loc=exp_post_2, scale=onp.sqrt(var_post))

        return onp.array([self._theta_1, self._theta_2])

# Laplace in posterior instead of normal
class gandy_scott_sampler_error_4(gandy_scott_sampler):
    def drawPosterior(self, rng=None):
        if rng is None:
            rng = self._rng_s

        var_post = 1/(1/self._sigma**2 + self._N/self._sigma_epsilon**2)

        # Random scan for reversibility
        if rng.choice(2) == 1:
            exp_post_1 = var_post * ((self._y-self._theta_2).sum()/self._sigma_epsilon**2)
#             self._theta_1 = rng.normal(loc=exp_post_1, scale=onp.sqrt(var_post))
            self._theta_1 = rng.laplace(loc=exp_post_1, scale=onp.sqrt(var_post))
            exp_post_2 = var_post * ((self._y-self._theta_1).sum()/self._sigma_epsilon**2)
#             self._theta_2 = rng.normal(loc=exp_post_2, scale=onp.sqrt(var_post))
            self._theta_2 = rng.laplace(loc=exp_post_2, scale=onp.sqrt(var_post))
        else:
            exp_post_2 = var_post * ((self._y-self._theta_1).sum()/self._sigma_epsilon**2)
#             self._theta_2 = rng.normal(loc=exp_post_2, scale=onp.sqrt(var_post))
            self._theta_2 = rng.laplace(loc=exp_post_2, scale=onp.sqrt(var_post))
            exp_post_1 = var_post * ((self._y-self._theta_2).sum()/self._sigma_epsilon**2)
#             self._theta_1 = rng.normal(loc=exp_post_1, scale=onp.sqrt(var_post))
            self._theta_1 = rng.laplace(loc=exp_post_1, scale=onp.sqrt(var_post))
        return onp.array([self._theta_1, self._theta_2])
```

# Setup

The model is
\begin{align}
    y &= \theta_1 + \theta_2 + \epsilon \\
    \theta_i &\sim \mathcal{N}(0, \sigma^2), i=1,2 \\
    \epsilon &\sim \mathcal{N}(0, \sigma_\epsilon^2)
\end{align}

The posterior distributions of $\theta_1$ and $\theta_2$ are also normal

\begin{align}
\theta_{i} \sim \mathcal{N}(
\frac{\sigma^{2}}{\sigma_{\epsilon}^{2}+\sigma^{2}}\left(y-\theta_{j}\right)
, \frac{1}{\frac{1}{\sigma_{\epsilon}^{2}}+\frac{1}{\sigma^{2}}})
\end{align}

# Experiments

```python
def exp_label(x):
    if 'sampler_error_3' in x:
        return 'Type II - Mean Swap'
    elif 'sampler_error_4' in x:
        return 'Type II - Laplace'
    else:
        return 'Type I'
```

# BC Tests

```python
def run_trial(model, num_samples, bc_burn_in, alpha, seed):
    num_samples = int(num_samples)
    bc_burn_in = int(bc_burn_in)
    num_samples_bc = int(1/(bc_burn_in+2) * num_samples)

    num_samples_mc_bc = num_samples_bc
    num_samples_mc = num_samples_mc_bc

    model.set_seed(seed)

    samples_mc = model.sample_mc(num_samples_mc)
    samples_bc = model.sample_bc(num_samples_bc, bc_burn_in)

#     data_mc = samples_mc[:, :model._N]
#     data_bc = samples_bc[:, :model._N]

    samples_mc = model.test_functions(samples_mc)
    samples_bc = model.test_functions(samples_bc)

    ## Rank
    test_functions_aux = lambda samples: model.test_functions(samples)[:, -2:]
    test_functions_theta = lambda samples: model.test_functions(samples)[:, :-2]
    rej_rank_aux = rank_test(model, N=int(num_samples/(bc_burn_in+1)), L=int(bc_burn_in+1), test_functions=test_functions_aux)['result'].max()
    rej_rank_theta = rank_test(model, N=int(num_samples/(bc_burn_in+1)), L=int(bc_burn_in+1), test_functions=test_functions_theta)['result'].max()
    rej_rank_auxtheta = rank_test(model, N=int(num_samples/(bc_burn_in+1)), L=int(bc_burn_in+1))['result'].max()

    ## KS
    rej_ks_aux = ks_test(samples_mc[:, -2:], samples_bc[:, -2:])['result'].max()
    rej_ks_theta = ks_test(samples_mc[:, :-2], samples_bc[:, :-2])['result'].max()
    rej_ks_auxtheta = ks_test(samples_mc, samples_bc)['result'].max()

    ## MMD
    col_mmd = onp.hstack([onp.arange(len(model.theta_indices)),[-2, -1]])
    X_mmd = samples_mc[:, col_mmd]
    Y_mmd = samples_bc[:, col_mmd]

#     # Add data
#     X_mmd = onp.hstack([data_mc, X_mmd])
#     Y_mmd = onp.hstack([data_bc, Y_mmd])

    # Normalize scale
    X_mmd, Y_mmd = normalizeTwoSamples(X_mmd, Y_mmd)

    # Aux Linear
    rej_mmd_aux_linear = mmd_test(X_mmd[:, -2:], Y_mmd[:, -2:], linear_kernel, alpha=alpha, mmd_type='unbiased')['result']
    # Theta Linear
    rej_mmd_theta_linear = mmd_test(X_mmd[:, :-2], Y_mmd[:, :-2], linear_kernel, alpha=alpha, mmd_type='unbiased')['result']

    # Aux Nonlinear
    rej_mmd_aux_rbf = mmd_test(X_mmd[:, -2:], Y_mmd[:, -2:], rbf_kernel, alpha=alpha, mmd_type='unbiased')['result']
    # Theta Nonlinear
    rej_mmd_theta_rbf = mmd_test(X_mmd[:, :-2], Y_mmd[:, :-2], rbf_kernel, alpha=alpha, mmd_type='unbiased')['result']

    # Aux & Theta
    rej_mmd_auxtheta_linear = mmd_test(X_mmd, Y_mmd, linear_kernel, alpha=alpha, mmd_type='unbiased')['result']
    rej_mmd_auxtheta_rbf = mmd_test(X_mmd, Y_mmd, rbf_kernel, alpha=alpha, mmd_type='unbiased')['result']

    return onp.array([rej_mmd_aux_linear, rej_mmd_theta_linear, rej_mmd_auxtheta_linear, rej_mmd_aux_rbf, rej_mmd_theta_rbf, rej_mmd_auxtheta_rbf,
                      rej_ks_aux, rej_ks_theta, rej_ks_auxtheta, rej_rank_aux, rej_rank_theta, rej_rank_auxtheta])
```

```python
os.environ['MKL_NUM_THREAD'] = '1'

num_trials = 100
alpha = 0.05

bc_burn_in = 5-1

lst_experiments = [gandy_scott_sampler(), gandy_scott_sampler_error_3(), gandy_scott_sampler_error_4()]
lst_sample_budget = [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000]

lst_res = []

with joblib.Parallel(n_jobs=5) as para:
    for model in tqdm(lst_experiments):
        for n in tqdm(lst_sample_budget):
            num_samples_bc = int(1/(bc_burn_in+2) * n)
            test_size_bc = num_samples_bc

            results = para(joblib.delayed(run_trial)(model, n, bc_burn_in, alpha, seed) for seed in range(num_trials))
            results = onp.vstack(results).mean(0)

            # Convert rejection rate to Type I/II error
            if 'error' in type(model).__name__:
                results = 1-results

            lst_inputs = ['aux', 'theta', 'aux, theta']*4
            lst_test = ['mmd']*6 + ['ks']*3 + ['rank']*3
            lst_kernel = ['linear']*3 + ['rbf']*3 + [None]*3 + [None]*3
            lst_res.append(pd.DataFrame({'experiment': type(model).__name__, 'test':lst_test, 'inputs':lst_inputs, 'kernel':lst_kernel, 'test_size':test_size_bc, 'sample_budget':n, 'result':results}))
```

    HBox(children=(FloatProgress(value=0.0, max=3.0), HTML(value='')))

    HBox(children=(FloatProgress(value=0.0, max=8.0), HTML(value='')))

    HBox(children=(FloatProgress(value=0.0, max=8.0), HTML(value='')))

    HBox(children=(FloatProgress(value=0.0, max=8.0), HTML(value='')))

```python
df_results = pd.concat(lst_res)
df_results.loc[df_results.kernel.isnull(), 'kernel'] = 'linear'
df_results['test_size'] = pd.to_numeric(df_results['test_size'])
df_results['sample_budget'] = pd.to_numeric(df_results['sample_budget'])
df_results['result'] = pd.to_numeric(df_results['result'])
```

```python
# df_results=pd.read_pickle('gandy_scott_bc')

# Nonlinear vs linear kernel
df_hline = pd.DataFrame({'experiment':['gandy_scott_sampler'], 'alpha':[alpha]})
mask = df_results.test.str.contains('mmd') & df_results.inputs.str.contains('aux, theta') & ~df_results.experiment.str.contains('3')
plot_kernel=ggplot(df_results.loc[mask], aes(x='test_size', y='result', color='kernel')) + \
        geom_point() + geom_line() + geom_hline(df_hline, aes(yintercept='alpha'), linetype='dotted') +  \
        facet_grid('~experiment', labeller=exp_label) + \
        labs(x='Test Sample Size', y='Error Rate', color='Kernel') + \
        scale_color_discrete(labels=['Linear', 'Gaussian'], palette=palette) + theme_bw()
plot_kernel
```

```python
ggsave(plot_kernel, 'gandy_scott_kernel')
```

    /Users/andrew/miniconda3/envs/msc/lib/python3.8/site-packages/plotnine/ggplot.py:727: PlotnineWarning: Saving 6.4 x 4.8 in image.
    /Users/andrew/miniconda3/envs/msc/lib/python3.8/site-packages/plotnine/ggplot.py:730: PlotnineWarning: Filename: gandy_scott_kernel

```python
# df_results=pd.read_pickle('gandy_scott_bc')

# Value of Aux
df_hline = pd.DataFrame({'experiment':['gandy_scott_sampler'], 'alpha':[alpha]})
mask = ~((df_results.test == 'mmd') & (df_results.kernel == 'linear')) & ~((df_results.experiment == 'gandy_scott_sampler') & (df_results.test_size>450))
plot_aux=ggplot(df_results.loc[mask], aes(x='test_size', y='result', color='inputs', linetype='test', shape = 'test')) + \
        geom_point() + geom_line() + geom_hline(df_hline, aes(yintercept='alpha'), linetype='dotted') +  \
        facet_wrap('~experiment', labeller=exp_label, scales='free_x') + \
        labs(x='Test Sample Size', y='Error Rate', color='Test Functions', linetype='Test', shape='Test') + \
        scale_color_discrete(labels=[r'$P(y|\theta), P(\theta)$', r'$P(y|\theta), P(\theta), \theta$', r'$\theta$'], palette=palette) + \
        scale_linetype_discrete(labels=['KS', 'MMD-BC', 'Rank']) + scale_shape_discrete(labels=['KS', 'MMD-BC', 'Rank']) + \
        theme_bw()
plot_aux
```

```python
ggsave(plot_aux, 'gandy_scott_aux')
```

    /Users/andrew/miniconda3/envs/msc/lib/python3.8/site-packages/plotnine/ggplot.py:727: PlotnineWarning: Saving 6.4 x 4.8 in image.
    /Users/andrew/miniconda3/envs/msc/lib/python3.8/site-packages/plotnine/ggplot.py:730: PlotnineWarning: Filename: gandy_scott_aux

Performance vs autocorrelation. Increasing $\sigma_\epsilon$ decreases autocorrelation.

```python
%matplotlib inline
import matplotlib
from matplotlib import pyplot as plt
import seaborn as sns
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('retina')
sns.set(rc={'figure.figsize':(11.7,8.27)})
sns.set_palette(sns.color_palette())

import pandas as pd
import pickle
from tqdm.notebook import tqdm
from plotnine import *

from mcmcmd.samplers import *
from mcmcmd.tests import *

from statsmodels.tsa.stattools import acf
import joblib
```

```python
def run_trial(model, num_samples, bc_burn_in, sc_thinning, alpha, seed):
    num_samples = int(num_samples)
    bc_burn_in = int(bc_burn_in)
    sc_thinning = int(sc_thinning)

    num_samples_bc = int(1/(bc_burn_in+2) * num_samples)
    num_samples_sc = int((sc_thinning)/(sc_thinning+1) * num_samples)

    num_samples_mc_bc = num_samples_bc
    num_samples_mc_sc = num_samples-num_samples_sc
    num_samples_mc = max(num_samples_mc_bc, num_samples_mc_sc)

    model.set_seed(seed)
    samples_mc = model.test_functions(model.sample_mc(num_samples_mc))
    samples_sc = model.test_functions(model.sample_sc(num_samples_sc))[onp.arange(0, num_samples_sc, sc_thinning), :]
    samples_bc = model.test_functions(model.sample_bc(num_samples_bc, bc_burn_in))

    rej_ks = ks_test(samples_mc[:num_samples_bc, :], samples_bc)['result'].max()

    rej_geweke = geweke_test(samples_mc[:(num_samples-num_samples_sc), :], samples_sc, l=0.08, alpha=alpha)['result'].max()

    # Normalize for MMD
    col_mmd = onp.hstack([onp.arange(len(model.theta_indices)),[-2, -1]])
    X_mmd_wb, Y_mmd_wb = normalizeTwoSamples(samples_mc[:(num_samples-num_samples_sc), :][:, col_mmd], samples_sc[:, col_mmd])
    X_mmd, Y_mmd = normalizeTwoSamples(samples_mc[:num_samples_bc, :][:, col_mmd], samples_bc[:, col_mmd])

    rej_mmd = mmd_test(X_mmd, Y_mmd, rbf_kernel, alpha=alpha, mmd_type='unbiased')['result']
    rej_mmd_wb = mmd_wb_test(X_mmd_wb, Y_mmd_wb, rbf_kernel, alpha=alpha)['result']

#     col_theta = onp.arange(len(model.theta_indices))
#     rej_mmd_theta = mmd_test(X_mmd[:, col_theta], Y_mmd[:, col_theta], rbf_kernel, alpha=alpha, mmd_type='unbiased')['result']
#     rej_mmd_wb_theta = mmd_wb_test(X_mmd_wb[:, col_theta], Y_mmd_wb[:, col_theta], rbf_kernel, alpha=alpha)['result']

    rej_rank = rank_test(model, N=int(num_samples/(bc_burn_in+1)), L=int(bc_burn_in+1))['result'].max()

    return onp.array([rej_ks, rej_geweke, rej_mmd_wb, rej_mmd, rej_rank])
```

```python
os.environ['MKL_NUM_THREAD'] = '1'

num_trials = 100
alpha = 0.05

# Same amount of effort
bc_burn_in = 5-1
sc_thinning = 5

lst_std = [0.1, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4]
lst_experiments = [gandy_scott_sampler(sigma_epsilon=x) for x in lst_std] + \
                    [gandy_scott_sampler_error_4(sigma_epsilon=x) for x in lst_std]
lst_sample_size = [1800]

lst_res = []

with joblib.Parallel(n_jobs=5) as para:
    for model in tqdm(lst_experiments):
        for n in tqdm(lst_sample_size):
            num_samples_bc = int(1/(bc_burn_in+2) * n)
            num_samples_sc = int((sc_thinning)/(sc_thinning+1) * n)
            test_size_bc = num_samples_bc
            test_size_sc = n-num_samples_sc

            results = para(joblib.delayed(run_trial)(model, n, bc_burn_in, sc_thinning, alpha, seed) for seed in range(num_trials))
            results = onp.vstack(results).mean(0)

           # Convert rejection rate to Type I/II error
            if 'error' in type(model).__name__:
                results = 1-results

            lst_test = ['ks', 'geweke', 'mmd_wb', 'mmd', 'rank']
            lst_test_size = [test_size_bc, test_size_sc, test_size_sc, test_size_bc, test_size_bc]
            lst_res.append(pd.DataFrame({'experiment': type(model).__name__, 'sigma_epsilon':model._sigma_epsilon, 'test':lst_test,  'test_size':lst_test_size, 'sample_budget':n, 'result':results}))
```

```python
df_results = pd.concat(lst_res)
df_results['test_size'] = pd.to_numeric(df_results['test_size'])
df_results['sample_budget'] = pd.to_numeric(df_results['sample_budget'])
df_results['result'] = pd.to_numeric(df_results['result'])
```

```python
df_results.loc[df_results.test.str.contains('geweke|mmd_wb'), 'posterior_sampler'] = 'SC'
df_results.loc[~df_results.test.str.contains('geweke|mmd_wb'), 'posterior_sampler'] = 'BC'
```

```python
# df_results=pd.read_pickle('gandy_scott_auto')

df_hline = pd.DataFrame({'experiment':['gandy_scott_sampler'], 'alpha':[alpha]})
mask = ~(df_results.test.str.contains('rank'))

plot_auto = ggplot(df_results.loc[mask], aes(color='test', group='test', x='sigma_epsilon',  y='result', linetype='posterior_sampler')) + \
    geom_point() + geom_line() + geom_hline(df_hline, aes(yintercept='alpha'), linetype='dotted') + \
    facet_grid('~experiment', labeller = exp_label) + \
    labs(x=r'$\sigma_{\epsilon}$', y='Error Rate', color='Test', linetype='Test Class') + \
    scale_color_discrete(labels=['Geweke', 'KS', 'MMD-BC', 'MMD-SC'], palette=palette) + \
    scale_linetype_discrete(labels=['BC', 'SC']) + theme_bw()

plot_auto
```

```python
ggsave(plot_auto, 'gandy_scott_auto')
```

# Joint / Joint Product / Joint Sum Kernels

```python
def run_trial(model, num_samples, bc_burn_in, alpha, seed):
    num_samples = int(num_samples)
    bc_burn_in = int(bc_burn_in)
    num_samples_bc = int(1/(bc_burn_in+2) * num_samples)

    num_samples_mc_bc = num_samples_bc
    num_samples_mc = num_samples_mc_bc

    model.set_seed(seed)

    X_mmd = model.sample_mc(num_samples_mc)
    Y_mmd = model.sample_bc(num_samples_bc, bc_burn_in)

    rej_mmd_joint_prod = mmd_test(X_mmd, Y_mmd, prod_kernel, alpha=alpha, mmd_type='unbiased',
                                  lst_classes=[rbf_kernel, rbf_kernel], lst_groups=[onp.arange(model._N), model.theta_indices])['result']

    rej_mmd_joint_sum = mmd_test(X_mmd, Y_mmd, sum_kernel, alpha=alpha, mmd_type='unbiased',
                                  lst_classes=[rbf_kernel, rbf_kernel], lst_groups=[onp.arange(model._N), model.theta_indices])['result']

    X_mmd, Y_mmd = normalizeTwoSamples(X_mmd, Y_mmd)
    rej_mmd_joint = mmd_test(X_mmd, Y_mmd, rbf_kernel, alpha=alpha, mmd_type='unbiased')['result']

    return onp.array([rej_mmd_joint, rej_mmd_joint_prod, rej_mmd_joint_sum])
```

```python
os.environ['MKL_NUM_THREAD'] = '1'

num_trials = 100
alpha = 0.05

bc_burn_in = 5-1

lst_experiments = [gandy_scott_sampler(), gandy_scott_sampler_error_3(), gandy_scott_sampler_error_4()]
lst_sample_budget = [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000]

lst_res = []

with joblib.Parallel(n_jobs=5) as para:
    for model in tqdm(lst_experiments):
        for n in tqdm(lst_sample_budget):
            num_samples_bc = int(1/(bc_burn_in+2) * n)
            test_size_bc = num_samples_bc

            results = para(joblib.delayed(run_trial)(model, n, bc_burn_in, alpha, seed) for seed in range(num_trials))
            results = onp.vstack(results).mean(0)

            # Convert rejection rate to Type I/II error
            if 'error' in type(model).__name__:
                results = 1-results

            lst_inputs = ['aux, theta']*3
            lst_test = ['mmd']*3
            lst_kernel = ['rbf', 'rbf_prod', 'rbf_sum']
            lst_res.append(pd.DataFrame({'experiment': type(model).__name__, 'test':lst_test, 'inputs':lst_inputs, 'kernel':lst_kernel, 'test_size':test_size_bc, 'sample_budget':n, 'result':results}))
```

```python
df_results=pd.concat(lst_res)
df_hline = pd.DataFrame({'experiment':['gandy_scott_sampler'], 'alpha':[alpha]})
plot_jps=ggplot(df_results, aes(x='test_size', y='result', color='kernel')) + \
        geom_point() + geom_line() + geom_hline(df_hline, aes(yintercept='alpha'), linetype='dotted') +  \
        facet_grid('~experiment', labeller=exp_label) + \
        labs(x='Test Sample Size', y='Error Rate', color='Kernel') + \
        scale_color_discrete(labels=['Joint', 'Product', 'Sum'], palette=palette) + theme_bw()
plot_jps
```