Exemplo n.º 1
0
def get_model_estimate(walks: List[List[int]], base_guess: float) -> Tuple[List[float], str]:
    """
    Uses the Akaike information criterion
    https://en.wikipedia.org/wiki/Akaike_information_criterion#Modification_for_small_sample_size
    to get the optimal model.
    AIC = 2k - 2ln(L)
    opt.minimize(negative_log_likelihood_params, guess, method='Nelder-Mead', args=(model, walks)) returns directly
    - ln(L)
    :param walks:
    :param base_guess:
    :return:
    """
    result = [ERROR_VALUE, ERROR_VALUE]
    current_model = ERROR_VALUE
    min_akaike = sys.float_info.max

    # single lambda models
    guess = np.repeat(base_guess, 2)
    bounds = opt.Bounds((0, 0), (1, 1), keep_feasible=True)
    model = 'success_punished'
    min_akaike, result, current_model = find_akaike(guess, model, walks, result, current_model, min_akaike, bounds)

    model = 'success_rewarded'
    min_akaike, result, current_model = find_akaike(guess, model, walks, result, current_model, min_akaike, bounds)

    # two lambdas models
    guess = np.repeat(base_guess, 3)
    bounds = opt.Bounds((0, 0, 0), (1, 1, 1), keep_feasible=True)
    model = 'success_punished_two_lambdas'
    min_akaike, result, current_model = find_akaike(guess, model, walks, result, current_model, min_akaike, bounds)

    model = 'success_rewarded_two_lambdas'
    min_akaike, result, current_model = find_akaike(guess, model, walks, result, current_model, min_akaike, bounds)

    return result, current_model
Exemplo n.º 2
0
def updateReviewParams(review,params,modelParams,termvec):
    # maximization to find phi
    K = len(modelParams['gamma'])
    wn = np.where(termvec>0)[0] # nonzero elements
    gamma = modelParams['gamma']
    phi = params['phi']
    newphi = phi
    bounds = opt.Bounds([1e-10 for i in range(K)],[1 for i in range(K)])
    constraints = opt.LinearConstraint([1 for i in range(K)], 1, 1)
    for idx, j in enumerate(wn):
        x0 = phi[:,idx]
        x0 = [max(val,1e-10) for val in x0]
        x0 = [min(val,1) for val in x0]
        res = opt.minimize(
                        lambda x: -phifunction(x,idx,wn,review,modelParams,params, termvec), x0,
                           bounds = bounds, constraints = constraints)
        newphi[:,idx] = res.x

    params['phi']=newphi
    
    # update eta
    neweta = gamma
    for idx, n in enumerate(wn):
        neweta = neweta + phi[:,idx]
    params['eta']=neweta
    
    # update lambda
    lamb = params['lamb']
    beta = modelParams['beta']
    sbar = np.zeros(K)
    sVar = np.zeros(K)
    for i in range(K):
        for idx, j in enumerate(wn):
            sbar[i] = sbar[i] + termvec[j]*beta[i,j]*phi[i,idx]
            sVar[i] = sVar[i] + termvec[j]*(beta[i,j]**2)*phi[i,idx]*(1-phi[i,idx])
    bounds = opt.Bounds([0 for i in range(K)],[1 for i in range(K)])
    x0 = lamb
    x0 = [max(val,0) for val in x0]
    x0 = [min(val,1) for val in x0]
    res = opt.minimize(
                lambda x: lambdafunction(x,review,modelParams,params, termvec),
                            x0, bounds = bounds, constraints = constraints
                        )
    newlamb= res.x
    params['lamb']=newlamb
    
    # update sigma
    delta2 = modelParams['delta2']
    SIG = modelParams['SIG']
    SIGinv = np.linalg.inv(SIG)
    newsigma = np.zeros(params['sigma'].shape)
    for i in range(K):
        newsigma[i] = delta2/(sVar[i]+sbar[i]**2+delta2/SIGinv[i,i])
    
    reviewParams = {'eta':neweta,'phi':newphi,'lamb':newlamb,'sigma':newsigma}
    return reviewParams
Exemplo n.º 3
0
def get_bounds(xc_type='PBE0'):
    '''Prodive the bounds to optimize the DF-like cost function'''
    if xc_type == 'PBE0':
        bounds = optimize.Bounds([-1.0, -1.0, -1.0], [1.0, 1.0, 1.0])
    elif xc_type == 'B3LYP':
        bounds = optimize.Bounds([-1.0, .0, .0, .0, .0], [1.0, 1., 1., 1., 1.])
    elif xc_type == 'CAMB3LYP':
        bounds = optimize.Bounds([0.0, .0000001, .0, .0, .0, .0, .0],
                                 [1., 1.0, 1., 1., 1., 1., 1.])
    elif xc_type == 'RSH-PBE0':
        bounds = optimize.Bounds([-1., -1.0], [1., 1.0])
    return bounds
Exemplo n.º 4
0
def risk_parity(df_cov):

    assets = df_cov.index
    cov = df_cov.values
    n = len(assets)
    w0 = np.repeat(1 / n, n)
    A = np.repeat(1, n)[np.newaxis, :]
    lb = np.array([1])
    ub = np.array([1])
    constraints = opt.LinearConstraint(A=A, lb=lb, ub=ub)
    bounds = opt.Bounds(ub=np.repeat(np.inf, n), lb=np.repeat(0, n))

    def target(w):
        target = 0
        p_variance = w.T.dot(cov).dot(w)
        for i in range(n):
            denominator = cov[i, :].dot(w) * n
            target += (w[i] - p_variance / denominator)**2
        return target

    res = opt.minimize(target,
                       w0,
                       method='SLSQP',
                       constraints=constraints,
                       bounds=bounds)

    return pd.Series(res.x, index=assets)
Exemplo n.º 5
0
    def optimize(self, graph, demands, initial):
        options = {
            'maxiter': self.max_iters,
            'ftol': self.threshold
        }

        initial = initial if initial is not None else np.zeros(shape=(graph.number_of_edges(),), dtype=float)
        bounds = optimize.Bounds(lb=0, ub=np.inf)
        constraint = self._constraint(graph, demands, as_dict=True)

        flows_per_iter = []

        def callback(x):
            flows_per_iter.append(x)
            return False

        result = optimize.minimize(fun=self.cost_fn,
                                   x0=initial,
                                   bounds=bounds,
                                   method='SLSQP',
                                   jac='2-point',
                                   constraints=[constraint],
                                   callback=callback,
                                   options=options)

        return np.array(flows_per_iter), result
Exemplo n.º 6
0
    def optimize(self, graph, demands, initial=None):
        options = {
            'maxiter': self.max_iters,
            'factorization_method': 'SVDFactorization'
        }

        initial = initial if initial is not None else np.zeros(shape=(graph.number_of_edges(),), dtype=float)
        bounds = optimize.Bounds(lb=0, ub=np.inf)
        constraint = self._constraint(graph, demands)

        flows_per_iter = []

        def callback(x, state):
            flows_per_iter.append(x)
            return False

        hess = optimize.BFGS()
        result = optimize.minimize(fun=self.cost_fn,
                                   x0=initial,
                                   callback=callback,
                                   bounds=bounds,
                                   method='trust-constr',
                                   constraints=[constraint],
                                   jac='2-point',
                                   hess=hess,
                                   options=options)

        return np.array(flows_per_iter), result
Exemplo n.º 7
0
def trim():
    initial_guess = [0.02, -2]
    angle_bound = optimize.Bounds(-30, 30)
    result = optimize.minimize(trimlift,
                               initial_guess,
                               method='POWELL',
                               bounds=angle_bound,
                               options={
                                   'ftol': 0.001,
                                   'xtol': 0.00001
                               })
    print(result.success)
    angles = result.x
    print(angles)
    alpha_t = angles[0] * Q_("rad")
    de_t = angles[1] * Q_("deg")
    Cltrimw, Cdtrimw, Cmtrimw, xcp = lookup_data(alpha_t, 0.3, 0.)
    Cltrimh, Cdtrimh, Cmtrimh, xcp = lookup_data(alpha_t, 0.5, de_t)
    q_dp = 0.5 * rho * V**2
    Cn_h = -Cltrimh * np.cos(alpha_t) - Cdtrimh * np.sin(alpha_t)
    Cn_w = -Cltrimw * np.cos(alpha_t) - Cdtrimh * np.sin(alpha_t)
    L_h = q_dp * -Cn_h * S_h
    L_w = -Cn_w * S_w * q_dp
    F_z = W * np.cos(alpha_t) + L_w + L_h
    Treq = (np.cos(alpha_t) *
            (Cdtrimw * S_w + Cdtrimh * S_h) + np.sin(alpha_t) *
            (Cltrimw * S_w + Cltrimh * S_h)) * q_dp
    return alpha_t, de_t, Treq
Exemplo n.º 8
0
def fit(x, y, start_point, end_point):
    """ function that does the fitting

    :param x:
    :param y:
    :param start_point:
    :param end_point:
    :return:
    """
    # initial guess is just a point in the middle of the arc
    initial_guess = np.asarray([x[1], y[1]])

    # goal is to find the third point that determines the arc
    # the third point much be located in the bounding box of all provided points with a buffer of 20 pixels (?)
    lb = np.asarray([np.min(x), np.min(y)]) - 20
    ub = np.asarray([np.max(x), np.max(y)]) + 20
    bounds = optimize.Bounds(lb, ub)
    third_point = optimize.minimize(fun=cost,
                                    x0=initial_guess,
                                    args=(x, y, start_point, end_point),
                                    bounds=bounds)
    third_point = third_point.x
    xc, yc, radius = find_circle(start_point[0], start_point[1],
                                 third_point[0], third_point[1], end_point[0],
                                 end_point[1])
    return xc, yc, radius
Exemplo n.º 9
0
def XYvT(J, hpj=1):
    Temp = np.linspace(0, 10, 201)
    rranges = (slice(0, 1, 0.01), slice(0, 0.5, 0.01))

    con = opt.LinearConstraint([[1, 0], [-1, 1], [1, 1]],
                               [0, -np.inf, -np.inf], [1, 0, 1])
    bound = opt.Bounds([0, 0], [1, 0.5])

    mX, mY = [], []

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    for Tpj in Temp:
        fun = lambda x: F(J, hpj * abs(J), x[0], x[1], Tpj * abs(J))
        res = opt.minimize(fun, [0.5, 0.25],
                           method='trust-constr',
                           constraints=con,
                           bounds=bound)
        mX.append(res.x[0])
        mY.append(res.x[1])
        working = "Calculating T/J:" + str(Tpj)
        print(working)

    ax.plot(mX, mY, Temp, label='min')
    ax.set_xlabel('Composition: X')
    ax.set_ylabel('Bond Frequency: Y')
    ax.set_zlabel('Temperature (T/J)')

    plt.show()
Exemplo n.º 10
0
    def __init__(self,
                 game,
                 dist_measures=None,
                 dist_tensor=None,
                 messages=None,
                 epsilon=1e-4):
        """
        Parameters
        ----------

        dist_measures: A list of integers with the distortions from
        self.dist_tensor to be considered
        """
        RDT.__init__(self, game, dist_tensor, epsilon)
        self.states = len(self.pmf)
        if dist_measures:
            self.dist_measures = dist_measures
        else:
            self.dist_measures = range(self.dist_tensor.shape[0])
        if messages:
            self.messages = messages
        else:
            self.messages = self.states
        self.enc_dec_length = self.messages * (self.states + self.outcomes)
        # length of the encoder_decoder vector we optimize over.
        self.hess = opt.BFGS(exception_strategy='skip_update')
        self.bounds = opt.Bounds(0, 1)
        self.constraint = self.lin_constraint(self.dist_measures)
        self.default_enc_dec_init = self.enc_dec_init()
def PlumleeEstimates(ydata, numsamples, A, sens, spec, rglrWt=0.1):
    ydata = np.array(ydata)
    numsamples = np.array(numsamples)
    beta0 = -6 * np.ones(A.shape[1] + A.shape[0])

    def invlogit_INTERIOR(beta):
        return np.exp(beta) / (np.exp(beta) + 1)

    def mynegloglik_INTERIOR(beta, ydata, numsamples, A, sens, spec):
        betaI = beta[0:A.shape[1]]
        betaJ = beta[A.shape[1]:]
        probs = (1 - invlogit_INTERIOR(betaJ)) * np.matmul(
            A, invlogit_INTERIOR(betaI)) + invlogit_INTERIOR(betaJ)
        probsz = probs * sens + (1 - probs) * (1 - spec)
        return -np.sum(ydata * np.log(probsz) + (numsamples-ydata) * np.log(1-probsz)) \
            + rglrWt*np.sum(np.abs((betaJ - beta0[A.shape[1]:]))) #have to regularize to prevent problems

    bds = spo.Bounds(beta0 - 8, beta0 + 8)
    opval = spo.minimize(mynegloglik_INTERIOR,
                         beta0 + 1,
                         args=(ydata, numsamples, A, sens, spec),
                         method='L-BFGS-B',
                         options={'disp': False},
                         bounds=bds)
    return invlogit_INTERIOR(opval.x)[0:A.shape[1]], invlogit_INTERIOR(
        opval.x)[A.shape[1]:]
def PlumleeEstimates(ydata, nsamp, A, sens, spec):
    beta0 = -5 * np.ones(A.shape[1] + A.shape[0])

    def invlogit(beta):
        return np.exp(beta) / (np.exp(beta) + 1)

    def logit(p):
        return np.log(p / (1 - p))

    def mynegloglik(beta, ydata, nsamp, A, sens, spec):
        betaI = beta[0:A.shape[1]]
        betaJ = beta[A.shape[1]:]
        probs = (1 - invlogit(betaJ)) * np.array(
            A @ invlogit(betaI)) + invlogit(betaJ)
        probsz = probs * sens + (1 - probs) * (1 - spec)
        return -np.sum(ydata * np.log(probsz) + (nsamp-ydata) * np.log(1-probsz)) \
            + 4 * 1/2*np.sum(np.abs((betaJ - beta0[A.shape[1]:]))) #have to regularize to prevent problems

    bounds = spo.Bounds(beta0 - 2, beta0 + 8)
    opval = spo.minimize(mynegloglik,
                         beta0 + 5,
                         args=(ydata, nsamp, A, sens, spec),
                         method='L-BFGS-B',
                         options={'disp': False},
                         bounds=bounds)
    return invlogit(opval.x)[0:A.shape[1]], invlogit(opval.x)[A.shape[1]:]
Exemplo n.º 13
0
 def __init__(self, num_features, lambda_min=-6.0, lambda_max=6.0, alpha=0.1,
              D=None, method="BFGS", max_splits=10, num_lambdas=40, last_argmin=True,
              prior_weights=None, one_se_rule=False, verbose=True, nonnegative=False):
     if D is None:
         self.D = stew.utils.create_diff_matrix(num_features=num_features)
     else:
         self.D = D
     self.method = method
     if prior_weights is None:
         self.start_weights = np.random.normal(loc=0, scale=0.1, size=num_features)
     else:
         self.start_weights = prior_weights
     self.max_splits = max_splits
     self.num_lambdas = num_lambdas
     self.lambda_min = lambda_min
     self.lambda_max = lambda_max
     self.lambdas = np.insert(np.logspace(self.lambda_min, self.lambda_max, num=self.num_lambdas-1), 0, 0.0)
     self.last_argmin = last_argmin
     self.verbose = verbose
     self.alpha = alpha
     self.one_se_rule = one_se_rule
     self.nonnegative = nonnegative
     if self.nonnegative:
         # self.bounds = optim.Bounds(np.repeat(-0.1, num_features), np.repeat(np.inf, num_features))
         self.bounds = optim.Bounds(np.repeat(0, num_features), np.repeat(np.inf, num_features))
         self.method = "L-BFGS-B"
Exemplo n.º 14
0
def scipy(fcm_weights, agg_weights, const, func):
    flat_weights = np.concatenate(
        (fcm_weights.flatten(), agg_weights.flatten()), axis=None)

    bounds = optimize.Bounds(-np.ones(flat_weights.shape),
                             np.ones(flat_weights.shape))
    nonlinc = optimize.NonlinearConstraint(func, 0, 0)

    res = optimize.minimize(
        func,
        flat_weights,
        method='trust-constr',
        bounds=bounds,
        # constraints=nonlinc,
        options={
            'disp': True,
            'maxiter': 300,
            'xtol': 1e-10
        })
    # res = optimize.minimize(func, flat_weights, method='trust-constr', constraints=nonlinc, options={'disp': True}, bounds=bnds)

    n, m = const

    err = func(flat_weights)

    fcm_weights = np.reshape(res.x[:n * n], (n, n))
    agg_weights = np.reshape(res.x[n * n:], (m, n))

    return fcm_weights, agg_weights, err
def example_4():
    # Demonstrate syntactic usage of scipy global optimizers:
    k_initial = np.mean((k_min, k_max))
    # model_initial_poor = np.array([35, Vp_c/k_initial])

    Vp = [Vp_c]
    rho = [rho_c]
    fixed_args = (flux_comp, mantle_props, Vp, rho, FLUX_WINDOW)
    H_min, H_max = (25.0, 45.0)
    bounds = optimize.Bounds(np.array([H_min, Vp_c / k_max]),
                             np.array([H_max, Vp_c / k_min]))

    # - Basin hopping
    # logging.info('Trying basinhopping...')
    # soln_bh = optimize.basinhopping(objective_fn, model_initial_poor, T=0.5, stepsize=1.0,
    #                                 minimizer_kwargs={'args': fixed_args, 'bounds': bounds})
    # logging.info('Result:\n{}'.format(soln_bh))

    # - Differential evolution
    logging.info('Trying differential_evolution...')
    soln_de = optimize.differential_evolution(objective_fn_wrapper,
                                              bounds,
                                              fixed_args,
                                              workers=-1,
                                              popsize=25,
                                              tol=1.0e-3,
                                              mutation=(0.5, 1.2),
                                              recombination=0.5)
    logging.info('Result:\n{}'.format(soln_de))
Exemplo n.º 16
0
def SigmaStruct(nom, unc):
    nOut, nIn = nom.shape[:2]

    def unpack(x, nOut, nIn):
        lenX = nOut * nIn

        r = np.reshape(x[:lenX], (nOut, nIn))
        ph = np.reshape(x[lenX:], (nOut, nIn))
        c = r * np.exp(-1j * ph)

        return c

    def optCostFunc(x, nom, unc):
        nOut, nIn = nom.shape

        c = unpack(x, nOut, nIn)

        p = nom + c * unc

        sv = np.linalg.svd(p, full_matrices=True, compute_uv=False)
        svMin = np.min(sv, axis=0)
        return svMin

    crit = np.zeros_like(nom, dtype=complex)
    svMin = np.zeros(nom.shape[-1])
    for iFreq in range(nom.shape[-1]):
        # Initial Guess as nominal SVD transform
        uNom, svNom, vhNom = np.linalg.svd(nom[..., iFreq],
                                           full_matrices=True,
                                           compute_uv=True)

        c0 = uNom.conjugate().T @ np.eye(nOut) @ vhNom.conjugate().T
        optInitX = [np.abs(c0), np.arctan2(c0.real, c0.imag)]

        # Setup Optimization
        dimX = nOut * nIn
        optBnds = optimize.Bounds(lb=[-1] * dimX + [-np.inf] * dimX,
                                  ub=[1] * dimX +
                                  [np.inf] * dimX)  # lb <= x <= ub
        optArgs = (nom[..., iFreq], unc[..., iFreq])
        # optMethod = 'SLSQP' # Slower than L-BFGS-B
        optMethod = 'L-BFGS-B'
        optOptions = {}
        optOptions['disp'] = True

        # Solve
        optRes = optimize.minimize(optCostFunc,
                                   optInitX,
                                   args=optArgs,
                                   method=optMethod,
                                   bounds=optBnds,
                                   options=optOptions)

        # Store solution
        svMin[iFreq] = optRes.fun

        c = unpack(optRes.x, nOut, nIn)
        crit[..., iFreq] = nom[..., iFreq] + c * unc[..., iFreq]

    return svMin, crit
def example_6():
    # Example 6: Using custom MCMC solver on single-layer model.
    k_initial = np.mean((k_min, k_max))
    model_initial_poor = np.array([34, Vp_c / k_initial])

    Vp = [Vp_c]
    rho = [rho_c]
    fixed_args = (flux_comp, mantle_props, Vp, rho, FLUX_WINDOW)
    H_min, H_max = (25.0, 50.0)
    bounds = optimize.Bounds(np.array([H_min, Vp_c / k_max]),
                             np.array([H_max, Vp_c / k_min]))

    # - Custom MCMC solver
    logging.info('Trying custom MCMC solver...')
    soln_mcmc = optimize_minimize_mhmcmc_cluster(objective_fn_wrapper,
                                                 bounds,
                                                 fixed_args,
                                                 x0=model_initial_poor,
                                                 T=0.025,
                                                 burnin=500,
                                                 maxiter=5000,
                                                 collect_samples=2000,
                                                 logger=logging)
    logging.info('Result:\n{}'.format(soln_mcmc))

    # Run grid search purely for visualization purposes
    H_space = np.linspace(H_min, H_max, 51)
    k_space = np.linspace(k_min, k_max, 51)
    H, k, Esu = flux_comp.grid_search(mantle_props,
                                      [LayerProps(Vp_c, None, rho_c, None)],
                                      0,
                                      H_space,
                                      k_space,
                                      flux_window=FLUX_WINDOW,
                                      ncpus=-3)

    def overlay_mcmc(axes):
        x = soln_mcmc.x.copy()
        for i, _x in enumerate(x):
            color = '#13f50e'
            cluster = soln_mcmc.clusters[i]
            axes.scatter(Vp_c / cluster[:, 1],
                         cluster[:, 0],
                         c=color,
                         s=5,
                         alpha=0.3)
            axes.scatter(_x[0], _x[1], marker='x', s=100, c=color, alpha=0.9)
            axes.scatter(_x[0],
                         _x[1],
                         marker='o',
                         s=160,
                         facecolors='none',
                         edgecolors=color,
                         alpha=0.9,
                         linewidth=2)
        # end for

    # end func

    plot_Esu_space(H, k, Esu, decorator=overlay_mcmc)
def example_3():
    # Example 3: Using a global energy minimization solver to find solution.

    logging.info(
        "Computing optimal crust properties by SU flux minimization...")

    # Use single layer (crust only) model in this example.
    Vp = [Vp_c]
    rho = [rho_c]
    fixed_args = (flux_comp, mantle_props, Vp, rho, FLUX_WINDOW)
    H_initial = 40.0
    k_initial = np.mean((k_min, k_max))
    model_initial = np.array([H_initial, Vp_c / k_initial])
    H_min, H_max = (25.0, 45.0)
    bounds = optimize.Bounds([H_min, Vp_c / k_max], [H_max, Vp_c / k_min])

    # Find local minimum relative to initial guess.
    soln = optimize.minimize(objective_fn_wrapper,
                             model_initial,
                             fixed_args,
                             bounds=bounds)
    H_crust, Vs_crust = soln.x
    logging.info(
        'Success = {}, Iterations = {}, Function evaluations = {}'.format(
            soln.success, soln.nit, soln.nfev))
    logging.info('Solution H_crust = {}, Vs_crust = {}, SU energy = {}'.format(
        H_crust, Vs_crust, soln.fun))
Exemplo n.º 19
0
def param_bounds(var_bounds, var_names):
    """Pack group-level parameters."""

    group_lb = [var_bounds[k][0] for k in [*var_names]]
    group_ub = [var_bounds[k][1] for k in [*var_names]]
    bounds = optim.Bounds(group_lb, group_ub)
    return bounds
def example_5():
    # Example 5: Adding a sedimentary layer and directly using global minimizer

    # Assumed sediment property constants
    Vp_s = 2.1
    rho_s = 1.97

    Vp = [Vp_s, Vp_c]
    rho = [rho_s, rho_c]
    fixed_args = (flux_comp, mantle_props, Vp, rho, FLUX_WINDOW)
    H_initial = [1.0, 35.0]  # sediment, crust
    Vs_initial = [0.8, 3.4]  # sediment, crust
    # model_initial_sed = np.array(zip(H_initial, Vs_initial))
    H_sed_min, H_sed_max = (0, 3.5)
    Vs_sed_min, Vs_sed_max = (0.3, 2.5)
    H_cru_min, H_cru_max = (20.0, 60.0)
    Vs_cru_min, Vs_cru_max = (Vp_c / k_max, Vp_c / k_min)
    bounds = optimize.Bounds([H_sed_min, Vs_sed_min, H_cru_min, Vs_cru_min],
                             [H_sed_max, Vs_sed_max, H_cru_max, Vs_cru_max])

    logging.info('Differential_evolution (sedimentary)...')
    soln_de = optimize.differential_evolution(objective_fn_wrapper,
                                              bounds,
                                              fixed_args,
                                              workers=-1,
                                              popsize=25,
                                              tol=1.0e-3,
                                              mutation=(0.5, 1.2),
                                              recombination=0.5)
    logging.info('Result:\n{}'.format(soln_de))
Exemplo n.º 21
0
def subj_bounds(var_bounds, group_vars, subj_vars, n_subj):
    """
    Set boundaries of group and subject parameters.

    Parameters
    ----------
    var_bounds : dict of (str: tuple of float)
        Lower and upper bounds of each parameter.

    group_vars : list of str
        Names of parameters included as group parameters.

    subj_vars : list of str
        Names of parameters included as subject parameters.

    n_subj : int
        Number of subjects.

    Returns
    -------
    bounds : scipy.optimize.Bounds
        Bounds object for use with scipy optimization functions.
    """
    group_lb = [var_bounds[k][0] for k in [*group_vars]]
    group_ub = [var_bounds[k][1] for k in [*group_vars]]

    subj_lb = np.hstack(
        [np.tile(var_bounds[k][0], n_subj) for k in [*subj_vars]])
    subj_ub = np.hstack(
        [np.tile(var_bounds[k][1], n_subj) for k in [*subj_vars]])
    bounds = optim.Bounds(np.hstack((group_lb, subj_lb)),
                          np.hstack((group_ub, subj_ub)))
    return bounds
Exemplo n.º 22
0
    def _calculate_risk_allocation(self, date, active_markets):

        # get window length and raw prices
        window = self.window_length

        # prepare price window, cut for active markets and dates in date window
        current_date_loc = self.returns.index.get_loc(date)
        first_date = self.returns.index[current_date_loc -
                                        window]  # window + 1 observiations

        # calculate returns from window
        returns = self.returns.loc[first_date:date]
        n_assets = returns.shape[1]

        # calculate covariance
        cov_mat = self.calculate_covariance(returns)  # pca_cleaning

        # Ignore correlations < 0
        cov_mat[cov_mat < 0] = 0
        budget = np.array(np.ones(
            (n_assets, 1)) * 1 / n_assets)  # numpy matrix nAssets x 1

        wgts = np.ones((n_assets, 1)) * 1 / n_assets
        cons = ({'type': 'eq', 'fun': lambda wgts: sum(wgts) - 1})

        def rb_function(wgts, cov_mat, budget, n_assets):
            cov_mat = np.array(cov_mat)
            wgts = wgts.reshape(n_assets, 1)
            RC = cov_mat.dot(
                wgts)  # vector of risk contributions: np.array nAssets x 1
            TR = wgts.T.dot(cov_mat).dot(wgts)  # 1x1 np.array

            value_vector = np.power(np.multiply(wgts, RC) / TR - budget, 2)
            function_value = np.sum(value_vector)

            return function_value

        #todo: different bounds input... check optimize bounds
        res = optimize.minimize(rb_function,
                                wgts,
                                args=(cov_mat, budget, n_assets),
                                method='SLSQP',
                                bounds=optimize.Bounds(self.lower_bound,
                                                       self.upper_bound),
                                constraints=cons,
                                options={
                                    'disp': False,
                                    'maxiter': 1000,
                                    'ftol': 1e-6
                                })

        result = res.x

        # Funky results with huge weights occur a couple of times throughout the history
        result[result > 1.0] = 0.0
        result[result < 0.0] = 0.0

        # Set weight for markets where all returns are nan to 0. cc all zero

        return result
Exemplo n.º 23
0
def p1_optimal(
        p1=100,
        p2=100,
        alpha=[1, 0, 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1, 0., 0., 0.],
        n_clients_per_class=[50, 20, 10, 5]):
    '''
    Inputs :
        ------------
        
    Output :
        p1 : the best prize found for the first item
        
    '''
    bounds_p1 = so.Bounds(0, np.inf)  # p1 prize of first item positive

    x0 = [p1]

    gradient_obj_fun = grad(obj_fun)

    res = so.minimize(obj_fun,
                      x0=x0,
                      args=(p2, alpha, n_clients_per_class),
                      jac=gradient_obj_fun,
                      bounds=bounds_p1)

    x_sol = res.x

    return np.round(x_sol[0])
Exemplo n.º 24
0
    def fit_GH_h4(x, success=False):
        his_data = np.histogram(x, bins=30)
        optim = so.minimize(err,
                            x0=(1, 0, 200),
                            args=((his_data[1][1:] + his_data[1][:-1]) / 2,
                                  his_data[0]),
                            bounds=so.Bounds([0, -np.inf, 0.1],
                                             [np.inf, np.inf, np.inf]))

        optim_gh = so.minimize(
            err_gh,
            x0=(0, 0),
            args=((his_data[1][1:] + his_data[1][:-1]) / 2, his_data[0],
                  optim['x'][0], optim['x'][1], optim['x'][2]),
            bounds=so.Bounds([-0.5, -0.5],
                             [0.5, 0.5]))  #print(optim['success'])
        return (optim_gh['x'][1])
Exemplo n.º 25
0
def get_parameters_estimate(model_type: str, walks: List[List[int]], base_guess: float) -> Union[List[float], np.array]:
    if model_type == 'success_punished' or model_type == 'success_rewarded':
        guess = np.repeat(base_guess, 2)
        bounds = opt.Bounds((0, 0), (1, 1), keep_feasible=True)
    elif model_type == 'success_punished_two_lambdas' or model_type == 'success_rewarded_two_lambdas':
        guess = np.repeat(base_guess, 3)
        bounds = opt.Bounds((0, 0, 0), (1, 1, 1), keep_feasible=True)
    else:
        raise Exception(f'Unexpected walk type: {model_type}')

    opt_result = opt.minimize(negative_log_likelihood_params, guess, method=OPTIMIZATION_ALGORITHM,
                              args=(model_type, walks))
    if opt_result.success:
        logging.debug("Fitted successfully.")
        return opt_result.x
    else:
        return np.repeat(ERROR_VALUE, len(guess))
def load_boundaries(N, ind):
    #Introduce boundaries for the optimization, set diag zero
    lb = -np.inf * np.ones([N, N, ind + 1])
    [np.fill_diagonal(lb[:, :, l], 0) for l in range(0, lb.shape[2])]
    ub = np.inf * np.ones([N, N, ind + 1])
    [np.fill_diagonal(ub[:, :, l], 0) for l in range(0, ub.shape[2])]
    bnds = optimize.Bounds(lb.flatten(), ub.flatten())
    return bnds
Exemplo n.º 27
0
    def _acquire_point(self, acq_func, acq_func_params, n_iter):
        """
        Choose next point to evaluate based on acquistion function.
        Maximizes the acquisition function to do so.
        Parameters:
            acq_func: acquisition function to use. Defaults to EI
                (expected improvement).
            acq_func_params: tuple of the parameters for the acquisition function
                other than a point, the gpr, and the current optimum
            n_iter: maximum number of times to try optimizing acq_func
                using minimize
        """
        #Expected improvement acquisition function
        max_acq = None
        # Multiply acq_func by -1 so minimize works
        if acq_func_params is None:
            min_acq_func = lambda x, gpr, opt: -acq_func(x, gpr, opt)
        else:
            min_acq_func = lambda x, gpr, opt, params: -acq_func(x, gpr, opt, params)
        for i in range(n_iter):
            seed = uniform(self._bounds[0, :], self._bounds[1, :]).reshape(1, -1)

            # Check if acq_func_params is None, otherwise pass it to the acq_func as
            # param in the minimization
            if acq_func_params is None:
                result = minimize(min_acq_func, seed, method='L-BFGS-B',
                                    bounds=optimize.Bounds(np.array(self._bounds[0, :]),
                                                           np.array(self._bounds[1, :])),
                                    args=(self._gpr, self._current_opt[1]))
            else:
                result = minimize(min_acq_func, seed, method='L-BFGS-B',
                                    bounds=optimize.Bounds(np.array(self._bounds[0, :]),
                                                           np.array(self._bounds[1, :])),
                                    args=(self._gpr, self._current_opt[1], *acq_func_params))
            # Check if optimization was successful, break if it was
            if result.success:
                if max_acq is None or -result.fun >= max_acq:
                    next_eval_point = result.x
                    max_acq = -result.fun
                    break
        # Check if any of the optimizations were successful, if not choose a random point
        if max_acq is None:
            next_eval_point=uniform(self._bounds[0, :], self._bounds[1, :])

        return next_eval_point
Exemplo n.º 28
0
def create_bounds(values, feasible, missing_intervals):
    max_value = np.nanmax(values)
    min_value = np.nanmin(values)
    upper_bounds = np.copy(values)
    lower_bounds = np.copy(values)
    for interval in missing_intervals:
        upper_bounds[interval[0]:interval[1] + 1] = max_value
        lower_bounds[interval[0]:interval[1] + 1] = min_value
    return optimize.Bounds(lower_bounds, upper_bounds, feasible)
Exemplo n.º 29
0
 def solveJacTransposed(self, joints, target):
     limits = self.getJointsLimits()
     cost_func = lambda x : np.linalg.norm(self.computeMGD(x) - target, 2)
     jac_func = lambda x : - 2 * (self.computeJacobian(x).transpose() @
                                  (target - self.computeMGD(x)))
     res = optimize.minimize(cost_func, joints,
                             jac= jac_func,
                             bounds = optimize.Bounds(limits[:,0], limits[:,1]))
     return res.x
 def optimize(self, fun, x0, agents, N, bk):
     # write your optimization call using scipy.optimize.minimize
     n = x0.shape[0]
     # Define the bounds of the variables in our case the limits of the actions variables
     bounds = opt.Bounds(np.ones(n) * (-self.ul), np.ones(n) * self.ul)
     # minimize the cost function. Note that I added the as arguments the extra variables needed for the function.
     res = opt.minimize(fun, x0, args=(agents, N, bk), method=self.method, jac=self.jac, hess=self.hess, bounds=bounds)
     #  options={'verbose': 1})
     return res