46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835 | class SolrCMFCV(BaseEstimator):
_parameter_constraints = {
"structure_penalty": [
Interval(Real, 0, None, closed="left"),
"array-like",
],
"max_rank": [Interval(Integral, 1, None, closed="left"), "array-like"],
"factor_penalty": [
Interval(Real, 0, None, closed="neither"),
"array-like",
None,
],
"factor_pruning": ["boolean"],
"cv": [Interval(Integral, 2, None, closed="left"), BaseSplitter],
"cv_strategy": [
StrOptions({"structure_first_debiased_cv", "penalized_cv"})
],
"score": [
StrOptions(
{
"neg_mean_squared_error",
"neg_sum_squared_error",
"weighted_neg_mean_squared_error",
}
)
],
"refit": [
StrOptions(
{
"mean_debiased",
"mean_penalized",
"1se_debiased",
"1se_penalized",
}
)
],
"init": [StrOptions({"random", "custom"})],
"init_kwargs": [dict, None],
"rho": [Interval(Real, 0.0, None, closed="neither"), None],
"alpha": [Interval(Real, 0.0, None, closed="left"), None],
"mu": [Interval(Real, 0.0, None, closed="neither"), None],
"max_iter": [Interval(Integral, 1, None, closed="left")],
"abs_tol": [Interval(Real, 0.0, None, closed="neither")],
"rel_tol": [Interval(Real, 0.0, None, closed="neither")],
"verbose": ["boolean"],
"n_jobs": [Integral, None],
}
def __init__(
self,
*,
structure_penalty: float | ArrayLike = 1.0,
max_rank: int | ArrayLike = 10,
factor_penalty: float | ArrayLike | None = None,
factor_pruning: bool = True,
cv: int | BaseSplitter = 10,
cv_strategy: str = "structure_first_debiased_cv",
score: str = "neg_mean_squared_error",
refit: str = "1se_debiased",
init: str = "random",
init_kwargs: dict | None = None,
rho: float | None = None,
alpha: float | None = None,
mu: float | None = None,
max_iter: int = 1000,
abs_tol: float = 1e-6,
rel_tol: float = 1e-6,
verbose: bool = False,
n_jobs: int | None = None,
):
self.structure_penalty = structure_penalty
self.max_rank = max_rank
self.factor_penalty = factor_penalty
self.factor_pruning = factor_pruning
self.cv = cv
self.cv_strategy = cv_strategy
self.score = score
self.refit = refit
self.init = init
self.init_kwargs = init_kwargs
self.rho = rho
self.alpha = alpha
self.mu = mu
self.max_iter = max_iter
self.abs_tol = abs_tol
self.rel_tol = rel_tol
self.verbose = verbose
self.n_jobs = n_jobs
def _check_parameter_grid(self):
# Scalars to 1d-arrays
structure_penalty, max_rank, factor_penalty = atleast_1d(
self.structure_penalty, self.max_rank, self.factor_penalty
)
# Check that all are indeed 1d
assert (
ndim(structure_penalty)
== ndim(max_rank)
== ndim(factor_penalty)
== 1
), (
f"In {self.__class__.__name__} arguments 'structure_penalty',"
" 'max_rank', and 'factor_penalty' need to be one-dimensional or"
" equal to a single number (or 'None' for 'factor_penalty')"
)
structure_penalty, max_rank, factor_penalty = broadcast_arrays(
structure_penalty, max_rank, factor_penalty
)
return list(zip(structure_penalty, max_rank, factor_penalty))
def fit(
self,
X: dict[ViewDesc, NDArray[float64]],
y=None,
*,
structure_weights: (
dict[ViewDesc, NDArray[float64] | float64] | None
) = None,
factor_weights: dict[Hashable, NDArray[float64] | float64]
| None = None,
vs: list[dict[Hashable, NDArray[float64]]] | None = None,
ds: list[dict[ViewDesc, NDArray[float64]]] | None = None,
us: list[dict[Hashable, NDArray[float64]]] | None = None,
):
self._validate_params()
parameter_grid = self._check_parameter_grid()
n_params = len(parameter_grid)
if isinstance(self.cv, Integral):
cv = ElementwiseFolds(self.cv)
elif isinstance(self.cv, BaseSplitter):
cv = self.cv
if self.score == "neg_mean_squared_error":
score_fn = neg_mean_squared_error
elif self.score == "neg_sum_squared_error":
score_fn = neg_sum_squared_error
elif self.score == "weighted_neg_mean_squared_error":
score_fn = weighted_neg_mean_squared_error
results = {
"structure_penalty": [s for s, _, _ in parameter_grid],
"max_rank": [m for _, m, _ in parameter_grid],
"factor_penalty": [f for _, _, f in parameter_grid],
}
if self.init_kwargs is None:
init_kwargs = {}
else:
init_kwargs = self.init_kwargs
# If one of these is provided all need to be the same length
# (if only vs and ds are provided then us is a list of None)
if vs is not None or ds is not None or us is not None:
assert (
vs is not None and ds is not None and len(vs) == len(ds) >= 1
), (
"If initial values are provided to"
f" {self.__class__.__name__}.fit(), then 'vs' and 'ds' both"
" need to provided and have to be the same length"
)
assert us is None or len(us) == len(vs), (
"If initial values for 'u' are provided to"
f" {self.__class__.__name__}.fit(), then 'us' needs have the"
" same length as 'vs' and 'ds'"
)
if self.init == "random":
n_reps = 1
if "repetitions" in init_kwargs:
n_reps = init_kwargs.pop("repetitions")
def inits():
for i in range(n_reps):
yield i, (None, None, None)
# If an rng or seed is supplied, extract it
if "rng" in init_kwargs:
rng = default_rng(init_kwargs["rng"])
else:
rng = default_rng()
elif self.init == "custom":
n_reps = len(vs)
def inits():
for i in range(n_reps):
yield i, (vs[i], ds[i], us[i] if us is not None else None)
else:
raise ValueError(f"Unknown init method {self.init}")
base_est = SolrCMF(
factor_pruning=self.factor_pruning,
init=self.init,
init_kwargs=init_kwargs,
rho=self.rho,
alpha=self.alpha,
mu=self.mu,
max_iter=self.max_iter,
abs_tol=self.abs_tol,
rel_tol=self.rel_tol,
)
if self.cv_strategy == "structure_first_debiased_cv":
tmpdir = TemporaryDirectory()
tmppath = Path(tmpdir.name)
def _estimate_structure(
idx_params,
idx_init,
structure_penalty,
max_rank,
factor_penalty,
vs,
ds,
us,
rng,
):
est: SolrCMF = clone(base_est)
est.set_params(
structure_penalty=structure_penalty,
max_rank=max_rank,
factor_penalty=factor_penalty,
)
if est.init == "random":
est.init_kwargs["rng"] = default_rng(rng)
est.fit(
X,
structure_weights=structure_weights,
factor_weights=factor_weights,
vs=vs,
ds=ds,
us=us,
)
if not est.converged_:
warn(
"Penalized estimation with parameters"
f" (structure_penalty={structure_penalty},"
f" max_rank={max_rank},"
f" factor_penalty={factor_penalty}):"
f" {est.__class__.__name__} did not converge"
f" after {est.n_iter_} iterations."
)
# Save estimator for later
dump(est, tmppath / f"{idx_params}_{idx_init}.pkl")
return (
est.objective_value_,
est.elapsed_process_time_,
est.est_max_rank_,
# compute relative to supplied max_rank;
# est_max_rank_ could be less in case of
# factor pruning
(max_rank - est.est_max_rank_) * len(X)
+ sum(
[sum(1 - p) for p in est.structure_pattern().values()]
),
(
sum(
[
(max_rank - est.est_max_rank_)
* (p.shape[0] - 1)
+ sum(1 - p)
for p in est.factor_pattern().values()
]
)
if factor_penalty is not None
else 0
),
)
if self.verbose:
print(
f"Perform structure estimation ({n_reps * n_params} tasks)"
)
if self.init == "random":
# We need to split the randomness for random initialization
child_states = reshape(
rng.bit_generator._seed_seq.spawn(n_reps * n_params),
(n_params, n_reps),
)
else:
# Dummy otherwise
child_states = full((n_params, n_reps), None)
out = Parallel(
n_jobs=self.n_jobs, verbose=10 if self.verbose else 0
)(
delayed(_estimate_structure)(
idx_params,
idx_init,
structure_penalty,
max_rank,
factor_penalty,
vs,
ds,
us,
child_states[idx_params, idx_init],
)
for idx_params, (
structure_penalty,
max_rank,
factor_penalty,
) in enumerate(parameter_grid)
for idx_init, (vs, ds, us) in inits()
)
(
objective_values,
elapsed_process_times,
est_max_rank,
structural_zeros,
factor_zeros,
) = zip(*out)
if self.verbose:
print("Determine best runs")
# Rely on the fact that joblib returns results in the same
# order as the inputs
objective_values = split(asarray(objective_values), n_params)
best_runs = [int(argmin(vals)) for vals in objective_values]
results["objective_value_penalized"] = [
vals[idx] for idx, vals in zip(best_runs, objective_values)
]
elapsed_process_times = split(
asarray(elapsed_process_times), n_params
)
results["mean_elapsed_process_time_penalized"] = [
mean(ts) for ts in elapsed_process_times
]
results["std_elapsed_process_time_penalized"] = [
std(ts) for ts in elapsed_process_times
]
est_max_rank = split(asarray(est_max_rank), n_params)
results["est_max_rank"] = [
rks[idx] for idx, rks in zip(best_runs, est_max_rank)
]
structural_zeros = split(asarray(structural_zeros), n_params)
results["structural_zeros"] = [
zs[idx] for idx, zs in zip(best_runs, structural_zeros)
]
factor_zeros = split(asarray(factor_zeros), n_params)
results["factor_zeros"] = [
zs[idx] for idx, zs in zip(best_runs, factor_zeros)
]
def _debiased_cv_score(
est_in: SolrCMF,
train_indices: dict[ViewDesc, NDArray[intp]],
test_indices: dict[ViewDesc, NDArray[intp]],
):
est: SolrCMF = clone(base_est)
est.set_params(
init="custom",
init_kwargs={"reduce_max_rank": True},
factor_pruning=False, # Set to False always
)
est.fit(
X,
indices=train_indices,
structure_pattern=est_in.structure_pattern(),
factor_pattern=est_in.factor_pattern(),
vs=est_in.vs_,
ds=est_in.ds_,
us=est_in.us_ if hasattr(est_in, "us_") else None,
)
if not est.converged_:
warn(
"Fixed structure estimation of"
f" {est.__class__.__name__} did not converge after"
f" {est.n_iter_} iterations."
)
return (
score_fn(X, est.transform(X), indices=test_indices),
est.elapsed_process_time_,
)
# Reads fitted penalized estimators from cache and
# extracts structure/factor patterns
def solrcmf_estimators():
for idx_params, idx_init in zip(range(n_params), best_runs):
yield load(tmppath / f"{idx_params}_{idx_init}.pkl")
# We want exactly the same splits for all parameter combinations,
# so we produce the splits once and then reuse them.
cv_splits = list(cv.split(X))
n_folds = cv.get_n_splits(X)
if self.verbose:
print(
"Perform debiased cross-validation"
f" ({n_params * n_folds} tasks)"
)
out = Parallel(
n_jobs=self.n_jobs, verbose=10 if self.verbose else 0
)(
delayed(_debiased_cv_score)(
est,
train_indices,
test_indices,
)
for est in solrcmf_estimators()
for train_indices, test_indices in cv_splits
)
(
scores,
elapsed_process_times,
) = zip(*out)
for i in range(n_folds):
results[f"{self.score}_fold{i}"] = [
scores[j * n_folds + i] for j in range(n_params)
]
elapsed_process_times = split(
asarray(elapsed_process_times), n_params
)
results["mean_elapsed_process_time_fixed"] = [
mean(ts) for ts in elapsed_process_times
]
results["std_elapsed_process_time_fixed"] = [
std(ts) for ts in elapsed_process_times
]
elif self.cv_strategy == "penalized_cv":
def _penalized_cv_score(
structure_penalty,
max_rank,
factor_penalty,
vs,
ds,
us,
train_indices,
test_indices,
rng,
):
est: SolrCMF = clone(base_est)
est.set_params(
structure_penalty=structure_penalty,
max_rank=max_rank,
factor_penalty=factor_penalty,
)
if est.init == "random":
est.init_kwargs["rng"] = default_rng(rng)
est.fit(
X,
indices=train_indices,
structure_weights=structure_weights,
factor_weights=factor_weights,
vs=vs,
ds=ds,
us=us,
)
if not est.converged_:
warn(
"Penalized estimation with parameters"
f" (structure_penalty={structure_penalty},"
f" max_rank={max_rank},"
f" factor_penalty={factor_penalty}):"
f" {est.__class__.__name__} did not converge"
f" after {est.n_iter_} iterations."
)
return (
score_fn(X, est.transform(X), indices=test_indices),
est.elapsed_process_time_,
est.est_max_rank_,
# compute relative to supplied max_rank;
# est_max_rank_ could be less in case of
# factor pruning
(max_rank - est.est_max_rank_) * len(X)
+ sum(
[sum(1 - p) for p in est.structure_pattern().values()]
),
(
sum(
[
(max_rank - est.est_max_rank_)
* (p.shape[0] - 1)
+ sum(1 - p)
for p in est.factor_pattern().values()
]
)
if factor_penalty is not None
else 0
),
)
# We want exactly the same splits for all parameter combinations,
# so we produce the splits once and then reuse them.
cv_splits = list(cv.split(X))
n_folds = cv.get_n_splits(X)
if self.verbose:
print(
"Perform penalized cross-validation"
f" ({n_params * n_reps * n_folds} tasks)"
)
if self.init == "random":
# We need to split the randomness for random initialization
child_states = reshape(
rng.bit_generator._seed_seq.spawn(
n_reps * n_params * n_folds
),
(n_params, n_reps, n_folds),
)
else:
# Dummy otherwise
child_states = full((n_params, n_reps, n_folds), None)
out = Parallel(
n_jobs=self.n_jobs, verbose=10 if self.verbose else 0
)(
delayed(_penalized_cv_score)(
structure_penalty,
max_rank,
factor_penalty,
vs,
ds,
us,
train_indices,
test_indices,
child_states[idx_param, idx_init, idx_fold],
)
for idx_param, (
structure_penalty,
max_rank,
factor_penalty,
) in enumerate(parameter_grid)
for idx_init, (vs, ds, us) in inits()
for idx_fold, (train_indices, test_indices) in enumerate(
cv_splits
)
)
(
scores,
elapsed_process_times,
est_max_rank,
structural_zeros,
factor_zeros,
) = zip(*out)
for i in range(n_folds):
results[f"{self.score}_fold{i}"] = [nan] * n_params
best_runs = [-1] * n_params
best_score = [inf] * n_params
for idx_params, scores_params in enumerate(
split(asarray(scores), n_params)
):
for idx_init, scores_inits in enumerate(
split(scores_params, n_reps)
):
if mean(scores_inits) < best_score[idx_params]:
best_score[idx_params] = mean(scores_inits)
best_runs[idx_params] = idx_init
for i in range(n_folds):
results[f"{self.score}_fold{i}"][
idx_params
] = scores_inits[i]
elapsed_process_times = split(
asarray(elapsed_process_times), n_params
)
results["mean_elapsed_process_time"] = [
mean(ts) for ts in elapsed_process_times
]
results["std_elapsed_process_time"] = [
std(ts) for ts in elapsed_process_times
]
results["est_max_rank"] = [
mean(
est_max_rank[
(
idx_params * n_reps * n_folds
+ best_runs[idx_params] * n_folds
) : (
idx_params * n_reps * n_folds
+ (best_runs[idx_params] + 1) * n_folds
)
]
)
for idx_params in range(n_params)
]
results["structural_zeros"] = [
mean(
structural_zeros[
(
idx_params * n_reps * n_folds
+ best_runs[idx_params] * n_folds
) : (
idx_params * n_reps * n_folds
+ (best_runs[idx_params] + 1) * n_folds
)
]
)
for idx_params in range(n_params)
]
results["factor_zeros"] = [
mean(
factor_zeros[
(
idx_params * n_reps * n_folds
+ best_runs[idx_params] * n_folds
) : (
idx_params * n_reps * n_folds
+ (best_runs[idx_params] + 1) * n_folds
)
]
)
for idx_params in range(n_params)
]
# Post-processing on the full dictionary. Same for both cases
scores = vstack(
[results[f"{self.score}_fold{i}"] for i in range(n_folds)]
)
results.update(
{
f"mean_{self.score}": scores.mean(0),
f"std_{self.score}": scores.std(0),
}
)
self.cv_results_ = results
if self.verbose:
print("Re-fit final estimator")
if self.refit.startswith("mean"):
self.best_index_ = argmax(results[f"mean_{self.score}"])
elif self.refit.startswith("1se"):
# Choose the solution with maximal structure sparsity within
# 1 standard error of the best solution
max_index = argmax(results[f"mean_{self.score}"])
candidates = flatnonzero(
results[f"mean_{self.score}"]
>= (
results[f"mean_{self.score}"][max_index]
- results[f"std_{self.score}"][max_index]
)
)
# Primarily choose the solution with the most
# structural zeros and then select the solution with the
# most factor zeros if factor sparsity was requested
structural_zeros = asarray(
[results["structural_zeros"][i] for i in candidates]
)
most_sz_candidates = candidates[
flatnonzero(structural_zeros == max(structural_zeros))
]
factor_zeros = [
results["factor_zeros"][i] for i in most_sz_candidates
]
self.best_index_ = most_sz_candidates[argmax(factor_zeros)]
structure_penalty, max_rank, factor_penalty = parameter_grid[
self.best_index_
]
if self.verbose:
print(
"Best fit with\n"
f" structure_penalty = {structure_penalty}\n"
f" max_rank = {max_rank}\n"
f" factor_penalty = {factor_penalty}\n\n"
" estimated max_rank = "
f"{results['est_max_rank'][self.best_index_]}"
)
# Re-fit best run on all data
if self.cv_strategy == "structure_first_debiased_cv":
# Load respective penalized estimator from cache
est = load(
tmppath
/ f"{self.best_index_}_{best_runs[self.best_index_]}.pkl"
)
self.best_estimator_ = clone(base_est)
if self.refit.endswith("debiased"):
self.best_estimator_.set_params(
init="custom",
init_kwargs={"reduce_max_rank": True},
factor_pruning=False, # Set to False always
)
self.best_estimator_.fit(
X,
structure_pattern=est.structure_pattern(),
factor_pattern=est.factor_pattern(),
vs=est.vs_,
ds=est.ds_,
us=est.us_ if hasattr(est, "us_") else None,
)
elif self.refit.endswith("penalized"):
self.best_estimator_.set_params(
structure_penalty=structure_penalty,
max_rank=max_rank,
factor_penalty=factor_penalty,
init="custom",
)
self.best_estimator_.fit(
X,
vs=est.vs_,
ds=est.ds_,
us=est.us_ if hasattr(est, "us_") else None,
)
tmpdir.cleanup()
elif self.cv_strategy == "penalized_cv":
# A penalized fit needs to be performed irrespectively.
# Either because it is the final fit or because we need the
# structure/factor pattern.
if self.init == "custom":
vs_init = vs[best_runs[self.best_index_]]
ds_init = ds[best_runs[self.best_index_]]
if us is not None:
us_init = us[best_runs[self.best_index_]]
else:
us_init = None
else:
vs_init = None
ds_init = None
us_init = None
final_est = clone(base_est)
final_est.set_params(
structure_penalty=structure_penalty,
max_rank=max_rank,
factor_penalty=factor_penalty,
)
final_est.fit(
X,
vs=vs_init,
ds=ds_init,
us=us_init,
)
if self.refit.endswith("debiased"):
final_est_debiased = clone(base_est)
final_est_debiased.set_params(
init="custom",
init_kwargs={"reduce_max_rank": True},
factor_pruning=False, # Set to False always
)
final_est_debiased.fit(
X,
structure_pattern=final_est.structure_pattern(),
factor_pattern=final_est.factor_pattern(),
vs=final_est.vs_,
ds=final_est.ds_,
us=final_est.us_ if hasattr(final_est, "us_") else None,
)
self.best_estimator_ = final_est_debiased
elif self.refit.endswith("penalized"):
self.best_estimator_ = final_est
self.best_max_rank_ = self.best_estimator_.est_max_rank_
return self
|