Sunday, October 2, 2011

Monte Carlo integration: How the number of iterations used affects precision

While it may be obvious that with Monte Carlo integration, the more iterations performed will result in higher precision, it's always fun to look at something graphically. Since I had already written a program that approximated the value of pi (check it out here), I decided to look at how standard deviations change with increasing numbers of iterations.

I chose to run 10 sets of iterations: 2, 4, 8, 32, 64, 128, 256, 512, and 1024. Why these values were chosen becomes obvious in the script, but you can use any set of iterations you want. Each set of iterations was run 10,000 times and standard deviations were calculated from these.
Figure 1: Stdev vs. number of iterations. Note the x-axis is log scale so that the trend can more easily be seen.
Here's my script:

% Graphing standard deviations of pi vs. number of iterations
% using Monte Carlo integration
close all
clear all

for x = 1:10;           
   
y=2^x;
   
for i = 1:10000;
    n = 2*(rand(2,y))- 1;    % generates random numbers between -1 and 1
   
    pts = 0;                  % starting value for total points
    circ = 0;                 % starting value for points within circle
   
    for R = 1:y;
        d = sqrt((n(1,R))^2 + (n(2,R))^2);
        pts = pts + 1;
        if d <= 1;
            circ = circ + 1;
        end  
    end
    Pi(1,i) = (4*circ)/pts;

end

STD(1,x) = std(Pi);
IT(1,x) = y;

end

plot (log10(IT),STD)
title('Standard Deviation vs. Number of Iterations');
xlabel('log10(number of iterations)');
ylabel('Standard deviation');

Saturday, October 1, 2011

Calculating Pi: Monte Carlo Integration

Thank you physicists, Matlab, and (pseudo)random number generators. Welcome to Monte Carlo integration. A couple of days ago, my boyfriend was telling me how he has surpassed me in his Matlab skills. Dorky, I know.  Not one to be bettered, I set out to prove him wrong. He had an assignment to approximate the value of pi using Monte Carlo integration in C. I'd do it in Matlab.

But I had to figure out what Monte Carlo integration was first.

Enter Wikipedia. I don't care what professors say about it, Wikipedia is a magical place filled with more knowledge than could ever be stuffed into my head. I'm not going to start citing the website in anything important, but I will give it my thanks. So here it is: Monte Carlo integration offers an approximate evaluation of definite integrals using random numbers.   

So what did I have to do exactly? My bf gave me a hint: use a circle and a square. After some more prodding he added, "put the circle in the square." Alright. Thanks a lot.

So I drew up a circle with radius = 1, inside a square with sides = 2 (Figure 1).


Figure 1: Circle with radius = 1 within a square with sides = 2.
We know that the area of the square is L2= 22 = 4.

We know the area of the circle is
A = πr2. To find π, we need to know the area of the circle. Unfortunately, that's impossible to know without π.

But there is another way to find pi. I first set the center of the circle as (0,0) in a coordinate plane. I then could generate sets of random numbers between -1 and 1 and use these as x and y coordinates. Using Pythagorean's Theorem I can find the distance of these points from the center (the origin):

c = √(a2 + b2).

If a point's distance from the origin is < 1, then it falls within the circle. The ratio of points within the circle to the total number of points (points within the square) is proportionate to the ratio of the area of the circle to the area of the square:

                                               # points in circle   =    πr2           where r=1
                                                # points total             4
So:    

                                              π    =    4(# points in circle)
                                                              # points total
With that in mind, my Matlab adventure began.

The script was pretty easy to write (find it at the bottom of this page). The hardest part for me was actually plotting the figure. Obviously, the more iterations (# of points) you do, the more accurate your result will be (Table 1). It also vastly increases the computing time. Past 1,000,000 iterations, my Matlab program got pissed and said it didn't have enough memory.


# of Iterations
Approximate value of Pi
10
2.4
100
3.24
1000
3.092
10000
3.1104
100000
3.13168
1000000
3.143080




                            
Here's my Matlab script for 1000 iterations:

% Calculating pi using Monte Carlo integration
close all
clear all

n = 2*(rand(2,1000))- 1;        % generates random numbers between -1 and 1
t=sum(n.*n,1)<1;
pts = 0;                       % starting value for total points
circ = 0;                      % starting value for points within circle

for R = 1:1000;
    d = sqrt((n(1,R))^2 + (n(2,R))^2);
    pts = pts + 1;
    if d <= 1;
        circ = circ + 1;
    end
end

format long
Pi = (4*circ)/pts

plot(n(1,~t),n(2,~t),'b.','MarkerSize',15)
hold on
plot(n(1,t),n(2,t),'r.','MarkerSize',15)

theta = linspace(0,2*pi,100);    
x = cos(theta);                 
y = sin(theta);                  
plot(x,y,'k'); 
axis ('equal');
axis([-1 1 -1 1]);
title('100 Iterations');

Monday, September 26, 2011

Trawling the deep: aliens in our bathtub




I recently went on a research cruise in the Pacific ocean, trawling, deploying CTDs, and (scientific) fishing. Here are just a few of the organisms we pulled up from our own salty bathtub.
Posted by Picasa

Trawling the Deep: aliens in our bathtub

I think I'll just let the pictures do the talking.


Posted by Picasa

Saturday, September 10, 2011

Open ocean aquaculture: what's the problem?


"Look out Hawaii! Open ocean aquaculture (OOA) has many problems," warns Neil Frazer, professor of geophysics at UH Manoa. Focusing on the new OOA system off the Big Island, Frazer cites multiple shortcomings of the system in his November 2010 article to the Honolulu Star Advertiser. First, he argues OOA of carnivorous fish is "worse than over-fishing" because production of these fish requires massive amounts of food in the form of wild caught fish; fish that would otherwise be feeding people of Third World countries. Second, farmed fish have higher levels of pollutants than wild caught fish. Third, sewage from these caged fish negatively impacts the ocean environment. He further argues that OOA can act as an agent of disease for wild populations of fish. Wild fish would starve or get eaten when they fall ill. However there are no predation or starvation pressures on these caged fish, allowing them to spread disease over long periods of time. 

One of his arguments stood in stark opposition to what we had heard in the aquaculture class I'm taking this semester: the sewage issue. In class we learned one of the pros of offshore, open-ocean cages was that you did not have to worry about waste products. The waste would be distributed by oceanic currents and diluted thanks to the massive expanse of the ocean itself. No worries, right? Frazer argues dilution is not the solution; it just becomes someone else’s problem. These differing opinions I believe are a result of each person’s different experiences with aquaculture. My professor is speaking from an aquaculturist’s point of view. Someone who has been in the business to do just that: business. After all, aquaculture is a business, not a science. Frazer’s viewpoint is that of an observer, a possible consumer (although not likely judging by the article). His interests lie with environmental well-being, not with making money. Both opinions are valid; they just come from differing points of view.
In oligotrophic and relatively fast moving waters like Hawaii, I don’t think a few OOA operations would be a problem. The added nutrients from the waste may add to increase in primary productivity, but not much. And would increasing primary productivity a bit be such a bad thing? It may increase food availability for higher trophic position fishes, commercially important fishes. Could OOA increase wild stock biomass? Further, the cages themselves may attract fish, providing refuge in an otherwise featureless environment. This is an observed phenomenon; buoys miles from the islands have been found to attract fishes in larger concentrations than they’d be otherwise.
The promotion of disease in wild stocks is one of Frazer’s most troubling arguments. By protecting and feeding sick fish, OOA cages are a “reservoir of infection.” Diseases among caged fish could transfer to wild populations. Further, Frazer worries some caged fish escape the nets and interbreed with wild stocks, lowering the genetic robustness of the population. Is it possible that OOA is hurting populations of wild fish?
Frazer ends the article by talking about the advantages of Native Hawaiian fishponds and seems to suggest we should be using that aquaculture method rather than OOA. But could we support our demand for aquatic protein entirely from fishponds? What Frazer fails to mention is that the Native Hawaiian population was much smaller than Hawaii's population today, and even with that smaller population it is highly unlikely they were able to provide for themselves enough protein through fishponds.
I think we need to give OOA a chance. We are over-fishing our wild fish stocks and will soon need an alternative source of fish protein. Plus, OOA is far more sustainable and ecologically friendly than mass cattle, pig, and chicken production. Although we could all just become vegetarians and avoid this whole issue. Anyone?  
Check out the article here.

Saturday, September 3, 2011

Adventures in Oregon: the family reunion continues

The Box-R ranch was absolutely gorgeous. Way up, high in the mountains close to California, this ranch appears. A long gravel driveway. Dry grassland. Coniferous trees everywhere. Deer. At the end of the long gravel road we pull up to an open meadow. A pasture dotted with cows, horses, and donkeys stretches in front of us. To our left is a white two story house set behind a large droopy tree. The owner's house. Our cabin is further up the gravel road,

Wednesday, August 10, 2011

Adventures in Oregon

Family reunion. Enough said, right?

My sister, mom, and I left for Oregon on July 31st. We met up with my grandma and uncle (mom's mom and brother) at the terminal. True to form, my grandmother and uncle were worrying about what they'd do if we missed our flight.

Thursday, July 21, 2011

Sunrise, Sunset

It's a little strange watching the sun rise as your own sun sets. Well, I guess it's all the same sun. What I mean is, I'm watching the webcast of the Billabong Pro, J-Bay and it's 7am there, 7 pm here. The sun is tired of hanging out here in Hawaii, it wants to watch the contest too. But even now as it sets, shadows growing longer in my corner of the world, it's getting lighter on my webcast, South Africa demanding all attention. So I'll give it that, my attention I mean. Although I'm sure it's a heck of a lot more grateful to have the sun as a spectator than me.

Friday, July 15, 2011

Billabong Pro J-Bay 2011



Round 1 is completed!

The trouble with women's surfing

As the women's second to last event for the season, the Roxy Pro Biarritz, comes to a close, now more than ever the sport deserves some reflection. Yes it's true, a new breed of surfers are here; and they're here to stay. Led by freshly crowned ASP 2011 champ Carissa Moore, these surfers are all characterized by two unifying factors: 1) age and, 2) more progressive surfing.