<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 What is a bug?
Bugs are codes that result in errors or wrong results.
Step1:  Question
Step2:  What's the bug here and how do we resolve?
Step3:  We should have documented the inputs to the function!
Step4:  Now it works fine for the first set of inputs. Let's try other inputs.
Step5:  We get nan, which stands for "Not a Number".  What's going on here?
Often the first thing to try is to simply print things and see what's going on.
Within the file, you can add some print statements in key places
Step6:  By printing some of the intermediate items, we see the problem
Step7:  Using a Debugger
Python comes with a built-in debugger called pdb.  It allows you to step line-by-line through a computation and examine what's happening at each step.  Note that this should probably be your last resort in tracing down a bug.  I've probably used it a dozen times or so in five years of coding.  But it can be a useful tool to have in your toolbelt.
You can use the debugger by inserting the line
python
import pdb; pdb.set_trace()
within your script. To leave the debugger, type "exit()". To see the commands you can use, type "help".
Let's try this out
Step8:  This can be a more convenient way to debug programs and step through the actual execution.
<ASSISTANT_TASK:>
Python Code:
# Syntax error
x = 1; y = 2
b = x == y # Boolean variable that is true when x & y have the same value
b = 1 == 2  # Syntax error
b
# Exception - invalid operation
a = 0
5/a  # Division by zero
# Exception - invalid operation
input = '40'
float(input)/11  # Incompatiable types for the operation
# Incorrect logic
import math
x = 55
math.sin(x)**2 + math.cos(x)**2 == 1  # Should be math.cos(x)**2
Explanation: What is a bug?
Bugs are codes that result in errors or wrong results.
End of explanation
def entropy(ps):
    items = ps * np.log(ps)
    return -np.sum(items)
ps = [0.1, 0.3, 0.5, 0.7, 0.9]
entropy(ps)
Explanation: Question: If incorrect code is never executed, is it a bug?
This is the software equivalent to "If a tree falls and no one hears it, does it make a sound?"
How Do We Find And Resolve Bugs?
Debugging has the following steps:
Detection of an exception or invalid results. We detail with this in depth in testing.
Isolation of where the program causes the error. This is often the most difficult step.
Resolution of how to change the code to eliminate the error. Mostly, it's not too bad, but sometimes this can cause major revisions in codes.
Isolation Of Bugs
There are three main methods commonly used for bug isolation:
1. The "thought" method. Think about how your code is structured and so what part of your could would most likely lead to the exception or invalid result.
2. Inserting print statements (or other logging techniques)
3. Using a line-by-line debugger like pdb.
Typically, all three are used in combination, often repeatedly.
print statements
Say we're trying to compute the entropy of a set of probabilities.  The
form of the equation is
$$
H = -\sum_i p_i \log(p_i)
$$
We can write the function like this:
End of explanation
def entropy(ps):
    print ps
    items = ps * np.log(ps)
    return -np.sum(items)
ps = "0.1, 0.3, 0.5, 0.7, 0.9"
entropy(ps)
Explanation: What's the bug here and how do we resolve?
End of explanation
def entropy(ps):
    print ps
    items = ps * np.log(ps)
    return -np.sum(items)
ps = [0.1, 0.3, 0.5, 0.7, 0.9]
entropy(ps)
Explanation: We should have documented the inputs to the function!
End of explanation
# Create a vector of probabilities.
ps = np.arange(5.)
ps /= ps.sum()
ps
entropy(ps)
Explanation: Now it works fine for the first set of inputs. Let's try other inputs.
End of explanation
def entropy1(ps):
    print("ps=%s" % str(ps))
    items = ps * np.log(ps)
    if np.isnan(items[0]):
      print(items)
    return -np.sum(items)
entropy1([.1, .2])
np.isnan(np.nan)
Explanation: We get nan, which stands for "Not a Number".  What's going on here?
Often the first thing to try is to simply print things and see what's going on.
Within the file, you can add some print statements in key places:
End of explanation
def entropy2(ps):
    ps = np.asarray(ps)  # convert p to array if necessary
    print(ps)
    items = []
    for val in ps:
        item = val * np.log(val)
        if np.isnan(item):
          print("%f makes a nan" % val)
        items.append(item)
    #items = ps * np.log(ps)
    return -np.sum(items)
entropy2(ps)
Explanation: By printing some of the intermediate items, we see the problem: 0 * np.log(0) is resulting in a NaN. Though mathematically it's true that limx→0[xlog(x)]=0limx→0[xlog⁡(x)]=0, the fact that we're performing the computation numerically means that we don't obtain this result.
Often, inserting a few print statements can be enough to figure out what's going on.
End of explanation
def entropy(ps):
    items = ps * np.log(ps)
    if np.isnan(items[0]):
      import pdb; pdb.set_trace()
    return -np.sum(items)
Explanation: Using a Debugger
Python comes with a built-in debugger called pdb.  It allows you to step line-by-line through a computation and examine what's happening at each step.  Note that this should probably be your last resort in tracing down a bug.  I've probably used it a dozen times or so in five years of coding.  But it can be a useful tool to have in your toolbelt.
You can use the debugger by inserting the line
python
import pdb; pdb.set_trace()
within your script. To leave the debugger, type "exit()". To see the commands you can use, type "help".
Let's try this out:
End of explanation
ps = [0, .1, .1, .3]
entropy(ps)
Explanation: This can be a more convenient way to debug programs and step through the actual execution.
End of explanation
<END_TASK>