alnurali / cvxstoc

Disciplined convex stochastic programming. For the cvxstoc home page, please see:
http://alnurali.github.io/cvxstoc/
31 stars 11 forks source link

Using a the same random variable twice is understood as two different random variables #1

Open benjaminbiegel opened 8 years ago

benjaminbiegel commented 8 years ago

I have experienced what I think is a bug: If you declare a random variable and use it two places, cvxstoc will see it as two different random variables which is not what I was expecting.

The example below gives different results depending if you set obj2 = obj2a and if you set obj2 = obj2b although the results should be the same.

Best :) Ben

import cvxpy as cvx
import cvxstoc
import numpy as np
import matplotlib.pyplot as plt

price2_delta = np.array([-1, 1])
price2_probs = np.ones(np.alen(price2_delta))/np.alen(price2_delta)
price2_delta_rv = cvxstoc.RandomVariableFactory().create_categorical_rv(price2_delta.tolist(), price2_probs.tolist())
print price2_delta_rv._val_map

price2 = price2_delta_rv

p1 = cvx.Variable()
p2 = cvx.Variable()
buy2 = cvx.NonNegative()
sell2 = cvx.NonNegative()

pmax = 1
pmin = -1

# Second stage problem
obj2a = (buy2 - sell2) * price2
obj2b = buy2 * price2 - sell2 * price2
obj2 = obj2b

constr2 = [p2 - p1 == sell2 - buy2,
           p2 >= pmin,
           p2 <= pmax,
           sell2 <= 100,
           buy2 <= 100]

problem2 = cvx.Problem(cvx.Minimize(obj2), constr2)
Q = cvx.partial_optimize(problem2, [p2, buy2, sell2], [p1])

# First stage problem
obj1 = cvxstoc.expectation(Q, want_de=True)
constr1 = [p1 >= pmin,
           p1 <= pmax]

problem1 = cvx.Problem(cvx.Minimize(obj1), constr1)
problem1.solve(verbose=False)

print problem1.status

print -obj1.value
alnurali commented 8 years ago

Hey -- thanks for reporting the issue. I'll do my best to take a look at this over the next few days ...

On Tue, Feb 9, 2016 at 5:08 AM, benjaminbiegel notifications@github.com wrote:

I have experienced what I think is a bug: If you declare a random variable and use it two places, cvxstoc will see it as two different random variables which is not what I was expecting.

The example below gives different results depending if you set obj2 = obj2a and if you set obj2 = obj2b although the results should be the same.

Best :) Ben

import cvxpy as cvx import cvxstoc import numpy as np import matplotlib.pyplot as plt

price2_delta = np.array([-1, 1]) price2_probs = np.ones(np.alen(price2_delta))/np.alen(price2_delta) price2_delta_rv = cvxstoc.RandomVariableFactory().create_categorical_rv(price2_delta.tolist(), price2_probs.tolist()) print price2_delta_rv._val_map

price2 = price2_delta_rv

p1 = cvx.Variable() p2 = cvx.Variable() buy2 = cvx.NonNegative() sell2 = cvx.NonNegative()

pmax = 1 pmin = -1

Second stage problem

obj2a = (buy2 - sell2) * price2 obj2b = buy2 * price2 - sell2 * price2 obj2 = obj2b

constr2 = [p2 - p1 == sell2 - buy2, p2 >= pmin, p2 <= pmax, sell2 <= 100, buy2 <= 100]

problem2 = cvx.Problem(cvx.Minimize(obj2), constr2) Q = cvx.partial_optimize(problem2, [p2, buy2, sell2], [p1])

First stage problem

obj1 = cvxstoc.expectation(Q, want_de=True) constr1 = [p1 >= pmin, p1 <= pmax]

problem1 = cvx.Problem(cvx.Minimize(obj1), constr1) problem1.solve(verbose=False)

print problem1.status

print -obj1.value

— Reply to this email directly or view it on GitHub https://github.com/alnurali/cvxstoc/issues/1.

alnur

alnurali commented 8 years ago

If I'm not mistaken, the bug goes away if you don't use "want_de=True" (the deterministic equivalent mode in general is buggy in places, and not really fully-supported unfortunately).

Are you able to do that? Just replace "want_de=True" w/ something like "num_samples=100".

Can you confirm the issue resolves for you in this case?

On Tue, Feb 9, 2016 at 11:27 PM, Alnur Ali alnurali@cmu.edu wrote:

Hey -- thanks for reporting the issue. I'll do my best to take a look at this over the next few days ...

On Tue, Feb 9, 2016 at 5:08 AM, benjaminbiegel notifications@github.com wrote:

I have experienced what I think is a bug: If you declare a random variable and use it two places, cvxstoc will see it as two different random variables which is not what I was expecting.

The example below gives different results depending if you set obj2 = obj2a and if you set obj2 = obj2b although the results should be the same.

Best :) Ben

import cvxpy as cvx import cvxstoc import numpy as np import matplotlib.pyplot as plt

price2_delta = np.array([-1, 1]) price2_probs = np.ones(np.alen(price2_delta))/np.alen(price2_delta) price2_delta_rv = cvxstoc.RandomVariableFactory().create_categorical_rv(price2_delta.tolist(), price2_probs.tolist()) print price2_delta_rv._val_map

price2 = price2_delta_rv

p1 = cvx.Variable() p2 = cvx.Variable() buy2 = cvx.NonNegative() sell2 = cvx.NonNegative()

pmax = 1 pmin = -1

Second stage problem

obj2a = (buy2 - sell2) * price2 obj2b = buy2 * price2 - sell2 * price2 obj2 = obj2b

constr2 = [p2 - p1 == sell2 - buy2, p2 >= pmin, p2 <= pmax, sell2 <= 100, buy2 <= 100]

problem2 = cvx.Problem(cvx.Minimize(obj2), constr2) Q = cvx.partial_optimize(problem2, [p2, buy2, sell2], [p1])

First stage problem

obj1 = cvxstoc.expectation(Q, want_de=True) constr1 = [p1 >= pmin, p1 <= pmax]

problem1 = cvx.Problem(cvx.Minimize(obj1), constr1) problem1.solve(verbose=False)

print problem1.status

print -obj1.value

— Reply to this email directly or view it on GitHub https://github.com/alnurali/cvxstoc/issues/1.

alnur

alnur