Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update objective computation: multiply quadratic biases with (1-alpha) #18

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions dwave/plugins/sklearn/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,18 @@ def correlation_cqm(
dtype=np.result_type(X, y),
mode="w+",
shape=(X_copy.shape[1], X_copy.shape[1]),
)

)
# main calculation. It modifies X_copy in-place
corrcoef(X_copy, out=correlations, rowvar=False, copy=False)

# we don't care about the direction of correlation in terms of
# the penalty/quality
np.absolute(correlations, out=correlations)

# multiplying all but last columns and rows with (1 - alpha)
np.multiply(correlations[:-1, :-1], (1 - alpha), out=correlations[:-1, :-1])
# our objective
# we multiply by 2 because the matrix is symmetric
np.fill_diagonal(correlations, correlations[:, -1] * (-2 * alpha * num_features))

# Note: the full symmetric matrix (with both upper- and lower-diagonal
# entries for each correlation coefficient) is retained for consistency with
# the original formulation from Milne et al.
# we multiply by num_features to have consistent performance
# with the increase of the number of features
np.fill_diagonal(correlations, correlations[:, -1] * (- alpha * num_features))
it = np.nditer(correlations[:-1, :-1], flags=['multi_index'], op_flags=[['readonly']])
cqm.set_objective((*it.multi_index, x) for x in it if x)

Expand Down
3 changes: 3 additions & 0 deletions releasenotes/notes/algorithm-update-d73cd4f7c854a8b3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
fixes:
- Multiply off-diagonal terms of the correlation matrix with 1-alpha.
4 changes: 4 additions & 0 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ def test_alpha_0(self):
cqm = SelectFromQuadraticModel.correlation_cqm(self.X, self.y, num_features=3, alpha=0)
self.assertTrue(not any(cqm.objective.linear.values()))

def test_alpha_1_no_quadratic(self):
cqm = SelectFromQuadraticModel.correlation_cqm(self.X, self.y, num_features=3, alpha=1)
self.assertTrue(not any(cqm.objective.quadratic.values()))

def test_alpha_1(self):
rng = np.random.default_rng(42)

Expand Down