{"record":{"id":"9ce83a404b48f498","repo":"microsoft/qlib","slug":"the-length-of-sample-ratios-should-be-equal-to-bin","errorCode":null,"errorMessage":"The length of sample_ratios should be equal to bins_fs.","messagePattern":"The length of sample_ratios should be equal to bins_fs\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/double_ensemble.py","lineNumber":50,"sourceCode":"        epochs=100,\n        early_stopping_rounds=None,\n        **kwargs,\n    ):\n        self.base_model = base_model  # \"gbm\" or \"mlp\", specifically, we use lgbm for \"gbm\"\n        self.num_models = num_models  # the number of sub-models\n        self.enable_sr = enable_sr\n        self.enable_fs = enable_fs\n        self.alpha1 = alpha1\n        self.alpha2 = alpha2\n        self.bins_sr = bins_sr\n        self.bins_fs = bins_fs\n        self.decay = decay\n        if sample_ratios is None:  # the default values for sample_ratios\n            sample_ratios = [0.8, 0.7, 0.6, 0.5, 0.4]\n        if sub_weights is None:  # the default values for sub_weights\n            sub_weights = [1] * self.num_models\n        if not len(sample_ratios) == bins_fs:\n            raise ValueError(\"The length of sample_ratios should be equal to bins_fs.\")\n        self.sample_ratios = sample_ratios\n        if not len(sub_weights) == num_models:\n            raise ValueError(\"The length of sub_weights should be equal to num_models.\")\n        self.sub_weights = sub_weights\n        self.epochs = epochs\n        self.logger = get_module_logger(\"DEnsembleModel\")\n        self.logger.info(\"Double Ensemble Model...\")\n        self.ensemble = []  # the current ensemble model, a list contains all the sub-models\n        self.sub_features = []  # the features for each sub model in the form of pandas.Index\n        self.params = {\"objective\": loss}\n        self.params.update(kwargs)\n        self.loss = loss\n        self.early_stopping_rounds = early_stopping_rounds\n\n    def fit(self, dataset: DatasetH):\n        df_train, df_valid = dataset.prepare(\n            [\"train\", \"valid\"], col_set=[\"feature\", \"label\"], data_key=DataHandlerLP.DK_L\n        )","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/double_ensemble.py#L32-L68","documentation":"Thrown by DEnsembleModel's __init__ when len(sample_ratios) != bins_fs. The Double Ensemble algorithm bins features into bins_fs groups and applies one sampling ratio per bin, so the two parameters must agree in length. The default sample_ratios has 5 entries, matching the default bins_fs of 5.","triggerScenarios":"Passing DEnsembleModel(bins_fs=3) while leaving sample_ratios at its default [0.8, 0.7, 0.6, 0.5, 0.4]; or supplying a custom sample_ratios list whose length differs from bins_fs.","commonSituations":"Tuning the number of feature-select bins without adjusting the ratio list; copying hyperparameters between configs where one of the two was changed independently.","solutions":["Make len(sample_ratios) equal bins_fs, e.g. DEnsembleModel(bins_fs=3, sample_ratios=[0.8, 0.7, 0.6])","Or change bins_fs to match the length of your sample_ratios list"],"exampleFix":"# before\nmodel = DEnsembleModel(num_models=5, bins_fs=3)  # default sample_ratios has len 5\n\n# after\nmodel = DEnsembleModel(num_models=5, bins_fs=3, sample_ratios=[0.8, 0.7, 0.6])","handlingStrategy":"validation","validationCode":"if sample_ratios is None:\n    sample_ratios = [0.8, 0.7, 0.6, 0.5, 0.4][:bins_fs]\nassert len(sample_ratios) == bins_fs, \"sample_ratios length must match bins_fs\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set sample_ratios and bins_fs together in config","Derive one from the other in your config builder instead of hardcoding both"],"tags":["double-ensemble","hyperparameters","config-validation","qlib"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}