Friday, May 22, 2009

Answers to the Programming Quiz:

Tom got the answer to number one. The assignment

(-1 ^ (j - 1))

will always be -1 for any integer value of j, because of Visual Basic's order of operations. The carat operator is applied before the minus operator. This added about 15 minutes to my debugging time.

As for the second question, Dave was right on with his assessment:
"it seems to take a set of real numbers and multiplies them against each other, alternately adding/subtracting the products."
That is, in fact, exactly what it does, and had he known the law in question, you probably would have gotten it from here. The law (or maybe formula would be a better term) is the general form of the Addition Law for n Independent, Non-exclusive Events, and is used in the field of probability to determine the chances that at least one of n events occurs, given the probabilities of each individual event.

To explain this, let me first define independent and non-exclusive. Two events are independent if the occurrence of one does not affect the occurrence of the other. Example: a die rolling 6 and a coin landing heads.

Two events are exclusive if they cannot occur at the same time. Example: a coin landing heads, and the same coin landing tails.

So the probability of either of two independent, non-exclusive events occurring is given by the equation:

P(A+B) = P(A) + P(B) - P(A)∙P(B)

Where P(A+B) is the probability that either A or B occurs, P(A) is the probability that A occurs independently, and P(B) is the probability that B occurs independently. (I am using + notation because I can't find the stupid Union symbol in the character map.)

This can be easier to understand with a Venn diagram.



If we want to find the area covered by both ellipses (the probability that at least one of the events occurs), we start by adding the area of each ellipse (P(A)+P(B)). But then we have added the intersection of the two events twice, so we need to subtract it out once (–P(A)∙P(B)).

We can determine the probability that any of three independent, non-exclusive events occurs with the equation:

P(A+B+C) = P(A) + P(B) + P(C) - P(A)∙P(B) - P(A)∙P(C) - P(B)∙P(C) + P(A)∙P(B)∙P(C)

To understand this, let's look at another Venn diagram.



To find the area covered by all three circles, we begin by adding in the area of each circle (the independent probability of each event, P(A) + P(B) + P(C)), but then we have added some regions more than once. To compensate, we can subtract out the intersection of every two circles (–P(A)∙P(B) – P(A)∙P(C) – P(B)∙P(C)). But now we have completely subtracted out the center region, the intersection of all three circles (P(A)∙P(B)∙P(C)).

I think you see where this is going for four events, etc. It gets very complex. In fact, the general form of the Addition Law, for n independent, non-exclusive events is:


(I)

In essence, we add in the independent probability of each event, subtract the products of each combination of two events, add the products of each combination of three events, subtract the products of each combination of four, etc., until we finally add or subtract (depending on if there are an odd or even number of events) the product of every event probability. This is what my VB code was attempting.

(There is an alternate, less complex way of attempting this. Instead of a brute force method, we can instead consider the inverse probabilities. Instead of finding the area covered by the circles/ellipses, we can find the area not covered by them, and subtract that from 1. To wit:



De Morgan's theorem states: P(A+B)' = P(A'∙B') = P(A')∙P(B')

That is, the probability that neither A nor B occurs is the probability that A does not occur and B does not occur. So if we subtract that from 1, we get:

P(A+B) = 1 – P(A')∙P(B') = 1 – (1 – P(A))∙(1 – P(B))

Which, for the general form, expands to:


(II)
)

So what brought this all up?

Probability theory figures heavily in fault tree analysis. A fault tree is, essentially, a collection of events (each with an associated probability of occurrence, specifically a probability of failure) connected via logic gates, such as AND and OR gates (and sometimes VOTE gates).

What? Explanation of the logic gates? Ok, we can do that.



This is an AND gate. It denotes that the output occurs if all of the input events occur.



An OR gate denotes that the output occurs if any of the inputs occurs.



A VOTE gate denotes that the output occurs if at least n of the inputs occurs (in this case 2).

(These symbols may seem familiar to those of you who took computer architecture. The symbols are the same ones used in logic gate diagrams, but turned sideways.)

There are also other miscellaneous gates, such as a NOT gate or XOR gate, but that's a bit beyond the scope of a simple introduction.

So the basic premise of fault tree analysis is we want to know about how often (the probability) a TOP event will occur, based on the occurrence of basic events. The TOP event is usually some hazard that we would like to prevent. A basic event is a failure or occurrence which may cause the TOP event to occur.

So, to create a fault tree, we connect these basic events to the TOP event through intermediate logic gates. These logic gates represent how the failures combine to cause a TOP event occurrence. A small fault tree might look like this:



The way to read this is by looking at the logic symbol of each gate to determine which of its input events must occur to cause the gate to occur. For example, if EVENT5 and EVENT6 occur, this will cause GATE3 to occur, because it's an AND gate and requires the occurrence of all input events. If GATE3 occurs, GATE2 will also occur, because it's an OR gate and the occurrence of any of its inputs will cause its occurrence. Now, if any two of the inputs to GATE1 occurs, say EVENT1 and EVENT3, then GATE1 will occur (because it's a VOTE gate). And if both GATE1 and GATE2 occur, then TOP1 will also occur. So we could say that TOP1 will occur if EVENT1, EVENT3, EVENT5, and EVENT6 all occur at the same time. We call this occurrence of events a minimal cut set. (I might talk more about cut sets in the future, if you wish, but as it is, this post is running too long.) Another minimal cut set might be EVENT2.EVENT3.EVENT4. (Note, EVENT2.EVENT3.EVENT4.EVENT5 is also a cut set, but not minimal. Do you see the difference?)

I will skip over how to produce the full list of minimal cut sets for this tree, and just list them all. They are: 

EVENT1.EVENT2.EVENT5.EVENT6
EVENT1.EVENT2.EVENT4
EVENT1.EVENT3.EVENT5.EVENT6
EVENT1.EVENT3.EVENT4
EVENT2.EVENT3.EVENT5.EVENT6
EVENT2.EVENT3.EVENT4

By this point, you can probably see how to produce them yourself. Now each cut set can be thought of as an event, itself. So, basically, what we have here is a list of non-exclusive, independent events, the occurrence of any of which will cause the TOP event.

Sound familiar?

The obvious way to solve this, then, would be to use... the Addition Law!

And this is exactly what the software (that company I work for) writes does. You tell it the events, how they're logically connected, and some quantitative data, such as a failure rate or MTTF (mean time to failure), and it will calculate:

1) The probability of each event occurring,
2) The probability of each cut set occurring (simple: the product of the probabilities of each event in the cut set)
3) The probability of occurrence of the TOP gate (using the Addition Law).

Note how quickly the addition law gets complex. This small and simple tree produces six cut sets, which would require 6 + 15 + 20 + 15 + 6 + 1 = 63 product terms in the addition law. Just the other day I was working with a fault tree that had 152 cut sets for one intermediate-level gate. This is why our software will use approximation methods and all sorts of other tricks to be able to solve the tree. In fact, one of the approximation methods involves using de Morgan's theorem and equation II. This is called the Esary-Proschan approximation method, but you didn't really want to know that.

If you would like, I could talk a little bit more about fault tree analysis later, and give real examples, instead of odd abstractions. But this post has gone on too long.

And now you know what I do for a living!

So back to the topic at hand, why was I writing a VB program to implement the Addition Law? Basically, I needed to verify the results of a fault tree. By hand.

Wednesday, May 20, 2009

Programming Quiz of the Day/Week/Month.

Question 1: What is the value of x after this code executes?

dim j as integer, x as integer
j = 5
x = (-1 ^ (j - 1))

Question 2: What mathematical law does the following code implement?

'Globals
dim i as integer, j as integer
dim dValues() as double

Private Function DoEet() As Double

    i = UBound(dValues)
    For j = 1 To i + 1
        DoEet = DoEet + RecursiveHell(-1, 0, -1, 1)
    Next j

End Function

Private Function RecursiveHell(k As Integer, l As Integer, _
    m As Integer, x As Double) As Double
Dim temp As Double

    If k = -1 Then
        k = 0
        RecursiveHell = RecursiveHell + RecursiveHell(k, 1, k, 1)
    Else
        If l < j
            Do While m < i - (j - 1)
                temp = x * dValues(m)
                RecursiveHell = RecursiveHell + RecursiveHell(k, l + 1, m + 1, temp)
                m = m + 1
            Loop
        Else    'l = j
            Do While m < i
                temp  = x * dValues(m) * ((-1) ^ (j + 1))
                RecursiveHell = RecursiveHell + temp 
                m = m + 1
            Loop
        End If
    End If

End Function

(Might want to resize your browser so that this fits on one line:)
-----------------------------------------------------------------------------------

(Don't know what recursion is? Click here to find out.)

Tuesday, May 19, 2009

Cat Yodeling.



I need to try this with Bastet. She's far easier to annoy.

Monday, May 18, 2009

No, YOU'RE wrong!

[Warning: long rant on religion and politics follows]

So someone recently made the statement that a Catholic who supported the the invasion of Iraq must be just as morally confused as someone who is pro-choice.  Although I provided a clear and concise rebuttal to the originator of the statement ("your stoopid"), I thought I'd explore this statement in a little more detail.

First off, "moral confusion" is an imprecise and ambiguous phrase, so when I use it what I really mean (and what I assume the original commentator meant) is "morally incorrect."  "You're confused" as I usually see it used in this context seems to be intended as a more polite way of saying "you're wrong," except it's not more polite because everyone knows what it means, and condescending because it implies the other person isn't emotionally capable of dealing with someone telling them they're wrong.

Let's summarize why someone might be pro-choice (this has been discussed at length on this blog):
1. Unborn babies aren't human, so abortion is not an act that murders a human.
2. There's nothing inherently wrong with murdering innocent people.
3. Unborn babies are human, but it's okay to murder them because they aren't born (or crossed some arbitrary development threshold).

Note that I am assuming that we all accept that something that is inherently immoral is always morally wrong (i.e., if under some circumstances an act is not wrong, then it was not inherently so: the ends cannot justify the means).  Item #1 represents "moral confusion" by virtue of incorrect facts.  It is morally correct under the premise that unborn babies aren't human, but biology indicates otherwise.  Item #2 represents a point of view that most people do not hold, and I don't honestly think the original commentator believes that all those who support the war hold this view.  Item #3 is only morally correct with the introduction of a new moral principle that specifically excludes humans of some developmental stage or status from the prohibition against murder.  I have never seen a justification, much less a clear statement, of this new principle.  This also requires item #2, although supporters of abortion rarely admit it.  The only other possible position for someone who supports legal abortion is that it is the immoral murder of an innocent human and should not be prohibited by government.  This is an unusual view of the role of government.

In order for supporters of the war to be guilty of the same moral confusion as pro-choicers, then it must be because they believe that fighting a war is not inherently immoral, and possibly that there is some special reason that the invasion of Iraq need not be justified according to traditional just war theory.  But the Church teaches explicitly that waging war is not inherently immoral, and proponents of the war that argue it is justified within the traditional just war theory (whether you agree with their reasoning or not) do not require some new moral principle (example example example).  Therefore, proponents of the war in Iraq (including one singled out by the original commentator) clearly do not suffer the same "moral confusion" as pro-choicers in any meaningful sense.

The only other context given with the original offending comment was that the "morally confused" should have realized, after five years of tragedy, that the original decision was unjust.  This implies that the unintended, undesired, unforeseen consequences can affect whether a decision is just after it has been made, which leads to a ridiculous impossibility of making moral judgments, since all possible effects of a decision cannot be foreseen.

It is a logic error (and perhaps a lack of charity) to assume that those who seem to be wrong on a moral issue must be suffering from deficient moral reasoning.   Anyone can understand how identical logic applied to different premises can lead to different conclusions.

The only other statement by the original commentator I wish to comment on is "it is a reflection of ideology, not considered thought, to think that."  But how can one think that which is not thought?  Please be more clear.  Did you mean, "it is a reflection of belief not based on reason"?  But that is false on the face of it: as I showed above, the position (support for the war) is reasoned from facts and moral principles.  As far as I can tell, this statement means nothing.

Comments are welcome.

Wednesday, May 13, 2009


Trekkies Bash New Star Trek Film As 'Fun, Watchable'

Quotable quotes: "Gene Roddenberry was the hack who created the Star Trek television show, way back in the 40's or something."

Monday, May 11, 2009

Because the Murloc won't stop bugging me about it...
Photodump.

The Murloc's domestic instinct has kicked in. She cooked Easter dinner. Lamb. It was good.



The Murloc and her friend went to the Renaissance Faire. They dressed up. They were pretty.





As usual, the Murloc got her hair done. By the same girl as usual. It was pretty.







Pictures from the latest ultrasound. In the upper-right, he's playing with his feet. It is cute.



Happy? Now STOP BUGGING ME!
Book Reviews

From the sublime to the ridiculous.

I've been on a reading kick lately, and since I'm a blogger I know you're all dying to know what I think about whatever.  So here are some (very) short reviews:

A fascinating story by the father of science fiction.  Part nature documentary, part high-seas adventure, it's kind of like a 19th-century underwater Star Trek.  Highly recommended.

Funny short story about the passive-aggressive office worker that won't be pushed around, and the power of behavioral precedent.  It has a poignant touch to it, too.  Good story.

A Christmas Carol - Charles Dickens
A popular classic is both for good reasons, one of those stories where you can call it "heartwarming" without being just an over-used cliche.  Read it.

Daemon - Daniel Suarez
It's about a dying brilliant computer game programmer that takes revenge on the world by writing what is both the ultimate virus and the ultimate computer game.  What's really scary is that all of the technology in the story exists.  It's totally possible!  I do believe this will get made into a movie, and there is a sequel on the way.  Highly recommended for nerds, geeks, other techies, or paranoid conspiracy theorists.

The son of Mel Brooks writes the ultimate survival guide on how to survive the coming zombie apocalypse.  Hint: be prepared, and aim for the head.  Great dry humor.

A Brief History of Time - Stephen Hawking
A summary of physics from the Greeks to string theory, this book is basically a recap of how man has tried to understand the workings of the universe.  It's well-written and easy to understand, although it's a little frustrating for someone like me who knows enough about the topics to have serious questions, which to answer would require detail well beyond the scope of this book.  Oh, and he repeats the usual nonsense about Gallileo (Church vs. Science!).  I have the original 1988 edition, so I don't know if this was corrected along with the changes made in later editions.  Still, everybody should read this book.

Orphans/Fugitives/Titans of Chaos - John C. Wright
Sort of Harry Potter meets Star Wars meets The Box of Delights, it's one of the most imaginative stories I've read, it's hard to figure out exactly which genre it's in.  Maybe fantasy, maybe sci-fi?  The characters are interesting, and it's just impossible to tell where the story is going to go.  One of the characters can move and see in four dimensions, which presented some difficulty for me since I visualize everything I read.  It also has the most ominous line I've read lately: "Prepare the thousand-dimensional object!"  Recommended.

Watchmen - Alan Moore/Dave Gibbons
Who reads the Watchmen?  I did, and it was actually pretty good.  I had read the whole debate about the book vs. the movie, and I already knew most of the plot (including the ending), but it was still very interesting.  What you get out of the story depends on your world view.  The point of view of the story is very nihilistic and cynical, although the characters are quite deep and draw you in.  I thought the ending was disappointing, in part because I don't see the universe in the way the author portrays it in this story, although as a whole I enjoyed reading the book.  Although I haven't seen the movie, from what I've heard it's much much more violent than the comic.  The worst violence in the comic is described or implied, and not depicted.  And Nixon isn't portrayed as evil (he's hardly portrayed at all).  Recommended for those who are interested.

Currently reading:
The Night Land - William Hope Hodgson
Set one million years in the future when the sun has burnt out (written in 1910, they didn't how long it would actually last back then), one of the last survivors of the human race sets out across a land of perpetual darkness to rescue his beloved.  It's very atmospheric, early sci-fi, and I'm only halfway through it, so don't tell me how it comes out.

Ender's Game - Orson Scott Card
A fellow geek chastised me for not having read Ender's Game, so I just started that one today.  I don't have anything to say about it yet.  Don't spoil it!