Serving the Quantitative Finance Community

 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

Re: Python tricks

July 24th, 2025, 9:40 am

import boost_histogram as bh
import numpy as np
import matplotlib as plt



# Compose axis however you like; this is a 2D histogram
hist = bh.Histogram(bh.axis.Regular(2, 0, 1), bh.axis.Regular(4, 0.0, 1.0))

# Filling can be done with arrays, one per dimension
hist.fill([0.3, 0.5, 0.2], [0.1, 0.4, 0.9])

# NumPy array view into histogram counts, no overflow bins
counts = hist.view()
print(counts)
Tags,
similar output?

1989
Image
1981

Image
 
User avatar
tags
Topic Author
Posts: 3623
Joined: February 21st, 2010, 12:58 pm

Re: Python tricks

October 11th, 2025, 9:34 am

Any comments on the brand new Python 3.14 e.g. no-GIL and then?
 
User avatar
tags
Topic Author
Posts: 3623
Joined: February 21st, 2010, 12:58 pm

Re: Python tricks

October 11th, 2025, 9:57 am

import boost_histogram as bh
import numpy as np
import matplotlib as plt



# Compose axis however you like; this is a 2D histogram
hist = bh.Histogram(bh.axis.Regular(2, 0, 1), bh.axis.Regular(4, 0.0, 1.0))

# Filling can be done with arrays, one per dimension
hist.fill([0.3, 0.5, 0.2], [0.1, 0.4, 0.9])

# NumPy array view into histogram counts, no overflow bins
counts = hist.view()
print(counts)
Tags,
similar output?

1989
Image
1981

Image


import numpy as np

# Define bin edges
x_bins = np.linspace(0, 1, 3)
y_bins = np.linspace(0, 1, 5)

# Sample data points
x_data = np.array([0.3, 0.5, 0.2])
y_data = np.array([0.1, 0.4, 0.9])

# Compute 2D histogram
hist, x_edges, y_edges = np.histogram2d(x_data, y_data, bins=[x_bins, y_bins])

# Print result
print(hist)
mere numpy version
boost_histogram has some cool features. thanks for mentioning it.