Skip to content

Optimization

Optimizer

Bases: object

Source code in src/mopeds/optimization.py
 43
 44
 45
 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
class Optimizer(object):
    def __init__(
        self,
        model: Model,
        variable_lists: list[VariableList],
        simulator_name: str,
        simulator_settings: dict | None,
    ) -> None:
        self.solver_name: str
        self.solver_settings: dict
        self._created_with_options = get_options()

        if not isinstance(variable_lists, list):
            raise (Exception("Variable list should be nested of type list"))
        self.logger: logging.Logger = logging.getLogger(__name__)
        self.model: Model = model

        # Deepcopy is used to avoid manipulating input variable list
        self.list_input_varlist: list[VariableList] = copy.deepcopy(variable_lists)
        for varlist in self.list_input_varlist:
            self.model.subsitute_casadi_symbols(varlist)

        # Each varlist holds respective variables
        self.varlist_decision: VariableList = VariableList()
        self.varlist_decision_direct: VariableList = VariableList()
        self.varlist_parameter: VariableList = VariableList()
        self.varlist_control: VariableList = VariableList()
        self.varlist_state: VariableList = VariableList()
        self.varlist_algebraic: VariableList = VariableList()
        self.nlpsol_g: None | ca.MX = None

        self.simulator_name: str = simulator_name
        self.simulator_settings: dict | None = simulator_settings

        self.list_simulators: Sequence[Simulator | SimulatorNLE]

    @abstractmethod
    def optimize(self, scale):
        # Runs optimization once
        raise (NotImplementedError)

    @_consistent_scaling_decorator
    def variables_dict_to_list(
        self, variables_dict: dict[str, float], *, scaling=None
    ) -> list[float]:
        """Takes a dictionary with {"var_name": var_value} and transforms to list
        corresponding to the order of self.varlist_decision variables"""
        if not isinstance(scaling, bool):
            scaling = self._created_with_options["variable_scaling"]

        with options(variable_scaling=scaling):
            selected_variables: list[float] = []
            for var_name in variables_dict.keys():
                if var_name not in self.varlist_decision.keys():
                    if "time_sp" in var_name or "weight_" in var_name:
                        raise ValueError(
                            f"Variable {var_name} is not a decision variable!"
                        )
                    print(f"Supplied value for variables {var_name} is ignored!")
            for var_name, var in self.varlist_decision.items():
                try:
                    scaled_value = var.scale_from_original(variables_dict[var_name])
                    selected_variables.append(scaled_value)
                except KeyError:
                    raise KeyError(f"Missing value for {var_name}")

        return selected_variables

    @_consistent_scaling_decorator
    def _setup_initialization(self) -> None:
        """Sets initials and bounds for optimizer, and as default no scaling.
        If guess equals 0, 1 is used instead to avoid division by 0 during initialization"""
        guess = []
        lower_bound = []
        upper_bound = []

        for var in self.varlist_decision.values():
            if var.guess == 0:
                guess.append(1.0)
            else:
                guess.append(var.scale_from_original(var.guess))
            lower_bound.append(var.scale_from_original(var.lower_bound))
            upper_bound.append(var.scale_from_original(var.upper_bound))

        self.guess: np.ndarray = np.array(guess)
        self.lower_bound: np.ndarray = np.array(lower_bound)
        self.upper_bound: np.ndarray = np.array(upper_bound)
        self.logger.debug(
            f"Initialized:\nguess {self.guess}\nlower_bound {self.lower_bound}\nupper_bound {self.upper_bound}"
        )

    @abstractmethod
    def _objective(self) -> tuple[ca.MX | ca.DM, ca.MX | ca.DM]:
        """Returns a way to calculate and objective. Dependent on optimization type."""
        raise (NotImplementedError)

    def check_result_bounds(self, result) -> pd.DataFrame:
        """Use output of self.optimize() and check if decision variables are not on the bounds
        Returns dataframe with variables that are not in bounds."""
        if result["x_unscaled"].shape[0] == len(self.varlist_decision):
            varlist_decision = self.varlist_decision
            lower_bound = self.lower_bound
            upper_bound = self.upper_bound
        elif result["x_unscaled"].shape[0] == len(self.varlist_decision_direct):
            varlist_decision = self.varlist_decision_direct
            lower_bound = self.lower_bound_direct
            upper_bound = self.upper_bound_direct
        else:
            raise ValueError

        results_with_bound = {}
        for i, (var, res_i) in enumerate(
            zip(varlist_decision.values(), result["x_unscaled"])
        ):
            value = var.scale_to_original(float(res_i[0]))
            lb = var.scale_to_original(lower_bound[i])
            ub = var.scale_to_original(upper_bound[i])
            results_with_bound[var.name] = [lb, value, ub]
        df = pd.DataFrame.from_dict(results_with_bound).T
        selected_df = df[(df[0] >= df[1]) | (df[2] <= df[1])]
        selected_df.columns = ["lb", "value", "ub"]
        return selected_df

    def _change_guess(self, guess_values):
        """Overwrite the guess correcly both in self.guess and self.guess_direct"""
        arr = np.asarray(guess_values)
        if arr.ndim != 1:
            raise ValueError

        num_guess = arr.shape[0]
        self.guess = guess_values
        if self.model.equations_differential is None:
            self.guess_direct[:num_guess] = guess_values

    @_consistent_scaling_decorator
    def _optimize(
        self,
        scale: bool = None,
        direct_optimization: bool = False,
        *,
        reuse_solver: bool = False,
    ) -> dict[str, ca.DM | ca.MX]:
        """Runs optimizer, uses scaling if needed. Returned values is scaled back."""
        if scale is not None:
            warn("Scale argument is deprecated", FutureWarning, 5)

        if direct_optimization:
            varlist_decision = self.varlist_decision_direct
        else:
            varlist_decision = self.varlist_decision

        self.nlpsol_dict = {
            "x": varlist_decision.get_casadi_variables(),
            "f": self._objective()[0],
            "p": self._nlpsol_p_mx,
        }

        if direct_optimization:
            self.nlpsol_dict["g"] = self.nlpsol_g_direct

        if not (hasattr(self, "solver") and reuse_solver):
            self.solver: ca.Function = ca.nlpsol(
                "solver",
                self.solver_name,
                self.nlpsol_dict,
                self.solver_settings,
            )

        if direct_optimization:
            self.nlpsol_args = {
                "x0": self.guess_direct,
                "lbx": self.lower_bound_direct,
                "ubx": self.upper_bound_direct,
                "lbg": [0] * self.nlpsol_g_direct.shape[0],
                "ubg": [0] * self.nlpsol_g_direct.shape[0],
            }

        else:
            self.nlpsol_args = {
                "x0": self.guess,
                "lbx": self.lower_bound,
                "ubx": self.upper_bound,
            }
        self.nlpsol_args["p"] = self._nlpsol_p_values

        res_solver = self.solver.call(self.nlpsol_args)

        res_solver["x_unscaled"] = res_solver["x"].toarray()
        res_solver["x_all"] = np.asarray(
            varlist_decision.scale_to_original(res_solver["x"])
        )
        if direct_optimization:
            res_solver["x"] = res_solver["x_all"][: len(self.varlist_decision)]
        else:
            res_solver["x"] = res_solver["x_all"]

        res_dict = {}
        for solution, var_name in zip(
            res_solver["x"], list(self.varlist_decision.keys())
        ):
            res_dict[var_name] = float(solution[0])

        if direct_optimization:
            res_dict_all = {}
            for solution, var_name in zip(
                res_solver["x_all"], list(varlist_decision.keys())
            ):
                res_dict_all[var_name] = float(solution[0])
        else:
            res_dict_all = res_dict

        res_solver["x_dict"] = res_dict
        res_solver["x_dict_all"] = res_dict

        return res_solver


    @_consistent_scaling_decorator
    def map_objective(self, plot: bool = True) -> None:
        """Calculate objective function for different values of parameters and plot, if needed.
        Currently support only 3 unfixed decision variables."""
        import matplotlib.pyplot as plt

        decision_variables = self.varlist_decision.get_casadi_variables()
        if decision_variables.shape == (3, 1):
            objective_function = ca.Function(
                "objective",
                [decision_variables],
                [self._objective()[0]],
                ["x"],
                ["f"],
            )

            # Generate steps for each variable between lb and ub
            axis_steps = []
            for lb, ub in zip(self.lower_bound, self.upper_bound):
                axis_steps.append(np.linspace(lb, ub, 6))

            # Create every possible combination of cordinates
            # Code the value of objective function as a color information
            xx = []
            yy = []
            zz = []
            objective = []
            for x in axis_steps[0]:
                for y in axis_steps[1]:
                    for z in axis_steps[2]:
                        xx.append(x)
                        yy.append(y)
                        zz.append(z)
                        objective.append(float(objective_function([x, y, z]).toarray()))

            if plot:
                color = np.array(objective)
                fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
                colormap = plt.get_cmap("tab20b")

                ax.set_xlabel(f"x {decision_variables[0]}")
                ax.set_ylabel(f"y {decision_variables[1]}")
                ax.set_zlabel(f"z {decision_variables[2]}")

                quantile = np.quantile(color, 0.1)
                selected_objective = copy.deepcopy(color)
                selected_objective[selected_objective > quantile] = color.max()
                marker_size = (
                    selected_objective.max() - selected_objective
                ) / selected_objective.max() * 20 + 7

                surf = ax.scatter(xx, yy, zz, c=color, s=marker_size, cmap=colormap)
                fig.colorbar(surf)
                plt.show()
        else:
            raise NotImplementedError

    def generate_multistart_guess(self, num_initials: int):
        if isinstance(self, ParameterEstimationNLE_control):
            hammersley_seeds = np.array(
                list(
                    zip(
                        self.lower_bound[0 : self.num_parameters],
                        self.upper_bound[0 : self.num_parameters],
                    )
                )
            )
        else:
            hammersley_seeds = np.array(list(zip(self.lower_bound, self.upper_bound)))

        list_startpoint = utilities.make_startpoints(
            hammersley_seeds, num_initials, sampling="lhs"
        )
        return list_startpoint

    @_consistent_scaling_decorator
    def optimize_multistart(
        self,
        num_initials: int,
        scale: bool = True,
        max_iterations: int = 20,
    ) -> list[dict[str, ca.DM]]:
        """Runs multiple optimizations with gueses spread between upper and lower bound.
        Helps to find feasible starting point for optimization in a few steps.
        WIP: recalcution of algebraic variables doesn't work.
        """
        list_startpoint = self.generate_multistart_guess(num_initials)
        results_with_f = []

        # Optimizer settings and guess are overwritten for multistart, and then returned back
        initial_guess = copy.deepcopy(self.guess)
        initial_settings = copy.deepcopy(self.solver_settings)

        self.solver_settings = {
            "verbose": False,
            "print_time": False,
            "ipopt": {
                "max_iter": max_iterations,
                "print_level": 0,
            },
        }
        for index, guess in tqdm(enumerate(list_startpoint)):
            if isinstance(self, ParameterEstimationNLE_control):
                for index_guess, current_guess in enumerate(guess):
                    self.guess[index_guess] = current_guess
            else:
                self.guess = guess

            print(f"Optimization number {index} started")
            # for sim in self.list_simulators:
            #     sim.calculate_algebraic_initials(apply_intials=True)

            res = self.optimize(scale)
            print(f"Objective: {res['f']}")
            res_f = float(res["f"])
            results_with_f.append([float("inf") if res_f == 0 else res_f, res])

        results_with_f = sorted(results_with_f, key=lambda res: res[0])
        result_list_sorted = [res[1] for res in results_with_f]

        self.solver_settings = initial_settings
        return result_list_sorted

    @_consistent_scaling_decorator
    def _setup_direct_optimization(self, mode: str):
        """Setup all the variables needed for direct optimization of steady state models.
        mode is needed to differentiate between PE and OED"""
        if mode not in ("PE", "OED"):
            raise NotImplementedError
        self.varlist_algebraic_direct = VariableList()

        lower_bound = self.lower_bound.tolist()
        upper_bound = self.upper_bound.tolist()
        lower_bound_inf = self.lower_bound.tolist()
        upper_bound_inf = self.upper_bound.tolist()
        guess = self.guess.tolist()

        guess_dict = {}
        for var in self.varlist_decision.values():
            guess_dict[var.name] = var.guess
        if mode == "OED":
            for par_name, par_value in zip(
                self.varlist_parameter.keys(), self.parameter_values_unscaled
            ):
                guess_dict[par_name] = par_value

        parameter_symbols = []
        parameter_values = []

        for variable_name, var in self.model.varlist_independent(
            self.list_input_varlist[0]
        ).items():
            if isinstance(var, VariableParameter):
                parameter_symbols.append(var.casadi_var)
                if (var.fixed is True) or (mode == "OED"):
                    parameter_values.append(var.scale_from_original(var.value[0]))
                else:
                    parameter_values.append(var.casadi_var)

        equality_constraints = []
        meas_symbols = []
        jacobian_list = []
        self.varlist_decision_direct = copy.deepcopy(self.varlist_decision)

        for sim_index, input_varlist in enumerate(self.list_input_varlist):
            simulator_i = self.list_simulators[sim_index]

            good_initial_guess = (
                simulator_i.simulate(
                    return_varlist=False, unfixed_variables=guess_dict
                )[0]["x"]
                .toarray()
                .flatten()
            )
            control_symbols = []
            control_values = []
            meas_symbols_sim = []

            for variable_name, var in self.model.varlist_independent(
                input_varlist
            ).items():
                if isinstance(var, VariableControlPiecewiseConstant):
                    raise NotImplementedError
                elif isinstance(var, VariableControl):
                    control_symbols.append(var.casadi_var)
                    if (var.fixed) or (mode == "PE"):
                        control_values.append(var.scale_from_original(var.value[0]))
                    else:
                        control_values.append(var.casadi_var)

            varlist_new_algebraic_i = VariableList()

            for variable_name, var_guess in zip(
                self.model.varlist_algebraic(input_varlist).keys(), good_initial_guess
            ):
                var = input_varlist[variable_name]

                new_var = var._create_copy(f"_sim{sim_index}")

                varlist_new_algebraic_i.add_variable(new_var)
                self.varlist_decision_direct.add_variable(new_var)

                lower_bound.append(new_var.scale_from_original(new_var.lower_bound))
                upper_bound.append(new_var.scale_from_original(new_var.upper_bound))
                lower_bound_inf.append(-ca.inf)
                upper_bound_inf.append(ca.inf)
                guess.append(var_guess)

                if var.name in self.names_of_measurements:
                    meas_symbols_sim.append(new_var.casadi_var)

            meas_symbols.append(ca.hcat(meas_symbols_sim))

            scaled_equations = ca.substitute(
                self.model.equations_algebraic,
                input_varlist.get_casadi_variables(),
                input_varlist.get_scaled_casadi_variables(),
            )

            equations_subs_alg = ca.substitute(
                scaled_equations,
                self.model.varlist_algebraic(input_varlist).get_casadi_variables(),
                varlist_new_algebraic_i.get_casadi_variables(),
            )

            if len(parameter_symbols) == 0:
                equations_subs_par = equations_subs_alg
            else:
                equations_subs_par = ca.substitute(
                    equations_subs_alg,
                    ca.vcat(parameter_symbols),
                    ca.vcat(parameter_values),
                )
            if len(control_symbols) == 0:
                equations_subs_all = equations_subs_par
            else:
                equations_subs_all = ca.substitute(
                    equations_subs_par,
                    ca.vcat(control_symbols),
                    ca.vcat(control_values),
                )

            self.varlist_algebraic_direct.update(**varlist_new_algebraic_i)
            # equality_constraints.append(equations_subs_all.printme(sim_index))
            equality_constraints.append(equations_subs_all)

            if mode == "OED":
                jac_function = simulator_i.function.jacobian()
                independent_variables = ca.substitute(
                    simulator_i._independent_variables,
                    ca.vcat(parameter_symbols),
                    ca.vcat(parameter_values),
                )
                args = {
                    "x": varlist_new_algebraic_i.get_casadi_variables(),
                    "p": independent_variables,
                }
                jac_all = jac_function.call(args)
                jac_parameters = ca.solve(jac_all["jac_rhs_x"], -jac_all["jac_rhs_p"])

                index_selected_parameters = []
                for var_name in self.varlist_parameter.keys():
                    index_selected_parameters.append(
                        simulator_i.mapping_independent_variables[var_name]
                    )

                jac_mx_selected_parameters = jac_parameters.get(
                    False, ca.Slice(), index_selected_parameters
                )
                jacobian_list.append(
                    jac_mx_selected_parameters.get(
                        False, self.index_measurements_in_sim, ca.Slice()
                    )
                )

        self.nlpsol_g_direct = ca.cse(ca.vcat(equality_constraints))
        self.lower_bound_direct = np.array(lower_bound)
        self.lower_bound_direct_inf = np.array(lower_bound_inf)
        self.upper_bound_direct = np.array(upper_bound)
        self.upper_bound_direct_inf = np.array(upper_bound_inf)
        self.guess_direct = np.array(guess)

        if mode == "PE":
            self.simulate_all_direct = ca.vcat(meas_symbols)
        elif mode == "OED":
            jac_mx = ca.vcat(jacobian_list)
            if not self.jacobian_scaled_mx_constant.is_empty():
                jac_mx = ca.vcat([self.jacobian_mx_constant, jac_mx])

            number_of_experiments = len(self._previous_measurements) + len(
                self.list_simulators
            )
            std_scaling = ca.repmat(
                self.array_inverted_scaled_std,
                number_of_experiments,
                len(self.varlist_parameter),
            )

            jac_mx_scaled = jac_mx * std_scaling

            self.jacobian_mx_direct = jac_mx
            self.jacobian_scaled_mx_direct = jac_mx_scaled

    @_consistent_scaling_decorator
    def check_decision_bounds(self, plot: bool = False) -> None:
        """Method is simulating model on upper and lower bounds of decision variables.
        Prints if there were porblems simulation some bounds, meaning that optimizer
        will also have problems, when going near that bounds.
        Only first simulator of optimizers is used"""
        if isinstance(self, ParameterEstimation):
            bound_pairs = []
            for lb, ub in zip(self.lower_bound, self.upper_bound):
                bound_pairs.append([lb, ub])

            list_of_bounds = np.array(np.meshgrid(*bound_pairs)).T.reshape(
                -1, len(bound_pairs)
            )

            simulation = self.list_simulators[0]
            results = []

            for set_of_bounds in list_of_bounds:
                bound_dictionary = {}
                for var, bound in zip(
                    list(self.varlist_decision.values()), set_of_bounds
                ):
                    bound_dictionary[var.name] = bound
                try:
                    result = simulation.simulate(
                        algebraic=True,
                        recalculate_algebraic=True,
                        unfixed_variables=set_of_bounds,
                    )
                    if plot:
                        result._get_varlist_to_plot(True).dataframe.plot(
                            subplots=True, title=str(bound_dictionary)
                        )
                    results.append(result)
                except Exception:
                    print(f"Failed for these bounds: {bound_dictionary}")
        else:
            raise NotImplementedError

check_decision_bounds

check_decision_bounds(plot: bool = False) -> None

Method is simulating model on upper and lower bounds of decision variables. Prints if there were porblems simulation some bounds, meaning that optimizer will also have problems, when going near that bounds. Only first simulator of optimizers is used

Source code in src/mopeds/optimization.py
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
@_consistent_scaling_decorator
def check_decision_bounds(self, plot: bool = False) -> None:
    """Method is simulating model on upper and lower bounds of decision variables.
    Prints if there were porblems simulation some bounds, meaning that optimizer
    will also have problems, when going near that bounds.
    Only first simulator of optimizers is used"""
    if isinstance(self, ParameterEstimation):
        bound_pairs = []
        for lb, ub in zip(self.lower_bound, self.upper_bound):
            bound_pairs.append([lb, ub])

        list_of_bounds = np.array(np.meshgrid(*bound_pairs)).T.reshape(
            -1, len(bound_pairs)
        )

        simulation = self.list_simulators[0]
        results = []

        for set_of_bounds in list_of_bounds:
            bound_dictionary = {}
            for var, bound in zip(
                list(self.varlist_decision.values()), set_of_bounds
            ):
                bound_dictionary[var.name] = bound
            try:
                result = simulation.simulate(
                    algebraic=True,
                    recalculate_algebraic=True,
                    unfixed_variables=set_of_bounds,
                )
                if plot:
                    result._get_varlist_to_plot(True).dataframe.plot(
                        subplots=True, title=str(bound_dictionary)
                    )
                results.append(result)
            except Exception:
                print(f"Failed for these bounds: {bound_dictionary}")
    else:
        raise NotImplementedError

check_result_bounds

check_result_bounds(result) -> pd.DataFrame

Use output of self.optimize() and check if decision variables are not on the bounds Returns dataframe with variables that are not in bounds.

Source code in src/mopeds/optimization.py
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
def check_result_bounds(self, result) -> pd.DataFrame:
    """Use output of self.optimize() and check if decision variables are not on the bounds
    Returns dataframe with variables that are not in bounds."""
    if result["x_unscaled"].shape[0] == len(self.varlist_decision):
        varlist_decision = self.varlist_decision
        lower_bound = self.lower_bound
        upper_bound = self.upper_bound
    elif result["x_unscaled"].shape[0] == len(self.varlist_decision_direct):
        varlist_decision = self.varlist_decision_direct
        lower_bound = self.lower_bound_direct
        upper_bound = self.upper_bound_direct
    else:
        raise ValueError

    results_with_bound = {}
    for i, (var, res_i) in enumerate(
        zip(varlist_decision.values(), result["x_unscaled"])
    ):
        value = var.scale_to_original(float(res_i[0]))
        lb = var.scale_to_original(lower_bound[i])
        ub = var.scale_to_original(upper_bound[i])
        results_with_bound[var.name] = [lb, value, ub]
    df = pd.DataFrame.from_dict(results_with_bound).T
    selected_df = df[(df[0] >= df[1]) | (df[2] <= df[1])]
    selected_df.columns = ["lb", "value", "ub"]
    return selected_df

map_objective

map_objective(plot: bool = True) -> None

Calculate objective function for different values of parameters and plot, if needed. Currently support only 3 unfixed decision variables.

Source code in src/mopeds/optimization.py
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
@_consistent_scaling_decorator
def map_objective(self, plot: bool = True) -> None:
    """Calculate objective function for different values of parameters and plot, if needed.
    Currently support only 3 unfixed decision variables."""
    import matplotlib.pyplot as plt

    decision_variables = self.varlist_decision.get_casadi_variables()
    if decision_variables.shape == (3, 1):
        objective_function = ca.Function(
            "objective",
            [decision_variables],
            [self._objective()[0]],
            ["x"],
            ["f"],
        )

        # Generate steps for each variable between lb and ub
        axis_steps = []
        for lb, ub in zip(self.lower_bound, self.upper_bound):
            axis_steps.append(np.linspace(lb, ub, 6))

        # Create every possible combination of cordinates
        # Code the value of objective function as a color information
        xx = []
        yy = []
        zz = []
        objective = []
        for x in axis_steps[0]:
            for y in axis_steps[1]:
                for z in axis_steps[2]:
                    xx.append(x)
                    yy.append(y)
                    zz.append(z)
                    objective.append(float(objective_function([x, y, z]).toarray()))

        if plot:
            color = np.array(objective)
            fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
            colormap = plt.get_cmap("tab20b")

            ax.set_xlabel(f"x {decision_variables[0]}")
            ax.set_ylabel(f"y {decision_variables[1]}")
            ax.set_zlabel(f"z {decision_variables[2]}")

            quantile = np.quantile(color, 0.1)
            selected_objective = copy.deepcopy(color)
            selected_objective[selected_objective > quantile] = color.max()
            marker_size = (
                selected_objective.max() - selected_objective
            ) / selected_objective.max() * 20 + 7

            surf = ax.scatter(xx, yy, zz, c=color, s=marker_size, cmap=colormap)
            fig.colorbar(surf)
            plt.show()
    else:
        raise NotImplementedError

optimize_multistart

optimize_multistart(num_initials: int, scale: bool = True, max_iterations: int = 20) -> list[dict[str, ca.DM]]

Runs multiple optimizations with gueses spread between upper and lower bound. Helps to find feasible starting point for optimization in a few steps. WIP: recalcution of algebraic variables doesn't work.

Source code in src/mopeds/optimization.py
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
@_consistent_scaling_decorator
def optimize_multistart(
    self,
    num_initials: int,
    scale: bool = True,
    max_iterations: int = 20,
) -> list[dict[str, ca.DM]]:
    """Runs multiple optimizations with gueses spread between upper and lower bound.
    Helps to find feasible starting point for optimization in a few steps.
    WIP: recalcution of algebraic variables doesn't work.
    """
    list_startpoint = self.generate_multistart_guess(num_initials)
    results_with_f = []

    # Optimizer settings and guess are overwritten for multistart, and then returned back
    initial_guess = copy.deepcopy(self.guess)
    initial_settings = copy.deepcopy(self.solver_settings)

    self.solver_settings = {
        "verbose": False,
        "print_time": False,
        "ipopt": {
            "max_iter": max_iterations,
            "print_level": 0,
        },
    }
    for index, guess in tqdm(enumerate(list_startpoint)):
        if isinstance(self, ParameterEstimationNLE_control):
            for index_guess, current_guess in enumerate(guess):
                self.guess[index_guess] = current_guess
        else:
            self.guess = guess

        print(f"Optimization number {index} started")
        # for sim in self.list_simulators:
        #     sim.calculate_algebraic_initials(apply_intials=True)

        res = self.optimize(scale)
        print(f"Objective: {res['f']}")
        res_f = float(res["f"])
        results_with_f.append([float("inf") if res_f == 0 else res_f, res])

    results_with_f = sorted(results_with_f, key=lambda res: res[0])
    result_list_sorted = [res[1] for res in results_with_f]

    self.solver_settings = initial_settings
    return result_list_sorted

variables_dict_to_list

variables_dict_to_list(variables_dict: dict[str, float], *, scaling=None) -> list[float]

Takes a dictionary with {"var_name": var_value} and transforms to list corresponding to the order of self.varlist_decision variables

Source code in src/mopeds/optimization.py
 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
@_consistent_scaling_decorator
def variables_dict_to_list(
    self, variables_dict: dict[str, float], *, scaling=None
) -> list[float]:
    """Takes a dictionary with {"var_name": var_value} and transforms to list
    corresponding to the order of self.varlist_decision variables"""
    if not isinstance(scaling, bool):
        scaling = self._created_with_options["variable_scaling"]

    with options(variable_scaling=scaling):
        selected_variables: list[float] = []
        for var_name in variables_dict.keys():
            if var_name not in self.varlist_decision.keys():
                if "time_sp" in var_name or "weight_" in var_name:
                    raise ValueError(
                        f"Variable {var_name} is not a decision variable!"
                    )
                print(f"Supplied value for variables {var_name} is ignored!")
        for var_name, var in self.varlist_decision.items():
            try:
                scaled_value = var.scale_from_original(variables_dict[var_name])
                selected_variables.append(scaled_value)
            except KeyError:
                raise KeyError(f"Missing value for {var_name}")

    return selected_variables

PE_base

Bases: Optimizer

Source code in src/mopeds/optimization.py
 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
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
class PE_base(Optimizer):
    @property
    def _nlpsol_p_mx(self):
        return self.array_data_mx

    @property
    def _nlpsol_p_values(self):
        return self.array_data

    def _objective_ols(self, direct_optimization: bool):
        """Objective function is a trace(Z.T * Z), where Z is a residual matrix with shape:
        numRows -> amount of supplied experiments, numCol -> amount of variables that have measurements
        If experiments do not supply a measurement for one of the measurements, self.array_data_mask will
        have 0 as the respective element of the martix, otherwise 1"""
        if direct_optimization:
            evaluation = self.simulate_all_direct
        else:
            evaluation = self.simulate_all_mx

        residuals = (
            (evaluation - self.array_data_mx)
            * self.array_data_mask
            * np.sqrt(self.experiments_scale)
        )
        objective = ca.sumsqr(residuals)

        return objective, residuals

    def _objective_wls(self, direct_optimization: bool):
        """Objective function is a trace(Z.T * inv(VarY) * Z), where Z is a same matrix as in _objective_ols
        inv(VarY) is the variance of the respective measurements in Z, and has the same shape.
        Thus, covariance of the measurements is assumed to be zero."""
        if direct_optimization:
            evaluation = self.simulate_all_direct
        else:
            evaluation = self.simulate_all_mx

        residuals = (evaluation - self.array_data_mx) * self.array_data_mask
        scaled_residuals = (
            residuals * self.array_inverted_scaled_std * np.sqrt(self.experiments_scale)
        )
        objective = ca.sumsqr(scaled_residuals)
        return objective, residuals

    def _objective_fair(self, direct_optimization: bool):
        if direct_optimization:
            evaluation = self.simulate_all_direct
        else:
            evaluation = self.simulate_all_mx

        c = 2
        residuals = (evaluation - self.array_data) * self.array_data_mask
        scaled_residuals = (
            residuals * self.array_inverted_scaled_std * np.sqrt(self.experiments_scale)
        )
        res_mod = ca.sqrt(scaled_residuals**2)
        objective = 2 * c**2 * (res_mod / c - ca.log(1 + res_mod / c))
        objective = ca.sum1(objective)
        objective = ca.sum2(objective)
        # objective = ca.sumsqr(scaled_residuals)
        return objective, residuals

    @_consistent_scaling_decorator
    def _unscale_y(self, y):
        return self._unscale_simulator_output(y, self.names_of_measurements)

    @_consistent_scaling_decorator
    def _unscale_simulator_output(self, y, variables_names):
        scaling_constants_measurements_v = []
        scaling_constants_measurements_r = []
        for meas_name in variables_names:
            v, r = self.list_input_varlist[0][meas_name]._get_scaling_constants()
            scaling_constants_measurements_v.append(v)
            scaling_constants_measurements_r.append(r)
        scale_factor_v = np.array([scaling_constants_measurements_v])
        scale_factor_r = np.array([scaling_constants_measurements_r])
        scaled_y = y * scale_factor_v + scale_factor_r
        return scaled_y

    @_consistent_scaling_decorator
    def _unscale_df(self, df_all):
        return self._unscale_simulator_output(df_all, df_all.columns)

    @_consistent_scaling_decorator
    def _unscale_residuals(self, residuals):
        scaling_constants_measurements = []
        for meas_name in self.names_of_measurements:
            scaling_constants_measurements.append(
                self.list_input_varlist[0][meas_name]._get_scaling_constants()[0]
            )
        scale_factor = np.array([scaling_constants_measurements])
        scale_factor = np.tile(scale_factor, (residuals.shape[0], 1))
        scaled_residuals = residuals * scale_factor
        return scaled_residuals

    @_consistent_scaling_decorator
    def _unscale_jacobian(self, jacobian):
        scaled_jacobian = self._unscale_jacobian_parameter_values(jacobian)
        scaled_jacobian = self._unscale_jacobian_measurement_values(scaled_jacobian)
        return scaled_jacobian

    @property
    @_consistent_scaling_decorator
    def array_data_unscaled(self):
        unscaled_list = []
        for i, meas_name in enumerate(self.names_of_measurements):
            scaled_column = self.list_input_varlist[0][meas_name].scale_to_original(
                self.array_data[:, i]
            )
            unscaled_list.append(scaled_column)
        return np.array(unscaled_list).T

    @_consistent_scaling_decorator
    def _unscale_jacobian_parameter_values(self, jacobian):
        scale_parameters = np.tile(
            np.array(self.varlist_decision._get_scaling_constants()[0]),
            (jacobian.shape[0], 1),
        )
        scaled_jacobian = jacobian / scale_parameters
        return scaled_jacobian

    @_consistent_scaling_decorator
    def _unscale_jacobian_measurement_values(self, jacobian):
        scaling_constants_measurements = []
        for meas_name in self.names_of_measurements:
            scaling_constants_measurements.append(
                self.list_input_varlist[0][meas_name]._get_scaling_constants()[0]
            )
        scaling_measurements = np.repeat(
            np.asarray([scaling_constants_measurements]),
            len(self.varlist_decision),
            axis=0,
        ).T

        scaling_all = []

        if isinstance(self.list_simulators[0], Simulator):
            for sim in self.list_simulators:
                len_time_grid = sim.time_grid_relative.shape[0] - 1
                scaling_simulator_i = np.repeat(
                    scaling_measurements, len_time_grid, axis=0
                )
                scaling_all.append(scaling_simulator_i)
            scale_measurements = np.concatenate(scaling_all, axis=0)
        else:
            scale_measurements = np.repeat(
                scaling_measurements, len(self.list_simulators), axis=0
            )

        scaled_jacobian = jacobian * scale_measurements
        return scaled_jacobian

    def setup_regularization(
        self,
        contribution: None | float = None,
        reference_parameters: None | np.ndarray = None,
    ):
        if contribution is None:
            self.regularization_contribution = 0
        else:
            self.regularization_contribution = contribution

        if reference_parameters is None:
            self.reference_parameters = np.zeros((len(self.varlist_decision), 1))
        else:
            if reference_parameters.shape[0] != len(self.varlist_decision):
                raise ValueError("Shape of supplied reference_parameters is incorrect")
            else:
                self.reference_parameters = reference_parameters

    def _objective_tikhonov(self, direct_optimization: bool):
        objective, residuals = self._objective_wls(
            direct_optimization=direct_optimization
        )

        penalty = ca.sqrt(
            ca.sumsqr(
                self.varlist_decision.get_casadi_variables() - self.reference_parameters
            )
        )
        regularization_part = 0.5 * (self.regularization_contribution**2) * penalty

        objective = objective + regularization_part

        return objective, residuals

    def optimize(
        self,
        scale=None,
        objective_function="wls",
        direct_optimization=False,
        *,
        reuse_solver=False,
    ):
        if objective_function == "wls":
            self._objective = partial(
                self._objective_wls, direct_optimization=direct_optimization
            )
        elif objective_function == "ols":
            self._objective = partial(
                self._objective_ols, direct_optimization=direct_optimization
            )
        elif objective_function == "fair":
            self._objective = partial(
                self._objective_fair, direct_optimization=direct_optimization
            )
        elif objective_function == "tikh":
            self._objective = partial(
                self._objective_tikhonov, direct_optimization=direct_optimization
            )
        else:
            raise NotImplementedError(
                f"Objective function '{objective_function}' is not supported"
            )

        return self._optimize(
            scale, direct_optimization=direct_optimization, reuse_solver=reuse_solver
        )

    def _setup_experiments_scale(self, scale_experiments):
        if isinstance(self, ParameterEstimation):
            if scale_experiments:
                experiments_scale: int | np.ndarray = self.experiments_weights
            else:
                experiments_scale = 1

            # This attribute is used while calculating Objective, and is either 1 or self.experiments_weights
            # It's used to make some experiments as valuable as others, even if they have less experimental points
            # So if you supply 2 experiments one with 10 and another with 20 time_stamps, effect of each experimental
            # point of second experiments on objective function is decreased by 2
            self.experiments_scale: int | np.ndarray = experiments_scale
        else:
            self.experiments_scale = 1

    @_consistent_scaling_decorator
    def calculate_objective_and_residual(
        self,
        parameters: dict[str, float],
        objective_function: str = "ols",
        experiment_weigts: bool = False,
    ) -> dict[str, float | np.ndarray]:
        if objective_function == "ols":
            obj_f = self._objective_ols(direct_optimization=False)
        elif objective_function == "wls":
            obj_f = self._objective_wls(direct_optimization=False)

        decision_variables = self.varlist_decision.get_casadi_variables()

        casadi_function = ca.Function(
            "objective",
            [decision_variables, self.array_data_mx],
            [obj_f[0], obj_f[1], self.simulate_all_mx, self._simulate_all_mx],
            ["x", "data_arrays"],
            ["f", "residuals", "y", "y_all"],
        )

        selected_parameters = self.variables_dict_to_list(parameters)
        res = casadi_function(x=selected_parameters, data_arrays=self.array_data)

        if isinstance(self, ParameterEstimationNLE):
            algebraic_names = list(
                self.model.varlist_algebraic(self.list_input_varlist[0]).keys()
            )
            df_all = pd.DataFrame(res["y_all"], columns=algebraic_names)
        else:
            all_names = list(
                self.model.varlist_state(self.list_input_varlist[0]).keys()
            )
            if self._use_algebraic_variables:
                all_names.extend(
                    self.model.varlist_algebraic(self.list_input_varlist[0]).keys()
                )

            index_from = 0
            index_till = 0
            list_df = []
            for sim in self.list_simulators:
                time = sim.time_grid_relative[1:]
                index_till += len(time)
                df_one_simulator = pd.DataFrame(
                    res["y_all"][index_from:index_till, :],
                    columns=all_names,
                    index=time,
                )
                index_from += len(time)
                list_df.append(df_one_simulator)

            df_all = pd.concat(
                list_df, keys=range(len(self.list_simulators)), names=["sim", "time"]
            )

        result_np = {
            "f": float(res["f"]),
            "residuals": res["residuals"].toarray(),
            "y": res["y"].toarray(),
            "df_all": df_all,
        }

        return result_np

    def _setup_varlist_decision(self):
        for var in self.model.varlist_independent(self.list_input_varlist[0]).values():
            if isinstance(var, VariableParameter):
                if var.fixed is False:
                    self.varlist_decision.add_variable(var)
        self.setup_regularization()

    @_consistent_scaling_decorator
    def generate_simulate_all_functions(self) -> None:
        """Combines simulate_fast() functions from simulator, and creates MX structure, that is used
        further in objective_function calculation"""
        if isinstance(self.list_simulators[0], Simulator):
            res_dict_name = "xf"
        elif isinstance(self.list_simulators[0], SimulatorNLE):
            if self.list_simulators[0]._solver_name == "ipopt":
                print(
                    "\nSimulators of PE optimizer use IPOPT nlpsol. Results can be incosistent\n"
                )
            res_dict_name = "x"

        list_simulation_T = []

        for simulator in self.list_simulators:
            if isinstance(self.list_simulators[0], Simulator):
                simulator.calculate_algebraic_initials(apply_intials=True)

            res_simulation = simulator.simulate_fast()

            if getattr(self, "_use_algebraic_variables", False):
                data = ca.vcat([res_simulation[res_dict_name], res_simulation["zf"]])
                list_simulation_T.append(data.T)
            else:
                list_simulation_T.append(res_simulation[res_dict_name].T)

        free_variables = self.varlist_decision.get_casadi_variables()
        all_selected_measurements = ca.vcat(list_simulation_T).get(
            False, ca.Slice(), self.index_measurements_in_sim
        )
        self.simulate_all_function = ca.Function(
            "sim_all", [free_variables], [all_selected_measurements]
        )
        self.simulate_all_mx = self.simulate_all_function(free_variables)

        _simulate_all_function = ca.Function(
            "sim_all", [free_variables], [ca.vcat(list_simulation_T)]
        )
        self._simulate_all_mx = _simulate_all_function(free_variables)

    @cached_property
    def _jacobian_function(self):
        decision_variables = self.varlist_decision.get_casadi_variables()
        jac_meas_mx = ca.jacobian(
            self.simulate_all_mx[:, list(range(len(self.names_of_measurements)))],
            decision_variables,
        )
        jac_meas_function = ca.Function("jac_meas", [decision_variables], [jac_meas_mx])
        return jac_meas_function

    @cached_property
    def _jacobian_scaler(self):
        flattened_std = self.array_inverted_std.flatten(order="F")
        return np.tile(flattened_std, (len(self.varlist_decision), 1)).T

    @_consistent_scaling_decorator
    def calculate_sensitivity_and_fim_fast(
        self, parameters: dict[str, float]
    ) -> dict[str, np.ndarray]:
        all_parameter_values = self.variables_dict_to_list(parameters)
        jac_all_dm = self._unscale_jacobian(
            self._jacobian_function(all_parameter_values)
        )
        jac_all_scaled = jac_all_dm * self._jacobian_scaler

        fim_matrix_scaled = jac_all_scaled.T @ jac_all_scaled
        parameter_covariance_matrix = np.linalg.inv(fim_matrix_scaled)  # type: ignore

        return jac_all_dm, jac_all_scaled, parameter_covariance_matrix

    @_consistent_scaling_decorator
    def calculate_jacobian_yao_fast(
        self, parameters: dict[str, float]
    ) -> dict[str, np.ndarray]:
        all_parameter_values = self.variables_dict_to_list(parameters)
        jac_all_dm = self._unscale_jacobian(
            self._jacobian_function(all_parameter_values)
        )

        decision_variables = self.varlist_decision.get_casadi_variables()
        res_simulation = ca.Function(
            "sim", [decision_variables], [self.simulate_all_mx]
        )(all_parameter_values)
        jacobian_index = [0, self.simulate_all_mx.shape[0]]
        jacs = []

        for index_measurement, meas_name in enumerate(self.names_of_measurements):
            scale_factor = self.list_input_varlist[0][
                meas_name
            ]._get_scaling_constants()
            jacobian_slice = ca.Slice(jacobian_index[0], jacobian_index[1])
            jac_meas_dm = jac_all_dm[jacobian_slice, :]
            jacobian_index[0] += self.simulate_all_mx.shape[0]
            jacobian_index[1] += self.simulate_all_mx.shape[0]

            jac_meas_selected_dm = (
                jac_meas_dm * self.array_data_mask[:, index_measurement]
            )

            jac_meas_selected_yao_dm = jac_meas_selected_dm * (
                1
                / (
                    res_simulation[:, index_measurement] * scale_factor[0]
                    + scale_factor[1]
                )
            )
            jacs.append(jac_meas_selected_yao_dm)

        jac_array_yao = np.concatenate(jacs)
        return jac_array_yao

    @_consistent_scaling_decorator
    def calculate_sensitivity_and_fim(
        self, parameters: dict[str, float], parameter_names: list[str] | None = None
    ) -> dict[str, np.ndarray]:
        """Calculate jacobian, scaled_jacobian, parameter_covariance_matrix only for parameters,
        which names are listed in paramtere_names, if None, for all unfixed paramters.
        Jacobian is calculated only for "measured" algebraic variables, the ones which
        had value, when ParameterEstimationNLE was initialized.

        parameters dictionary holds paramter values for all unfixed parameters, example:

        parameters = {"theta1": 1, "theta2": 2}
        parameter_names = ["theta1"]

        Jacobian of measured variables will be calculated for theta1=1 and theta2=2, but reported
        jacobian will only contain parameter theta2.

        Jacobian dimensions: dY/dp [NumOfMeasurements x NumOfParameters]
        """
        decision_variables = self.varlist_decision.get_casadi_variables()
        if parameter_names is not None:
            list_selected_parameters_index = []
            for par_index, par_name in enumerate(self.varlist_decision.keys()):
                if par_name in parameter_names:
                    list_selected_parameters_index.append(par_index)

        all_parameter_values = self.variables_dict_to_list(parameters)

        residuals = self.calculate_objective_and_residual(
            parameters, objective_function="ols"
        )["residuals"]

        scaled_residuals = self._unscale_residuals(residuals)

        measurement_variance_estimate = (
            np.diag(scaled_residuals.T @ scaled_residuals) / self.dof
        )
        print("OLS std: ", np.sqrt(measurement_variance_estimate))

        estimated_inverted_std = copy.deepcopy(self.array_inverted_std)

        if isinstance(measurement_variance_estimate, float):
            measurement_variance_estimate = [measurement_variance_estimate]

        # Avoid division by 0 later
        measurement_variance_estimate[measurement_variance_estimate == 0] = 1e-24

        for index_meas, meas_std in enumerate(measurement_variance_estimate):
            estimated_inverted_std[:, index_meas] = 1 / np.sqrt(meas_std)

        jacobian = {}
        jacobian_scaled = {}
        jacobian_scaled_estimated = {}
        jacobian_yao = {}
        res_simulation = ca.Function(
            "sim", [decision_variables], [self.simulate_all_mx]
        )(all_parameter_values)

        jac_meas_mx = ca.jacobian(
            self.simulate_all_mx[:, list(range(len(self.names_of_measurements)))],
            decision_variables,
        )
        jac_meas_function = ca.Function("jac_meas", [decision_variables], [jac_meas_mx])
        jac_all_dm = self._unscale_jacobian(jac_meas_function(all_parameter_values))
        jacobian_index = [0, self.simulate_all_mx.shape[0]]

        for index_measurement, meas_name in enumerate(self.names_of_measurements):
            scale_factor = self.list_input_varlist[0][
                meas_name
            ]._get_scaling_constants()
            jacobian_slice = ca.Slice(jacobian_index[0], jacobian_index[1])
            jac_meas_dm = jac_all_dm[jacobian_slice, :]
            jacobian_index[0] += self.simulate_all_mx.shape[0]
            jacobian_index[1] += self.simulate_all_mx.shape[0]

            jac_meas_selected_dm = (
                jac_meas_dm * self.array_data_mask[:, index_measurement]
            )
            jac_meas_selected_scaled_dm = (
                jac_meas_selected_dm * self.array_inverted_std[:, index_measurement]
            )
            jac_meas_selected_scaled_estimated_dm = (
                jac_meas_selected_dm * estimated_inverted_std[:, index_measurement]
            )
            jac_meas_selected_yao_dm = jac_meas_selected_dm * (
                1
                / (
                    res_simulation[:, index_measurement] * scale_factor[0]
                    + scale_factor[1]
                )
            )
            if parameter_names is None:
                jacobian[meas_name] = jac_meas_selected_dm
                jacobian_scaled[meas_name] = jac_meas_selected_scaled_dm
                jacobian_scaled_estimated[meas_name] = (
                    jac_meas_selected_scaled_estimated_dm
                )
                jacobian_yao[meas_name] = jac_meas_selected_yao_dm
            else:
                jacobian[meas_name] = jac_meas_selected_dm[
                    :, list_selected_parameters_index
                ]
                jacobian_scaled[meas_name] = jac_meas_selected_scaled_dm[
                    :, list_selected_parameters_index
                ]
                jacobian_scaled_estimated[meas_name] = (
                    jac_meas_selected_scaled_estimated_dm[
                        :, list_selected_parameters_index
                    ]
                )
                jacobian_yao[meas_name] = jac_meas_selected_yao_dm[
                    :, list_selected_parameters_index
                ]

        jac_array = np.concatenate(list(jacobian.values()))
        jac_array_scaled = np.concatenate(list(jacobian_scaled.values()))
        jac_array_scaled_estimated = np.concatenate(
            list(jacobian_scaled_estimated.values())
        )
        jac_array_yao = np.concatenate(list(jacobian_yao.values()))

        # Generate jacobian and hessian on obj function
        obj_func = ca.substitute(
            self._objective_wls(direct_optimization=False)[0],
            self.array_data_mx,
            self.array_data,
        )
        jac_objective = ca.Function(
            "jf",
            [decision_variables],
            [ca.jacobian(obj_func, decision_variables)],
        )(all_parameter_values)
        # Should be twice as big as fim_matrix_scaled
        try:
            hessian_objective_wls = ca.Function(
                "jf",
                [decision_variables],
                [ca.hessian(obj_func, decision_variables)[0]],
            )
            hessian_objective_wls = hessian_objective_wls(all_parameter_values)
        except RuntimeError:
            print("Failed to calculate hessian")
            hessian_objective_wls = None

        try:
            obj_func_tikhonov = ca.substitute(
                self._objective_tikhonov(direct_optimization=False)[0],
                self.array_data_mx,
                self.array_data,
            )
            hessian_objective_tikhonov = ca.Function(
                "jf",
                [decision_variables],
                [ca.hessian(obj_func_tikhonov)],
            )
            hessian_objective_tikhonov = hessian_objective_tikhonov(
                all_parameter_values
            )
        except RuntimeError:
            print("Failed to calculate hessian")
            hessian_objective_tikhonov = None

        if parameter_names is not None:
            jac_objective = jac_objective[:, list_selected_parameters_index]
            if hessian_objective_wls is not None:
                hessian_objective_wls = hessian_objective_wls[
                    list_selected_parameters_index, list_selected_parameters_index
                ]
            if hessian_objective_tikhonov is not None:
                hessian_objective_tikhonov = hessian_objective_tikhonov[
                    list_selected_parameters_index, list_selected_parameters_index
                ]

        fim_matrix = jac_array.T @ jac_array
        fim_matrix_scaled = jac_array_scaled.T @ jac_array_scaled
        parameter_covariance_matrix = np.linalg.inv(fim_matrix_scaled)  # type: ignore

        result = {}
        result["jac_full"] = jac_array
        result["jac_sorted"] = jacobian
        result["jac_scaled_full"] = jac_array_scaled_estimated
        result["jac_scaled_full_theory"] = jac_array_scaled
        result["jac_scaled_sorted"] = jacobian_scaled
        result["jac_yao_full"] = jac_array_yao
        result["jac_yao_sorted"] = jacobian_yao
        result["fim"] = fim_matrix
        result["fim_scaled"] = fim_matrix_scaled
        result["cov_par"] = parameter_covariance_matrix
        result["jac_wls"] = jac_objective
        result["hess_wls"] = hessian_objective_wls
        result["hess_tikh"] = hessian_objective_tikhonov
        result["s2"] = measurement_variance_estimate

        return result

    @_consistent_scaling_decorator
    def parameter_identifiability_chu2012(
        self,
        parameters: dict[str, float],
        unfixed_params: list[str],
        parameters_identifiable: list[str] | None = None,
        parameters_not_identifiable: list[str] | None = None,
    ):
        if parameters_identifiable is None:
            parameters_identifiable = []

        if parameters_not_identifiable is None:
            parameters_not_identifiable = []

        sorted_unfixed_params = []
        for par_name in self.varlist_decision.keys():
            if par_name in unfixed_params:
                sorted_unfixed_params.append(par_name)

        parameters_index = list(range(len(sorted_unfixed_params)))

        S = self.calculate_sensitivity_and_fim_fast(parameters)[1].toarray()
        S = S * np.array(self.variables_dict_to_list(parameters, scaling=False))

        info = []
        best_set = None
        max_det = 0

        for subset_size in range(1, len(unfixed_params) + 1):
            for subset_index in itertools.combinations(parameters_index, subset_size):
                S_selected = S[:, subset_index]
                FIM_selected = S_selected.T @ S_selected
                if subset_size == 1:
                    max_det_i = FIM_selected.item(0)
                else:
                    max_det_i = np.linalg.det(FIM_selected)
                if max_det_i > max_det:
                    max_det = max_det_i
                    best_set = subset_index
                subset_names = np.array(sorted_unfixed_params)[list(subset_index)]
                info_i = [subset_size, subset_names, max_det_i]
                info.append(info_i)

        df = pd.DataFrame(info, columns=["subset_size", "subset_names", "det"])
        parameters_identifiable = np.array(sorted_unfixed_params)[list(best_set)]

        parameters_not_identifiable = list(
            set(sorted_unfixed_params) - set(parameters_identifiable)
        )

        parameters_identifiable_sorted = []
        parameters_not_identifiable_sorted = []
        for par_name in sorted_unfixed_params:
            if par_name in parameters_identifiable:
                parameters_identifiable_sorted.append(par_name)
            else:
                parameters_not_identifiable_sorted.append(par_name)

        print(f"Estimable parameters: {parameters_identifiable_sorted}")
        print(f"Non identifiable parameters: {parameters_not_identifiable_sorted}")

        result = {}
        result["estimable"] = parameters_identifiable_sorted
        result["fixed"] = parameters_not_identifiable_sorted

        return result

    @_consistent_scaling_decorator
    def parameter_identifiability_lopez2013(
        self,
        parameters: dict[str, float],
        unfixed_params: list[str],
        parameters_identifiable: list[str] | None = None,
        parameters_not_identifiable: list[str] | None = None,
        eigenvalue_threshold: float = 10e-4,
    ):
        if parameters_identifiable is None:
            parameters_identifiable = []

        if parameters_not_identifiable is None:
            parameters_not_identifiable = []

        sorted_unfixed_params = []
        for par_name in self.varlist_decision.keys():
            if par_name in unfixed_params:
                sorted_unfixed_params.append(par_name)

        S = self.calculate_sensitivity_and_fim_fast(parameters)[1].toarray()
        S = S * np.array(self.variables_dict_to_list(parameters, scaling=False))

        # S = S * np.array(self.variables_dict_to_list(parameters))

        svd = np.linalg.svd(S, full_matrices=True)
        Q, R, P = linalg.qr(S, pivoting=True)

        conditional_number = (svd[1][0] / svd[1]) > 1000
        if (~conditional_number).all():
            num_identifiable = svd[1].shape[0]
        else:
            num_identifiable = list(conditional_number).index(True)
        parameters_ranked = np.array(sorted_unfixed_params)[P]
        parameters_identifiable = parameters_ranked[:num_identifiable]
        parameters_not_identifiable = parameters_ranked[num_identifiable:]

        parameters_identifiable_sorted = []
        parameters_not_identifiable_sorted = []
        for par_name in sorted_unfixed_params:
            if par_name in parameters_identifiable:
                parameters_identifiable_sorted.append(par_name)
            else:
                parameters_not_identifiable_sorted.append(par_name)

        print(f"Ranked parameters: {list(parameters_ranked)}")
        print(f"Estimable parameters: {parameters_identifiable_sorted}")
        print(f"Non identifiable parameters: {parameters_not_identifiable_sorted}")

        result = {}
        result["ranked"] = list(parameters_ranked)
        result["estimable"] = parameters_identifiable_sorted
        result["fixed"] = parameters_not_identifiable_sorted

        return result

    @_consistent_scaling_decorator
    def parameter_identifiability_quaiser2009(
        self,
        parameters: dict[str, float],
        unfixed_params: list[str],
        parameters_identifiable: list[str] | None = None,
        parameters_not_identifiable: list[str] | None = None,
        eigenvalue_threshold: float = 10e-4,
    ):
        """Do parameter ranking based on Quasier 2009, however use scaled sensitivity as in Yao 2003.
        Threshold is taken from Quasier 2009.
        Return ranked parameters in descending order, and divide them in identifiable and not"""

        if parameters_identifiable is None:
            parameters_identifiable = []

        if parameters_not_identifiable is None:
            parameters_not_identifiable = []

        sorted_unfixed_params = []
        for par_name in self.varlist_decision.keys():
            if par_name in unfixed_params:
                sorted_unfixed_params.append(par_name)

        S = self.calculate_sensitivity_and_fim_fast(parameters)[1].toarray()
        S = S * np.array(self.variables_dict_to_list(parameters, scaling=False))
        fim_matrix = S.T @ S

        for i in range(fim_matrix.shape[0]):
            vals, vecs = eigsorted(fim_matrix)

            index_max = np.argmax(np.abs(vecs[:, -1]))
            current_parameter_name = sorted_unfixed_params.pop(index_max)
            if np.abs(vals[-1]) > eigenvalue_threshold:
                parameters_identifiable.insert(0, current_parameter_name)
            else:
                parameters_not_identifiable.insert(0, current_parameter_name)
            fim_matrix = np.delete(fim_matrix, index_max, axis=0)
            fim_matrix = np.delete(fim_matrix, index_max, axis=1)

        parameters_ranked = []
        for parameter_name in parameters_identifiable + parameters_not_identifiable:
            parameters_ranked.append(parameter_name)

        print(f"Ranked parameters: {parameters_ranked}")
        print(f"Estimable parameters: {parameters_identifiable}")
        print(f"Non identifiable parameters: {parameters_not_identifiable}")

        result = {}
        result["ranked"] = parameters_ranked
        result["estimable"] = parameters_identifiable
        result["fixed"] = parameters_not_identifiable

        return result

    @_consistent_scaling_decorator
    def parameter_identifiability_yao2003(
        self,
        parameters: dict[str, float],
        unfixed_params: list[str],
        threshold: float = 4e-2,
    ):
        """Do parameter ranking based on Yao 2003. Cut-off value taken from Yao 2003.
        Return ranked parameters in descending order, and divide them in identifiable and not"""
        parameter_values_all: list[float] = []
        selected_parameters: list[float] = []
        unranked_parameters: list[str] = []

        sorted_unfixed_params = []
        for par_name in self.varlist_decision.keys():
            if par_name in unfixed_params:
                sorted_unfixed_params.append(par_name)

        for var_name in parameters.keys():
            if var_name in self.varlist_decision.keys():
                parameter_values_all.append(parameters[var_name])

        for var_name in parameters.keys():
            if var_name in unfixed_params:
                selected_parameters.append(parameters[var_name])
                unranked_parameters.append(var_name)

        results_sensitivity = self.calculate_jacobian_yao_fast(parameters)
        jacobian_yao = results_sensitivity * np.array(
            self.variables_dict_to_list(parameters, scaling=False)
        )

        XK = np.zeros(jacobian_yao.shape)

        parameters_identifiable = []
        parameters_not_identifiable = []

        for i in range(len(unranked_parameters)):
            if i == 0:
                eucnorm = np.linalg.norm(jacobian_yao, axis=0)
            else:
                eucnorm = np.linalg.norm(R, axis=0)

            index_most_identifiable_par = np.argsort(eucnorm)[-1]
            most_identifiable_parameter = unranked_parameters[
                index_most_identifiable_par
            ]

            if max(eucnorm) < threshold:
                parameters_not_identifiable.append(most_identifiable_parameter)
                break
            else:
                parameters_identifiable.append(most_identifiable_parameter)

            if i == 0:
                XK = jacobian_yao[:, index_most_identifiable_par].reshape(
                    (jacobian_yao.shape[0], 1)
                )
            else:
                XK = np.append(
                    XK,
                    jacobian_yao[:, index_most_identifiable_par].reshape(
                        (jacobian_yao.shape[0], 1)
                    ),
                    axis=1,
                )

            Z_hat = XK.dot(np.linalg.inv(XK.T.dot(XK))).dot(XK.T).dot(jacobian_yao)
            R = jacobian_yao - Z_hat

        parameters_identifiable_sorted = []
        parameters_not_identifiable_sorted = []
        for par_name in sorted_unfixed_params:
            if par_name in parameters_identifiable:
                parameters_identifiable_sorted.append(par_name)
            else:
                parameters_not_identifiable_sorted.append(par_name)

        print(f"Estimable parameters: {parameters_identifiable_sorted}")
        print(f"Non identifiable parameters: {parameters_not_identifiable_sorted}")

        result = {}
        result["estimable"] = parameters_identifiable
        result["fixed"] = parameters_not_identifiable

        return result

    @_consistent_scaling_decorator
    def parameter_analysis(self, parameters: dict[str, float], plot=True):
        import scipy.stats

        num_par = len(self.varlist_decision)

        selected_parameters = self.variables_dict_to_list(parameters, scaling=False)
        result_sens = self.calculate_sensitivity_and_fim(parameters)

        parameter_covariance_matrix = result_sens["cov_par"]

        parameter_variance = np.diag(parameter_covariance_matrix)
        parameter_std = np.sqrt(parameter_variance).flatten()

        students_t_dist_95 = scipy.stats.t.ppf(0.975, self.dof)
        marginal_conf_interval_95 = (parameter_std * students_t_dist_95).T

        print(parameter_std)
        for par, var_value in zip(selected_parameters, marginal_conf_interval_95):
            print(f"{par} +- {var_value} |  ({var_value * 100 / par}%)")

        if plot:
            import matplotlib.pyplot as plt
            from matplotlib.axes import Axes
            from matplotlib.patches import Ellipse

            fig, axes = plt.subplots(
                ncols=num_par - 1, nrows=num_par - 1, layout="constrained"
            )
            if isinstance(axes, Axes) == 1:
                axes = [axes]

            fisher_f_dist_95 = scipy.stats.f.ppf(0.95, num_par, self.dof)

            comb = list(combinations(range(num_par), 2))
            par_names = list(self.varlist_decision.keys())

            title = ""
            for par_value, var_variance_i, name in zip(
                selected_parameters, marginal_conf_interval_95, par_names
            ):
                title = (
                    title
                    + f"{name}: {round(par_value, 5)} ± {round(var_variance_i, 5)} |  ({round((var_variance_i / par_value) * 100, 1)}%)\n"
                )

            fig.suptitle(title)

            for i in comb:
                if len(axes) == 1:
                    ax = axes[0]
                else:
                    ax = axes[i[1] - 1, i[0]]
                index_subarray = np.ix_(i, i)
                parameters_i = []
                parameters_i.append(selected_parameters[i[0]])
                parameters_i.append(selected_parameters[i[1]])

                marginal_conf_interval_95_i = []
                marginal_conf_interval_95_i.append(marginal_conf_interval_95[i[0]])
                marginal_conf_interval_95_i.append(marginal_conf_interval_95[i[1]])
                cov_m = parameter_covariance_matrix[index_subarray]
                vals, vecs = eigsorted(cov_m)
                theta = np.degrees(np.arctan2(*vecs[:, 0][::-1]))

                # Width and height are "full" widths, not radius
                for fisher in [fisher_f_dist_95]:  # , fisher_f_dist_99]:
                    width, height = 2 * np.sqrt(num_par * fisher * vals)
                    ellip = Ellipse(
                        xy=[parameters_i[0], parameters_i[1]],
                        width=width,
                        height=height,
                        angle=theta,
                        alpha=0.3,
                        lw=2,
                        linestyle="-",
                        color="red",
                    )

                    ax.add_artist(ellip)
                ax.relim()
                ax.autoscale()

                if i[0] == 0:
                    ax.set_ylabel(f"{par_names[i[1]]}")

                if i[1] == len(par_names) - 1:
                    ax.set_xlabel(f"{par_names[i[0]]}")

                # ax.axvline(parameters_i[0] - marginal_conf_interval_95_i[0])
                # ax.axvline(parameters_i[0] + marginal_conf_interval_95_i[0])
                # ax.axhline(parameters_i[1] - marginal_conf_interval_95_i[1])
                # ax.axhline(parameters_i[1] + marginal_conf_interval_95_i[1])
        return marginal_conf_interval_95

    @property
    def dof(self):
        # Eq 7-13-22 Bard 1974
        return self.array_data.shape[0] - (
            len(self.varlist_decision) / len(self.names_of_measurements)
        )

calculate_sensitivity_and_fim

calculate_sensitivity_and_fim(parameters: dict[str, float], parameter_names: list[str] | None = None) -> dict[str, np.ndarray]

Calculate jacobian, scaled_jacobian, parameter_covariance_matrix only for parameters, which names are listed in paramtere_names, if None, for all unfixed paramters. Jacobian is calculated only for "measured" algebraic variables, the ones which had value, when ParameterEstimationNLE was initialized.

parameters dictionary holds paramter values for all unfixed parameters, example:

parameters = {"theta1": 1, "theta2": 2} parameter_names = ["theta1"]

Jacobian of measured variables will be calculated for theta1=1 and theta2=2, but reported jacobian will only contain parameter theta2.

Jacobian dimensions: dY/dp [NumOfMeasurements x NumOfParameters]

Source code in src/mopeds/optimization.py
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
@_consistent_scaling_decorator
def calculate_sensitivity_and_fim(
    self, parameters: dict[str, float], parameter_names: list[str] | None = None
) -> dict[str, np.ndarray]:
    """Calculate jacobian, scaled_jacobian, parameter_covariance_matrix only for parameters,
    which names are listed in paramtere_names, if None, for all unfixed paramters.
    Jacobian is calculated only for "measured" algebraic variables, the ones which
    had value, when ParameterEstimationNLE was initialized.

    parameters dictionary holds paramter values for all unfixed parameters, example:

    parameters = {"theta1": 1, "theta2": 2}
    parameter_names = ["theta1"]

    Jacobian of measured variables will be calculated for theta1=1 and theta2=2, but reported
    jacobian will only contain parameter theta2.

    Jacobian dimensions: dY/dp [NumOfMeasurements x NumOfParameters]
    """
    decision_variables = self.varlist_decision.get_casadi_variables()
    if parameter_names is not None:
        list_selected_parameters_index = []
        for par_index, par_name in enumerate(self.varlist_decision.keys()):
            if par_name in parameter_names:
                list_selected_parameters_index.append(par_index)

    all_parameter_values = self.variables_dict_to_list(parameters)

    residuals = self.calculate_objective_and_residual(
        parameters, objective_function="ols"
    )["residuals"]

    scaled_residuals = self._unscale_residuals(residuals)

    measurement_variance_estimate = (
        np.diag(scaled_residuals.T @ scaled_residuals) / self.dof
    )
    print("OLS std: ", np.sqrt(measurement_variance_estimate))

    estimated_inverted_std = copy.deepcopy(self.array_inverted_std)

    if isinstance(measurement_variance_estimate, float):
        measurement_variance_estimate = [measurement_variance_estimate]

    # Avoid division by 0 later
    measurement_variance_estimate[measurement_variance_estimate == 0] = 1e-24

    for index_meas, meas_std in enumerate(measurement_variance_estimate):
        estimated_inverted_std[:, index_meas] = 1 / np.sqrt(meas_std)

    jacobian = {}
    jacobian_scaled = {}
    jacobian_scaled_estimated = {}
    jacobian_yao = {}
    res_simulation = ca.Function(
        "sim", [decision_variables], [self.simulate_all_mx]
    )(all_parameter_values)

    jac_meas_mx = ca.jacobian(
        self.simulate_all_mx[:, list(range(len(self.names_of_measurements)))],
        decision_variables,
    )
    jac_meas_function = ca.Function("jac_meas", [decision_variables], [jac_meas_mx])
    jac_all_dm = self._unscale_jacobian(jac_meas_function(all_parameter_values))
    jacobian_index = [0, self.simulate_all_mx.shape[0]]

    for index_measurement, meas_name in enumerate(self.names_of_measurements):
        scale_factor = self.list_input_varlist[0][
            meas_name
        ]._get_scaling_constants()
        jacobian_slice = ca.Slice(jacobian_index[0], jacobian_index[1])
        jac_meas_dm = jac_all_dm[jacobian_slice, :]
        jacobian_index[0] += self.simulate_all_mx.shape[0]
        jacobian_index[1] += self.simulate_all_mx.shape[0]

        jac_meas_selected_dm = (
            jac_meas_dm * self.array_data_mask[:, index_measurement]
        )
        jac_meas_selected_scaled_dm = (
            jac_meas_selected_dm * self.array_inverted_std[:, index_measurement]
        )
        jac_meas_selected_scaled_estimated_dm = (
            jac_meas_selected_dm * estimated_inverted_std[:, index_measurement]
        )
        jac_meas_selected_yao_dm = jac_meas_selected_dm * (
            1
            / (
                res_simulation[:, index_measurement] * scale_factor[0]
                + scale_factor[1]
            )
        )
        if parameter_names is None:
            jacobian[meas_name] = jac_meas_selected_dm
            jacobian_scaled[meas_name] = jac_meas_selected_scaled_dm
            jacobian_scaled_estimated[meas_name] = (
                jac_meas_selected_scaled_estimated_dm
            )
            jacobian_yao[meas_name] = jac_meas_selected_yao_dm
        else:
            jacobian[meas_name] = jac_meas_selected_dm[
                :, list_selected_parameters_index
            ]
            jacobian_scaled[meas_name] = jac_meas_selected_scaled_dm[
                :, list_selected_parameters_index
            ]
            jacobian_scaled_estimated[meas_name] = (
                jac_meas_selected_scaled_estimated_dm[
                    :, list_selected_parameters_index
                ]
            )
            jacobian_yao[meas_name] = jac_meas_selected_yao_dm[
                :, list_selected_parameters_index
            ]

    jac_array = np.concatenate(list(jacobian.values()))
    jac_array_scaled = np.concatenate(list(jacobian_scaled.values()))
    jac_array_scaled_estimated = np.concatenate(
        list(jacobian_scaled_estimated.values())
    )
    jac_array_yao = np.concatenate(list(jacobian_yao.values()))

    # Generate jacobian and hessian on obj function
    obj_func = ca.substitute(
        self._objective_wls(direct_optimization=False)[0],
        self.array_data_mx,
        self.array_data,
    )
    jac_objective = ca.Function(
        "jf",
        [decision_variables],
        [ca.jacobian(obj_func, decision_variables)],
    )(all_parameter_values)
    # Should be twice as big as fim_matrix_scaled
    try:
        hessian_objective_wls = ca.Function(
            "jf",
            [decision_variables],
            [ca.hessian(obj_func, decision_variables)[0]],
        )
        hessian_objective_wls = hessian_objective_wls(all_parameter_values)
    except RuntimeError:
        print("Failed to calculate hessian")
        hessian_objective_wls = None

    try:
        obj_func_tikhonov = ca.substitute(
            self._objective_tikhonov(direct_optimization=False)[0],
            self.array_data_mx,
            self.array_data,
        )
        hessian_objective_tikhonov = ca.Function(
            "jf",
            [decision_variables],
            [ca.hessian(obj_func_tikhonov)],
        )
        hessian_objective_tikhonov = hessian_objective_tikhonov(
            all_parameter_values
        )
    except RuntimeError:
        print("Failed to calculate hessian")
        hessian_objective_tikhonov = None

    if parameter_names is not None:
        jac_objective = jac_objective[:, list_selected_parameters_index]
        if hessian_objective_wls is not None:
            hessian_objective_wls = hessian_objective_wls[
                list_selected_parameters_index, list_selected_parameters_index
            ]
        if hessian_objective_tikhonov is not None:
            hessian_objective_tikhonov = hessian_objective_tikhonov[
                list_selected_parameters_index, list_selected_parameters_index
            ]

    fim_matrix = jac_array.T @ jac_array
    fim_matrix_scaled = jac_array_scaled.T @ jac_array_scaled
    parameter_covariance_matrix = np.linalg.inv(fim_matrix_scaled)  # type: ignore

    result = {}
    result["jac_full"] = jac_array
    result["jac_sorted"] = jacobian
    result["jac_scaled_full"] = jac_array_scaled_estimated
    result["jac_scaled_full_theory"] = jac_array_scaled
    result["jac_scaled_sorted"] = jacobian_scaled
    result["jac_yao_full"] = jac_array_yao
    result["jac_yao_sorted"] = jacobian_yao
    result["fim"] = fim_matrix
    result["fim_scaled"] = fim_matrix_scaled
    result["cov_par"] = parameter_covariance_matrix
    result["jac_wls"] = jac_objective
    result["hess_wls"] = hessian_objective_wls
    result["hess_tikh"] = hessian_objective_tikhonov
    result["s2"] = measurement_variance_estimate

    return result

generate_simulate_all_functions

generate_simulate_all_functions() -> None

Combines simulate_fast() functions from simulator, and creates MX structure, that is used further in objective_function calculation

Source code in src/mopeds/optimization.py
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
@_consistent_scaling_decorator
def generate_simulate_all_functions(self) -> None:
    """Combines simulate_fast() functions from simulator, and creates MX structure, that is used
    further in objective_function calculation"""
    if isinstance(self.list_simulators[0], Simulator):
        res_dict_name = "xf"
    elif isinstance(self.list_simulators[0], SimulatorNLE):
        if self.list_simulators[0]._solver_name == "ipopt":
            print(
                "\nSimulators of PE optimizer use IPOPT nlpsol. Results can be incosistent\n"
            )
        res_dict_name = "x"

    list_simulation_T = []

    for simulator in self.list_simulators:
        if isinstance(self.list_simulators[0], Simulator):
            simulator.calculate_algebraic_initials(apply_intials=True)

        res_simulation = simulator.simulate_fast()

        if getattr(self, "_use_algebraic_variables", False):
            data = ca.vcat([res_simulation[res_dict_name], res_simulation["zf"]])
            list_simulation_T.append(data.T)
        else:
            list_simulation_T.append(res_simulation[res_dict_name].T)

    free_variables = self.varlist_decision.get_casadi_variables()
    all_selected_measurements = ca.vcat(list_simulation_T).get(
        False, ca.Slice(), self.index_measurements_in_sim
    )
    self.simulate_all_function = ca.Function(
        "sim_all", [free_variables], [all_selected_measurements]
    )
    self.simulate_all_mx = self.simulate_all_function(free_variables)

    _simulate_all_function = ca.Function(
        "sim_all", [free_variables], [ca.vcat(list_simulation_T)]
    )
    self._simulate_all_mx = _simulate_all_function(free_variables)

parameter_identifiability_quaiser2009

parameter_identifiability_quaiser2009(parameters: dict[str, float], unfixed_params: list[str], parameters_identifiable: list[str] | None = None, parameters_not_identifiable: list[str] | None = None, eigenvalue_threshold: float = 0.001)

Do parameter ranking based on Quasier 2009, however use scaled sensitivity as in Yao 2003. Threshold is taken from Quasier 2009. Return ranked parameters in descending order, and divide them in identifiable and not

Source code in src/mopeds/optimization.py
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
@_consistent_scaling_decorator
def parameter_identifiability_quaiser2009(
    self,
    parameters: dict[str, float],
    unfixed_params: list[str],
    parameters_identifiable: list[str] | None = None,
    parameters_not_identifiable: list[str] | None = None,
    eigenvalue_threshold: float = 10e-4,
):
    """Do parameter ranking based on Quasier 2009, however use scaled sensitivity as in Yao 2003.
    Threshold is taken from Quasier 2009.
    Return ranked parameters in descending order, and divide them in identifiable and not"""

    if parameters_identifiable is None:
        parameters_identifiable = []

    if parameters_not_identifiable is None:
        parameters_not_identifiable = []

    sorted_unfixed_params = []
    for par_name in self.varlist_decision.keys():
        if par_name in unfixed_params:
            sorted_unfixed_params.append(par_name)

    S = self.calculate_sensitivity_and_fim_fast(parameters)[1].toarray()
    S = S * np.array(self.variables_dict_to_list(parameters, scaling=False))
    fim_matrix = S.T @ S

    for i in range(fim_matrix.shape[0]):
        vals, vecs = eigsorted(fim_matrix)

        index_max = np.argmax(np.abs(vecs[:, -1]))
        current_parameter_name = sorted_unfixed_params.pop(index_max)
        if np.abs(vals[-1]) > eigenvalue_threshold:
            parameters_identifiable.insert(0, current_parameter_name)
        else:
            parameters_not_identifiable.insert(0, current_parameter_name)
        fim_matrix = np.delete(fim_matrix, index_max, axis=0)
        fim_matrix = np.delete(fim_matrix, index_max, axis=1)

    parameters_ranked = []
    for parameter_name in parameters_identifiable + parameters_not_identifiable:
        parameters_ranked.append(parameter_name)

    print(f"Ranked parameters: {parameters_ranked}")
    print(f"Estimable parameters: {parameters_identifiable}")
    print(f"Non identifiable parameters: {parameters_not_identifiable}")

    result = {}
    result["ranked"] = parameters_ranked
    result["estimable"] = parameters_identifiable
    result["fixed"] = parameters_not_identifiable

    return result

parameter_identifiability_yao2003

parameter_identifiability_yao2003(parameters: dict[str, float], unfixed_params: list[str], threshold: float = 0.04)

Do parameter ranking based on Yao 2003. Cut-off value taken from Yao 2003. Return ranked parameters in descending order, and divide them in identifiable and not

Source code in src/mopeds/optimization.py
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
@_consistent_scaling_decorator
def parameter_identifiability_yao2003(
    self,
    parameters: dict[str, float],
    unfixed_params: list[str],
    threshold: float = 4e-2,
):
    """Do parameter ranking based on Yao 2003. Cut-off value taken from Yao 2003.
    Return ranked parameters in descending order, and divide them in identifiable and not"""
    parameter_values_all: list[float] = []
    selected_parameters: list[float] = []
    unranked_parameters: list[str] = []

    sorted_unfixed_params = []
    for par_name in self.varlist_decision.keys():
        if par_name in unfixed_params:
            sorted_unfixed_params.append(par_name)

    for var_name in parameters.keys():
        if var_name in self.varlist_decision.keys():
            parameter_values_all.append(parameters[var_name])

    for var_name in parameters.keys():
        if var_name in unfixed_params:
            selected_parameters.append(parameters[var_name])
            unranked_parameters.append(var_name)

    results_sensitivity = self.calculate_jacobian_yao_fast(parameters)
    jacobian_yao = results_sensitivity * np.array(
        self.variables_dict_to_list(parameters, scaling=False)
    )

    XK = np.zeros(jacobian_yao.shape)

    parameters_identifiable = []
    parameters_not_identifiable = []

    for i in range(len(unranked_parameters)):
        if i == 0:
            eucnorm = np.linalg.norm(jacobian_yao, axis=0)
        else:
            eucnorm = np.linalg.norm(R, axis=0)

        index_most_identifiable_par = np.argsort(eucnorm)[-1]
        most_identifiable_parameter = unranked_parameters[
            index_most_identifiable_par
        ]

        if max(eucnorm) < threshold:
            parameters_not_identifiable.append(most_identifiable_parameter)
            break
        else:
            parameters_identifiable.append(most_identifiable_parameter)

        if i == 0:
            XK = jacobian_yao[:, index_most_identifiable_par].reshape(
                (jacobian_yao.shape[0], 1)
            )
        else:
            XK = np.append(
                XK,
                jacobian_yao[:, index_most_identifiable_par].reshape(
                    (jacobian_yao.shape[0], 1)
                ),
                axis=1,
            )

        Z_hat = XK.dot(np.linalg.inv(XK.T.dot(XK))).dot(XK.T).dot(jacobian_yao)
        R = jacobian_yao - Z_hat

    parameters_identifiable_sorted = []
    parameters_not_identifiable_sorted = []
    for par_name in sorted_unfixed_params:
        if par_name in parameters_identifiable:
            parameters_identifiable_sorted.append(par_name)
        else:
            parameters_not_identifiable_sorted.append(par_name)

    print(f"Estimable parameters: {parameters_identifiable_sorted}")
    print(f"Non identifiable parameters: {parameters_not_identifiable_sorted}")

    result = {}
    result["estimable"] = parameters_identifiable
    result["fixed"] = parameters_not_identifiable

    return result

ParameterEstimation

Bases: PE_base

Source code in src/mopeds/optimization.py
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
class ParameterEstimation(PE_base):
    def __init__(
        self,
        model: Model,
        variable_list: list[VariableList],
        simulator_name: str = "idas",
        simulator_settings: dict | None = None,
        *,
        use_idas_constraints: bool = None,
        recalculate_algebraic: bool = False,
    ):
        super().__init__(
            model,
            variable_list,
            simulator_name,
            simulator_settings,
        )

        self._setup_algebraic_flag()

        self._objective: Callable[[], tuple[ca.MX | ca.DM, ca.MX | ca.DM]] = (
            self._objective_ols
        )

        self._setup_simulator(
            use_idas_constraints=use_idas_constraints,
            recalculate_algebraic=recalculate_algebraic,
        )

        self.logger.debug(
            "Created Optimizer object: \n Data Shape {} \n Desicion Variables {}".format(
                self.array_data.shape,
                self.varlist_decision.get_variable_name(),  # type: ignore
            )
        )
        self._setup_initialization()

        self.solver_name = "ipopt"
        self.solver_settings = {
            "verbose": False,
            "ipopt": {"max_iter": 300},
        }

        self._setup_experiments_scale(False)

    def _setup_algebraic_flag(self):
        self._use_algebraic_variables = False
        for varlist_input in self.list_input_varlist:
            if len(varlist_input.get_algebraic()) == 0:
                continue
            if varlist_input.get_algebraic().dataframe[1:].empty is False:
                self._use_algebraic_variables = True

    @_consistent_scaling_decorator
    def _setup_simulator(
        self,
        *,
        use_idas_constraints: bool,
        recalculate_algebraic: bool,
    ) -> None:
        # It's not checked if all supplied varlist have same states etc.
        self._setup_varlist_decision()

        # Lists used to calculate experiments_weights
        list_timegrid_length = []
        size_simulation_output = []
        list_simulators = []
        experimental_data = []
        experimental_data_mask = []

        list_inverted_variances = []
        list_inverted_scaled_variances = []

        for simulator_index, varlist_input in enumerate(self.list_input_varlist):
            # Create a time_grid, that "stops" at every experimental data, for every state variable
            ordered_varlist_input = self.model.varlist(varlist_input)
            if not varlist_input.get_common_origin(
                strict=True, variable_type=VariableState
            ):
                raise (
                    ValueError(
                        f"Not all State Variables in one experiment have same time0, so simulations cannot be initialized:\n{varlist_input}"
                    )
                )
            data_frame = pd.DataFrame()

            for var in ordered_varlist_input.values():
                if isinstance(var, VariableState) or (
                    isinstance(var, VariableAlgebraic) and self._use_algebraic_variables
                ):
                    data_frame = data_frame.join(
                        var.scale_from_original(var.dataframe), how="outer"
                    )

                elif isinstance(var, VariableControl):
                    var.fixed = True
                    if isinstance(var, VariableControlPiecewiseConstant):
                        var.fixed = True
                        data_frame = data_frame.join(
                            var.scale_from_original(var.dataframe), how="outer"
                        )
                        # Column should be dropped, because it's needed only for unique timestamp
                        data_frame.drop(columns=var.name, inplace=True)

            if self._use_algebraic_variables:
                # TODO in further stepps I always assume that state variables are there, and algebraic are added
                # Without this sorting steps, self.data_array is not correctly sorted. However, the logic has to be fixed
                new_order = list(ordered_varlist_input.get_state().keys()) + list(
                    ordered_varlist_input.get_algebraic().keys()
                )
                data_frame = data_frame[new_order]

            time_grid_unique = (
                (data_frame.index - data_frame.index[0]).total_seconds().tolist()
            )

            list_timegrid_length.append(float(len(time_grid_unique)))

            simulator_settings = self.simulator_settings

            simulator = Simulator(
                self.model,
                np.array(time_grid_unique),
                ordered_varlist_input,
                self.simulator_name,
                simulator_settings,
                use_idas_constraints=use_idas_constraints,
                recalculate_algebraic=recalculate_algebraic,
            )

            list_simulators.append(simulator)

            # Generate an array (experiment_data_varlist) with Experimental data with the same dimensions as simulation results.
            new_experiment_data_varlist = data_frame.iloc[1:].to_numpy()
            experimental_data.append(new_experiment_data_varlist)
            new_experiment_data_mask_varlist = (
                data_frame.iloc[1:].notna().to_numpy().astype(int)
            )
            experimental_data_mask.append(new_experiment_data_mask_varlist)

            # Generate inverted_variances
            variable_name_list = list(ordered_varlist_input.get_state().keys())
            if self._use_algebraic_variables:
                variable_name_list.extend(
                    list(ordered_varlist_input.get_algebraic().keys())
                )
            inverted_variances_varlist = []
            inverted_scaled_variances_varlist = []
            for var_name in variable_name_list:
                var = ordered_varlist_input[var_name]
                scaled_variance = var.variance / var._get_scaling_constants()[0] ** 2
                inverted_variances_varlist.append(
                    1.0 / (np.full(len(time_grid_unique) - 1, var.variance))
                )
                inverted_scaled_variances_varlist.append(
                    1.0 / (np.full(len(time_grid_unique) - 1, scaled_variance))
                )
            inverted_variances_array = np.column_stack(inverted_variances_varlist)
            inverted_scaled_variances_array = np.column_stack(
                inverted_scaled_variances_varlist
            )

            list_inverted_variances.append(inverted_variances_array)
            list_inverted_scaled_variances.append(inverted_scaled_variances_array)

            size_simulation_output.append(inverted_variances_array.shape)

        # Calculate experiments_weights
        self.list_simulators: Sequence[Simulator] = list_simulators

        array_data = np.concatenate(experimental_data)
        all_measurements_names_list = list(ordered_varlist_input.get_state().keys())
        if self._use_algebraic_variables:
            all_measurements_names_list.extend(
                list(ordered_varlist_input.get_algebraic().keys())
            )

        all_measurements_names = np.array(all_measurements_names_list)

        index_columns_with_all_nans = np.isnan(array_data).all(axis=0)

        max_time_grid = max(list_timegrid_length)
        experiments_weights = []
        for time_grid_length, size_simulation in zip(
            list_timegrid_length, size_simulation_output
        ):
            experiments_weights.append(
                np.full(size_simulation, max_time_grid / time_grid_length)
            )

        """
        This list holds nested arrays with all experimental data. If data is not available for the time_stamp,
        it's replaced with 0. It has follwing form:
        [exp1_var1_time1, exp1_var2_time1, exp1_varN_time1, exp1_var1_time2 ... , exp1_varN_timeN, exp2_var1_time1 ...]
        """
        self.array_data = np.nan_to_num(array_data[:, ~index_columns_with_all_nans])
        self.array_data_mask = np.concatenate(experimental_data_mask)[
            :, ~index_columns_with_all_nans
        ]
        self.array_data_mx = ca.MX.sym("array_data", self.array_data.shape)
        self.array_data_mask_mx = ca.MX.sym("array_data_mask", self.array_data.shape)

        self.names_of_measurements: list[str] = all_measurements_names[
            ~index_columns_with_all_nans
        ].tolist()

        self.index_measurements_in_sim = []
        for name in self.names_of_measurements:
            try:
                index = self.list_simulators[0].mapping_state_variables[name]
            except KeyError:
                index = (
                    len(self.list_simulators[0].mapping_state_variables)
                    + self.list_simulators[0].mapping_algebraic_variables[name]
                )
            self.index_measurements_in_sim.append(index)

        # Inverted variances provided weightning matrix for PE problem
        array_inverted_variance: np.ndarray = np.concatenate(list_inverted_variances)[
            :, ~index_columns_with_all_nans
        ]
        self.array_inverted_std = np.sqrt(array_inverted_variance)

        array_inverted_scaled_variance: np.ndarray = np.concatenate(
            list_inverted_scaled_variances
        )[:, ~index_columns_with_all_nans]
        self.array_inverted_scaled_std = np.sqrt(array_inverted_scaled_variance)

        self.experiments_weights: np.ndarray = np.concatenate(experiments_weights)

        self.generate_simulate_all_functions()

    def optimize(
        self,
        scale=None,
        objective_function="wls",
        *,
        scale_experiments=False,
        reuse_solver=False,
    ) -> dict[str, ca.DM]:
        """Solves optimization problem. Scaling decreases amount of iterations,
        and should always almost be used
        """
        self._setup_experiments_scale(scale_experiments)
        return PE_base.optimize(self, scale, objective_function)

    @_consistent_scaling_decorator
    def plot_simulation(
        self,
        supplied_parameters: dict[str, float] | None = None,
        experiment_names: list[str] | None = None,
        savefig: bool = False,
        algebraic: bool = True,
        plot: bool = True,
    ) -> list[VariableList]:  # noqa: E501
        """Plots experimental points against simulated trajectories, first line, initial guess, than supplied values"""

        if experiment_names is None:
            experiment_names = []
            for index, _ in enumerate(self.list_input_varlist):
                experiment_names.append(f"EXP_{index}")

        if not len(experiment_names) == len(self.list_input_varlist):
            raise ValueError(
                "Length of experiment names is not same as ammount of experiments"
            )

        for input_varlist, simulator, exp_name in zip(
            self.list_input_varlist,
            self.list_simulators,
            experiment_names,
        ):
            res_guess = simulator.simulate(
                algebraic=algebraic,
                recalculate_algebraic=True,
                unfixed_variables=dict(zip(self.varlist_decision.keys(), self.guess)),
            )[2]

            if supplied_parameters is not None:
                res_supplied = simulator.simulate(
                    algebraic=algebraic,
                    recalculate_algebraic=True,
                    unfixed_variables=supplied_parameters,
                )[2]

            if plot:
                if pd.get_option("plotting.backend") == "plotly":
                    if supplied_parameters is None:
                        fig = res_guess.dataframe.plot(markers=True)
                    else:
                        fig = res_supplied.dataframe.plot(markers=True)
                    fig.show()
                else:
                    axes = res_guess.plot(
                        prefix="GUESS ", color="blue", algebraic=algebraic, show=False
                    )
                    if supplied_parameters is not None:
                        axes = res_supplied.plot(
                            prefix="FINAL ", ax=axes, color="red", algebraic=algebraic
                        )
                    axes[0].set_title(exp_name)
                    input_varlist.plot(
                        ax=axes,
                        marker="x",
                        color="black",
                        prefix="EXP ",
                        linestyle="None",
                        algebraic=algebraic,
                        show=True,
                    )

        if supplied_parameters is None:
            return [res_guess]
        else:
            return [res_guess, res_supplied]

optimize

optimize(scale=None, objective_function='wls', *, scale_experiments=False, reuse_solver=False) -> dict[str, ca.DM]

Solves optimization problem. Scaling decreases amount of iterations, and should always almost be used

Source code in src/mopeds/optimization.py
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
def optimize(
    self,
    scale=None,
    objective_function="wls",
    *,
    scale_experiments=False,
    reuse_solver=False,
) -> dict[str, ca.DM]:
    """Solves optimization problem. Scaling decreases amount of iterations,
    and should always almost be used
    """
    self._setup_experiments_scale(scale_experiments)
    return PE_base.optimize(self, scale, objective_function)

plot_simulation

plot_simulation(supplied_parameters: dict[str, float] | None = None, experiment_names: list[str] | None = None, savefig: bool = False, algebraic: bool = True, plot: bool = True) -> list[VariableList]

Plots experimental points against simulated trajectories, first line, initial guess, than supplied values

Source code in src/mopeds/optimization.py
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
@_consistent_scaling_decorator
def plot_simulation(
    self,
    supplied_parameters: dict[str, float] | None = None,
    experiment_names: list[str] | None = None,
    savefig: bool = False,
    algebraic: bool = True,
    plot: bool = True,
) -> list[VariableList]:  # noqa: E501
    """Plots experimental points against simulated trajectories, first line, initial guess, than supplied values"""

    if experiment_names is None:
        experiment_names = []
        for index, _ in enumerate(self.list_input_varlist):
            experiment_names.append(f"EXP_{index}")

    if not len(experiment_names) == len(self.list_input_varlist):
        raise ValueError(
            "Length of experiment names is not same as ammount of experiments"
        )

    for input_varlist, simulator, exp_name in zip(
        self.list_input_varlist,
        self.list_simulators,
        experiment_names,
    ):
        res_guess = simulator.simulate(
            algebraic=algebraic,
            recalculate_algebraic=True,
            unfixed_variables=dict(zip(self.varlist_decision.keys(), self.guess)),
        )[2]

        if supplied_parameters is not None:
            res_supplied = simulator.simulate(
                algebraic=algebraic,
                recalculate_algebraic=True,
                unfixed_variables=supplied_parameters,
            )[2]

        if plot:
            if pd.get_option("plotting.backend") == "plotly":
                if supplied_parameters is None:
                    fig = res_guess.dataframe.plot(markers=True)
                else:
                    fig = res_supplied.dataframe.plot(markers=True)
                fig.show()
            else:
                axes = res_guess.plot(
                    prefix="GUESS ", color="blue", algebraic=algebraic, show=False
                )
                if supplied_parameters is not None:
                    axes = res_supplied.plot(
                        prefix="FINAL ", ax=axes, color="red", algebraic=algebraic
                    )
                axes[0].set_title(exp_name)
                input_varlist.plot(
                    ax=axes,
                    marker="x",
                    color="black",
                    prefix="EXP ",
                    linestyle="None",
                    algebraic=algebraic,
                    show=True,
                )

    if supplied_parameters is None:
        return [res_guess]
    else:
        return [res_guess, res_supplied]

ParameterEstimationNLE

Bases: PE_base

Source code in src/mopeds/optimization.py
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
class ParameterEstimationNLE(PE_base):
    def __init__(
        self,
        model: Model,
        variable_lists: list[VariableList],
        simulator_settings=None,
        simulator_name="rootfinder",
        *,
        use_simulator_bounds=None,
        SimulatorClass=SimulatorNLE,
    ) -> None:
        if use_simulator_bounds is not None:
            warn(
                "use_simulator_bounds is not used anymore and will be ignored",
                FutureWarning,
                2,
            )
        super().__init__(model, variable_lists, simulator_name, simulator_settings)

        self._setup_simulator(SimulatorClass)
        self.logger.debug(
            "Created Optimizer object: \n Data Shape {} \n Desicion Variables {}".format(
                self.array_data.shape, self.varlist_decision.get_variable_name()
            )
        )
        self._setup_initialization()
        self._setup_direct_optimization("PE")

        self.solver_name = "ipopt"
        self.solver_settings = {
            "verbose": False,
            "ipopt": {"max_iter": 300},
        }
        # Set default objective
        self._objective: Callable[[], tuple[ca.MX | ca.DM, ca.MX | ca.DM]] = (
            self._objective_ols
        )

        self._setup_experiments_scale(False)
        self.setup_regularization(0, np.zeros((len(self.varlist_decision), 1)))

    @_consistent_scaling_decorator
    def _setup_simulator(self, SimulatorClass: SimulatorNLE) -> None:
        # It's not checked if all supplied varlist have same states etc.
        if not issubclass(SimulatorClass, SimulatorNLE):
            raise NotImplementedError("Provided simulator_class is not supported")

        self._setup_varlist_decision()

        list_data_mask = []
        list_simulators = []
        list_data = []
        list_inverted_variances = []
        list_inverted_scaled_variances = []

        for varlist_input in self.list_input_varlist:
            ordered_varlist_input = self.model.varlist(varlist_input)
            varlist_data = []
            varlist_data_mask = []
            varlist_variance = []
            varlist_scaled_variance = []
            for var in ordered_varlist_input.values():
                if isinstance(var, VariableControl):
                    if not var.fixed:
                        raise NotImplementedError
                if isinstance(var, VariableParameter):
                    # Avoid situations, where parameters unfixed differently
                    # in each self.list_input_varlist
                    fixed = var.name not in self.varlist_decision.keys()
                    var.fixed = fixed

            simulator = SimulatorClass(
                self.model,
                ordered_varlist_input,
                self.simulator_settings,
                self.simulator_name,
            )
            list_simulators.append(simulator)

            for var in ordered_varlist_input.get_algebraic().values():
                if var.value[0] is None or np.isnan(var.value[0]):
                    varlist_data.append(np.nan)
                    varlist_data_mask.append(0.0)
                else:
                    scaled_value = var.scale_from_original(var.value[0])
                    varlist_data.append(scaled_value)
                    varlist_data_mask.append(1.0)

                scaled_variance = var.variance / var._get_scaling_constants()[0] ** 2
                varlist_variance.append(1.0 / var.variance)
                varlist_scaled_variance.append(1.0 / scaled_variance)
            list_data.append(varlist_data)
            list_data_mask.append(varlist_data_mask)
            list_inverted_variances.append(varlist_variance)
            list_inverted_scaled_variances.append(varlist_scaled_variance)

        self.list_simulators: list[SimulatorNLE] = list_simulators

        array_data = np.array(list_data)
        all_measurements_names = np.array(
            list(ordered_varlist_input.get_algebraic().keys())
        )

        index_columns_with_all_nans = np.isnan(array_data).all(axis=0)

        self.array_data = np.nan_to_num(array_data[:, ~index_columns_with_all_nans])
        self.array_data_mask = np.array(list_data_mask)[:, ~index_columns_with_all_nans]

        self.array_data_mx = ca.MX.sym("array_data", self.array_data.shape)
        self.array_data_mask_mx = ca.MX.sym("array_data_mask", self.array_data.shape)

        self.names_of_measurements: list[str] = all_measurements_names[
            ~index_columns_with_all_nans
        ].tolist()
        array_inverted_variance = np.array(list_inverted_variances)[
            :, ~index_columns_with_all_nans
        ]
        self.array_inverted_std = np.sqrt(array_inverted_variance)
        array_inverted_scaled_variance = np.array(list_inverted_scaled_variances)[
            :, ~index_columns_with_all_nans
        ]
        self.array_inverted_scaled_std = np.sqrt(array_inverted_scaled_variance)

        self.index_measurements_in_sim = []
        for name in self.names_of_measurements:
            index = self.list_simulators[0].mapping_algebraic_variables[name]
            self.index_measurements_in_sim.append(index)

        self.generate_simulate_all_functions()

    @_consistent_scaling_decorator
    def calculate_inference_bounds(
        self,
        dict_of_params: dict,
        dict_of_responses: dict,
        dict_of_controls: dict,
        dict_of_artificial_controls: dict = None,
        rng: np.random.Generator = None,
    ):
        """Method to calculate one-dimensional inference bounds for a given dict of responses.
        If dict_of_artificial_controls is supplied, artificial data is generated and used for instead
        of experimental data. If rng is supplied, the given rng is used for articficial data generation.

        Parameters
        ----------
        dict_of_params : dict
            keys: parameter name
            values: corresponding parameter value
        dict_of_responses : dict
            keys: response names
            values: corresponding response variance
            Only used for artificial data generation.
            If values are None, the defualt response variance value is utilized.
        dict_of_controls : dict
            keys: control names
            values: corresponding list of necessary information about controls with
            list = list([lower bound: float, upper bound: float, number of points: int])
        dict_of_artificial_controls : dict, optional
            dict_of_controls used for artificial data generation, by default None
        rng : np.random.Generator, optional
            rng used for artifical data generation, by default None

        Output
        ------
        inference_results : dict
            Contains all inference bounds related results
        exp_data : dict
            Contains experimental data OR generated artifical data
        sim_results : dict
            Contains data of the simulation needed for inference computation
            Content of same dimension as content of inference_results
        """
        import scipy.stats

        def convert_varlist_to_data_dictionary(
            var_list_list: list[VariableList],
            dict_of_responses: dict,
            dict_of_controls: dict,
        ):
            sim_data = {}
            for var_name in [*dict_of_controls.keys(), *dict_of_responses.keys()]:
                var_values = []
                for simulation in var_list_list:
                    var = simulation[var_name]
                    var_values.append(var.scale_to_original(var.value[0]))
                sim_data[var_name] = np.array(var_values)

            return sim_data

        def generate_simulation_data(
            template_varlist: VariableList,
            dict_of_params: dict,
            dict_of_responses: dict,
            dict_of_controls: dict,
            perturbate: bool,
            rng: np.random.Generator = None,
        ):

            for param, param_value in dict_of_params.items():
                template_varlist[param].value = param_value
            for key, variance in dict_of_responses.items():
                if variance is not None:
                    template_varlist[key].variance = variance

            generated_var_lists, true_parameters, _ = (
                tools.generate_artificial_data_from_grid_nle(
                    self.model,
                    template_varlist,
                    dict_of_controls,
                    perturbate=perturbate,
                    rng=rng,
                    measurement_names=dict_of_responses.keys(),
                    keep_in_bounds=True,
                )
            )

            for parameter in dict_of_params:
                for simulation in generated_var_lists:
                    simulation[parameter].fixed = False

            sim_data = convert_varlist_to_data_dictionary(
                generated_var_lists, dict_of_controls, dict_of_responses
            )

            return generated_var_lists, sim_data

        if dict_of_artificial_controls is None:
            artificial_mode = False
            experimental_data = copy.deepcopy(self.list_input_varlist)
            exp_data = convert_varlist_to_data_dictionary(
                experimental_data,
                dict_of_responses,
                dict_of_controls,
            )
        else:
            artificial_mode = True
            experimental_data, exp_data = generate_simulation_data(
                copy.deepcopy(self.list_input_varlist[0]),
                dict_of_params,
                dict_of_responses,
                dict_of_artificial_controls,
                True,
                rng=rng,
            )

        variable_list_real, sim_data = generate_simulation_data(
            copy.deepcopy(self.list_input_varlist[0]),
            dict_of_params,
            dict_of_responses,
            dict_of_controls,
            False,
        )

        OLS = {}
        if artificial_mode:
            pe_artificial = ParameterEstimationNLE(self.model, experimental_data)
        else:
            pe_artificial = self

        residuals = pe_artificial.calculate_objective_and_residual(dict_of_params)[
            "residuals"
        ]
        OLS_values = np.diag(residuals.T @ residuals)
        OLS = dict(zip(self.names_of_measurements, OLS_values))

        jac = pe_artificial.calculate_sensitivity_and_fim(
            dict_of_params, list(dict_of_params.keys())
        )["jac_sorted"]

        pe_grid = ParameterEstimationNLE(self.model, variable_list_real)
        jac_grid = pe_grid.calculate_sensitivity_and_fim(
            dict_of_params, list(dict_of_params.keys())
        )["jac_sorted"]

        len_exp = len(experimental_data)
        len_param = len(dict_of_params)
        fisher95 = scipy.stats.f(len_param, self.dof).ppf(0.95)

        inference_results = {}
        for control in dict_of_controls:
            inference_results[control] = np.array(sim_data[control])

        for response in dict_of_responses:
            inference_results[response] = {}
            s = np.sqrt(OLS[response] / self.dof)
            R = np.linalg.qr(jac[response], mode="reduced")[1]
            bound = (
                s
                * np.linalg.norm(jac_grid[response] @ np.linalg.inv(R), axis=1)
                * np.sqrt(len_param * fisher95)
            )

            inference_results[response]["s"] = s
            inference_results[response]["R"] = R
            inference_results[response]["bound"] = bound
            inference_results[response]["lower bound"] = sim_data[response] - bound
            inference_results[response]["simulation"] = sim_data[response]
            inference_results[response]["upper bound"] = sim_data[response] + bound

        return inference_results, exp_data, sim_data

calculate_inference_bounds

calculate_inference_bounds(dict_of_params: dict, dict_of_responses: dict, dict_of_controls: dict, dict_of_artificial_controls: dict = None, rng: Generator = None)

Method to calculate one-dimensional inference bounds for a given dict of responses. If dict_of_artificial_controls is supplied, artificial data is generated and used for instead of experimental data. If rng is supplied, the given rng is used for articficial data generation.

Parameters

dict_of_params : dict keys: parameter name values: corresponding parameter value dict_of_responses : dict keys: response names values: corresponding response variance Only used for artificial data generation. If values are None, the defualt response variance value is utilized. dict_of_controls : dict keys: control names values: corresponding list of necessary information about controls with list = list([lower bound: float, upper bound: float, number of points: int]) dict_of_artificial_controls : dict, optional dict_of_controls used for artificial data generation, by default None rng : np.random.Generator, optional rng used for artifical data generation, by default None

Output

inference_results : dict Contains all inference bounds related results exp_data : dict Contains experimental data OR generated artifical data sim_results : dict Contains data of the simulation needed for inference computation Content of same dimension as content of inference_results

Source code in src/mopeds/optimization.py
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
@_consistent_scaling_decorator
def calculate_inference_bounds(
    self,
    dict_of_params: dict,
    dict_of_responses: dict,
    dict_of_controls: dict,
    dict_of_artificial_controls: dict = None,
    rng: np.random.Generator = None,
):
    """Method to calculate one-dimensional inference bounds for a given dict of responses.
    If dict_of_artificial_controls is supplied, artificial data is generated and used for instead
    of experimental data. If rng is supplied, the given rng is used for articficial data generation.

    Parameters
    ----------
    dict_of_params : dict
        keys: parameter name
        values: corresponding parameter value
    dict_of_responses : dict
        keys: response names
        values: corresponding response variance
        Only used for artificial data generation.
        If values are None, the defualt response variance value is utilized.
    dict_of_controls : dict
        keys: control names
        values: corresponding list of necessary information about controls with
        list = list([lower bound: float, upper bound: float, number of points: int])
    dict_of_artificial_controls : dict, optional
        dict_of_controls used for artificial data generation, by default None
    rng : np.random.Generator, optional
        rng used for artifical data generation, by default None

    Output
    ------
    inference_results : dict
        Contains all inference bounds related results
    exp_data : dict
        Contains experimental data OR generated artifical data
    sim_results : dict
        Contains data of the simulation needed for inference computation
        Content of same dimension as content of inference_results
    """
    import scipy.stats

    def convert_varlist_to_data_dictionary(
        var_list_list: list[VariableList],
        dict_of_responses: dict,
        dict_of_controls: dict,
    ):
        sim_data = {}
        for var_name in [*dict_of_controls.keys(), *dict_of_responses.keys()]:
            var_values = []
            for simulation in var_list_list:
                var = simulation[var_name]
                var_values.append(var.scale_to_original(var.value[0]))
            sim_data[var_name] = np.array(var_values)

        return sim_data

    def generate_simulation_data(
        template_varlist: VariableList,
        dict_of_params: dict,
        dict_of_responses: dict,
        dict_of_controls: dict,
        perturbate: bool,
        rng: np.random.Generator = None,
    ):

        for param, param_value in dict_of_params.items():
            template_varlist[param].value = param_value
        for key, variance in dict_of_responses.items():
            if variance is not None:
                template_varlist[key].variance = variance

        generated_var_lists, true_parameters, _ = (
            tools.generate_artificial_data_from_grid_nle(
                self.model,
                template_varlist,
                dict_of_controls,
                perturbate=perturbate,
                rng=rng,
                measurement_names=dict_of_responses.keys(),
                keep_in_bounds=True,
            )
        )

        for parameter in dict_of_params:
            for simulation in generated_var_lists:
                simulation[parameter].fixed = False

        sim_data = convert_varlist_to_data_dictionary(
            generated_var_lists, dict_of_controls, dict_of_responses
        )

        return generated_var_lists, sim_data

    if dict_of_artificial_controls is None:
        artificial_mode = False
        experimental_data = copy.deepcopy(self.list_input_varlist)
        exp_data = convert_varlist_to_data_dictionary(
            experimental_data,
            dict_of_responses,
            dict_of_controls,
        )
    else:
        artificial_mode = True
        experimental_data, exp_data = generate_simulation_data(
            copy.deepcopy(self.list_input_varlist[0]),
            dict_of_params,
            dict_of_responses,
            dict_of_artificial_controls,
            True,
            rng=rng,
        )

    variable_list_real, sim_data = generate_simulation_data(
        copy.deepcopy(self.list_input_varlist[0]),
        dict_of_params,
        dict_of_responses,
        dict_of_controls,
        False,
    )

    OLS = {}
    if artificial_mode:
        pe_artificial = ParameterEstimationNLE(self.model, experimental_data)
    else:
        pe_artificial = self

    residuals = pe_artificial.calculate_objective_and_residual(dict_of_params)[
        "residuals"
    ]
    OLS_values = np.diag(residuals.T @ residuals)
    OLS = dict(zip(self.names_of_measurements, OLS_values))

    jac = pe_artificial.calculate_sensitivity_and_fim(
        dict_of_params, list(dict_of_params.keys())
    )["jac_sorted"]

    pe_grid = ParameterEstimationNLE(self.model, variable_list_real)
    jac_grid = pe_grid.calculate_sensitivity_and_fim(
        dict_of_params, list(dict_of_params.keys())
    )["jac_sorted"]

    len_exp = len(experimental_data)
    len_param = len(dict_of_params)
    fisher95 = scipy.stats.f(len_param, self.dof).ppf(0.95)

    inference_results = {}
    for control in dict_of_controls:
        inference_results[control] = np.array(sim_data[control])

    for response in dict_of_responses:
        inference_results[response] = {}
        s = np.sqrt(OLS[response] / self.dof)
        R = np.linalg.qr(jac[response], mode="reduced")[1]
        bound = (
            s
            * np.linalg.norm(jac_grid[response] @ np.linalg.inv(R), axis=1)
            * np.sqrt(len_param * fisher95)
        )

        inference_results[response]["s"] = s
        inference_results[response]["R"] = R
        inference_results[response]["bound"] = bound
        inference_results[response]["lower bound"] = sim_data[response] - bound
        inference_results[response]["simulation"] = sim_data[response]
        inference_results[response]["upper bound"] = sim_data[response] + bound

    return inference_results, exp_data, sim_data