Variable elimination in Bayes Net on a node with single child - artificial-intelligence

If we have a node X, that has a child Y in a Bayes net, why is it correct to express P(Y) as P(Y|X)P(X)? Does it then follow that X is a necessary condition for Y?

Bayes Networks
An edge in a Bayes Network means the variable is conditionally dependent. If nodes are not connected by any path, they are conditionally independent.
Having a node X with a child Y means you need to learn:
Given X is True, what is the probability of Y being True?
Given X is False, what is the probability of Y being True?
More generally: If X can have n values and Y can have m values, then you have to learn n * (m - 1) values. The - 1 is there because the probabilities need to sum up to 1.
Example
Let's stick with the simple case that both variables are binary and use the following from Wikipedia:
Say X is RAIN and Y is SPRINKLER. You want to express Y (SPRINKLER) in terms of X (RAIN).
The Bayes theorem states:
P(Y|X) = P(X|Y) * P(Y) / P(X)
<=> P(Y) = P(Y | X) * P(X) / P(X | Y)
Now we apply the Law of total probability for X. This means, for X we simply go through all possible values:
P(Y) = P(Y | X = true) * P(X = true) +
P(Y | X = false) * P(X = false)
I guess this is what you refer to. P(X=true | Y) = 1, because the X=true means we already know that X=true happened. It doesn't matter what Y is.
To continue our case, we now look up the values in the tables (X is RAIN, Y is SPRINKLER):
P(Y) = 0.01 * 0.2 + 0.4 * 0.8
= 0.322

Related

How to create meshgrid with non-integer stepsize of list elements?

I have 2 lists of x and y coordinate that are independently generated, with a/h amount of points between 0 and a.
x = np.linspace(0, a, a/h)
y = np.linspace(0, d, d/h)
when a/h is such that 0 increases to a in steps of integers i.e. [0,1,2,..,a]. It's nice because then the number of elements within the list can be used as indices. And as a result I can usually create a meshgrid such that a third list V1 can be associated with it.
X, Y = plt.meshgrid(x, y)
def potential(V1):
return V1[X, Y]
where potential(V1) is now V1 corresponding to the meshgrid [x, y]. However I'm doing an assignment where I'm required to investigate how step-sizes affect my problem. As a result if I was to have a step-size of non-integers from 0 to a i.e. [0, 0.5, 1,...,a] Now I can't do what I did above since the indices are now non-integers. Raising the error
IndexError: arrays used as indices must be of integer (or boolean) type
How can I fix this so that I don't rely on the value of the element itself as the index of the elements, so that if there was a step-size of 0.25 between 0 to a for a list X say i.e.
X = [0, 0.25, 0.75. 1.0] or x = np.linspace(0,1,4)
such that I can have
x[0] = 0 corresponds to V[0]
x[1] = 0.25 corresponds to V[1]
x[2] = 0.75 corresponds to V[2]
x[3] = 1 corresponds to V[3]
?

How to find all possible options in C?

I'm trying to find a efficient algorithm in C, which provides me all options of a given equation.
I have equation AX + BY = M, where A, B and M i got on input (scanf).
For example lets have: 5X + 10Y = 45
1st option: 5 * 9 + 10 * 0
2nd option: 5 * 7 + 10 * 1
n-th option: 5 * 1 +
10 * 4
And also I need to count how many possible options exist?
Some tips, hints?
I forgot to say that X and Y are in Z and >= 0, so there is no infinite options.
The question makes sense if you restrict to non-negative unknowns.
Rewrite the equation as
AX = M - BY.
There can be positive solutions as long as the RHS is positive, i.e.
BY ≤ M,
or
Y ≤ M/B.
Then for a given Y, there is a solution iff
A|(M - BY)
You can code this in Python as
for Y in range(M / B + 1):
if (M - B * Y) % A == 0:
X= (M - B * Y) / A
The solutions are
9 0
7 1
5 2
3 3
1 4
The number of iterations equals M / B. If A > B, it is better to swap X and Y.
you can calcule every solution if you put some limit in your input value, for example: use X and Y in a value included from 0 to 9... in this way you can use for to calculate every solution.
The number of solution is infinite:
find a first solution like: X=9, Y=0.
you can create another solution by using:
X' = X+2*p
Y' = Y-p
For any p in Z.
This proves your program will never terminate.

How to find the shortest path in 2D Array with multiple endpoints?

I have a program that creates a 2D array that looks similar to this:
X X X X X X X B X X
B X X X X X X X X X
B X X X X X X X X X
X X X X X X X B X X
X X X X X O B X X X
B X X X X X B X X X
X X X X X X X X X X
X X X X X X X X X X
X = empty space free to roam
B = blocked areas not free to roam
O = object being moved
And I would like help on figuring out how to find the shortest paths to any of the end points. Normally I would of used the Dijkstra's Algorithm however I have multiple points that are to be considered instead of having 1 point. The point of the object is getting to the edge and I would like to find the shortest path there.
Lee's algorithm: http://en.wikipedia.org/wiki/Lee_algorithm
It's essentially a BF search, here's an example: http://www.oop.rwth-aachen.de/documents/oop-2007/sss-oop-2007.pdf
To implement it effectively, check out: Change FloodFill-Algorithm to get Voronoi Territory for two data points? - when I say mark, you mark it with the number on the position you came from + 1.
For example, if you have this grid, where a * = obstacle and you can move up, down, left and right, and you start from S and must go to D, and 0 = free position:
S 0 0 0
* * 0 *
* 0 0 *
0 0 * *
* 0 0 D
You put S in your queue, then "expand" it:
S 1 0 0
* * 0 *
* 0 0 *
0 0 * *
* 0 0 D
Then expand all of its neighbours:
S 1 2 0
* * 0 *
* 0 0 *
0 0 * *
* 0 0 D
And all of those neighbours' neighbours:
S 1 2 3
* * 3 *
* 0 0 *
0 0 * *
* 0 0 D
And so on, in the end you'll get:
S 1 2 3
* * 3 *
* 5 4 *
7 6 * *
* 7 8 9
So the distance from S to D is 9. The running time is O(NM), where N = number of lines and M = number of columns. I think this is the easiest algorithm to implement on grids, and it's also very efficient in practice. It should be faster than a classical dijkstra, although dijkstra might win if you implement it using heaps.
Do this for any starting point and endpoint and chose the endpoint with the lowest integer in your case.

How to define some variables as non-commutative in Maxima

For example, I'd like to define x and y as non-commutative, and a and b as commutative (as usual). In other words,
x y ≠ y x, a x = x a, a b = b a .
Further,
(x + a y) (x - a y) = x^2 + a (y x - x y) - a^2 y^2 .
What is a code for defining x and y, and a symbol for multiplication (such as * and . ) ?
You can work with Maxima's commutative * and non-commutative . products in the way that you want by following the next two steps:
Declare the symbols a and b as scalars:
declare([a, b], scalar)$
Enable dotscrules:
dotscrules: true$
This simplifies non-commutative products involving scalars to commutative products (i.e., a.x becomes a*x).
Now you are ready. For example,
expand((a*x + b*y) . (a*x - b*y))
returns
a*b*y.x - b^2*y^^2 - a*b*x.y + a^2*x^^2
(note that ^^ is the non-commutative exponentiation operator).

tsql conditional maths equation

I need to perform some summations during a select query however depending on 2 values i will need to perform a different equation. hopefully an example will demonstrate
basically i need to perform the following summations
if x > y then (x - y + z) or
if x < y then (x - x + z) basically i am setting this to 0.
So far i thought that i could use 2 tables to dump the x > y values and the x < y values and then perform the relevant equations.
any ideas
You can use a case expression.
select case
when x > y then x - y + z
when x < y then x - x + z
else 0 -- x = y
end
from YourTable

Resources