Archive for February, 2008

MATLAB, blog

Resolving the Birthday Paradox with Matlab

birthday_1.jpg
Photo taken by chipgriffin

Go shawty. It's your birthday. We gonna party like it's your birthday. -50 Cent

Since I started this week with a riddle/puzzle/problem, I figured I might as well end the week with another riddle/puzzle/problem. A very well known problem that I would like to discuss today is the Birthday Paradox. The problem is given this moniker because the solution is so surprising that people simply cannot believe it!

The Problem Statement

  • To keep things a little simpler, let's assume that we exclude any birthdays on February 29th (Leap Year babies). In addition, let's assume that all 365 birthdays are equally likely to occur.
  • Assume that there are x number of people in a given room and that these people are all chosen at random.
  • How many people would we need to have in the room in order to have better than 50% chance that there will be at least two people who share the same birthday?
  • How many people would we need to have to have a 99% chance?

This problem can be solved if you have a good background in probability. There's a pretty elegant solution that can be arrived at analytically. You can see how it's done here. But if you have no idea on how to proceed, let's try doing it in Matlab.

Solving the Birthday Paradox with Matlab

Matlab Logo

  1. Using Matlab to solve this problem might be a little easier than you would expect (it was certainly easier than I expected). My approach was to first write a functon that would simulate the percentage for a given number of people. I saved this function as an m-file. You can see the code below.

  2. function result = birthdayMatch(numPeople)
     
    total = 0;
     
    %do the simulation for 20,000 iterations
    for x = 1:20000
     
       %create a random vector of birthdays for the specified number of people
        birthdayVector  = ceil(rand(1,numPeople)*365);
     
        %the mode command returns the number (a) that occurs the most
        %if there are two numbers that occur with the same frequency, 
        %the lowest number is returned
        %the second return argument (b) is the number of times (a) occurs
        [a,b] = mode(birthdayVector);
     
        %if there is a birthday match
        if b > 1
            total = total + 1;
        end
     
    end
     
    result = total/x; %get the average percent of a matching birthday
  3. So now we have a function that will perform 20,000 iterations of the event, we can use this function to solve the above problem

  4. At the matlab command prompt, we can enter the following:

    %the following commands might take a couple of seconds to execute
    %so be patient!
     
    %get the percent chance of a birthday match with 5 people
    fivePeople = birthdayMatch(5)
     
    %get the percent chance fo a birthday match with 23 people
    twentythreePeople = birthdayMatch(23)
     
    %get the percent chance fo a birthday match with 57 people
    fiftysevenPeople= birthdayMatch(57)

The Answer!

The theoretical answer for this problem is 23 people for 50% chance and 57 people for 99% chance.

The simulations basically confirm that the theoretical answer is indeed correct, but the answers might be off by a little bit. If you want more precise answers, you can increase the number of iterations used. Of course, this will cause the computation time to increase as well.

How Would You Solve This Problem?

Did any of you guys have a different idea on how to solve this? I would love to hear how you guys went about solving this problem. Whether you used analytical methods or used a computer program, I'm always interested in how problems can be approached from different angles.

My first Math Problem in College

This was the first problem that was presented to me in Arthur Benjamin's probability class at Harvey Mudd. For those of you who don't know, Benjamin is a world renowned mathemagician (mathematician + magician). Click here if you're interested in seeing some of his spiffy tricks and feats.

MATLAB, blog

Montana Duck Hunters and Matlab

duckhunt.png
Photo taken from Nintendo game "Duck Hunt"

Duck Hunt from Nintendo always brings back fond memories for me. This post is a semi-tribute to the popular Nintendo game, but more than anything reflects my fascination for interesting puzzles/riddles. An interesting mathematical problem that I ran across a couple of years ago when I was browsing the web involves a bunch of duck hunters. Click here for the original link.

The Problem Statement

  • Montana duck hunters have 100% accuracy.
  • Ten Montana hunters are in a duck blind when 10 ducks fly over.
  • All 10 hunters pick a duck at random to shoot at, and all 10 hunters fire at the same time.
  • Thus, it is possible for 2 hunters to shoot at the same duck. In fact, its possible for all 10 hunters to shoot at the same duck as well!
  • How many ducks could be expected to escape, on average, if this experiment were repeated a large number of times?

How to Solve this Problem

You'd have to have a pretty solid understanding or probability to tackle this problem. To be honest, I don't think I was ever able to solve it analytically as it involves the use of binomial probability and some rugged counting techniques. If you click on the link I provided above, there's a pretty good analytical approach/solution to this problem. But since this is a website dedicated mostly to Matlab, let's use Matlab to help us solve this problem!

Hunting Ducks with Matlab

Matlab Logo

  1. Using Matlab to solve this problem might be a little easier than you would expect. My approach was to first write a functon that would simulate one single event of 10 hunters shooting at 10 ducks. I saved this function as an m-file.

    function numAlive = duckHunt
     
    %create a vector with 10 entries, all entries are 1's
    %0 = dead, 1 = alive
    ducks = ones(1,10); 
     
    %let's set aside a number from 1 to 10 for each duck
    %create a vector with 10 entries.  each entry is between 1 and 10
    %for example, we might get a vector [1,5,6,7,1,2,3,9,10,8], this
    %means that hunter 1 shoots at duck #1, hunter 2 shoots at duck #5
    %hunter 3 shoots at duck #6 . . . and so on
    hunter = ceil(rand(1,10)*10);
     
    %each hunter shoots at a random duck
    %it is possible for two hunters to shoot at the same duck!
    %any duck that was targeted goes from a 1 (alive) to a 0 (dead)
    ducks(hunter) = 0;
     
    %sums up the number of ducks still alive
    numAlive = sum(ducks);
  2. So now we have a function that will simulate one event. But we need to take many samples in order to get the average. Lets use a sample size of 10,000 events. I used the following code at the command prompt to call the function that I just wrote multiple times. After the FOR loop is done running, I took the average of the number of ducks that escaped.

    total = 0;
    for x= 1:10000
    total = total + duckHunt;
    end
    averageDucksAlive = total/10000

The Answer!

The theoretical answer for this problem is 9^10 / 10^9 = 3.4867.

How close did my simulation get? The answer I got was 3.4854. Not quite the exact answer, but pretty darn close!

How Would You Solve This Problem?

Did any of you guys have a different idea on how to solve this? I would love to hear how you guys went about solving this problem. Whether you used analytical methods or used a computer program, I'm always interested in how problems can be approached from different angles.

MATLAB, blog

Link Love and Interesting Sites/Blogs that I Frequent

lazer2.gif
Photo from explodingdog.com

One of the forums that I frequent everyday is the Matlab Forum over at comp.soft-sys.matlab. This forum is very lively, active, and have many people who are willing to help you. Any time that I have a question that I can't answer myself, I usually turn to the support group at this forum. I would suggest that you take the time to properly frame your question so that people know what you are talking about. I usually get a pretty good response 1-2 hours after I post my question, if not faster. I also go here to help brainstorm new ideas on possible tutorials. This forum is also mirrored at the Matlab site as well.

A lesser used Matlab Forum is kluid.com. I've been here a couple times, but the amount of activity pales in comparison to the forum at comp.soft-sys.matlab. But it's something that should be looked into.

The Mathworks personnel have some very informative blogs on a variety of topics. I mostly follow Doug, Loren, and Inside The Matlab Desktop. You will find quality information that will expand your Matlab knowledge in no time.

One of my blogging buddies, Ritu B Pant, has recently launched a new blog that I think shows a lot of promise and potential. His blog focuses on networking, how to be successful in blogging, productivity, and offers valuable information that will help you in everyday life. Ritu just recently launched this site, but I wouldn't be surprised if you saw some of his content at other well known blogging sites as he is constantly guest posting.

Another blog that I enjoy reading is Colin LaHay's Mixed Market Arts. I love the play on words from Mixed Martial Arts. This blog has a lot to offer in terms of helping you promote your blog through a myriad of different link building methods and other nifty tips. I like how he gives it to the audience in straight forth manner.

Some of the crazy art that you've seen here on blinkdagger comes from explodingdog.com. The pictures can be so wild and ridiculous at times that I just shake my head in disbelief. Interesting artwork to say the least.

blog

Engineers and Fashion: An Oxymoron?

This is a post by Angel Ng.

An example of a cool engineer: Michael Scofield from Prison Break

Photo from gossiportruth.com

Personally, I think engineering guys are a great catch. For example, consider Wentworth Miller, who plays the role of a Structural Engineer in the television series Prison Break. Unfortunately, not all engineers exhibit the same sense of fashion that Wentworth does.

Most of the engineering guys just need… umm… a little tweaking. I hear it over and over again from some of my intelligent, witty, sweet engineering guy friends, “Why do nice guys always lose?!” I wonder the same thing! Why do some women choose to date jerks and pass-up these well-qualified guys?! One conclusion I came to is: engineering guys in general don’t know how to package and market themselves. I mean, don’t you engineers check out a company’s website and immediately gage the company’s prowess (or lack thereof) by the website’s appearance? Or judge the latest RPG game by its graphics?

I am neither a great marketer nor a dating expert by any means, I just want to pass along some random (and seemingly obvious) observations to guys from a chick’s perspective. So for whatever it’s worth, here is the first set of practical advice to you male engineers, from a female non-engineer. Continue Reading »

blog

Are you Dressed for Success?

dress_success.jpg
Photo by zepfanman

As an engineer that falls under the "not so well dressed" category, I understand that it can be difficult for working professionals to dress appropriately at the office. I spend so much time with my pal Matlab that I sometimes forget about my wardrobe. It's not a stretch to say that I spend more time thinking about how to vectorize my code instead of what to wear to work. But like it or not, there is more to life than Matlab.

In this post, I'm going to single out the engineering crowd as the main crulpits. And while I'm mainly referring to the male engineers, I've also seen some pretty shabbily dressed female engineers at the work office as well, so this phenomenon isn't just limited to the engineering men.

Among the different career fields, I find that the engineering world is a bit lax with fashion and dresscode. All my other friends in the finance and consulting business are much better dressed in my opinion. In fact, I feel quite under dressed when I see what they wear to work. Granted it's a different industry, but each workplace has its own dresscode that should be followed to ensure that you fit seamlessly into the system.

Many of the freshly minted graduates entering the workforce are the most likely offenders. But I know of people who have been in the workforce for a couple of years who still have no clue. Dressing appropriately is something that should not be taken lightly. By dressing for the occasion, you do the following:

  1. Give off an aura of professionalism

  2. Display confidence

  3. Look prepared

  4. Show that you care and take pride in your appearance

  5. Show that you are serious about your work. If you don't look like you take your job seriously, how can anyone (colleagues and customers) else think you do?

And while its true that you shouldn't judge a book by it's cover, people already do it on a daily basis. There's just no stopping it. Dressing well can ONLY help you throughout your career. It isn't a stretch to say that it can help you climb the corporate ladder.

And while dressing well is something that is important, it won't mean much if your technical work is subpar and lacking.

Tips and Tricks on how to Dress

Anyhow, I probably shouldn't be offering advice on how to dress appropiately as I'm just as bad as the next engineer. But if you need some tips on how to dress appropriately in the work place, you should visit www.dresscodeguide.com. They tell you how to dress for any occasion. I found it quite useful.

In our next post, Angel Ng will discuss some of the finer points of fashion as it relates to the male engineers of the world. Don't miss it!

Next »