Infinity objective value given by CVXPY on a convex program - cvxpy

I am solving a convex problem using cvxpy. The constraints are rather simple, there are 3 variables, but we could eliminate one. The objective is convex and involves the entropy and logarithm. The solution is correct, in the sense that the variables have the expected values. Howver the objective value should be around -1.06, but it is infinite. Is there a bug with evaluating involved expressions ?
#!/usr/bin/env python3
import cvxpy as cx
import numpy as np
from math import log
def entr(x):
return -x * log(x)
def check_obj(a, b, c):
return -entr(2.0) + -2.0 * log(2.0) + -entr(1.0 + a) + -1.0 + a * log(2.0) + -entr(2.0 + a) -2.0 + a * log(1.0) -entr(1.0 + a + b + c) + -1.0 + a + b + c * log(2.0) + -entr(2.0) + -2.0 * log(2.0) + -entr(1.0 + b) -1.0 + b * log(2.0) + -entr(2.0 + b) + -2.0 + b * log(1.0) -entr(1.0 + b + a + c) -1.0 + b + a + c * log(2.0)
a = cx.Variable(name='a')
b = cx.Variable(name='b')
c = cx.Variable(name='c')
obj = -cx.entr(2.0) + -2.0 * cx.log(2.0) + -cx.entr(1.0 + a) + -1.0 + a * cx.log(2.0) + -cx.entr(2.0 + a) -2.0 + a * cx.log(1.0) -cx.entr(1.0 + a + b + c) + -1.0 + a + b + c * cx.log(2.0) + -cx.entr(2.0) + -2.0 * cx.log(2.0) + -cx.entr(1.0 + b) -1.0 + b * cx.log(2.0) + -cx.entr(2.0 + b) + -2.0 + b * cx.log(1.0) -cx.entr(1.0 + b + a + c) -1.0 + b + a + c * cx.log(2.0)
p = cx.Problem(cx.Minimize(obj), [0 <= a, 0<= b, 0 <= c, a + b + c == 1])
p.solve()
# should be 'optimal' and indeed it is
print(p.status)
# the following two values should be the same, but p.value is infinite and should be around -1.06
print(p.value)
print(check_obj(a.value, b.value, c.value))

It looks like a bug in the entropy atom. I fixed it and made a pull request here. It is merged now. If you run your code with the latest cvxpy from the master branch it should give correct results.

Related

Show full calculation in Mathematica to copy into latex

I'm new to Mathematica I was hoping that this is a functionality built in. I am calculating the conditional entropy of a table and have the following 4 variables:
a = 1/8*Log[(1/8)/(1/2)] + 1/16*Log[(1/16)/(1/2)] + 1/16*Log[(1/16)/(1/2)] + 1/4*Log[(1/4)/(1/2)]
b = 1/16*Log[(1/16)/(1/4)] + 1/8*Log[(1/8)/(1/4)] + 1/32*Log[(1/32)/(1/4)] + 0
c = 1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] + 1/16*Log[(1/16)/(1/8)] + 0
d = 1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] + 0
Then the final calculation is:
a + b + c + d
I was hoping there was a way to display the expanded a + b + c + d as a output so I can right-click/copy as latex to past into a document so I don't have to type it out. I can't figure out how to do it though. I'm guessing there is a way to expand those variables in the output to show the full calculation but maybe it is not possible. Thanks for any help.
I'm not sure how expanded you wanted the mathematical expression to be. See if the following expression is in the form you want to convert to LaTeX: (1/16 Log[1/(16/4)]+1/8 Log[1/(8/4)]+1/32 Log[1/(32/4)]+0)+(1/32 Log[1/(32/8)]+1/32 Log[1/(32/8)]+1/16 Log[1/(16/8)]+0)+(1/32 Log[1/(32/8)]+1/32 Log[1/(32/8)]+1/32 Log[1/(32/8)]+0)+(1/8 Log[1/(8/2)]+1/16 Log[1/(16/2)]+1/16 Log[1/(16/2)]+1/4 Log[1/(4/2)]).
You can get that by using HoldForm on the right-hand side of your assignments. Then use TeXForm[a+b+c+d] to convert:
a = HoldForm[
1/8*Log[(1/8)/(1/2)] + 1/16*Log[(1/16)/(1/2)] +
1/16*Log[(1/16)/(1/2)] + 1/4*Log[(1/4)/(1/2)]];
b = HoldForm[
1/16*Log[(1/16)/(1/4)] + 1/8*Log[(1/8)/(1/4)] +
1/32*Log[(1/32)/(1/4)] + 0];
c = HoldForm[
1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] +
1/16*Log[(1/16)/(1/8)] + 0];
d = HoldForm[
1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] +
1/32*Log[(1/32)/(1/8)] + 0];
TeXForm[a + b + c + d]

Regular expression from this dfa

what will be regular express of this transition graph anyone?can make and explain this please i will be very thankful to him.
We can write some equations for this:
(q0) = e + (q1)a + (q3)b
(q1) = (q0)a + (q2)b
(q2) = (q1)b + (q3)a
(q3) = (q0)b + (q2)a
You can read these equations like this: "the set of strings that leads me to state X is the set of strings that lead me to state Y followed by symbol c, or the set of strings that lead me to state Z followed by symbol d, or..."
We can now solve these equations using substitution and a rule to eliminate self-references, namely:
if (q) = (q)x + y, then (q) = y(x*)
We can start solving the system by eliminating (q3):
(q0) = e + (q1)a + [(q0)b + (q2)a]b
(q1) = (q0)a + (q2)b
(q2) = (q1)b + [(q0)b + (q2)a]a
Distributing:
(q0) = e + (q1)a + (q0)bb + (q2)ab
(q1) = (q0)a + (q2)b
(q2) = (q1)b + (q0)ba + (q2)aa
We can get rid of (q1) now:
(q0) = e + [(q0)a + (q2)b]a + (q0)bb + (q2)ab
(q2) = [(q0)a + (q2)b]b + (q0)ba + (q2)aa
Distributing:
(q0) = e + (q0)aa + (q2)ba + (q0)bb + (q2)ab
(q2) = (q0)ab + (q2)bb + (q0)ba + (q2)aa
Grouping like terms:
(q0) = e + (q0)(aa + bb) + (q2)(ab + ba)
(q2) = (q0)(ab + ba) + (q2)(aa + bb)
Using the rule to remove self-reference:
(q0) = (aa + bb)* + (q2)(ab + ba)(aa + bb)*
(q2) = (q0)(ab + ba)(aa + bb)*
Substituting to get rid of (q2):
(q0) = (aa + bb)* + [(q0)(ab + ba)(aa + bb)*](ab + ba)(aa + bb)*
Distributing:
(q0) = (aa + bb)* + (q0)(ab + ba)(aa + bb)*(ab + ba)(aa + bb)*
Using the rule for self-reference:
(q0) = (aa + bb)*[(ab + ba)(aa + bb)*(ab + ba)(aa + bb)*]*
from the given transition diagram or DFA, equations for each state can be written as:
q0 = ε+q1.a+q3.b
q1 = q0.a+q2.b
q2 = q1.b+q3.a
q3 = q0.b+q2.a
substitute q3 in q2,
q2=q1.b+(q0.b+q2.a)a
q2=q2.aa+q0.ba+q1.b
substitute q1 in q2
q2=q2.aa+q0.ba+(q0.a+q2.b)b
q2=q2.aa+q0.ba+q0.ab+q2.bb
finally,
q2=q2(aa+bb)+q0(ba+ab)
It is in the form of t=tr+s
According to Arden's theorem,
the solution is t=sr*
So,q2=q0(ba+ab)(aa+bb)
Now substitute q3 in the equation q0,
q0=ε+q1.a+(q0.b+q2.a)b
q0=ε+q1.a+q0.bb+q2.ab
q0=q0.bb+(q1.a+q2.ab+ε)
substitute q1 in q0,
q0=q0.bb+(q0.a+q2.b)a+q2.ab+ε
q0=q0.bb+q0.aa+q2.ba+q2.ab+ε
q0=q0(bb+aa)+q2(ba+ab)+ε
Now substitute the q2 in the above equation,
q0=q0(bb+aa)+q0(ba+ab)(aa+bb)*(ba+ab)+ε
q0=q0((bb+aa)+(ba+ab)(aa+bb)*(ba+ab))+ε
Now q0 is in the form of t=tr+s so the solution is t=sr*
q0=((bb+aa)+(ba+ab)(aa+bb)(ab+ba))
The Regular Expression for the given DFA is:
q0=((bb+aa)+(ba+ab)(aa+bb)*(ab+ba))

Need help getting for loop to iterate correctly

I am working on this for homework, and I have most of this done my problem is there is something wrong in one of the for loops as the display I get is only one line when it should be a line for every value in the range 50,000 - 60,000 with a step value of 50.
def computeTax(status, taxableIncome):
print("Taxable Income/" + "\t" + "Single/" + "\t" + "Married Joint/" + \
"\t" + "Married Separate/" + " " + "Head of Household")
for taxableIncome in range(50000, 60001, 50):
for status in range(1, 5, 1):
if (status == 1): #tax calculation for single person
tax1 = 8350 * .10 + (33950 - 8350) * 0.15 + \
(taxableIncome - 33950) * 0.25
tax1 = round(tax1,0)
if (status == 2): #tax calculation for married file jointly
tax2 = 16700 * .10 + (67900 - 16700) * 0.15 + \
(taxableIncome - 67900) * 0.15
tax2 = round(tax2,0)
if (status == 3): #tax calculation for married file separately
tax3 = 8350 * .10 + (33950 - 8350) * 0.15 + \
(taxableIncome - 33950) * 0.25
tax3 = round(tax3,0)
if (status == 4): #tax calculation for head of household
tax4 = 11950 * .10 + (45500 - 11950) * 0.15 + \
(taxableIncome - 45500) * 0.25
tax4 = round(tax4,0)
print(str(tax1) + "\t" + str(tax2) + "\t" + str(tax3) + "\t" + str(tax4))
return (status, taxableIncome)
computeTax(range(1, 5, 1),range(50000, 60001, 50))
I'm not familiar with python, but do you have to define code blocks like you would with PHP?
EG
if (status == 1): #tax calculation for single person
{
tax1 = 8350 * .10 + (33950 - 8350) * 0.15 + \
(taxableIncome - 33950) * 0.25
tax1 = round(tax1,0)
}
Like I said; not familiar with Python, but I thought I might have an answer for you; if I'm completely off then sorry and good luck.

Recurrence relation: T(n) = T(n - 1) + n - 1

I am trying to understand how to solve recurrence relations. I understand it to the point where we have to simplify.
T(N) = T(N-1) + N-1 Initial condition: T(1)=O(1)=1
T(N) = T(N-1) + N-1
T(N-1) = T(N-2) + N-2
T(N-2) = T(N-3) + N-3
……
T(2) = T(1) + 1
**Summing up right and left sides**
T(N) + T(N-1) + T(N-2) + T(N-3) + …. T(3) + T(2) =
= T(N-1) + T(N-2) + T(N-3) + …. T(3) + T(2) + T(1) +
(N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
** Canceling like terms and simplifying **
T(N) = T(1) + N*(N-1)/2 1 + N*(N - 1)/2
T(N) = 1 + N*(N - 1)/2
I really don't understand the last part. I understand canceling like terms but don't understand how the simplification below works:
T(N) = T(1) + (N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
T(N) = T(1) + N*(N-1)/2 1 + N*(N - 1)/2
How is the second line derived from the first? Doesn't make any sense to me.
Would be a great help if someone can help me understand this. Thanks =)
In your second-to-last-line:
S = (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
You can say:
2S = S + S
= (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
1 + 2 + 3 + ... + (N-3) + (N-2) + (N-1)
= N + N + N + ... + N + N + N
|__________________ N-1 times ________________|
You're counting from N - 1 to 1, so there are N - 1 terms in the sequence. But the whole sequence is just N so you can say:
2S = N * (N - 1)
S = (N * (N - 1)) / 2
So in your last chunk:
T(N) = T(1) + (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
= T(1) + (N * (N - 1)) / 2
T(N) = T(1) + (N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
= T(1) + (N-1) + (N-2) + (N-3) + ..... + ( N-(N-3)) + (N-(N-2)) + (N-(N-1))
= T(1) + [N+N+N+..... n-1 times] - [1+2+3+......+(N-3)+(N-2)+(N-1)]
= T(1) + N*(N-1) - (N*(N-1))/2
= T(1) + N*(N-1)/2

Recurrence Relation Homework Struggles

Here's the question:
Solve the recurrence by obtaining a theta bound for T(n) given that T(1) = theta(1).
T(n) = n + T(n-3)
Attempted Solution:
T(n) = T(n-6) + (n-3) + n
= T(n-9) + (n-6) + (n-3) + n
= T(n-(n-1)) + [(n-n) + (n-(n-3)) + (n-(n-6)) + ... + n]
= T(1) + [0 + 3 + 6 + ... + n]
= theta(1) = 3[1 + 2 + 3 + ... + n/3]
= theta(1) + [(n/3)(n/3 + 1)]/2
= theta(1) + (n^2+3n)/6
When I double check to see if the solution fits the recurrence, it doesn't work.
The issue was that you were getting the wrong summation.
It doesn't start at 0, since your last T function was T(n - (n-1)) , which means previous one was T(n-(n-4)). So the summation starts at 4, and goes up till n.
If you don't know how to find the summation of this, I'd suggest you look at some of the proofs from the summation formula. This is what the solution looks like.
T(n) = T(n-3) + n
= T(n-6) + (n-3) + n
= T(n-(n-1)) + [ (n-(n-4)) + (n-(n-7)) + ... + n]
= T(1) + [4 + 7 + ... + n]
= theta(1) + (4 + n) * (n - 1)/6

Resources