DAX If Value is 0 then dont use it in calculation - dynamic

I have four columns with respective weights (0.1667 or 0.3333) and if one of them (Ta, Tb, Tc, Td) are 0 I don't want them to be involved in my end calculation. Can someone help me with the denomiator part since it needs to be dynamic regarding the output?
var Tb= ('T2'[Col2] * 0.1667)
var Tc = ('T3'[Col3] * 0.3333)
var Td = ('T4'[Col4] * 0.3333)
Return end_calculation = Ta + Tb + Tc + Td / 0.1667 + 0.1667 + 0.3333 + 0.3333 ```
(Only take all four weights in the denomiator into account if it is not empty)

Use the IF function, and use DIVIDE to add support for divide-by-zero.
divide(Ta + Tb + Tc + Td, if(Ta>0,0.1667,0) + ...

Related

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

How TradingView Pine Script RMA function works internally?

I'm trying to re-implement the rma function from TradingView pinescript but I cannot make it output the same result as the original function.
Here is the code I developed, the code is basically the ema function, but it differs greatly from the rma function plot result when charting:
//#version=3
study(title = "test", overlay=true)
rolling_moving_average(data, length) =>
alpha = 2 / (length + 1)
sum = 0.0
for index = length to 0
if sum == 0.0
sum := data[index]
else
sum := alpha * data[index] + (1 - alpha) * sum
atr2 = rolling_moving_average(close, 5)
plot(atr2, title="EMAUP2", color=blue)
atr = rma(close, 5)
plot(atr, title="EMAUP", color=red)
So my question is how is the rma function works internally so I can implement a clone of it?
PS. Here is the link to the documentation https://www.tradingview.com/study-script-reference/#fun_rma It does show a possible implementation, but it does not work when running it.
Below is the correct implementation:
plot(rma(close, 15))
// same on pine, but much less efficient
pine_rma(x, y) =>
alpha = 1/y
sum = 0.0
sum := alpha * x + (1 - alpha) * nz(sum[1])
plot(pine_rma(close, 15))
There is a mistake in the code on TradingView, the alpha should be 1/y not y. This Wikipedia page has the correct formula for RMA
Wikipedia - moving averages

What is the Big-O of this nested loop?

int z=1;
for(int i=0;i*i<n;i++){
z*=3;
for(int j=0;j<z;j++){
// Some code
}
}
Answer is O(3^n).
Is it correct? How to figure out time complexity of nested loop?
outer loop: i goes from 1 to sqrt(n);
inner loop: j,z goes up to 3^(sqrt(n));
"some code" will run 1 + 3 + 3^2 + ... + 3^(sqrt(n)) times
let sum = 1 + 3 + 3^2 + ... + 3^(sqrt(n))
sum - 3*sum = 1 - 3(sqrt(n) + 1)
sum = 1 - 3(sqrt(n) + 1) / (1-3) = 2( 3^(sqrt(n)+1) - 1 )
2( 3^(sqrt(n)+1) - 1 ) << O( 3^sqrt(n) )
O(3^sqrt(n)) is more accurate
You could approach the problem using Sigma notation this way:

Recurrence Relation without using Master Theorem

I can easily solve some recurrence relations using the master theorem but I want to understand how to solve them w/o using the theorem
EX:
T(n) = 5T(n/2) + O(n) T(1) =1
Answer: O(n^{log_2(5)}
Expanding,
T(n) = 5T(n/2) + cn = 5(5T(n/4) + c(n/2)) + cn =
..... = 5^i * T(n/(2^i)) + cn*(1 + (5/2) + (5/2)^2 +......+ (5/2)^i)
Now let i= log_2(n)
then
5^(log_2(n)) * T(1) + cn*(1 + (5/2) + (5/2)^2 +......+ (5/2)^(log_2(n)))
After this I am lost . How do I get something similar to n^{log_2(5)?
Update:
Using the formula for the sum of geometric series (Sum = a* (1-r^n)/(1-r))
I get Sum = 1*(1-(5/2)^{log_2(n)})/(-3/2) = 2/3*c*(5^{log_2(n)} - n
How are 5^{log_2(n)} and n^{log_2(5)} related?
Thanks :D
I did not check the rest of your calculation, but note that
a^b = exp(b * ln(a))
and
log_b(a) = ln(a) / ln(b)
And thus
5^{log_2(n)} = exp(log_2(n) * ln(5)) = exp(ln(n) / ln(2) * ln(5))
and also
n^{log_2(5)} = exp(ln(5) / ln(2) * ln(n))