How To write a query for testing the following code that written with eplex Lib in ECLiPSe-CLP - eclipse-clp

I am new in ECLiPSe and have a follwing problem.
when I write and compile this simple program:
---------------------------------
:- lib(eplex).
main1(Cost, Vars) :-
Vars = [A1, A2, A3, B1, B2, B3, C1, C2, C3, D1, D2, D3],
Vars :: 0.0..inf, % variables
A1 + A2 + A3 $= 200, % demand constraints
B1 + B2 + B3 $= 400,
C1 + C2 + C3 $= 300,
D1 + D2 + D3 $= 100,
A1 + B1 + C1 + D1 $=< 500, % capacity constraints
A2 + B2 + C2 + D2 $=< 300,
A3 + B3 + C3 + D3 $=< 400,
optimize(min( % solve
10*A1 + 7*A2 + 11*A3 +
8*B1 + 5*B2 + 10*B3 +
5*C1 + 5*C2 + 8*C3 +
9*D1 + 3*D2 + 7*D3), Cost).
------------------------------
I dont know how to test it? or what is simple query for test it?
I will be very glad ,If you guide me .
Thanks in Advance

The two arguments of main1 are both outputs, so you can just provide two variables (upper case names) in your query, for example main1(C,Vs). Type this into the query prompt (or the query entry box, if you are using tkeclipse), then the system will run the code and print the answer bindings:
[eclipse 1]: main1(C, Vs).
C = 6600.0
Vs = [100.0, 0.0, 100.0, 100.0, 300.0, 0.0, 300.0, 0.0, 0.0, 0.0, 0.0, 100.0]
Yes (0.00s cpu)

Related

How can I see the effect sizes of my predictor variables on both my dependent variables in a system of equations?

I originally got two formulas that were expected two have the same predictor variables (X-variables) and the same control variables (C-variables):
Y1 = X1 + X2 + X3 + C1 + C2 + C3
Y2 = X1 + X2 + X3 + C1 + C2 + C3
However, my dependent variable Y1 could also be used as a dependent variable in the regression for Y2. So my new formula became:
Y1 = X1 + X2 + X3 + C1 + C2 + C3
Y2 = Y1 + X1 + X2 + X3 + C1 + C2 + C3
I tried to model this as a system of equations in R. I understand that "X1 + X2 + X3 + C1 + C2 + C3" should not be in the formula for Y2 as they already are in the formula for Y1. Now the problem is that I am interested in the effect of the predictor variables on Y1 as well as Y2 (in order to reject or approve hypotheses), but the output form my system of equations only gives the effect of Y1 on Y2. How can I account for the effect of Y1 on Y2 and also be able to see the effects of X1, X2 and X3 on both formulas?
I have used the following formula:
model_1 <- ivreg(Y2 ~ Y1 + C1
|. - Y1 + X1 + X2 + X3 + C2 + C3, data = df)
This gives the following output:

How to appear a a plot in R with step

Hello I try to appear a plot in R with step 0.1 inside the space [0-89] and I do not know how.
The code is :
c1 = 1500
c2 = 1600
c3 = 1850
p1 = 1000
p2 = 1300
p3 = 1500
h = 100
f = 500
w = 2 *pi * f
k2 = w / c2
i = complex( real = 0, imaginary = 1 )
th1 = ( 0:89 )
th1 = th1 * pi / 180
th2 = asin( pmin(pmax((c2 / c1) * sin (th1),-1.0),1.0) )
th3 = asin(pmin(pmax(( c3 / c2) * sin (th2),-1.0),1.0) )
R12 = (p2 * c2 * cos(th1) - p1 * c1 * cos(th2) ) / (p2 * c2 * cos(th1) + p1 * c1 * cos (th2))
R23 = (p3 * c3 * cos(th2) - p2 * c2 * cos(th3)) / (p3 * c3 * cos(th2) + p2 * c2 * cos (th3))
phi2 = k2 * h * cos(th2)
R13 = (R12 + R23 * exp(2 * i * phi2 ))/ ( 1+ R12 * R23 * exp(2 * i* phi2))
y=abs(R13)
th1=th1*180/pi
plot(th1 , y, type = "l", xlab="Angle of Incidence (Deg)", ylab="|R13|")
axis(side=1, at=seq(0, 100, by=10))
axis(side=2, at=seq(0, 1, by=0.1))
The plot that I take is :
I did it in matlab and the command is t = 0:0.1:89. So if I use this step 0.1 I have a plot like this:
Can you help me on how can I make this work and in R ?
In your code th1 = ( 0:89 ) generates a sequence with a step of 1. If you change that line with th1 = seq(0,89, 0.05), the step will now be 0.05 instead of 1.

Convergence Failure: Iteration limit reached without convergence (10)

I have some difficulties getting a specific curve to fit data to an nls model.
This is the formula for the data:
((b1 * ((b2 * x)^b4)) / (1 + ((b2 * x)^b4)))^(b3 / b4)
I use nls2 package with a random algorithm to find the inital values.
library(nls2)
#FORMULA
eq <- y ~ (b1 * ((b2 * x)^b4)) / (1 + ((b2 * x)^b4))^(b3 / b4)
#LIMITS
values <- data.frame(
b1 = c(60, 63)
b2 = c(0, 0.05)
b3 = c(0, 1)
b4 = c(0, 0.9)
fit <- nls2(eq,
data = .data,
start = values,
algorithm = "random",
control = mls.control(maxiter = 1000))
nls(eq, .data, start = coef(fit), alg = "port", lower = 0)
plot(.data)
The values should be:
b1 = 62.2060
b2 = 0.0438
b3 = 0.9692
b4 = 0.8693
However, when I try to run the codes, I always ended on an error message: Convergence Failure: Iteration limit reached without convergence (10)
How can I avoid the convergence failure error? Any help is highly appreciated. Thank You.
0. TLDR
You did not set the lower and upper bound in nls, so you didn't get a converging result. If you set them your will get a result near the boundary. See the code I wrote in the last paragraph.
Actually, even if you set the boundary, due to the bad data quality(sample size is small and do not consist with you formula), it's hard to fit a optimal value near your true b1,'b2','b3' and b4. See nontechnical reason.
1. Nontechnical reason of convergence failure
I think your code is right, and this convergence fail is due to your data quality or your misspecification of formula.
In general, it's hard for you to estimate 4 parameters with only 6 point. If you have good data which actully fits your model well, nlm will converge. In your case, either your data is wrong or you formula specification bias is huge.
I draw a plot to show your that:
Code
# generate a line using true parameters:b1,b2,b3,b4
b1 = 62.2060
b2 = 0.0438
b3 = 0.9692
b4 = 0.8693
x_points = seq(50,420,length.out = 200)
y_points = (b1 * ((b2 * x_points)^b4)) / (1 + ((b2 * x_points)^b4))^(b3 / b4)
# plot the function
plot(x = x_points ,y = y_points, type ='l',col ='black',lwd = 5,
xlim = c(min(yourdata$x)-5,max(yourdata$x)+5),
ylim = c(min(yourdata$y)-5,max(yourdata$y)+5))
# plot the data your got
points(yourdata$x,yourdata$y,cex = 2)
Output:
If we generate a data from your formula, we can fit them quite easily, like this:
## generate data
b1 = 62.2060
b2 = 0.0438
b3 = 0.9692
b4 = 0.8693
x <- runif(6,60,450)
y <- (b1 * ((b2 * x)^b4)) / (1 + ((b2 * x)^b4))^(b3 / b4)
data <- data.frame(x,y)
yourdata <- data.frame(x = c(409.56, 195.25, 60.53, 359.56, 188.79, 67.12),
y = c(39.76100, 20.11875, 7.23675, 41.01100, 20.28035, 7.07200))
#FORMULA
eq <- y ~ (b1 * ((b2 * x)^b4)) / (1 + ((b2 * x)^b4))^(b3 / b4)
#LIMITS
values <- data.frame(
b1 = c(60, 63),
b2 = c(0, 0.05),
b3 = c(0, 1),
b4 = c(0, 0.9))
fit <- nls2(eq,
data = data,
start = values,
algorithm = "random",
control = nls.control(maxiter = 1000))
nls(eq, data, start = coef(fit), alg = "port",
control = nls.control(maxiter = 1000,tol = 1e-05),
low = c(60,0,0,0),upper =c(63,0.05,1,0.9) ,trace = TRUE)
plot(x,y)
Output:
Nonlinear regression model
model: y ~ (b1 * ((b2 * x)^b4))/(1 + ((b2 * x)^b4))^(b3/b4)
data: data
b1 b2 b3 b4
62.2060 0.0438 0.9692 0.8693
residual sum-of-squares: 3.616e-24
Algorithm "port", convergence message: absolute function convergence (6)
Alse note that, in the above, I generate only6 numbers to fit the model. If you generate more data, for instance 60, you will have a better convergency!
2.Technical reason
After reading the PORT docs, I think that this error can mean
gradient is calculated incorrectly
stopping tolerances are too tight
gradient is discontinous near some iterate
And all these may have a relationship with you data and training task(your boundary and formula).
Try code below and you will get a better result:
Code:
yourdata <- data.frame(x = c(409.56, 195.25, 60.53, 359.56, 188.79, 67.12),
y = c(39.76100, 20.11875, 7.23675, 41.01100, 20.28035, 7.07200))
#FORMULA
eq <- y ~ (b1 * ((b2 * x)^b4)) / (1 + ((b2 * x)^b4))^(b3 / b4)
#LIMITS
values <- data.frame(
b1 = c(60, 63),
b2 = c(0, 0.05),
b3 = c(0, 1),
b4 = c(0, 0.9))
fit <- nls2(eq,
data = yourdata,
start = values,
algorithm = "random",
control = nls.control(maxiter = 1000))
nls(eq, yourdata, start = coef(fit), alg = "port",
control = nls.control(maxiter = 1000,tol = 1e-05),
low = c(60,0,0,0),upper =c(63,0.05,1,0.9) ,trace = TRUE)
plot(x,y)
Outputs:
Nonlinear regression model
model: y ~ (b1 * ((b2 * x)^b4))/(1 + ((b2 * x)^b4))^(b3/b4)
data: yourdata
b1 b2 b3 b4
63.00000 0.00155 0.00000 0.90000
residual sum-of-squares: 22.28
Algorithm "port", convergence message: both X-convergence and relative convergence (5)
As we can see, it converges to the boundary, which means that your data is unconsitant with your settings(formula or boundary).

How to run the pgmm command?

Please see a sample of my data, and my pgmm code, and let me know if I am using the correct syntax.
Y1 is my dependent variable, and X* with C* variables are my independent and control variables. I am trying to run the dynamic GMM model with 2 year lags, but this is the first time that I am using PGMM and I am not sure if this is the correct syntax.
Sample Data
I am trying to run the pgmm command below:
country <- pdata.frame(country, index = c('Co_Code', 'YEAR'))
model.gmm <- Y1 ~ lag(X1, 2) + lag(X2, 2) + lag(X3, 2) + lag(X7, 2) +
lag(X6, 2) + lag(X4, 2) + lag(X5, 2) + lag(X8, 2) + lag(X9, 2) +
lag(X10, 2) + lag(C1, 2) + lag(C2, 2) + lag(C3, 2) + lag(C6, 2) + lag(C7, 2)
gmm.form = update.formula(model.gmm, . ~ . | lag(Y1, 2))
gmm.form[[3]] <- gmm.form[[3]][[2]]
gmm.fit <- pgmm(gmm.form, data = country, effect = "twoways", model =
"twosteps")
summary(gmm.fit)
Edit: I've also generated the code below:
gmm.fit <- pgmm(Y1 ~ X1 + X2 + X3 + X6 + X7 + X4 + X5 + X8 + X9 + X10 +
C1 + C2 + C3 + C6 |lag(X1, 2) + lag(X2, 2) + lag(X3, 2) + lag(X7, 2) +
lag(X6, 2) + lag(X4, 2) + lag(X5, 2) + lag(X8, 2) + lag(X9, 2) +
lag(X10, 2) + lag(C1, 2) + lag(C2, 2) + lag(C3, 2) + lag(C6, 2), data =
country, effect = "twoways", model = "twosteps")
Yes, your updated version appears correct for what you say. You may prefer using dynformula, the basic structure is:
gmm.form <- dynformula(Y1~ X + C, lag.form=list(2,2,2))
And this easily generalises for multiple X and C:
gmm.form <- dynformula(Y1~ X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 +X10 + C1 + C2
+ C3 + C4 + C5 +C6, lag.form=list(rep(2,17)))
This command means you will be including up to and including 2 lags for all the variables (noting that the first in the lag.form list above is Y1 - dynformula will automatically put the lags of Y1 on the right hand side of the equation).
[Edit: I note you haven't specified instruments. Seeing your data, for standard dynamic panel approach of lagged Y, I'd put gmm.inst=~Y1,gmm.lag=list(c(3,99))]

Constraints for nls coefficients

I'm trying to fit data with nls() function where the nature of data gives me bounds for one coefficient and for sum of two coefficients. Let me introduce short example to see where is the problem. I want parameter b1 to be between 0 and 1 and I want sum of parameters b1 and b2 to be between 0 and 1 as well.
set.seed(123)
# example where everything is OK
x <- 1:200
g <- rbinom(200, 1, 0.5)
y <- 3 + (0.7 + 0.2 * g) * x
yeps <- y + rnorm(length(y), sd = 0.1)
# both parameter b1 and sum of parameters b1 and b2 are between 0 and 1
nls(yeps ~ a + (b1 + b2 * g) * x, start = list(a = 0.12345, b1 = 0.54321, b2 = 0.4213))
# using more extreme values
x <- 1:200
g <- rbinom(200, 1, 0.5)
y <- 3 + (0.9 - 0.99 * g) * x
yeps <- y + rnorm(length(y), sd = 15)
# b1 is OK, but b1 + b2 < 0
nls(yeps ~ a + (b1 + b2 * g) * x,
start = list(a = 0.12345, b1 = 0.54321, b2 = 0.4213))
# trying constraints, not good, sum is still out of range
nls(yeps ~ a + (b1 + b2 * g) * x,
start = list(a = 0.12345, b1 = 0.54321, b2 = 0.4213),
lower = list(a = -Inf, b1 = 0, b2 = -1),
upper = list(a = Inf, b1 = 1, b2 = 1),
algorithm = "port")
What I'm looking for is something like that (does not work):
nls(yeps ~ a + (b1 + b2 * g) * x,
start = list(a = 0.12345, b1 = 0.54321, b2 = 0.4213),
lower = list(a = -Inf, b1 = 0, b2 = -b1),
upper = list(a = Inf, b1 = 1, b2 = 1 - b1),
algorithm = "port")
Is it possible to set constraints with other parameters in nls() function? Thanks for any suggestions!
Let B2 = b1+b2 so b2 = B2-b1 and substituting B2-b1 for b2 we get a problem in terms of a, b1 and B2 of which the latter two are between 0 and 1 so:
fm <- nls(yeps ~ a + (b1 + (B2-b1) * g) * x, lower = c(-Inf, 0, 0), upper = c(Inf, 1, 1),
start = list(a = 0.1, b1 = 0.5, B2 = 0.1), alg = "port")
giving the following (hence b2 = B2 - b1 = 0 - 0.9788 = -0.9788)
> fm
Nonlinear regression model
model: yeps ~ a + (b1 + (B2 - b1) * g) * x
data: parent.frame()
a b1 B2
-5.3699 0.9788 0.0000
residual sum-of-squares: 42143
Algorithm "port", convergence message: both X-convergence and relative convergence (5)
and plotting:
plot(yeps ~ x)
points(fitted(fm) ~ x, pch = 20, col = "red")

Resources