PyMC and MCMC: How to ignore a sample? - pymc

I have an if-condition in my model. Only when this if-statement is true, I want to return the result to the sampler.
For the else-statement, is there a return value I can use to tell the sampler to ignore the draw?

Any time you return a likelihood of -inf you will be guaranteed that a draw is rejected, and the current draw will be retained, according to the Metropolis algorithm. So, you can return -np.inf when your condition is not satisfied, if you like.

Related

Can someone explain me this code intuitively?

I understand recursion and what the advantages it brings to writing code efficiently. While I can code recursive functions, I cannot seem to wrap my head around how they work. I would like someone to explain me recursion instinctively.
For example, this code:
int fact(int n)
{ if n<0:
return -1
elif n==0:
return 1
else
return n*fact(n-1)
}
These are some of my questions:
Let's say n=5. On entering the function,the control goes to the last return statement since none of the previous conditions are satisfied.
Now, roughly, the computer 'writes' something like this: 5*(fact(4))
Again, the fact() function is called and the same process gets repeated except now we have n=4.
So, how exactly does the compiler multiply 5*4 and so on until 2 since its not exactly 5*4 but 5*fact(4). How does it 'remember' that it has to multiply two integers and where does it store the temporary value since we haven't provided any explicit data structure?
Again let's say n=5. The same process goes on and eventually n gets decremented to 0. My question is why/how doesn't the function simply return 1 as stated in the return statement. Similar to my previous question, how does the compiler 'remember' that it also has 180 stored for displaying?
I'd be really thankful if someone explains this to me completely so that can understand recursion better and intuitively.
Yeah, for beginners recursion can be quite confusing. But, you are already on the right track with your explanation under "1.".
The function will be called recursively until a break condition is satisfied. In this case, the break condition is satisfied when n equals 0. At this point, no recursive calls will be made anymore. The result of each recursive call is returned to the caller. The callers always "wait" until they get a result. That's how the algorithm "knows" the receiver of the results. The flow of this procedure is handled by the so called stack.
Hence, in your informal notation (in this example n equals 3):
3*(fact(2)) = 3*(2*fact(1)) = 3*(2*(1*fact(0))).
Now, n equals 0. The inner fact(0) therefore returns 1:
3*(2*(1*(1)))) = 3*(2*(1)) = 3*(2) = 6
You can see a bit like this
The function fact(int n) is like a class and every time you call fact(int n) you create an instance of that class. By creating them (calling them) from the same function, you are creating a chain of instances. Once you reach break condition, those functions start returning one by one and the value they returned to calculate a new value in the return statement return n*fact(n-1) e.g. return 3*fact(2);

Why is the region growing implementation not working properly?

Region growing is a simple region-based image segmentation method. It is also classified as a pixel-based image segmentation method since it involves the selection of initial seed points.I wrote the following in matlab and there seems to be a infinite loop apparently.I wish to know where the implementation is failing.
import java.util.LinkedList
a=imread('C:\Users\hpw\Desktop\1.jpeg');
s=size(a);
visited=zeros(s(1),s(2));
x=179;
y=180;
%seed chosen
visited(179,180)=1;
boundaryx = LinkedList();
boundaryy = LinkedList();
boundaryx.add(x);
boundaryy.add(y);
while(boundaryx.size()>0 &&boundaryy.size()>0)
nextx=boundaryx.pop();
nexty=boundaryy.pop();
if(a(nextx,nexty)>110)
visited(nextx,nexty)=2;
end
%taking 4 neighbors only
if(nextx>1 && nexty>1)%right neighbor
if(visited(nextx+1,nexty)==0)
boundaryx.add(nextx+1);
boundaryy.add(nexty);
end
if(visited(nextx-1,nexty)==0)
boundaryx.add(nextx+1);
boundaryy.add(nexty);
end
if(visited(nextx,nexty+1)==0)
boundaryx.add(nextx+1);
boundaryy.add(nexty);
end
if(visited(nextx+1,nexty-1)==0)
boundaryx.add(nextx+1);
boundaryy.add(nexty);
end
end
end
You will always get a problem like that when using the while loop. Try implementing the condition at which it's out of bounds. Or implement a condition at which the you break; out of the loop.
Like something like this right at before the end %while:
if boundaryy.size() >= 1000 && boundaryx.size() >= 1000
break;
end
It's maybe not the condition you search for but this loop was infinite until I set a condition at which while can break;. If you look at your boundary condition for your while loop you can see that boundaryy.size()>0 is ALWAYS true. This leads to another Method to stop the while loop without break;.
while(boundaryx.size()<1000 &&boundaryy.size()<1000)
...
end
This way the boundaryy.size() and boundaryx.size() will eventually increase and reach the boundary condition 1000.
Hope this helps :)

Depth-Limited Search Recursive Pseudocode

I'm looking at a pseudocode implementation of Depth-Limited Search, and I'm having trouble understanding it.
The pseudocode is:
Function Recursive-DLS(node, problem, limit)
cutoff-occurred? = false
if Goal-Test(problem, State[node]) then return node
else if Depth[node] = limit then return cutoff
else for each successor in Expand(node, problem) do
result = Recursive-DLS(successor, problem, limit-1)
if result = cutoff then cutoff-occurred? = true
else if result != failure then return result
if cutoff-occurred? then return cutoff else return failure
Im mainly having trouble understanding the reason for recurring the algo with limit-1 for every successor. Can someone run through this with me? Some graphical explanation would be nice haha.
I'm going to look at other sources in the meantime. Thanks for reading!
The pseudo-code appears to be wrong. (It's actually possible for the base case to never be encountered if the node depth/limit values skip eachother - being simultaneously increased and decreased in each recursive call.)
The recursive case was written with the limit - 1 so a base case can be reached (instead of "limit", think "remaining").
However, the base case for the limit is if Depth[node] = limit then return cutoff. Following David Chan's suggestion it should be if 0 = limit then return cutoff, which would agree with the limit - 1 in the recursive case.
Alternatively, just pass limit in the recursive case and leave the current base case alone.

OR between two function call

What is the meaning of a || between two function call
like
{
//some code
return Find(n.left,req)||Find(n.right,req);
}
http://www.careercup.com/question?id=7560692
can some one help me to understand . Many thanks in advance.
It means that it returns true if one of the two functions is true (or both of them).
Depends on the programming language, the method calls Find(n.left,req) -> if it's true - returns true. if it's false, it calls Find(n.right,req) and returns its Boolean value.
In Java (and C and C#) || means "lazy or". The single stroke | also means "or", but operates slightly differently.
To calculate a||b, the computer calculates the truth value (true or false) of a, and if a is true it returns the value true without bothering to calculate b, hence the word "lazy". Only if a is false, will it checks b to see if it is true (and so if a or b is true).
To calculate a|b, the computer works out the value of a and b first, then "ors" the answers together.
The "lazy or" || is more efficient, because it sometimes does not need to calculate b at all. The only reason you might want to use a|b is if b is actually a function (method) call, and because of some side-effect of the method you want to be sure it executes exactly once. I personally consider this poor programming technique, and on the very few occasions that I want b to always be explicitly calculated I do this explicitly and then use a lazy or.
Eg consider a function or method foo() which returns a boolean. Instead of
boolean x = a|foo(something);
I would write
boolean c=foo(something);
boolean x = a||c;
Which explicitly calls foo() exactly once, so you know what is going on.
Much better programming practice, IMHO. Indeed the best practice would be to eliminate the side effect in foo() entirely, but sometimes that's hard to do.
If you are using lazy or || think about the order you evaluate it in. If a is easy to calculate and usually true, a||b will be more efficient than b||a, as most of the time a simple calculation for a is all that is needed. Conversely if b is usually false and is difficult to calculate, b||a will not be much more efficient than a|b. If one of a or b is a constant and the other a method call, you should have the constant as the first term a||foo() rather than foo()||a as a method call will always be slower than using a simple local variable.
Hope this helps.
Peter Webb
return Find(n.left,req)||Find(n.right,req);
means execute first find {Find(n.left,req)} and return true if it returns true or
execute second find return the value true if it return true, otherwise false.

Binary trees- pseudo codes?

I'm trying to write this pseudo code for checking whether a data-structure based on trees
is a binary tree.
I'm having a little problem with understanding this pseudo code form.
This is what I wrote:
Is-Binary(x)
if (x=null) {Then Return True
}
else {
if (x.Right<>Null) {Then
if (x.key<x.right.key){Then
Is-Binary(x.Right)}
else {Return False}}
if (x.Left<>Null) {Then
if (x.key>x.Left.key){Then
Is-binart(x.Left)}
else {Return False}}
}
My question: Can I assume that after the first true will be accepted, the program wont finish?
What does the return means here? will it sum up all the true/false and give the final soulotion (based on the fact that true*false=false?)
If so, what else can I do?
Thank you
You'll only get one result, either true or false, because you're not accumulating anything. Assuming you're starting from the top of a tree, say you only go one level deep, you'll only get true or false as a result. Then, if you get another level deeper (with another call), you're just facing the same possibilities: True, false, or deeper in the tree.
[Edit, after further observation:] Unless I'm mistaken, you'll get True the first time you hit a null, which might never happen because you never call Is-Binary on a null-value.
So if X is null you get true, else you get false.

Resources