quantgirluk / aleatory

📦 Python library for Stochastic Processes Simulation and Visualisation
MIT License
143 stars 15 forks source link

Paths fail to plot when there is only 1 histogram bin #4

Closed alexhrubin closed 8 months ago

alexhrubin commented 10 months ago

Very nice package! I'm learning stochastic calculus and this is a really cool tool!

I found a small bug: The paths plotted by SPExplicit.draw() are colored to match the histogram bin that their final point falls into. If we plot N=3 or fewer paths, then we set n_bins = int(np.sqrt(N)) <= 1, and then this logic which helps set the path colors:

bin_centers = 0.5 * (bins[:-1] + bins[1:])
col = bin_centers - min(bin_centers)  # scale values to interval [0,1]
col /= max(col)

results in a divide by zero

/Users/alexrubin/code/aleatory/aleatory/utils/utils.py:76: RuntimeWarning: invalid value encountered in divide
    col /= max(col)

because col == [0.].

image

It seems like the 3 lines of logic above could be replaced by col = np.linspace(0, 1, n_bins, endpoint=True).

quantgirluk commented 10 months ago

Many thanks for spotting this! Will include this in the next release :-)

quantgirluk commented 8 months ago

Change has been released in v.0.1.3 🎉