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

Inappropriate Shape of stock.spot Due to Floating-Point Precision #638

Open
kano5266 opened this issue Oct 12, 2024 · 0 comments · May be fixed by #639
Open

Inappropriate Shape of stock.spot Due to Floating-Point Precision #638

kano5266 opened this issue Oct 12, 2024 · 0 comments · May be fixed by #639

Comments

@kano5266
Copy link

Hi, I was exploring the no transaction band network and encountered an issue while working with BrownianStock. I set dt = 1/365 and time_horizon = 30/365, and after running the simulation, the generated tensor had the expected shape of (n_path, 31).

However, when I changed the time_horizon to 29/365, the tensor should have had a shape of (n_path, 30), but it still generated a shape of (n_path, 31).

This seems to be caused by the precision of floating-point numbers and the use of math.ceil() in line 140:

n_steps=ceil(time_horizon / self.dt + 1),

Example:

from math import ceil

dt = 1/365
time_horizon = 28/365

print(time_horizon/dt + 1)
print(ceil(time_horizon/dt + 1))
29.0
29
from math import ceil

dt = 1/365
time_horizon = 29/365

print(time_horizon/dt + 1)
print(ceil(time_horizon/dt + 1))
30.000000000000004
31 <--WRONG!
from math import ceil

dt = 1/365
time_horizon = 30/365

print(time_horizon/dt + 1)
print(ceil(time_horizon/dt + 1))
30.999999999999996
31

This issue arises due to the floating-point representation, where time_horizon/dt slightly exceeds 30, causing ceil() to return 31 instead of 30.

Proposed Fix

To address this, we can use the round() function to ensure more precise floating-point calculations. For instance:

from math import ceil

dt = 1/365
time_horizon = 29/365

print(time_horizon/dt + 1)
print(ceil(round(time_horizon/dt, 8) + 1)) # Rounds to 8 decimal places
30.000000000000004
30 <-- Correct

This minor adjustment should fix the tensor shape mismatch. Thank you for considering this, and please let me know if any further adjustments are needed!

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

Successfully merging a pull request may close this issue.

1 participant