tf.where() not behaving as expected for manipulating tensors - tensorflow

I have tried the following code:
a = tf.where(tf.greater_equal(x,1.0),x*tf.math.log(b + 1e-19), (1-x)*tf.math.log(1 - b + 1e-19))
does not produce the same results as:
a = x*tf.math.log(b + 1e-19) + (1-x)*tf.math.log(1 - b + 1e-19)
Here x is a binary variable either 0 or 1. b is real valued between 0 and 1.
Is there something I am missing?
The way I compared the 2 answers was tf.reduce_sum(a)
Solution found: The 2 are indeed equivalent for x = 0 or x = 1. The data I used was a 2D tensor, which had some bits not 0 or 1.
This was discovered via
tf.unique(tf.reshape(x, (-1,))

Code sample:
# When x = 0.0
x = 0.0
b = 0.5
a = tf.where(tf.greater_equal(x,1.0),x*tf.math.log(b + 1e-19), (1-x)*tf.math.log(1 - b + 1e-19)) # -0.6931472 from (1-x)*tf.math.log(1 - b + 1e-19)
c = x*tf.math.log(b + 1e-19) + (1-x)*tf.math.log(1 - b + 1e-19) # 0 + (1-x)*tf.math.log(1 - b + 1e-19) = -0.6931472
# When x = 1.0
x = 1.0
b = 0.5
a = tf.where(tf.greater_equal(x,1.0),x*tf.math.log(b + 1e-19), (1-x)*tf.math.log(1 - b + 1e-19)) # -0.6931472 from x*tf.math.log(b + 1e-19)
c = x*tf.math.log(b + 1e-19) + (1-x)*tf.math.log(1 - b + 1e-19) # x*tf.math.log(b + 1e-19) + 0 = -0.6931472
# When x = 0.4
x = 0.4
b = 0.5
a = tf.where(tf.greater_equal(x,1.0),x*tf.math.log(b + 1e-19), (1-x)*tf.math.log(1 - b + 1e-19)) # -0.41588834 from (1-x)*tf.math.log(1 - b + 1e-19)
c = x*tf.math.log(b + 1e-19) + (1-x)*tf.math.log(1 - b + 1e-19) # x*tf.math.log(b + 1e-19) + (1-x)*tf.math.log(1 - b + 1e-19) = -0.27725 + -0.41588 = -0.6931471824645996.
The only case where both codes mentioned in your question will produce the same results is when x = 1 or 0.
tf.reduce_sum(a)
Here, a is a scalar so this won't change the value of a.

Related

How to maximise an function in vb.net?

I have a function T which I'm wanting to maximise:
T = (B((A + 1.646) + 0.583 w) + 311)((1 + ((R + 0.553) + 0.0389 x)((D) + 0.0389 y)
with the constraints
w + x + y + z = 25
w >= 0
x >= 0
y >= 0
z >= 0
B, A, R, D are all constants.
How would I go about doing this?

Determine time complexity of arithmetic progression

I am novice in analysing time complexity.some one can help me with the time complexity of below algorithm?
public void test(int n)
{
for(int i=1;i<=n;i=i+2)
{
for(int j=1;j<=i;j++)
{}
}
}
outer loop will run n/2 times.Inner loop will run (1+3+5+7+9...n) times.
what will be time complexity of inner loop and how can we calculate sum of such arithmitic progression?
Assume n is odd. Then n = 2k + 1 for some k. Now
1 + 3 + … + n
= 1 + 3 + … + 2k+1
= (1 + 3 + … + 2k + 1) + (1 + 1 + … + 1) - (1 + 1 + … + 1)
= (1 + 1) + (3 + 1) + … + (2k + 1 + 1) - (1 + 1 + … + 1)
= 2 + 4 + … + 2k+2 - (1 + 1 + … + 1)
= 2(1 + 2 + … + k+1) - (1 + 1 + … + 1)
= 2(k+1)(k+2)/2 - (k+1)
= k^2 + 3k + 2 - k - 1
= k^2 + 2k + 1
= (k+1)^2
= (n+1)^2/4
We can test a few terms...
n f(n) Series Sum
1 1 1 = 1
3 4 1 + 3 = 4
5 9 1 + 3 + 5 = 9
7 16 1 + 4 + 5 + 7 = 16
Looks like it checks out.

How to plot implicit functions (with two variables) in scipy python using matplotlib?

I have a function with two independent variables x & y and three parameters a,b & c. I done optimization of parameters using least_squares. Now I want to plot the function using the obtained parameters.
xdata = [0.3063191028,-0.0156344344,-0.0155750443,-0.7206687321,-0.7473645659,-0.9174428618,-0.8839320182,-1.0399645639,-0.9997277955,-1.0157928079,-0.9888297188,-0.4533985964,0.0091163748,0.0026577054,0.5926386016,0.5992457462,1.004345373,0.9909529136,1.0392221881,1.0405287695,1.0606471537,1.0283835014,1.0149316519,0.9416591604,0.9685628594,0.9155869223,0.9088016075,0.6344640542,0.6142268898]
ydata = [1.1154790304,0.9978867036,1.0111900779,0.5702040049,0.5903372366,-0.0072010453,-0.0007720708,-0.45206232,-0.4390262009,-1.0375889614,-0.9978570085,-1.0612855969,-0.957932038,-0.904673998,-0.7489532501,-0.7689528542,-0.0266364437,-0.0265473586,0.2857701407,0.5784443763,0.5849624358,0.8579043226,0.8446900334,0.9316519346,0.9580805131,1.091470597,1.071560078,1.1199481327,1.0868233245]
xdata = np.array(xdata)
ydata = np.array(ydata)
def func1(coeff,x,y):
# x = X[0]
# y = X[1]
n = 8
# % A = ydata
# % B = -xdata
# % C = xdata. - ydata
# % H = zdata
g = np.subtract(x,y)
I_0 = np.subtract(x,y) # x-y = C
I_1 = np.multiply(I_0,coeff[2]) # c(x-y) = cC
I_2 = np.multiply(coeff[1],-x) #b(-x) = bB
I_3 = np.multiply(coeff[0],y) # aA
I3_0 = np.subtract(I_1,I_2) # cC-bB
I3_1 = np.subtract(I_3,I_1) # aA-cC
I3_2 = np.subtract(I_2,I_3) # bB-aA
I3_00 = np.multiply(I3_0,I3_1) # (cC-bB)(aA-cC)
I3_01 = np.multiply(I3_00,I3_2) # (cC-bB)(aA-cC)(bB-aA)
I3 = np.divide(I3_01,54) # (cC-bB)(aA-cC)(bB-aA)/54
I2_0 = np.power((I3_1),2) # (aA-cC)^2
I2_1 = np.power((I3_0),2) # (cC-bB)^2
I2_2 = np.power((I3_2),2) # (bB-aA)^2
I2_00 = np.add(I2_0,I2_1) # (aA-cC)^2 + (cC-bB)^2
I2_01 = np.add(I2_00,I2_2) # (aA-cC)^2 + (cC-bB)^2 + (bB-aA)^2
I2 = np.divide(I2_01,54) # ((aA-cC)^2 + (cC-bB)^2 + (bB-aA)^2)/54
th_0 = np.divide(I3,(np.power(I2,(3/2)))) # I3/(I2^(3/2))
# print(th_0)
th = np.arccos(np.clip((th_0),-1,1)) # arccos(I3/(I2^(3/2)))
# print(th)
ans_0 = np.divide(np.add((2*th),(np.pi)),6) # (2*th + pi)/6
ans_1 = np.divide(np.add((2*th),(3*np.pi)),6) # (2*th + 3*pi)/6
ans_2 = np.divide(np.add((2*th),(5*np.pi)),6) # (2*th + 5*pi)/6
ans_00 = np.multiply(np.cos(ans_0),2) # 2*cos((2*th + pi)/6)
ans_11 = np.multiply(np.cos(ans_1),2) # 2*cos((2*th + 3*pi)/6)
ans_22 = np.multiply(np.cos(ans_2),2) # 2*cos((2*th + 5*pi)/6)
ans_000 = np.power(np.absolute(ans_00),n) # (abs(2*cos((2*th + pi)/6)))^n
ans_111 = np.power(np.absolute(ans_11),n) # (abs(2*cos((2*th + 3*pi)/6)))^n
ans_222 = np.power(np.absolute(ans_22),n) # (abs(2*cos((2*th + 5*pi)/6)))^n
ans_0000 = np.add((np.power(np.absolute(ans_00),n)),(np.power(np.absolute(ans_11),n))) # (abs(2*cos((2*th + pi)/6)))^n + (abs(2*cos((2*th + 3*pi)/6)))^n
ans_1111 = np.add((ans_0000),(np.power(np.absolute(ans_22),n))) # (abs(2*cos((2*th + pi)/6)))^n + (abs(2*cos((2*th + 3*pi)/6)))^n + (abs(2*cos((2*th + 5*pi)/6)))^n
sna_0 = np.power(np.multiply(3,I2),(n/2)) # (3*I2)^(n/2) !!
sna_1 = 2*(np.power(1.0167,n)) # 2*(sigma^n) !!
sna_00 = np.multiply(sna_0,ans_1111)
sna_11 = np.subtract(sna_00,sna_1)
return sna_11
x0 = np.array([1.0, 1.0, 1.0])
res_lsq = least_squares(func1, x0,loss='cauchy',f_scale=0.001,args=(xdata, ydata))
res_lsq.x
You may wish to consider mplot3D from Matplotlib. Please check out https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

How is a local variable in another function affecting a variable in my main function?

So I have a "main" function (SolveSixODES) that calls a secondary function (AllODEs). And when it does this, the x value in the main function gets modified. I don't understand how this can be possible, seeing as it is not a global variable.
Here is the code, my inputs I used are as follows:
x=0, xmax=3, y=0-6, h=0.1, error=0.1
Public Function SolveSixODE(x As Double, xmax As Double, Y As Range, h As Double, error As Double) 'Weird bug: You must leave the first y4 value blank
Dim i As Integer, k(7, 7) As Double, j As Integer, m As Integer 'k(Order #, equation #)
Dim Y5(7) As Double, Y4(7) As Double, Y4Old(7) As Double
Dim delta0(7) As Double, delta1(7) As Double, delRatio(7) As Double, Rmin As Double
For i = 1 To 6 'Moving the input data so it can acutally be used
Y4(i) = Y(i)
Next i
While x < xmax
If x + h < xmax Then
x = x + h
Else
h = xmax - x
x = xmax
End If
For j = 1 To 6 'j is the order i is equation number
For i = 1 To 6 'Calculating all of the k(1) values for eq 1 to 6
k(j, i) = AllODES(x, Y4, i, j, k, h) '!!!!!SOME HOW THIS LOOP MAKES X negative...!!!!!!!
Next i
Next j
For i = 1 To 6
Y4Old(i) = Y4(i) 'Saving old y4 value to calc delta0
Y4(i) = Y4(i) + h * (k(1, i) * (37 / 378) + k(3, i) * (250 / 621) + k(4, i) * (125 / 594) + k(6, i) * (512 / 1771))
Y5(i) = Y4(i) + h * (k(1, i) * (2825 / 27648) + k(3, i) * (18575 / 48384) + k(4, i) * (13525 / 55296) + k(5, i) * (277 / 14336) + k(6, i) * (0.25))
delta0(i) = error * (Abs(Y4Old(i)) + Abs(h * AllODES(x, Y4Old, i, 1, k, h))) 'First order because we don't want to use the k vals
delta1(i) = Abs(Y5(i) - Y4(i))
delRatio(i) = Abs(delta0(i) / delta1(i)) 'Ratio of errors
Next i
Rmin = delRatio(1)
For i = 2 To 6
If delRatio(i) < Rmin Then
Rmin = delRatio(i) 'Determine the smallest error ratio
End If
Next i
If Rmin < 1 Then 'If this is true then the step size was too big must repeat step
x = x - h 'Set x and y's back to previous values
For i = 1 To 6
Y4(i) = Y4Old(i)
Next i
h = 0.9 * h * Rmin ^ 0.25 'adjust h value; 0.9 is a safety factor
Else
h = 0.9 * h * Rmin ^ 0.2 'Otherwise, we march on
End If
m = m + 1
Wend
SolveSixODE = Y4
End Function
Public Function AllODES(x As Double, Y() As Double, EqNumber As Integer, order As Integer, k() As Double, h As Double) As Double
Dim conc(7) As Double, i As Integer, j As Integer
If order = 1 Then
x = x - h
For i = 1 To 6 'Movin the data so I can use it
conc(i) = Y(i) 'also adjusting the x and y values for RK4 (Cash Karp values)
Next i
ElseIf order = 2 Then
x = x - h + h * 0.2
For i = 1 To 6
conc(i) = Y(i) + h * k(1, i) * 0.2
Next i
ElseIf order = 3 Then
x = x - h + 0.3 * h
For i = 1 To 6
conc(i) = Y(i) + h * (0.075 * k(1, i) + 0.225 * k(2, i))
Next i
ElseIf order = 4 Then
x = x - h + 0.6 * h
For i = 1 To 6
conc(i) = Y(i) + h * (0.3 * k(1, i) - 0.9 * k(2, i) + 1.2 * k(3, i))
Next i
ElseIf order = 5 Then
x = x - h + h
For i = 1 To 6
conc(i) = Y(i) + h * ((-11 / 54) * k(1, i) + 2.5 * k(2, i) - (70 / 27) * k(3, i) + (35 / 27) * k(4, i))
Next i
ElseIf order = 6 Then
x = x - h + 0.875 * h
For i = 1 To 6
conc(i) = Y(i) + h * ((1631 / 55296) * k(1, i) + (175 / 512) * k(2, i) + (575 / 13824) * k(3, i) + (44275 / (110592) * k(4, i) + (253 / 4096) * k(5, i)))
Next i
Else
MsgBox ("error")
End If
If EqNumber = 1 Then 'These are the actual equations
AllODES = x + Y(1)
ElseIf EqNumber = 2 Then
AllODES = x
ElseIf EqNumber = 3 Then
AllODES = Y(3)
ElseIf EqNumber = 4 Then
AllODES = 2 * x
ElseIf EqNumber = 5 Then
AllODES = 2 * Y(2)
ElseIf EqNumber = 6 Then
AllODES = 3 * x
Else
MsgBox ("You entered an Eq Number that was dumb")
End If
End Function
It's possible that it is something really trivial that I missed but this seems to contradict my knowledge of how variables work. So if you understand how the function is able to manipulate a variable from another function in this case, I would appreciate any advice and/or explanation!
Thanks in advance!
the x value in the main function gets modified. I don't understand how this can be possible, seeing as it is not a global variable
This is normal because you are passing x by reference to the function AllODES and you do change it there. When the keyword ByVal is not explicitly specified in the function/sub prototype, the default passing mechanism is ByRef, that is, by reference.
Public Function AllODES(x As Double, ...
means
Public Function AllODES(ByRef x As Double, ....
We observe that x is manipulated in this function, so the change will appear in the caller. If you want that the change of x does not report back in the caller's scope, pass x by value:
Public Function AllODES(ByVal x As Double, ....
' ^^^^^
Only in this case the x of the caller and the x of the callee will be two different variables.

Gamma distribution, incomplete beta function

I need to calculate Gamma cumulative distribution, and it seems this is fairly equivalent to calculating the incomplete beta function.
Excel does have an inculded calculator, but I found no trace of the used algorithm.
Do any of you know an accurate way to calculate this function?
I tried the following, translated into VB.NET from a website, but it gives stupid results:
Function IncompleteBetaFunc(x As Double, a As Double, b As Double) As Double
If x <= 0 Or x >= 1 Then Return 0
Dim bt As Double
bt = Math.Exp(GammaLn(a + b) - GammaLn(a) - GammaLn(b) + a * Math.Log(x) + b * Math.Log(1.0 - x))
If x < (a + 1.0) / (a + b + 2.0) Then
Return bt * betacf(a, b, x) / a
Else
Return 1.0 - bt * betacf(b, a, 1.0 - x) / b
End If
End Function
Function betacf(x As Double, a As Double, b As Double) As Double
Const MAXIT As Integer = 100
Const EPS As Double = 0.0000003
Const FPMIN As Double = 1.0E-30
Dim aa, c, d, del, h, qab, qam, qap As Double
Dim m, m2 As Integer
qab = a + b
qap = a + 1.0
qam = a - 1.0
c = 1.0
d = 1.0 - qab * x / qap
If (Math.Abs(d) < FPMIN) Then d = FPMIN
d = 1.0 / d
h = d
For m = 1 To MAXIT
m2 = 2 * m
aa = m * (b - m) * x / ((qam + m2) * (a + m2))
d = 1.0 + aa * d
If (Math.Abs(d) < FPMIN) Then d = FPMIN
c = 1.0 + aa / c
If (Math.Abs(c) < FPMIN) Then c = FPMIN
d = 1.0 / d
h *= d * c
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
d = 1.0 + aa * d
If (Math.Abs(d) < FPMIN) Then d = FPMIN
c = 1.0 + aa / c
If (Math.Abs(c) < FPMIN) Then c = FPMIN
d = 1.0 / d
del = d * c
h *= del
If (Math.Abs(del - 1.0) < EPS) Then Exit For
Next
Return h
End Function
Thanks!
Meta.Numerics includes well-tested and performant code for this any many other special functions. Its incomplete Beta function is documented here. The underlying code can be studied here. It also has a full-on Gamma distribution object, which will give moments, generate random variates, and do other distribution-related stuff in addition to computing the CDF. The package available via NuGet; just search for Meta.Numerics in the VS NuGet interface.