def binary_search(self, l, r, avg_daily_rets, C):
        N = len(self.syms)
        opt_params = self.get_opt_arrays(avg_daily_rets, C)

        frontier_returns = []
        frontier_risks = []
        sharperatios = [0, 0]

        mid = (l + r) / 2  #a peak-finding algorithm
        while l < r:
            for i in [0, 1]:  #find slope of frontier at mid and at mid+1
                opt_params[5][0, 0] = -(
                    mid + i
                ) * 0.00001  #how the QP encodes "find least-volatile at ___ return"
                print(
                    -opt_params[5][0, 0]
                )  #so I can see what the search is doing on the command line
                w, success = quadprog(*opt_params)
                if success:
                    x = np.sqrt(
                        w[0:N].T.dot(C.dot(w[0:N]))
                    )[0,
                      0]  #ugly because numpy is stupid. It's just w'Cw and rets'w
                    y = avg_daily_rets.T.dot(
                        w[0:N]
                    )[0,
                      0]  #w[0:N] contains the wieghts. The others are auxiliary to help with abs() in QP.
                    frontier_risks.append(x)
                    frontier_returns.append(y)
                    sharperatios[i] = y / x
                else:
                    sharperatios[
                        i] = -i  #so if I run off the upper end I get [0,-1], which tells it to bring right inward

            if sharperatios[0] > sharperatios[
                    1]:  #getting less steep (flattening)
                r = mid
            elif sharperatios[0] < sharperatios[
                    1]:  #getting more steep (still rising)
                l = mid + 1
            mid = (r + l) / 2

        #find best according to what mid is returned
        opt_params[5][0, 0] = -mid * 0.00001
        wstar, success = quadprog(*opt_params)  #should be successful

        frontier_risks.sort()
        frontier_returns.sort()
        opt = (np.sqrt(wstar[0:N].T.dot(C.dot(wstar[0:N])))[0, 0],
               avg_daily_rets.T.dot(wstar[0:N])[0, 0])

        return frontier_risks, frontier_returns, opt, wstar[0:N]
Exemple #2
0
    def fit(self, X, y):
        """
        @param X : an n-by-m array-like object containing n examples with m
                   features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        self._X = np.asmatrix(X)
        self._y = np.asmatrix(y).reshape((-1, 1))
        if self.scale_C:
            C = self.C / float(len(self._X))
        else:
            C = self.C

        K, H, f, A, b, lb, ub = self._setup_svm(self._X, self._y, C)

        # Solve QP
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)
Exemple #3
0
    def fit(self, X, y):
        """
        @param X : an n-by-m array-like object containing n examples with m
                   features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        self._X = np.asmatrix(X)
        self._y = np.asmatrix(y).reshape((-1, 1))
        if self.scale_C:
            C = self.C / float(len(self._X))
        else:
            C = self.C

        K, H, f, A, b, lb, ub = self._setup_svm(self._X, self._y, C)

        # Solve QP
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)
Exemple #4
0
def one_vs_all(hashes, dot_prod_x):	
	print "Computing dot_prod_matrix"
	full_mat = dot_prod_with_y(dot_prod_x, hashes)
	#print full_mat

	P = matrix(full_mat)
	q = matrix([-1.0 for item in range(len(hashes))])
	tmp_A = map(lambda r:r['y'] , hashes)
	#print tmp_A
	A = matrix(numpy.matrix(tmp_A))
	print "Calculating alphas"
	b = matrix([ 0.0 ])#for i in range(len(hashes))])

	#print qp(P, q, None, None, A, 0.0)

	# H, f, Aeq, beq, lb, ub
	alphas = quadprog.quadprog(full_mat, [-1.0 for item in range(len(hashes))],\
		A, b, matrix([0.0 for item in range(len(hashes))]), \
		matrix([1.0 for item in range(len(hashes))]))
	return alphas
Exemple #5
0
    def fit(self, bags, y):
        """
        @param bags : a sequence of n bags; each bag is an m-by-k array-like
                      object containing m instances with k features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        self._bags = map(np.asmatrix, bags)
        self._y = np.asmatrix(y).reshape((-1, 1))
        if self.scale_C:
            C = self.C / float(len(self._bags))
        else:
            C = self.C

        if self.verbose: print 'Setup QP...'
        K, H, f, A, b, lb, ub = self._setup_svm(self._bags, self._y, C)

        # Solve QP
        if self.verbose: print 'Solving QP...'
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)
Exemple #6
0
    def fit(self, bags, y):
        """
        @param bags : a sequence of n bags; each bag is an m-by-k array-like
                      object containing m instances with k features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        bs = BagSplitter(map(np.asmatrix, bags),
                         np.asmatrix(y).reshape((-1, 1)))
        self._bags = bs.neg_inst_as_bags + bs.pos_bags
        self._y = np.matrix(
            np.vstack([-np.ones((bs.L_n, 1)),
                       np.ones((bs.X_p, 1))]))
        if self.scale_C:
            iC = float(self.C) / bs.L_n
            bC = float(self.C) / bs.X_p
        else:
            iC = self.C
            bC = self.C
        C = np.vstack([iC * np.ones((bs.L_n, 1)), bC * np.ones((bs.X_p, 1))])

        if self.verbose:
            print 'Setup QP...'
        K, H, f, A, b, lb, ub = self._setup_svm(self._bags, self._y, C)

        # Adjust f with balancing terms
        factors = np.vstack([
            np.matrix(np.ones((bs.L_n, 1))),
            np.matrix([2.0 / len(bag) - 1.0 for bag in bs.pos_bags]).T
        ])
        f = np.multiply(f, factors)

        if self.verbose:
            print 'Solving QP...'
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)

        # Recompute predictions for full bags
        self._bag_predictions = self.predict(bags)
Exemple #7
0
    def fit(self, bags, y):
        """
        @param bags : a sequence of n bags; each bag is an m-by-k array-like
                      object containing m instances with k features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        bs = BagSplitter(map(np.asmatrix, bags),
                         np.asmatrix(y).reshape((-1, 1)))
        self._bags = bs.neg_inst_as_bags + bs.pos_bags
        self._y = np.matrix(np.vstack([-np.ones((bs.L_n, 1)),
                                       np.ones((bs.X_p, 1))]))
        if self.scale_C:
            iC = float(self.C) / bs.L_n
            bC = float(self.C) / bs.X_p
        else:
            iC = self.C
            bC = self.C
        C = np.vstack([iC * np.ones((bs.L_n, 1)),
                       bC * np.ones((bs.X_p, 1))])
																							
        if self.verbose:
            print 'Setup QP...'
        K, H, f, A, b, lb, ub = self._setup_svm(self._bags, self._y, C)

        # Adjust f with balancing terms
        factors = np.vstack([np.matrix(np.ones((bs.L_n, 1))),
                             np.matrix([2.0 / len(bag) - 1.0
                                        for bag in bs.pos_bags]).T])
        f = np.multiply(f, factors)

        if self.verbose:
            print 'Solving QP...'
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)

        # Recompute predictions for full bags
        self._bag_predictions = self.predict(bags)
        return self
Exemple #8
0
    def fit(self, bags, y):
        """
        @param bags : a sequence of n bags; each bag is an m-by-k array-like
                      object containing m instances with k features
        @param y : an array-like object of length n containing -1/+1 labels
        """
        self._bags = map(np.asmatrix, bags)
        self._y = np.asmatrix(y).reshape((-1, 1))
        if self.scale_C:
            C = self.C / float(len(self._bags))
        else:
            C = self.C

        if self.verbose:
            print 'Setup QP...'
        K, H, f, A, b, lb, ub = self._setup_svm(self._bags, self._y, C)

        # Solve QP
        if self.verbose:
            print 'Solving QP...'
        self._alphas, self._objective = quadprog(H, f, A, b, lb, ub,
                                                 self.verbose)
        self._compute_separator(K)
Exemple #9
0
def FRPY(SER, s, s2, s3, RPopts):

    # Reshape the s data to be a matrix (v. a vector)
    s2 = s2.reshape(-1, 1).copy()

    auxflag = True  # I don't see an option for this to not be true in the MATLAB code so I'm hard-coding it

    # Create copies of input parameters / data
    weightfactor = RPopts['weightfactor']
    weightparam = RPopts['weightparam']
    if RPopts['weight']:
        bigweight = RPopts['weight']
    TOLE = RPopts['TOLE']
    TOL = RPopts['TOLGD']

    SERA = SER['poles'].reshape(-1, 1).copy()
    SERC = SER['R'].copy()
    SERD = SER['D'].copy()
    SERE = SER['E'].copy()

    m = SERA.shape[0]
    n = SERA.shape[1]

    if m < n:
        SERA = SERA.T

    if not np.any(RPopts.get('H')):
        RPopts['H'] = []
        RPopts['oldDflag'] = -1
        RPopts['oldEflag'] = -1

    # This is used for checking Eq. (4), (5) in [6]
    Dflag, VD, eigD, invVD = create_eig_n(SERD)
    Eflag, VE, eigE, invVE = create_eig_n(SERE)

    SERCnew = SERC
    SERDnew = SERD
    SEREnew = SERE

    N = SERA.shape[0]

    Ns = s.shape[0]
    Ns2 = s2.shape[0]
    Ns3 = s3.shape[0]
    Nc = SERD.shape[0]
    Nc2 = Nc * Nc

    # LS problem:
    # Finding out which poles are complex:
    cindex = find_which_poles_are_complex(N, np.diagflat(SERA))

    # Build Asys as seen in Eq. (9a) in [6]
    if not np.any(RPopts.get('H')):
        if RPopts['outputlevel'] == OutputLevel.max:
            print("Building system equation (once)")

        # This sets the size of bigA based on whether D and E must be perturbed
        if Dflag + Eflag == 2:
            bigA = np.zeros((Ns * Nc2, Nc * (N + 2)), dtype=complex)
        elif Dflag + Eflag == 1:
            bigA = np.zeros((Ns * Nc2, Nc * (N + 1)), dtype=complex)
        else:
            bigA = np.zeros((Ns * Nc2, Nc * N), dtype=complex)

        # Setting up some of the matrices
        bigV = np.zeros((Nc, Nc * N))
        biginvV = np.zeros((Nc, Nc * N))
        bigD = np.zeros((Nc, N))

        for m in range(N):
            R = SERC[:, :, m]
            # Change the residues to real based on which type of pole it is
            if cindex[m] == PoleTypes.REAL:
                R = R
            elif cindex[m] == PoleTypes.COMPLEX_FIRST:
                R = R.real
            else:
                R = R.imag
            D, V = LA.eig(
                R
            )  # The eigenvalues / vectors differ from Octave but both should be valid
            D, V = sort_eigenvalues_eigenvectors_to_match_matlab(D, V)

            bigV[:Nc, m * Nc:(m + 1) * Nc] = V.real.copy()
            biginvV[:Nc,
                    m * Nc:(m + 1) * Nc] = np.linalg.matrix_power(V.real, -1)
            bigD[:, m] = D.real

        for k in range(Ns):
            sk = s[k].copy()

            # Calculating matrix Mmat (coefficients for LS-problem)
            Yfit = fitcalcPRE(sk.reshape(1, -1), SERA, SERC, SERD, SERE)

            if weightparam:
                weight = calculate_weight(weightparam, Nc, Yfit)
            else:
                weight = bigweight[:, :, k]

            bigA, bigA2 = fill_in_bigA(N, bigV, Nc, sk, cindex, SERA, weight,
                                       Dflag, Eflag, VD, invVD, VE, invVE,
                                       bigA, np.array([]), k, Nc2)

        # Introducing Samples Outside LS Region: One Sample per Pole (s4)
        s4 = np.array([])
        for m in range(N):
            if cindex[m] == PoleTypes.REAL:
                if (np.abs(SERA[m]) > s[Ns - 1] / 1j) or (np.abs(SERA[m]) <
                                                          s[0] / 1j):
                    s4 = np.append(s4, 1j * np.abs(SERA[m]))
            elif cindex[m] == PoleTypes.COMPLEX_FIRST:
                if (np.abs(SERA[m].imag) > s[Ns - 1] / 1j) or (np.abs(
                        SERA[m].imag) < s[0] / 1j):
                    s4 = np.append(s4, 1j * np.abs(SERA[m].imag))
        Ns4 = s4.shape[0]

        bigA2 = np.zeros((Ns4 * Nc2, Nc * (N + Dflag + Eflag)), dtype=complex)

        for k in range(Ns4):
            sk = s4[k]
            # Calculating matrix Mmat (coefficients for LS-problem)
            tell = 0
            offs = 0
            Yfit = fitcalcPRE(sk.reshape(1, -1), SERA, SERC, SERD, SERE)

            weight = calculate_weight(weightparam, Nc, Yfit)
            weight = weight * weightfactor

            bigA, bigA2 = fill_in_bigA(N, bigV, Nc, sk, cindex, SERA, weight,
                                       Dflag, Eflag, VD, invVD, VE, invVE,
                                       bigA, bigA2, k, Nc2)

        bigA = np.vstack((bigA, bigA2))
        bigA = np.vstack((bigA.real, bigA.imag))
        Acol = bigA.shape[1]
        Escale = np.zeros(Acol)
        for col in range(Acol):
            Escale[col] = LA.svd(bigA[:, col].reshape(-1, 1))[1]
            bigA[:, col] = bigA[:, col] / Escale[col]

        H = bigA.T @ bigA
        RPopts['H'] = H
        RPopts['Escale'] = Escale
        RPopts['bigV'] = bigV
        RPopts['biginvV'] = biginvV
        if RPopts['outputlevel'] == OutputLevel.max:
            print("Done")
    else:
        bigV = RPopts['bigV']
        biginvV = RPopts['biginvV']
        if Dflag != RPopts['oldDflag'] or Eflag != RPopts['oldEflag']:
            RPopts['H'] = RPopts['H'][:Nc * (N + Dflag + Eflag), :Nc *
                                      (N + Dflag + Eflag)]
            RPopts['Escale'] = RPopts['Escale'][:Nc * (N + Dflag + Eflag)]

    viol_G = np.array([])
    viol_D = np.array([])
    viol_E = np.array([])

    bigB = np.empty((0, Nc * (N + Dflag + Eflag)))
    bigc = np.array([])

    # Loop for constraint problem, Type #1 (violating eigenvalues in s2):
    viol_G, bigB, bigc = generate_violG(Nc, SERA, SERC, SERD, SERE, N, Ns2, s2,
                                        bigV, biginvV, TOL, cindex, Dflag,
                                        Eflag, VD, invVD, viol_G, Nc2, bigB,
                                        bigc)

    # Loop for Constraint Problem, Type #2 (all eigenvalues in s3)
    viol_G, bigB, bigc = generate_violG(Nc, SERA, SERC, SERD, SERE, N, Ns3, s3,
                                        bigV, biginvV, TOL, cindex, Dflag,
                                        Eflag, VD, invVD, viol_G, Nc2, bigB,
                                        bigc)

    # Adds constraint for the event D < 0
    if Dflag == 1:
        for n in range(Nc):
            dum = np.zeros((1, Nc * (N + Dflag + Eflag)))
            dum[0, Nc * N + n] = 1
            bigB = np.vstack((bigB, dum))
            bigc = np.append(bigc, np.diag(eigD)[n] - TOL)
            viol_G = np.append(viol_G, np.diag(eigD)[n])
            viol_D = np.append(viol_D, np.diag(eigD)[n])

    # Adds constraint for event E < 0
    if Eflag == 1:
        for n in range(Nc):
            dum = np.zeros((1, Nc * (N + Dflag + Eflag)))
            dum[0, Nc * (N + Dflag) + n] = 1
            bigB = np.vstack((bigB, dum))
            bigc = np.append(bigc, eigE[n] - TOLE)
            viol_E = np.append(viol_E, eigE[n])

    if bigB.shape[0] == 0:
        return SER, RPopts  # No passivity violations

    bigB = bigB.real
    for col in range(RPopts['H'].shape[0]):
        if bigB.shape[0] > 0:
            bigB[:, col] = bigB[:, col] / RPopts['Escale'][col]

    ff = np.zeros(RPopts['H'].shape[0])

    print("Starting quadprog...")

    # Solve Eq. (9a), (9b) in [6]
    dx = quadprog(RPopts['H'], ff, -bigB, bigc.real)
    dx = dx / RPopts['Escale'].T

    # New state space model created (see Eq. (8a), (8b), (8c) in [6])
    for m in range(N):
        if cindex[m] == PoleTypes.REAL:
            D1 = np.diagflat(dx[m * Nc:(m + 1) * Nc].copy())
            SERCnew[:, :,
                    m] = SERC[:, :, m] + bigV[:, m * Nc:(m + 1) *
                                              Nc] @ D1 @ biginvV[:, m * Nc:
                                                                 (m + 1) * Nc]
        elif cindex[m] == PoleTypes.COMPLEX_FIRST:
            GAMM1 = bigV[:, m * Nc:(m + 1) * Nc].copy()
            GAMM2 = bigV[:, (m + 1) * Nc:(m + 2) * Nc].copy()
            invGAMM1 = biginvV[:, m * Nc:(m + 1) * Nc].copy()
            invGAMM2 = biginvV[:, (m + 1) * Nc:(m + 2) * Nc].copy()

            D1 = np.diagflat(dx[m * Nc:(m + 1) * Nc]).copy()
            D2 = np.diagflat(dx[(m + 1) * Nc:(m + 2) * Nc]).copy()
            R1 = SERC[:, :, m].real.copy()
            R2 = SERC[:, :, m].imag.copy()
            R1new = R1 + GAMM1 @ D1 @ invGAMM1
            R2new = R2 + GAMM2 @ D2 @ invGAMM2
            SERCnew[:, :, m] = R1new + 1j * R2new
            SERCnew[:, :, m + 1] = R1new - 1j * R2new

    if Dflag == 1:
        DD = np.diagflat(dx[N * Nc:(N + 1) * Nc].copy())
        SERDnew = SERDnew + VD @ DD @ invVD

    if Eflag == 1:
        EE = np.diagflat(dx[(N + Dflag) * Nc:(N + Dflag + Eflag + 1) *
                            Nc].copy())
        SEREnew = SEREnew + VE @ EE @ invVE

    SERDnew = (SERDnew + SERDnew.T) / 2
    SEREnew = (SEREnew + SEREnew.T) / 2
    for m in range(N):
        SERCnew[:, :, m] = (SERCnew[:, :, m] + SERCnew[:, :, m].T) / 2
    SER['R'] = SERCnew.copy()
    SER['D'] = SERDnew.copy()
    SER['E'] = SEREnew.copy()
    SER = pr2ss(SER)

    RPopts['oldDflag'] = Dflag
    RPopts['oldEflag'] = Eflag
    return SER, RPopts
Exemple #10
0
s2_Z = array([[1]])
s2_jointXZ = r_[r_['-1', s2_X, s_XZ], r_['-1', s_XZ.T,
                                         s2_Z]]  # joint covariance

n_ = m_X.shape[0]  # target dimension
k_ = m_Z.shape[0]  # number of factors
i_n = eye(n_)
i_k = eye(k_)

# set inputs for quadratic programming problem
d = np.diagflat(1 / diag(s2_X))
pos = d @ s_XZ
g = -pos.flatten()
q = kron(s2_Z, d)

# set bound constraints
lb = 0.8 * ones((n_ * k_, 1))
ub = 1.2 * ones((n_ * k_, 1))

# compute optimal loadings
b = quadprog(q, g, None, None, lb, ub)

beta = reshape(b, (n_, k_), 'F')
alpha = m_X - beta @ m_Z

# joint distribution of residulas U and factor Z
m = r_[r_['-1', i_n, -beta], r_['-1', zeros((k_, n_)), i_k]]

m_jointUZ = m @ m_jointXZ - r_[alpha, zeros((k_, 1))]  # joint expectation
s2_jointUZ = m @ s2_jointXZ @ m.T  # joint covariance
Exemple #11
0
Operating conditions necessary {UTF-8/CrLf/Python2.7/numpy/matlot/Scipy}
@author: Hisashi Ikari
"""

# ===========================================================================================
# Function Name
#   Descrption
# ===========================================================================================
# *** Arguments ***
#   Name:Description
# ===========================================================================================
import numpy as np
import quadprog as qp

K = [[10.0, 5.0], [3.0, 4.0]]
L = [[1.0, -1.0]]

N = len(K)

f = []
Aeq = np.matrix(L).T
beq = [[1, 0]]
A = []
b = []
LB = np.zeros((N, 1))
UB = np.ones((N, 1)) / (0.3 * N)

alpha = qp.quadprog(K, f, Aeq, beq, LB, UB)

print alpha
Exemple #12
0
km = zeros((k_ * n_, k_ * n_))  # commutation matrix
for n in range(n_):
    for k in range(k_):
        km = km + kron(i_k[:, [k]] @ i_n[:, [n]].T,
                       i_n[:, [n]] @ i_k[:, [k]].T)

# set inputs for quadratic programming problem
invsigma2 = np.diagflat(1 / diag(s2_X))
pos = beta.T @ invsigma2 @ s2_X
g = -pos.flatten()
q = kron(s2_X, beta.T @ invsigma2 @ beta)
q_, _ = q.shape

# linear constraints
v = array([[-1, 1]])
d_eq = kron(i_k, v @ s2_X) @ km
b_eq = zeros((k_**2, 1))

# compute extraction matrix
# options = optimoptions(('quadprog','MaxIter.T, 2000, .TAlgorithm','interior-point-convex'))
c = quadprog(q, g, d_eq, b_eq)

gamma = reshape(c, (k_, n_), 'F')
alpha = (i_n - beta @ gamma) @ m_X

# joint distribution of residulas U and factor Z
m = r_[i_n - beta @ gamma, gamma]

m_jointUZ = m @ m_X - r_[alpha, zeros((k_, 1))]
s2_jointUZ = m @ s2_X @ m.T
Exemple #13
0
# +
d = np.diagflat(1 / diag(s2_X))
pos = d @ s_XZ
g = -pos.flatten()
q = kron(s2_Z, d)
q_, _ = q.shape

# set constraints
a_eq = ones((1, n_ * k_)) / (n_ * k_)
b_eq = array([[1]])
lb = 0.8 * ones((n_ * k_, 1))
ub = 1.2 * ones((n_ * k_, 1))

# compute optimal loadings
b = quadprog(q, g, a_eq, b_eq, lb, ub)
b = np.array(b)

beta = reshape(b, (n_, k_), 'F')
alpha = m_XZ[:n_] - beta @ m_XZ[n_:n_ + k_]
# -

# ## Residuals analysis
# ## compute statistics of the joint distribution of residuals and factors

# +
m = r_[r_['-1', I_n, -beta], r_['-1', zeros((k_, n_)), I_k]]
m_UZ = m @ m_XZ - r_[alpha, zeros((k_, 1))]  # joint expectation
s2_UZ = m @ s2_XZ @ m.T  # joint covariance

# compute correlation matrix