SAS Proc IML Optimization - optimization

proc iml;
start f_prob(beta) global(one_m_one, pone_m_one);
p = nrow(one_m_one);
td = j(p,3,0.);
a = 1;
do i = 1 to p;
td[i,1] = exp((one_m_one[i,1])*(beta[1]) + (one_m_one[i,2])*(beta[2]) + (one_m_one[i,3])*(beta[3]) + (one_m_one[i,4])*(beta[4]) + (one_m_one[i,5])*(beta[5]) + (one_m_one[i,6])*(beta[6]) + (one_m_one[i,7])*(beta[7]) + (one_m_one[i,8])*(beta[8]) + (one_m_one[i,9])*(beta[9]) + (one_m_one[i,10])*(beta[10]));
do j = a to 11+a;
td[i,2] = td[i,2] + exp((pone_m_one[j,1])*(beta[1]) + (pone_m_one[j,2])*(beta[2]) + (pone_m_one[j,3])*(beta[3]) + (pone_m_one[j,4])*(beta[4]) + (pone_m_one[j,5])*(beta[5]) + (pone_m_one[j,6])*(beta[6]) + (pone_m_one[j,7])*(beta[7]) + (pone_m_one[j,8])*(beta[8]) + (pone_m_one[j,9])*(beta[9]) + (pone_m_one[j,10])*(beta[10]));
end;
a = a + 12;
end;
td[,3] = td[,1]/td[,2];
f = 1;
do i = 1 to p;
f = f*td[i,3];
end;
return(f);
finish f_prob;
/* Set up the constraints: sum(x)=0 */
/* x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 SIGN VALUE */
con = {. . . . . . . . . . . ., /* specify lower bounds */
. . . . . . . . . . . ., /* specify upper bounds */
1 1 1 1 1 1 1 1 1 1 0 0}; /* constraints */
beta0 = j(1,10,0);
optn = {1,4};
call nlpnra(rc, result, "f_prob", beta0, optn) blc=con;
Hi, I am trying to optimise the function f that has 10 parameters in it with a constraint of all 10 parameters sum up to zero.
Can anyone suggest how can I write the code for the last part so that i can optimise f and get the results i want? Thanks in advance.

The documentation provides an example of how to specify a linear constraint matrix. For your example, use a 3 x 12 matrix.
On the first row (columns 1:10) put any lower-bound constraints for the parameters.
On the second row (columns 1:10) put any upper-bound constraints for the parameters.
On the third row, put all ones in columns 1:10. Put a 0 in column 11 to indicate the EQUAL sign. Put 0 in the 12th column to indicate the value of the constraint.
The code looks like this:
/* Set up the constraints: sum(x)=0 */
/* x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 SIGN VALUE */
con = {. . . . . . . . . . . ., /* specify lower bounds */
. . . . . . . . . . . ., /* specify upper bounds */
1 1 1 1 1 1 1 1 1 1 0 0}; /* constraints */
call nlpnra(rc, result, "f_prob", beta, optn) blc=con;
The last line specifies the coefficients of the matrix expression c*x = 0, where c = {1 1 ... 1} contains the of the third row.

Related

How to iterate complex numbers on the Riemann sphere in Maxima CAS?

I try to iterate complex numbers ( also inf and zero) in Maxima CAS
I use rational function and it's derivative
The only one attracting cycle is the period 3-cycle consisting of the points 0, −1, and infinity.
kill(all);
display2d:false;
ratprint : false; /* remove "rat :replaced " */
define(f(z), (1 -z^2)/(z^2));
F(z0):= block(
[z],
if is(z0 = 0) then z: limit(f(z),z,0)
elseif is(z0 = infinity) then z: limit(f(z),z,inf)
else z:f(z0),
return(z)
)$
define( dz(z), ratsimp(diff(f(z),z,1)));
Dz(z0) := block(
[m],
if is(z0 = 0) then m: limit(dz(z),z,0)
elseif is(z0 = infinity) then m: limit(dz(z),z,inf)
else m:dz(z0),
return(m)
)$
GiveStability(z0, p):=block(
[z,d],
/* initial values */
d : 1,
z : z0,
for i:1 thru p step 1 do (
d : Dz(z)*d,
z: F(z),
print("i = ", 0, " d =",d, " z = ", z)
),
return (d)
)$
GiveStability(-1,3);
The simple computations work fine:
F(0);
(%o10) inf
(%i11) F(-1);
(%o11) 0
(%i12) F(infinity);
(%o12) -1
(%i13) Dz(0);
(%o13) infinity
(%i14) Dz(infinity);
(%o14) 0
(%i15) Dz(-1);
(%o15) 2
But when I try to use the las t functionL
a:GiveStability(-1,3);
i = 0 d = 2 z = 0
expt: undefined: 0 to a negative exponent.
#0: dz(z=0)
#1: Dz(z0=0)
#2: GiveStability(z0=-1,p=3)
-- an error. To debug this try: debugmode(true);
How should I do it properly ?

Dynamic Neo4j Cypher Query

Is there a way to write the below neo4j cypher script to handle n case whens depending on the size of the array being read? I have varying array sizes on which to calculate co2 consumption so to use a one size fits all case when would be highly inefficient.
MATCH paths = allShortestPaths((a: Flights {label: 'Paris'})-[: FLIGHT*]->(b:Flights {label: 'Sydney'}))
WITH paths, relationships(paths) AS rels
UNWIND rels AS rel
WITH paths,
collect(rel.co2) AS co2,
collect(rel.engine_consumption) as ec
RETURN
reduce(acc1=0.0,
x IN range(0, size(fc)-1) |
case when x=0 then acc1 + co2[x]
when x=1 then acc1 + co2[x] * ec[x-1]
when x=2 then acc1 + co2[x] * ec[x-1] * ec[x-2]
when x=3 then acc1 + co2[x] * ec[x-1] * ec[x-2] * ec[x-3]
...
when x=size(ec)-1 then acc1 + co2[x] * ec[x-1] * ... * ec[0]
end
) AS normalised_co2
;
I did this in the end using the python library py2neo, and created the cypher query using python
case_when = ''
accumulator = ''
for x in range(max_paths):
accumulator += f'* co2[{x}]'
case_when += f'when x={x+1} then acc + co2[{x+1}]' + accumulator + ' \n '

How to solve simple linear programming problem with lpSolve

I am trying to maximize the function $a_1x_1 + \cdots +a_nx_n$ subject to the constraints $b_1x_1 + \cdots + b_nx_n \leq c$ and $x_i \geq 0$ for all $i$. For the toy example below, I've chosen $a_i = b_i$, so the problem is to maximize $0x_1 + 25x_2 + 50x_3 + 75x_4 + 100x_5$ given $0x_1 + 25x_2 + 50x_3 + 75x_4 + 100x_5 \leq 100$. Trivially, the maximum value of the objective function should be 100, but when I run the code below I get a solution of 2.5e+31. What's going on?
library(lpSolve)
a <- seq.int(0, 100, 25)
b <- seq.int(0, 100, 25)
c <- 100
optimal_val <- lp(direction = "max",
objective.in = a,
const.mat = b,
const.dir = "<=",
const.rhs = c,
all.int = TRUE)
optimal_val
b is not a proper matrix. You should do, before the lp call:
b <- seq.int(0, 100, 25)
b <- matrix(b,nrow=1)
That will give you an explicit 1 x 5 matrix:
> b
[,1] [,2] [,3] [,4] [,5]
[1,] 0 25 50 75 100
Now you will see:
> optimal_val
Success: the objective function is 100
Background: by default R will consider a vector as a column matrix:
> matrix(c(1,2,3))
[,1]
[1,] 1
[2,] 2
[3,] 3

Calculate time complexity for the following snippet

Can someone please calculate the the no. of steps it will take to execute the above code?
And verify the solution, with some input values of n.
(found some relevant question, but not helping)
int count=0;
for(int i=1; i<=n ;i=i*2)
{
for(int j=1; j<=i; j=j*2)
{
count++;
}
}
We can make a table:
i = 1: j = 1 --> 1 count
i = 2: j = 1,2 --> 2 counts
i = 4: j = 1,2,4 --> 3 counts
i = 8: j = 1,2,4,8 --> 4 counts
The pattern should be clear from here. We can reimagine the pattern such that i = 1, 2, 3, 4, ..., and instead of going from 1 to n, let's just say it goes from 1 to log n. This means that the total count should be the sum from i = 1 to log (base 2) n of i. The sum from i = 1 to x of i is simply x(x+1)/2, so if x = log_2(n), then this sum is simply (log_2(n) * log_2(n)+1)/2
EDIT: It seems like I made a mistake somewhere, and what I wrote is actually f(n/2) based on empirical tests. Thus, the correct answer is actually (log_2(2n) * log_2(2n)+1)/2. Nevertheless, this is the logic I would follow to solve a problem like this
EDIT 2: Caught my mistake. Instead of saying "let's just say it goes from 1 to log n", I should have said "let's just say it goes from 0 to log n" (i.e., I need to take the log of every number in the series)
inner-loop
i = 1 --> log(1) = 0
i = 2 --> log(2) = 1
i = 4 --> log(4) = 2
i = 8 --> log(8) = 3
i = 16 -> log(16) = 4
i = 32 -> log(32) = 5
i = 64 -> log(64) = 6
.
.
.
i = n -> log(n) = log(n)
That is the amount of work and it will stop after log(n) iterations as i hits n.
1 + 2 + 3 + 4 +...+ log(n) = [(1+log(n))*log(n)]/2 = O(log^2(n))

Hive - selecting the id for which the other field's value is ascending in consecutive timestamps

I need to select the equipment_id for which the "Reading" is ascending in in consecutive Timestamps from the below Hive table 'Whether_report'.
station_id equipment_id timpe_stamp Reading
1 100 00:00:01 60
2 100 00:00:02 61
3 100 00:00:03 62
4 100 00:00:04 60
5 100 00:00:05 61
. . . .
. . . .
16 114 00:00:11 66
17 114 00:00:12 65
. . . .
. . . .
. . . .
. . . .
29 112 00:00:23 71
30 113 00:00:24 69
for example:- i need to select the euipment_id whose Reading is in ascending for five consecutive timestamps (eg:- 60->61->62->63->64->65) and should not select the equipment_id for which the readings for consequent timestamps (eg:- 60->61->62->60->61). I am struggling to get the correct query.Any suggestion is much appreciated.
I tried a loop to for your requirement:
List<Integer> lis = new ArrayList<Integer>();
int j=0, flag=1, width=0;
lis.add(0, 60);
lis.add(1, 61);
lis.add(2, 61);
lis.add(3, 60);
lis.add(4, 61);
lis.add(5, 62);
lis.add(6, 64);
lis.add(7, 66);
lis.add(8, 68);
Iterable<Integer> itr = lis;
for(int i : itr)
{
if( j != 0) {
if( width == 4)
break;
if( i>j ) {
flag = 1;
width++;
}
else if( i<j && width != 4) {
flag = 0;
width = 0;
}
}
System.out.println(i);
j=i;
}
System.out.println("flag = "+flag+"width = "+ (width));
}
Output:
60
61
61
60
61
62
64
66
flag = 1 width = 4
I think if this can be plugged in the reducer class where the key is IntWritable equipment_id and value is Iterable IntWritable values and feed the values to this loop, assuming all time stamp values are unique.
Don't know if this is an optimal solution, considering the volume of data. Hope it helps !!!!!
You probably have to go to pig or MR. You are trying to find a sorted sub_sequence of length 5 in an bunch of readings, which probably cannot be achieved in a single query.