Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with the negative value appearing in the np.log() and np.sqrt() function when calculating density #1

Closed
yangysc opened this issue Dec 5, 2022 · 1 comment

Comments

@yangysc
Copy link

yangysc commented Dec 5, 2022

Hi, Justin

Thanks for your great codes. However, I am wondering how to deal with the negative value appearing in the np.log() and np.sqrt() function when calculating the density.

For example, for the OzakiDensity, Kt can be negative if 1 + self._model.drift(x0, t) * (np.exp(self._model.drift_x(x0, t) * t) - 1) / ( x0 * self._model.drift_x(x0, t)) < -1, and Vt can be negative when performing Vt=np.sqrt(Vt)

Kt = (1 / t) * np.log(1 + self._model.drift(x0, t) * (np.exp(self._model.drift_x(x0, t) * t) - 1) / (
                    x0 * self._model.drift_x(x0, t)))
Vt = sig ** 2 * (np.exp(2 * Kt * t) - 1) / (2 * Kt)
Vt = np.sqrt(Vt)

Thanks in advance for any suggestion! I am not sure if the strategy you did for other densities is optimal, e.g.,

if z <= 0:
return 0

Could you kindly share some references for handling this negative values like returning zero?
Best,
Shanchao

@jkirkby3
Copy link
Owner

Thanks for your great question! I have rewritten the Ozaki density function to make clear that each of these terms is indeed well-defined (this form is also more efficient :). You can see that (exp(x*t) -1)/x is always positive for positive t ):

    sig = self._model.diffusion(x0, t)
    mu = self._model.drift(x0, t)
    mu_x = self._model.drift_x(x0, t)
    temp = mu * (np.exp(mu_x * t) - 1) / mu_x

    Mt = x0 + temp
    Kt = (2 / t) * np.log(1 + temp / x0)
    Vt = sig * np.sqrt((np.exp(Kt * t) - 1) / Kt)

    return np.exp(-0.5 * ((xt - Mt) / Vt) ** 2) / (np.sqrt(2 * np.pi) * Vt)

Also, I removed the if z <=0 expression from above, as you mention this is not going to be optimal, and its better that if something like this happens we should return a nan value, allowing the optimizer to adapt the parameters accordingly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants