Skip to content

Fix datasample MATLAB compatibility - #505

Open
sahilphad07-sudo wants to merge 1 commit into
gnu-octave:mainfrom
sahilphad07-sudo:fix-datasample-matlab-behavior
Open

sahilphad07-sudo wants to merge 1 commit into
gnu-octave:mainfrom
sahilphad07-sudo:fix-datasample-matlab-behavior

Conversation

@sahilphad07-sudo

Copy link
Copy Markdown
Contributor

datasample did not match MATLAB behavior for invalid and infinite sampling weights.

NaN and all-zero weights were not rejected during validation, and Inf weights could lead to incorrect sampling because normalizing infinite values produced invalid probabilities.

This change updates weight validation and sampling logic to handle these cases consistently with MATLAB.

Before

octave:> datasample(1:5, 2, 'Weights', [NaN 1 1 1 1])
ans =

   5   5

octave:> datasample(1:5, 2, 'Weights', [Inf 1 1 1 1], 'Replace', true)
ans =

   5   5

After

octave:> datasample(1:5, 2, 'Weights', [NaN 1 1 1 1])
error: datasample: the sampling weights must be defined as a vector of positive values

octave:> datasample(1:5, 2, 'Weights', [Inf 1 1 1 1], 'Replace', true)
ans =

   1   1

octave:> datasample(1:5, 2, 'Weights', [Inf 1 1 1 1], 'Replace', false)
ans =

   1   2

MATLAB Behavior

  • NaN weights are rejected.
  • All-zero weights are rejected.
  • An Inf weight is always selected when sampling with replacement.
  • With replacement disabled, the Inf-weighted element is selected first.

Regression Tests Added

  • NaN weights
  • All-zero weights
  • Inf weights with replacement
  • Inf weights without replacement

Test Results

All existing and new datasample BISTs pass:

PASSES 26 out of 26 tests.

This branch has not been deployed

No deployments
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 this pull request may close these issues.

1 participant