Cvxpy: How to add arithmetic mean and variance constraint?

Created on 23 Mar 2020  路  1Comment  路  Source: cvxgrp/cvxpy

The variance and arithmetic mean of affine function are both convex. There is no such API support for there two constraints. Is it possible to add variance and arithmetic mean in the constraint?

Most helpful comment

import cvxpy as cp

def mean(x):
    return cp.sum(x) / x.size

def variance(x, mode='unbiased'):
    if mode == 'unbiased':
        scale = x.size - 1
    elif mode == 'mle':
        scale = x.size
    else:
        raise ValueError('unknown mode: ' + str(mode))
    return cp.sum_squares(x - mean(x)) / scale

If someone wants to make a PR for these functions, I'd be happy to review it. Right now those functions only work on individual vectors. A proper implementation will handle axis options, and do more careful parsing of the mode string in the variance function (e.g. convert to lowercase).

Edit: x in those functions can be any affine expression.

>All comments

import cvxpy as cp

def mean(x):
    return cp.sum(x) / x.size

def variance(x, mode='unbiased'):
    if mode == 'unbiased':
        scale = x.size - 1
    elif mode == 'mle':
        scale = x.size
    else:
        raise ValueError('unknown mode: ' + str(mode))
    return cp.sum_squares(x - mean(x)) / scale

If someone wants to make a PR for these functions, I'd be happy to review it. Right now those functions only work on individual vectors. A proper implementation will handle axis options, and do more careful parsing of the mode string in the variance function (e.g. convert to lowercase).

Edit: x in those functions can be any affine expression.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aliirmak picture aliirmak  路  5Comments

rbf22 picture rbf22  路  7Comments

wfrece picture wfrece  路  9Comments

moehle picture moehle  路  5Comments

gbanjac picture gbanjac  路  9Comments