how can I model the incremental summation of one variable in GAMS constraints like the following:
y(t) is variable;
t is period index that t=1,…,4;
in constraints section I want this summation in each period:
t=1 : y(t=1) < t * 10
t=2 : y(t=2) < t * 10 – y(t=1)
t=3 : y(t=3) < t * 10 – y(t=2) – y(t=1)
t=4 : y(t=3) < t * 10 – y(t=3) – y(t=2) – y(t=1)
set t /1,2,3,4/;
* Create an alias of set t
alias (t,t1);
variable y(t);
equation incremental_summation(t);
* use ord and $-condition to formulate your equation
incremental_summation(t)..
y(t) <= t * 10 - sum(t1$(ord(t1)<ord(t)),y(y));
Related
I have this table called ltv
tick_lower
tick_upper
ltv_usdc
ltv_eth
204800
204880
38470
-30.252800179
204620
205420
1107583.610283
867.663698001
The problem is that ltv_usdc and ltv_eth is distributed equally between tick_lower and tick_upper. So, I aim to return a table where ltv will be calculated for each tick.
Here is an example of calculations for the first row in the above table.
tick
ltv_usdc
ltv_eth
204800
38470/(tick_upper-tick_lower) = 480.875
-30.252800179/(tick_upper-tick_lower) = -0.37816000223
204801
480.875
-0.37816000223
...
...
...
204880
480.875
-0.37816000223
Finally, I'm willing to group all the rows for each tick.
So far I haven't found a solution.
This can be done in a few steps:
Get the max ticks (tick_upper-tick_lower) in ltv
Generate a number series from 0 to max_ticks
Calculate all ticks for each row in ltv
Aggregate ltv_usdc and ltv_eth to tick
Tested in MySQL 8 db<>fiddle
with recursive cte_param as (
select max(tick_upper - tick_lower) as max_ticks
from ltv),
cte_n (i) as (
select 0 as i
union all
select n.i + 1
from cte_n n
where i < (select max_ticks from cte_param)),
cte_ltv as (
select tick_lower,
tick_upper,
ltv_usdc / (tick_upper - tick_lower) as ltv_usdc,
ltv_eth / (tick_upper - tick_lower) as ltv_eth
from ltv)
select l.tick_lower + n.i as tick,
sum(l.ltv_usdc) as ltv_usdc,
sum(l.ltv_eth) as ltv_eth
from cte_ltv l, cte_n n
where l.tick_lower + n.i <= l.tick_upper
group by tick
order by tick;
I am developing HR system, I want to calculate how many days off the employee can take.
In the HR table I have three attributes.
1- start_date(when the employee starting to work in the company).
2- leavestaken(how many days off the employee took before).
3- leaves(the number of days available for the employee to take off) which equal to=(months between current date and start_date) * 2.5(beacuse each month give 2.5 days off) - leavestaken
third attribute is automated and not from the user, how I can get the first and second attributive values from field forms, use them in the calculation and save the result in the leaves attribute?
I'm using sql and this only for server side
Server side:
If your start_date is unix timestamp:
if ($model->load(Yii::$app->request->post())) {
n_month = (time() - $model->leaves) / (30 * 24 * 60 * 60);
$model->leaves = ceil(n_month * 2.5) - $model->leavestaken;
# code...
//ceil : Because one day off is not complete, Rounds a number down to the nearest integer
But if it's not unix, you can use the date_diff() method or ...
You can do this in the model file, in the beforeSave or beforeValidate method.
You can use JavaScript to calculate the client side.
My solution was
if ($model->load(Yii::$app->request->post()) ) {
$date = date('m/d/Y');
$datetime1 = date_create($date);
$datetime2 = date_create($model->start_date);
// calculates the difference between DateTime objects
$interval = $datetime2->diff($datetime1);
$n_month= $interval->m + 12*$interval->y;
$model->leaves = ceil($n_month * 2.5) - $model->leavestaken;
$model->save();
Calculating geometrically link returns
How do you multiply record2 * record1?
The desire is to return a value for actual rate and annulized rate
Given table unterval:
EndDate PctReturn
-------------------------------
1. 05/31/06 -0.2271835
2. 06/30/06 -0.1095986
3. 07/31/06 0.6984908
4. 08/31/06 1.4865360
5. 09/30/06 0.8938896
The desired output should look like this:
EndDate PctReturn Percentage UnitReturn
05/31/06 -0.2271835 -0.002272 0.997728
06/30/06 -0.1095986 -0.001096 0.996634669
07/31/06 0.6984908 0.006985 1.00359607
08/31/06 1.4865360 0.014865 1.018514887
09/30/06 0.8938896 0.008939 1.027619286
Percentage = PctReturn/100
UnitReturn (1 + S1) x (1 + S2) x ... (1 + Sn) - 1
Aggregating values desired:
Actual Rate 2.761928596
Annulized 6.757253223
Mathematics on aggregating value:
Actual Rate 1.027619 1.027619-1 = 0.027619 * 100 = 2.761928596
Annulized Rate 6.757253 (ActualRate^(12/number of intervals)-1)*100
Number of intervals in Example = 5
there are only 5 records or intervals
I did try utilizing the sum in the select statement but this did not allow for multiplying record2 by record1 to link returns. I thought utilizing the while function would allow for stepping record by record to multiply up the values of unitreturn. My starter level in SQL has me looking for help.
You have two option for getting a product in SQL Server.
1. Simulate using logs and exponents:
SQL Fiddle
create table returns
(
returnDate date,
returnValue float
)
insert into returns values('05/31/06', -0.002271835)
insert into returns values('06/30/06', -0.001095986)
insert into returns values('07/31/06', 0.006984908)
insert into returns values('08/31/06', 0.014865360)
insert into returns values('09/30/06', 0.008938896)
select totalReturn = power
(
cast(10.0 as float)
, sum(log10(returnValue + 1.0))
) - 1
from returns;
with tr as
(
select totalReturn = power
(
cast(10.0 as float)
, sum(log10(returnValue + 1.0))
) - 1
, months = cast(count(1) as float)
from returns
)
select annualized = power(totalReturn + 1, (1.0 / (months / 12.0))) - 1
from tr;
This leverages logs and exponents to simulate a product calculation. More info: User defined functions.
The one issue here is that it will fail for return < -100%. If you don't expect these it's fine, otherwise you'll need to set any values < 100% to -100%.
You can then use this actual return to get an annualized return as required.
2. Define a custom aggregate with CLR:
See Books Online.
You can create a CLR custom function and then link this an aggregate for use in your queries. This is more work and you'll have to enable CLRs on your server, but once it's done once you can use it as much as required.
I have collection of multidimensional object (e.g class Person = {age : int , height : int, weight : int etc...}).
I need to query the collection with queries where some dimensions are fixed and the rest unspecified (e.g getallPersonWith {age = c , height = a} or getAllPersonWith {weigth = d}...)
Right now i have a multimap with {age, Height,...} (e.g all dimension that can be fixed) -> List : Person.To perform a query i first compute the set of keys that verify the query, then merge the corresponding list from the map.
Is there anything better, in terms of query speed ? in particular is there anything closer to using one sorted list by dimension (which i believe to be the fastest solutions, but too cumbersome to manage:) )
Just to be clear, i am not looking for an sql query.
For your purpose you can have a look at:
http://code.google.com/p/cqengine/
Should get you in the right direction
You mean something like:
SELECT * FROM person p
WHERE gender = 'F'
AND age >=18
AND age < 30
AND weight > 60 -- metric measures here !!
AND weight < 70
AND NOT EXISTS (
SELECT * from couple c
WHERE c.one = p.id OR c.two=p.id
);
Why do you think I use SQL?
I have a database with 101 simulations for, lets say, 5 different asset classes returns.
I need to write a query that will calculate the respective correlations between each of the 5 classes. Table will look something like this:
AssetClass_ID | Simulation | AssetClass_Value
Any ideas? I am struggling to get even close.
(Depending on difficulty I may end up having to tell the end user to just download all the simulations and do the stats using inbuilt EXCEL functions, but I am unlikely to be popular for doing so)
Ok, with some google and some work I came up with:
SELECT
AssetID_1, AssetID_2,
((psum - (sum1 * sum2 / n)) / sqrt((sum1sq - sum1*sum1 / n) * (sum2sq - sum2*sum2 / n))) AS [Correlation Coefficient],
n
FROM
(SELECT
n1.AssetClass_ID AS AssetID_1,
n2.AssetClass_ID AS AssetID_2,
SUM(n1.RunResults_Value) AS sum1,
SUM(n2.RunResults_Value) AS sum2,
SUM(n1.RunResults_Value * n1.RunResults_Value) AS sum1sq,
SUM(n2.RunResults_Value * n2.RunResults_Value) AS sum2sq,
SUM(n1.RunResults_Value * n2.RunResults_Value) AS psum,
COUNT(*) AS n
FROM
dbo.tbl_RunResults AS n1
LEFT JOIN dbo.tbl_RunResults AS n2 ON n1.Simulation_ID = n2.Simulation_ID
WHERE
n1.AssetClass_ID < n2.AssetClass_ID AND
n1.series_ID = 2332 AND
n2.series_ID = 2332
GROUP BY
n1.AssetClass_ID, n2.AssetClass_ID) AS step1
ORDER BY
AssetID_1
Answers match Excel inbuilt functions so far, so good.