Optimise the model parameters.
499781
def optimise(args, **kwargs):
    

    expected_output_files = kwargs.pop("expected_output_files", None)
    if not expected_output_files:
        expected_output_files = ["optimised.pkl"]
        if args.plotting:
            expected_output_files.extend([
                "projection-estimate.{}".format(args.plot_format),
                "projection-optimised.{}".format(args.plot_format)
            ])
        
    # Estimate the model parameters, unless they are already specified.
    model = sick.models.Model(args.model)
    initial_theta = model._configuration.get("initial_theta", {})
    if len(set(model.parameters).difference(initial_theta)) == 0:
        model, data, metadata = _pre_solving(args, expected_output_files)

    else:
        model, data, metadata, initial_theta = estimate(args, 
            expected_output_files=expected_output_files, __return_result=True)

    try:
        theta, chisq, dof, model_fluxes = model.optimise(data, 
            initial_theta=initial_theta, full_output=True, debug=args.debug)

    except:
        logger.exception("Failed to optimise model parameters")
        raise

    metadata["optimised"] = {
        "theta": theta,
        "chi_sq": chisq,
        "dof": dof,
        "r_chi_sq": chisq/dof
    }

    logger.info("Optimised model parameters are:")
    _announce_theta(theta)
    logger.info("With a chi-sq value of {0:.1f} (reduced {1:.1f}; DOF {2:.1f})"\
        .format(chisq, chisq/dof, dof))

    if args.plotting:
        fig = sick.plot.spectrum(data, model_flux=model_fluxes)
        filename = _prefix(args, "projection-optimised.{}".format(
            args.plot_format))
        fig.savefig(filename)
        logger.info("Created figure {}".format(filename))

    if kwargs.pop("__return_result", False):
        return (model, data, metadata, theta)

    # Write the results to file.
    _write_output(_prefix(args, "optimised.pkl"), metadata)
    return None
1260612
def setup_parameters(self):
        """"""
        t = self.template
        parameters = self.get_parameter_definitions()

        if not parameters:
            logger.debug("No parameters defined.")
            return

        for name, attrs in parameters.items():
            p = build_parameter(name, attrs)
            t.add_parameter(p)
921213
def _update_params(self, constants):
        """"""
        constants = np.max(np.min(constants, 1))
        self.params['r']['value'] = max([self.params['r']['value'],
                                         constants])
        epsilon = constants / self.params['r']['value']
        influence = self._calculate_influence(epsilon)
        # Account for learning rate
        return influence * epsilon
65089
public function optimize()
    {
        $path = $this->getCachePath();

        $packages = $this->getPackages();

        $this->writeCache($path, $packages);
    }
646641
def setParams(self, estimator=None, estimatorParamMaps=None, evaluator=None, trainRatio=0.75,
                  parallelism=1, collectSubModels=False, seed=None):
        
        kwargs = self._input_kwargs
        return self._set(**kwargs)
388735
def parametrize(self):
        
        """
        for element in self._chain:
            if not element.is_reader and not element._estimated:
                element.estimate(element.data_producer, stride=self.param_stride, chunksize=self.chunksize)

        self._estimated = True
937066
def optimizeTraitByTrait(self, G, verbose=False, n_times=10, factr=1e3):
        
        assert self.nullST is not None, 'fit null model beforehand'
        RV = {}
        self.infoOptST = {}
        for p in range(self.P):
            trait_id = self.traitID[p]
            self.stSet.Y = self.Y[:, p:p+1]
            self.stSet.setNull(self.nullST[trait_id])
            RV[trait_id] = self.stSet.optimize(G, n_times=n_times, factr=factr, verbose=verbose)
            self.infoOptST[trait_id] = self.stSet.getInfoOpt()
        return RV
742545
def optimize(self):
        """"""

        def te(weights, r, proxies):
            """Helper func.  `pyfinance.tracking_error` doesn't work here."""
            if isinstance(weights, list):
                weights = np.array(weights)
            proxy = np.sum(proxies * weights, axis=1)
            te = np.std(proxy - r)  # not anlzd...
            return te

        ew = utils.equal_weights(n=self.n, sumto=self.sumto)
        bnds = tuple((0, 1) for x in range(self.n))
        cons = {"type": "eq", "fun": lambda x: np.sum(x) - self.sumto}

        xs = []
        funs = []
        for i, j in zip(self._r, self._proxies):
            opt = sco.minimize(
                te,
                x0=ew,
                args=(i, j),
                method="SLSQP",
                bounds=bnds,
                constraints=cons,
            )
            x, fun = opt["x"], opt["fun"]
            xs.append(x)
            funs.append(fun)
        self._xs = np.array(xs)
        self._funs = np.array(funs)

        return self
333530
def _adapt_params(self, stepsize, stepsize_bar, h_bar, mu, index_i, alpha, n_alpha=1):
        
        gamma = 0.05  # free parameter that controls the amount of shrinkage towards mu
        t0 = 10.0  # free parameter that stabilizes the initial iterations of the algorithm
        kappa = 0.75
        # See equation (6) section 3.2.1 for details

        estimate = 1.0 / (index_i + t0)
        h_bar = (1 - estimate) * h_bar + estimate * (self.delta - alpha / n_alpha)

        stepsize = np.exp(mu - sqrt(index_i) / gamma * h_bar)
        i_kappa = index_i ** (-kappa)
        stepsize_bar = np.exp(i_kappa * np.log(stepsize) + (1 - i_kappa) * np.log(stepsize_bar))

        return stepsize, stepsize_bar, h_bar
186294
def _create_optimizer_node(self):
        """"""
        self.lr_var = tf.Variable(0.0, trainable=False)
        tvars = tf.trainable_variables()
        grads, _ = tf.clip_by_global_norm(tf.gradients(self.cost, tvars),
                                          self.max_grad_norm)
        optimizer = tf.train.GradientDescentOptimizer(self.lr_var)
        self._train_op = optimizer.apply_gradients(zip(grads, tvars))
401565
def _set_parameters(self, parameters):
        """"""
        for theta in self.variables['theta']:
            val = 1.0
            # TODO: improve approach for initializing parameters.
            if parameters is not None:
                if theta.name in parameters:
                    val = parameters[theta.name]
            # Add parameter.
            
            self.link_parameters(Param(theta.name, val, None))
404965
def optimize(self, angles0, target):
        """"""
        def new_objective(angles):
            a = angles - angles0
            if isinstance(self.smooth_factor, (np.ndarray, list)):
                if len(a) == len(self.smooth_factor):
                    return (self.f(angles, target) +
                            np.sum(self.smooth_factor * np.power(a, 2)))
                else:
                    raise ValueError('len(smooth_factor) != number of joints')
            else:
                return (self.f(angles, target) +
                        self.smooth_factor * np.sum(np.power(a, 2)))

        return scipy.optimize.minimize(
            new_objective,
            angles0,
            **self.optimizer_opt).x
389700
def params(self, dict):
        """"""
        self._configuration.update(dict)
        self._measurements.update()
475319
def initial_guesses(self):
        
        return np.array([param.value for param in self.model.params])
1369997
def optimize(self, f=None, df=None, f_df=None, duplicate_manager=None):
        
        self.f = f
        self.df = df
        self.f_df = f_df

        ## --- Update the optimizer, in case context has beee passed.
        self.optimizer = choose_optimizer(self.optimizer_name, self.context_manager.noncontext_bounds)

        ## --- Selecting the anchor points and removing duplicates
        if self.type_anchor_points_logic == max_objective_anchor_points_logic:
            anchor_points_generator = ObjectiveAnchorPointsGenerator(self.space, random_design_type, f)
        elif self.type_anchor_points_logic == thompson_sampling_anchor_points_logic:
            anchor_points_generator = ThompsonSamplingAnchorPointsGenerator(self.space, sobol_design_type, self.model)

        ## -- Select the anchor points (with context)
        anchor_points = anchor_points_generator.get(duplicate_manager=duplicate_manager, context_manager=self.context_manager)

        ## --- Applying local optimizers at the anchor points and update bounds of the optimizer (according to the context)
        optimized_points = [apply_optimizer(self.optimizer, a, f=f, df=None, f_df=f_df, duplicate_manager=duplicate_manager, context_manager=self.context_manager, space = self.space) for a in anchor_points]
        x_min, fx_min = min(optimized_points, key=lambda t:t[1])

        #x_min, fx_min = min([apply_optimizer(self.optimizer, a, f=f, df=None, f_df=f_df, duplicate_manager=duplicate_manager, context_manager=self.context_manager, space = self.space) for a in anchor_points], key=lambda t:t[1])

        return x_min, fx_min
188347
def set_parameters(self):
        
        for key in dir(self):
            if key.startswith('REQUIRES_'):
                setattr(self, key, getattr(self.gmpe, key))
            if key.startswith('DEFINED_'):
                if not key.endswith('FOR_INTENSITY_MEASURE_TYPES'):
                    setattr(self, key, getattr(self.gmpe, key))
320546
def run_models(self):
        

        self.linear_regression()
        self.lasso_regression()
        self.ridge_regression()
        self.elastic_net_regression()
        self.random_forest()
        self.ann()

        # Index of the model with max score
        best_model_index = self.max_scores.index(max(self.max_scores))

        # Store name of the optimal model
        self.best_model_name = self.model_names[best_model_index]

        # Store optimal model
        self.best_model = self.models[best_model_index]

        return self.metrics