[SYSTEMDS-3925] Add lhs builtin function#2545
Open
jancies2301 wants to merge 21 commits into
Open
Conversation
The function only supports 2 dimensional matrices and return an NxN matrix of 0s and 1s. Also add a rudimentary test of the function, that checks if the number of samples is correct.
…of a NxN binary matrix The function now handles the multidimensional case. Also update the unit tests to check if the returned matrix is a valid latin hypercube.
the methods generate candidates and pick the best one based on distance to already chosen points TODO: there is a lot of code duplication between the two methods, candidate generation can maybe be vectorized
the improved and maximin method differ only in how they evaluate candidates, therefore they are consolidated into one method "build" additional goal parameter is added, which determines how the candidates are determined also add new method "cp_sweep", which instead of creating potential candidates for new points, swaps values in columns to create the biggest improvement
… and maximin algorithms
…o simplify objective calculation between methods change maximin and sOptimality to minimize the negatie value objective to match other metods add return_type parameter, which controls whether matrix with integer coordianates or double uniform distribution 0-1 is returned
…rate functions by method
fix minor bug in build method remove unnecessary metrics in benchmark script
added 2 commits
July 15, 2026 12:35
make build method work for N=2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR introduces a new builtin function lhs() to Apache SystemDS that implements Latin Hypercube Sampling (LHS), a statistical sampling method for generating well-distributed samples from a multidimensional uniform distribution.
Implementation
New builtin function: lhs() - performs Latin hypercube sampling with multiple algorithm choices.
Parameters
Returns:
Following sampling methods are supported:
Each of the methods optimizes a given objective function, to make the hypercube as space-filling as possible. Following goals are supported:
When using the Maximin and Avg Dist goals, the negative value of the goal is minimized, so that all goals are optimized in the same direction.
The return_type parameter controls how the matrix should be returned:
Method Comparison
The following table contains the results of running the following snippet:
Note: the table contains positive values for Min Distance and sum of inverse squared distance for readability, in practice the function return negative values.
In all categories the CP-Sweep method provides the best results, it is however the most computationally expensive method, since all possible swaps per column have to be compared.
Benchmarking against Random Sampling
To evaluate the effectiveness of LHS the following benchmarking experiment has been conducted:
Results
The results show that the KL-Divergence converges to nearly zero already with N < 20, while random sampling requires a larger number of samples to converge. Similarly in case of the mean LL, for small N there is a wide gap between train and test mean LL for random sampling, while LHS converges immediately, showcasing the ability of LHS to draw samples representative of the underlying distribution, even with very small N.
References:
[1] Stocki, Rafal. "A method to improve design reliability using optimal Latin hypercube sampling." Computer Assisted Mechanics and Engineering Sciences 12.4 (2005): 393.
[2] Beachkofski, Brian, and Ramana Grandhi. "Improved distributed hypercube sampling." 43rd AIAA/ASME/ASCE/AHS/ASC Structures, Structural Dynamics, and Materials Conference. 2002.