Skip to content

Commit

Permalink
Speed-up Weighted Quantile Calculation (#11)
Browse files Browse the repository at this point in the history
* Speeds up weighted quantile calculation

Order quantiles monotonically and reuse floor and ceil indices.
  • Loading branch information
reidjohnson authored Oct 4, 2023
1 parent 361dbb7 commit 8b4beec
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions quantile_forest/_quantile_forest_fast.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ cpdef vector[double] calc_weighted_quantile(
if not issorted:
parallel_qsort_asc(inputs, weights, 0, n_inputs-1)

# Get monotonic sorting of quantiles for efficient calculation.
sorted_quantile_idx = vector[double](n_quantiles)
for i in range(<SIZE_t>(sorted_quantile_idx.size())):
sorted_quantile_idx[i] = <double>i
parallel_qsort_asc(quantiles, sorted_quantile_idx, 0, n_quantiles-1)

cum_weights = vector[double](n_inputs)

# Calculate the empirical cumulative distribution function (ECDF).
Expand All @@ -331,16 +337,20 @@ cpdef vector[double] calc_weighted_quantile(

out = vector[double](n_quantiles)

idx_floor = 0
idx_ceil = 1

for i in range(n_quantiles):
quantile = quantiles[i]

# Assign the output based on the input quantile ordering.
i = <SIZE_t>sorted_quantile_idx[i]

# Calculate the quantile's proportion of total weight.
p = quantile * f + C

# Find the first index where the proportion of weight exceeds p.
idx_floor = 0
idx_ceil = 1
for j in range(n_inputs):
for j in range(idx_floor, n_inputs):
if p >= cum_weights[j]:
if weights[j] > 0:
idx_floor = j
Expand Down

0 comments on commit 8b4beec

Please sign in to comment.