cvxpy: Possible sign flip for Lagrange Multiplier/dual variable - cvxpy

I have encountered a very strange problem when using cvxpy. Consider the following two problems:
x = cvx.Variable(1, "x")
obj = cvx.Minimize(x)
cons = [x==1]
prob = cvx.Problem(obj, cons)
prob.solve()
print(cons[0].dual_value)
Output: -1
x = cvx.Variable(1, "x")
obj = cvx.Maximize(x)
cons = [x==1]
prob = cvx.Problem(obj, cons)
prob.solve()
print(cons[0].dual_value)
Output: 1
The only difference is that one is a minimization problem and the other is a maximization problem, but the sign of the dual variable is flipped.
Conceptually, this shouldn't happen as in both cases the Lagrangian is L=x + lambda*(x-1), but I cannot find the documentation on how it is defined.
Does anyone have an explanation on why this is happening?

Related

Tessellation error in spatstat - Error in as.im.default(image) : Can't convert X to a pixel image

I have been dealing with this for several days and can't find a way out. I'm using spatstat in R for spatial analysis of my cells. I have follow some tutorials, but as always happens, things don't come out as they should. I have some xy coordinates and I am generating a density layer with kde2d. However, when I try to perform the Tessellation (tess function) I get the following error. I have not been able to get around it. I hope you can offer me some help.
Iba1_kde = kde2d(Iba1_Section5$Iba1_Coor_X, Iba1_Section5$Iba1_Coor_Y, n = 200)
contour(Iba1_kde)
Iba1_Raster = raster(Iba1_kde)
Iba1_Frame = as.data.frame(Iba1_Raster , xy = T)
Cell_density <- Iba1_Frame$layer
b <- quantile(Cell_density, probs = (0:5)/5)
Cut <- cut(Cell_density, breaks = b, labels = 1:5)
tess <- tess(image = Cut)
Error in as.im.default(image) : Can't convert X to a pixel image
I appreciate your feedback

The complier cannot call 'pivothighs' with argument

I've been trying to make a pivot point high low multitimeframe indicator but still a new learner and have no idea how to fix.
I tried to put 'tf' function in multiple places of code but it's not working.
//#version=4
study("Pivot Prices", overlay=true)
tf=input('120')
leftbars = input(10, minval=1, title='Bars to the left')
rightbars = input(2, minval=1, title='Bars to the right')
phigh = pivothigh(high, tf, leftbars,rightbars)
plow = pivotlow(low, tf, leftbars, rightbars)
if phigh
label1 = label.new(bar_index[rightbars], high[rightbars], text=tostring(high[rightbars]), style=label.style_labeldown, color=color.orange)
if plow
label2 = label.new(bar_index[rightbars], low[rightbars], text=tostring(low[rightbars]), s``tyle=label.style_labelup, color=color.green)
I want it to be showing multitimeframe perspective but couldn't figure what is wrong in the code.
Read the documentation. pivothigh() and pivotlow() can take two or three arguments.
pivothigh(source, leftbars, rightbars) → series[float]
pivothigh(leftbars, rightbars) → series[float]
You are passing four arguments.

K-means initialization with further-first traversal and k-mean++

I am confused about k-mean++ initialization. I understand k-mean++ choose and furthest data point as next data center. But how about outlier? What is the different between `initialization with further-first traversal and k-mean++ ?
I saw someone explain in this way:
Here is a one-dimensional example. Our observations are [0, 1, 2, 3, 4]. Let the first center, c1, be 0. The probability that the next
cluster center, c2, is x is proportional to ||c1-x||^2. So, P(c2 = 1)
= 1a, P(c2 = 2) = 4a, P(c2 = 3) = 9a, P(c2 = 4) = 16a, where a = 1/(1+4+9+16).
Suppose c2=4. Then, P(c3 = 1) = 1a, P(c3 = 2) = 4a, P(c3 = 3) = 1a,
where a = 1/(1+4+1).
What is this array or list is [0,1,2,4,5,6,100]. Obviously, 100 is the outlier in this case and it will be chosen as the data center at some point. Can someone give a better explanation?
K-means chooses points with probability.
But yes, with extreme outliers it is likely to chose the outlier.
That is fine, because so will k-means. Most likely the best SSQ solution has a one-element cluster containing only that point.
If you have such data, the k-means solutions tend to be rather useless, and you probably should choose another algorithm such as DBSCAN instead.

Computing remainder without using modulus operator F#

I have implemented my code which is basically to compute remainder in two numbers without using modulus operator however, I am stuck in a situation which is just hectic. I know the logic however I am newbie in f# and dont know how to implement it.
let rec modulus a b =
if b = 0 Console.WriteLine("Sorry Wrong Divisor")
let bool neg = a < 0
a = abs a
b = abs b
modulus(val-divisor,divisor)
All I know is I am getting a pretty basic mistake here, Any help,
The first step towards getting this to work is to fix your indentation and turn your sketch into valid F# code that actually compiles and runs - that should help you get to the next step, which is to fix the logic of the implementation.
A minimal code that is similar to yours and actually runs looks like this:
let rec modulus value divisor : int =
printfn "value=%d, divisor=%d" value divisor
if divisor = 0 then Console.WriteLine("Sorry Wrong Divisor")
let neg = value < 0
let value = abs value
let divisor = abs divisor
modulus (value-divisor) divisor
modulus 10 5
I fixed the indentation - F# is indentation sensitive, so this matters.
I replaced your a = abs a with let - the let keyword defines a new variable, hiding the existing one (as you cannot mutate existing variables - they are immutable in F#)
I renamed your variables to consistently use divisor and value names
I added printfn so that you can see how the function runs (it will get into an infinite loop, because it currently never checks for the termination condition!)
I had to add type annotation : int to say that the result will be int - as your function never returns, this is required (but you can remove it once you fix this)
You can calculate modulus in simpler way
let modulus a b=
if b = 0.0 then failwith "Cannot divide by zero"
a - b * truncate(a / b);

How to estimate? "simple" Nonlinear Regression + Parameter Constraints + AR residuals

I am new to this site so please bear with me. I want to
the nonlinear model as shown in the link: https://i.stack.imgur.com/cNpWt.png by imposing constraints on the parameters a>0 and b>0 and gamma1 in [0,1].
In the nonlinear model [1] independent variable is x(t) and dependent are R(t), F(t) and ξ(t) is the error term.
An example of the dataset can be shown here: https://i.stack.imgur.com/2Vf0j.png 68 rows of time series
To estimate the nonlinear regression I use the nls() function with no problem as shown below:
NLM1 = nls(**Xt ~ (aRt-bFt)/(1-gamma1*Rt), start = list(a = 10, b = 10, lamda = 0.5)**,algorithm = "port", lower=c(0,0,0),upper=c(Inf,Inf,1),data = temp2)
I want to estimate NLM1 with allowing for also an AR(1) on the residuals.
Basically I want the same procedure as we go from lm() to gls(). My problem is that in the gnls() function I dont know how to put contraints for the model parameters a, b, gamma1 and the model estimates wrong values for them.
nls() has the option for lower and upper bounds. I cant do the same on gnls()
In the gnls(): I need to add the contraints something like as in nls() lower=c(0,0,0),upper=c(Inf,Inf,1)
NLM1_AR1 = gnls( model = Xt ~ (aRt-bFt)/(1-gamma1*Rt), data = temp2, start = list(a =13, b = 10, lamda = 0.5),correlation = corARMA(p = 1))
Does any1 know the solution on how to do it?
Thank you

Resources