示例#1
0
    def setUp(self):
        """Setup simple problem"""
        np.random.seed(1)
        # This needs to work for different
        p = 50
        n = 200
        F = np.random.randn(n, p)
        D = np.diag(np.random.rand(n) * np.sqrt(p))
        Sigma = F.dot(F.T) + D
        gamma = 1.0
        mu = cp.Parameter(n, name='mu')
        x = cp.Variable(n)
        cost = -mu @ x + gamma * cp.quad_form(x, Sigma) + .5 * cp.norm(x, 1)
        constraints = [cp.sum(x) == 1, x >= 0]
        problem = cp.Problem(cp.Minimize(cost), constraints)

        # Define optimizer
        # Force mosek to be single threaded
        m = mlopt.Optimizer(problem,
                            #  log_level=logging.DEBUG,
                            )
        '''
        Sample points
        '''
        theta_bar = 10 * np.random.rand(n)
        radius = 0.8
        '''
        Train and solve
        '''

        # Training and testing data
        n_train = 100
        n_test = 10

        # Sample points from multivariate ball
        X_d = uniform_sphere_sample(theta_bar, radius, n=n_train)
        self.df_train = pd.DataFrame({'mu': list(X_d)})

        #  # Train and test using pytorch
        #  params = {
        #      'learning_rate': [0.01],
        #      'batch_size': [100],
        #      'n_epochs': [200]
        #  }
        #
        #  m.train(df, parallel=False, learner=mlopt.PYTORCH, params=params)

        # Testing data
        X_d_test = uniform_sphere_sample(theta_bar, radius, n=n_test)
        df_test = pd.DataFrame({'mu': list(X_d_test)})

        # Store stuff
        self.m = m
        self.df_test = df_test
示例#2
0
    def setUp(self):
        """Setup simple problem"""
        np.random.seed(1)
        # This needs to work for different
        p = 10
        n = 30
        F = np.random.randn(n, p)
        D = np.diag(np.random.rand(n) * np.sqrt(p))
        Sigma = F.dot(F.T) + D
        gamma = 1.0
        mu = cp.Parameter(n, name='mu')
        x = cp.Variable(n)
        cost = -mu @ x + gamma * cp.quad_form(x, Sigma) + .5 * cp.norm(x, 1)
        constraints = [cp.sum(x) == 1, x >= 0]
        problem = cp.Problem(cp.Minimize(cost), constraints)

        # Define optimizer
        m = mlopt.Optimizer(problem,
                            #  log_level=logging.DEBUG
                            )
        '''
        Sample points
        '''
        theta_bar = 10 * np.random.rand(n)
        radius = 1.0
        '''
        Train and solve
        '''

        # Training and testing data
        n_train = 300
        n_test = 10

        # Sample points from multivariate ball
        X_d = uniform_sphere_sample(theta_bar, radius, n=n_train)
        df = pd.DataFrame({'mu': list(X_d)})

        m.train(df,
                filter_strategies=True,
                parallel=False,
                learner=mlopt.PYTORCH,
                n_train_trials=10)
        #  m.train(df, parallel=False, learner=mlopt.XGBOOST, n_train_trials=10)

        # Testing data
        X_d_test = uniform_sphere_sample(theta_bar, radius, n=n_test)
        df_test = pd.DataFrame({'mu': list(X_d_test)})

        # Store stuff
        self.m = m
        self.df_test = df_test
示例#3
0
    def test_parallel_resolve(self):
        """Test parallel resolve (to avoid hanging)"""

        np.random.seed(1)
        # This needs to work for different
        p = 10
        n = p * 10
        F = np.random.randn(n, p)
        D = np.diag(np.random.rand(n) * np.sqrt(p))
        Sigma = F.dot(F.T) + D
        gamma = 1.0
        mu = cp.Parameter(n, name='mu')
        x = cp.Variable(n)
        cost = -mu @ x + gamma * cp.quad_form(x, Sigma)
        constraints = [cp.sum(x) == 1, x >= 0]

        # Define optimizer
        problem = cp.Problem(cp.Minimize(cost), constraints)
        m = Optimizer(problem, name="portfolio")
        '''
        Sample points
        '''
        theta_bar = np.random.randn(n)
        radius = 1.0
        '''
        Train and solve
        '''

        # Training and testing data
        n_train = 1000
        n_test = 1000
        # Sample points from multivariate ball
        X_d = uniform_sphere_sample(theta_bar, radius, n=n_train)
        X_d_test = uniform_sphere_sample(theta_bar, radius, n=n_test)
        df = pd.DataFrame({'mu': list(X_d)})
        df_test = pd.DataFrame({'mu': list(X_d_test)})

        # Train and test using pytorch
        m.train(df,
                parallel=True,
                filter_strategies=True,
                n_train_trials=10,
                learner=PYTORCH)
        m.performance(df_test, parallel=True)

        # Run parallel loop again to enforce instability
        # in multiprocessing
        m.performance(df_test, parallel=True)
示例#4
0
    def test_parallel_vs_serial_learning(self):
        """Test parallel VS serial learning"""

        # Generate data
        np.random.seed(1)
        T = 5
        M = 2.
        h = 1.
        c = 1.
        p = 1.
        x_init = 2.
        radius = 2.
        n_train = 1000  # Number of samples

        # Define problem
        x = cp.Variable(T + 1)
        u = cp.Variable(T)

        # Define parameter and sampling points
        d = cp.Parameter(T, nonneg=True, name="d")
        d_bar = 3. * np.ones(T)
        X_d = uniform_sphere_sample(d_bar, radius, n=n_train)
        df = pd.DataFrame({'d': list(X_d)})

        # Constaints
        constraints = [x[0] == x_init]
        for t in range(T):
            constraints += [x[t + 1] == x[t] + u[t] - d[t]]
        constraints += [u >= 0, u <= M]

        # Objective
        cost = cp.sum(cp.maximum(h * x, -p * x)) + c * cp.sum(u)

        # Define problem
        cvxpy_problem = cp.Problem(cp.Minimize(cost), constraints)
        problem = Problem(cvxpy_problem)

        # Solve for all theta in serial
        results_serial = problem.solve_parametric(df, parallel=False)

        # Solve for all theta in parallel
        results_parallel = problem.solve_parametric(df, parallel=True)

        # Assert all results match
        for i in range(n_train):
            serial = results_serial[i]
            parallel = results_parallel[i]

            # Compare x
            npt.assert_array_almost_equal(serial['x'],
                                          parallel['x'],
                                          decimal=TOL)
            # Compare cost
            npt.assert_array_almost_equal(serial['cost'],
                                          parallel['cost'],
                                          decimal=TOL)

            # Compare strategy
            self.assertTrue(serial['strategy'] == parallel['strategy'])
示例#5
0
def sampling_function(n):
    """
    Sample data points.
    """
    theta_bar = 3. * np.ones(5)

    # Sample points from multivariate ball
    X = uniform_sphere_sample(theta_bar, 1, n=n)

    df = pd.DataFrame({'d': list(X)})

    return df
def sample_portfolio(n, k, T=5, N=100):
    """Sample portfolio parameters."""

    rad = 0.01

    # mean values
    np.random.seed(0)
    SigmaF_FT_bar = np.random.randn(k, n)
    sqrt_D_bar = np.random.rand(n)
    #  Sigma_F_diag_bar = np.random.rand(k)
    hat_r_bar = np.random.rand(n)
    w_init_bar = np.random.rand(n)
    w_init_bar /= np.sum(w_init_bar)

    df = pd.DataFrame()
    for i in range(N):

        w_init = uniform_sphere_sample(w_init_bar, rad).flatten()
        w_init /= np.sum(w_init)

        SigmaF_FT = uniform_sphere_sample(SigmaF_FT_bar.flatten(),
                                          rad).reshape(k, n)

        x = pd.Series({
            #  "F": F,
            "sqrt_D":
            uniform_sphere_sample(sqrt_D_bar, rad).flatten(),
            "SigmaF_FT":
            SigmaF_FT,
            "w_init":
            w_init,
        })

        for t in range(1, T + 1):
            x["hat_r_%s" % str(t)] = uniform_sphere_sample(hat_r_bar,
                                                           rad).flatten()

        df = df.append(x, ignore_index=True)

    return df
示例#7
0
    def setUp(self):
        # Generate data
        np.random.seed(0)
        T = 5
        M = 2.
        h = 1.
        c = 1.
        p = 1.
        x_init = 2.
        self.radius = 2.
        n = 1000  # Number of points
        n_test = 100

        # Define problem
        x = cp.Variable(T + 1)
        u = cp.Variable(T)

        # Define parameter and sampling points
        d = cp.Parameter(T, nonneg=True, name="d")
        self.d_bar = 2. * np.ones(T)
        X_d = uniform_sphere_sample(self.d_bar, self.radius, n=n)
        X_d_test = uniform_sphere_sample(self.d_bar, self.radius, n=n_test)
        self.df = pd.DataFrame({'d': list(X_d)})
        self.df_test = pd.DataFrame({'d': list(X_d_test)})

        # Constaints
        constraints = [x[0] == x_init]
        for t in range(T):
            constraints += [x[t + 1] == x[t] + u[t] - d[t]]
        constraints += [u >= 0, u <= M]
        self.constraints = constraints

        # Objective
        self.cost = cp.sum(cp.maximum(
            h * x, -p * x)) + c * cp.sum_squares(u) + 0.001 * cp.sum_squares(x)

        # Define problem
        problem = cp.Problem(cp.Minimize(self.cost), self.constraints)
        self.optimizer = Optimizer(problem)