Thursday, March 7, 2013

Acceptance Rejection Method for Simulation

In simulation, use the acceptance rejection method to simulate a random variable X with density f(x), for which there is no direct method to use the inverse transformation to generate it. However, suppose that there is a known algorithm to generate random variable Y with density g(x) such that f(x) / g(x) is bounded, that is f(x) / g(x) ≤ c. To find c, take the derivative of f(x) / g(x) to find the maximum value. Here's the algorithm:
  1. Generate Y according to the known algorithm
  2. Generate U, the uniform distribution from 0 to 1
  3. If U ≤ f(Y) / c*g(Y), then set X = Y
  4. Otherwise, start all over
Note that to produce each copy of X, the loop runs until condition 3 is satisfied. In the end, c become the average number of runs to produce a copy of X. In the following, a random variable with f(x) = 2x*e^(-x^2) is generated using MATLAB. The chosen g(x) = e^(-x), so f(x) / g(x) = 2x*e(x-x^2), and the maximum value c turns out to be exactly 2. 

There's no way to verify that this method does indeed generate a random variable with the given f(x). Instead, to test the accuracy of the algorithm here, two values (a and b) are inputted at the end. The program first calculates the integral of f(x) from [a, b], or the probability that X falls between the range of [a, b]. Finally, the program uses the simulated copies of X to count the proportion of the copies of X that fell in that interval of [a, b]. The two values should be close to each other.
func = @(x) 2*x.*exp(-x.^2);
n = 100000;
results = zeros(1,n);
numTimes = zeros(1,n);

for i=1:n
    trialDone = false;
    count = 0;
    while ~trialDone
        count = count + 1;
        y = -log(rand);
        u = rand;
        if u <= y*exp(y-y^2)
            results(i) = y;
            trialDone = true;
        end
    end
    numTimes(i) = count;
end

a = input('Lower bound for x: ');
b = input('Upper bound for x: ');
disp 'Evaluated integral using f(x): ';
quad(func, a, b)
disp 'Simulated integral using acceptance / rejection method: ';
length(results(results>a & results
disp 'Average trial runs per simulated copy: '
mean(numTimes)

Choose values between 0 and 2.5 for a and b, for over 99.8% of the data are in that range. Upon running the script using different values, the calculated integral closely matches the frequency proportion obtained from the simulation technique using the acceptance rejection method. While this does not verify the accuracy of the method, it at least illustrates that the resultant output resembles what the expected output should be. Finally, the resultant average trial runs per simulated copy hovers closely to 2 as expected.

Sunday, March 3, 2013

Simulating Correlated Pairs of Normally Distributed Random Variables

See previous related article: Simulating Normal Distribution with Polar Method

The previous article walked through how to generate independent copies of normally distributed random variables using the polar method. Here's an extension of that on how to generate pairs of normally distributed variables with a certain correlation between them. Polar method is still used to generate the variables. Recall that correlation ρ = cov(x,y) / (σ_x*σ_y). Both x and y can have its own σ and μ values.

n = input('Input the number of copies: ');
u_x = input('Input the mean of x: ');
s_x = input('Input the standard deviation of x: ');
u_y = input('Input the mean of y: ');
s_y = input('Input the standard deviation of y: ');
p = input('Input the correlation between x and y: ');
x = zeros(1,n);
y = zeros(1,n);

for i = 1:n
    z1 = sqrt(-2*log(rand))*cos(2*pi*rand);
    z2 = sqrt(-2*log(rand))*sin(2*pi*rand);
    x(i) = s_x*z1+u_x;
    y(i) = s_y*p*z1+s_y*sqrt(1-p^2)*z2+u_y;
end

disp 'Calculated mean and standard deviation for x:';
[mean(x), std(x)]
disp 'Calculated mean and standard deviation for y:';
[mean(y), std(y)]
disp 'Calculated correlation between x and y:';
corr(x',y')

The middle block of the codes here is the most essential. Variables z1 and z2 denote two independent copies of N(0,1). One copy is then used to generate x1 as σ_x*z1 + μ_x. Then the correlated pair y1 is generated as σ_y*ρ*z1 + σ_y*sqrt(1-ρ^2)*z2 + μ_y. Note that the z1 term is used in generating x1 as well as y1. Furthermore, note that the middle term σ_y*sqrt(1-ρ^2)*z2 disappears when ρ = ±1.

Simulating Normal Distribution with Polar Method

The polar method allows for quick generation of normally distributed random variables. For each trial, generate r^2 as exponentially distributed with λ = 1/2, and θ uniformly distributed between 0 and 2π. Within each trial, in fact 2 independent copies of the normally distributed random variables are generated: Z1 = sqrt(r^2)*cos(θ) and Z2 = sqrt(r^2)*sin(θ), both with μ = 0 and σ = 1. For normally distributed random variables with different parameters of μ and σ, simply use the transformation σ*Z+μ.
n = input('Input the number of copies: ');
u = input('Input the mean: ');
s = input('Input the standard deviation: ');
results = zeros(1,n);
for i = 1:2:n
    rsq = -2*log(rand);
    theta = 2*pi*rand;
    results(i) = s*sqrt(rsq)*cos(theta)+u;
    results(i+1) = s*sqrt(rsq)*sin(theta)+u;
end
disp 'Calculated mean and standard deviation using the polar method:';
[mean(results), std(results)]

The program at the end calculates the μ and σ values of the n copies of the random variables created as a result of this simulation, stored in the array named "results."

Friday, February 1, 2013

“Math Will Rock Your World” Digest

The article “Math Will Rock Your World”, a cover story featured in BusinessWeek and published on January 23, 2006, talks about the increasingly important role that mathematics and data analysis have played in industries and daily lives. In particular, subjects seemingly incongruous with analytic, such as linguistics, have become intertwined. This was discussed in the startup company Inform Technologies LLC, in which the algorithm “combs through thousands of press articles and blog posts” and “analyze each article by its language and context.” At the foundation of this data analysis are mathematical algorithms. Subjects and relationships between subjects combine to construct the polytope, “an object floating in space that has an edge for every known scrap of information.” This development is today’s informational revolution.

Technology companies, from Google to Facebook, are increasingly trying to make use of the gigabytes of information they have. The challenge is to use the information, most of which are stored as qualitative idea, into quantitative algorithms that can be propagated. These developments can be observed presently through efforts such as personally-targeted advertisement on Google searches or Facebook profiles. The article stresses the importance of data analysis in today’s business when it talks about how Ford Motor “could have sold an additional $625 million worth of trucks if it had lifted its online ad budget from 2.5% to 6% of the total.” Online advertisement allows companies to “profile customers” as the companies “know where their prospective customers are browsing, what they click on, and often, what they buy.” These ideas altogether illustrate the idea that access to information and the efficient mathematical analysis of the information can lead to great business solutions.

While this development fosters efficiency, it also raises some concerns that the article addresses. Utmost concern is privacy, which companies from Google and Facebook have all grappled with in the recent years. The inevitability of the “power of mathematicians to make sense of personal data and to model the behavior of individuals” will compromise privacy, and this is a concern not just for the individuals who data are being utilized. If the individuals fear for their data being manipulated beyond their range of comfort, they may lock the information up and prevent them from being utilized. This would hamper efforts of the mathematicians to develop algorithms and determine business or practical solutions. Another concern is the complexity of the new development. Managers must “understand enough about math to question the assumptions behind the numbers,” given that it becomes much easier to deceit “someone by having analysis based on lots of data and graphs.” As a result, this is the challenge for United States, as the article mentions. The country “must breed more top-notch mathematicians at home” by revamping education and simultaneously “cultivate greater math savvy” as the subject becomes more prevalent in the business profession.

For students studying mathematics and related fields, now is a great opportunity to foster these interests. Computer scientists and quantitative analysts are in high demand, and there is much room for development in this inchoate field. But even for those not directly working in this field, an understanding of the subject becomes increasingly important as well. A solid knowledge foundation allows for critical analysis of the technological improvements. As the field of data mining continues to revolutionaries business and the way society progresses, it is in the best interest of individuals to not only know how to best utilize these developments, but also to protect one’s own information to ensure that privacy is not greatly compromised in the reach for progress.

Source:

Tuesday, January 29, 2013

Inverse Transform Demonstration with Excel VBA

Given F(x) = 1-e^(-λ*x) as the cumulative distribution function, the inverse transform gives -1/λ*ln(U) as the function that has F(x) as its cdf, where U is the uniform distribution from [0, 1]. Here, the following VBA codes allow users to visualize this transformation in Microsoft Excel.

Upon the execution of this procedure, the user inputs a value for lambda. Then 10,000 simulations are run, initially generating a random number from [0, 1] and then inputting that random number into -1/λ*ln(U), and outputting the result in column A. At the end of the execution, column C contains the different x values from 0 to the maximum, in increments of 0.001. Column D reflects the cdf by counting entries of A that are smaller than the corresponding x value. Finally, column E calculates the true value of 1-e^(-λ*x). The idea is that the outputs in columns D and E are similar.

Sub InverseTransform()
'Demonstrates inverse transform of the cdf F(x) = 1-e^(-lambda*x)
Columns("A:E").Clear
Range("B1").Value = 1# * InputBox("Enter a positive value for lambda: ", "Input", 2)

'10,000 simulation trials to be performed
'Transform of F(x) gives -1/lambda*ln(U), where U is uniform distribution [0,1]
For i = 1 To 10000
Cells(i, 1) = -1 / Range("B1").Value * Log(Rnd)
Next i

'Determine the maximum for the range of numbers to work with
Range("B2").FormulaR1C1 = "=MAX(C[-1])"

'To determine the cumulative probability density, use 0.001 gradient from 0 to the maximum value as the counter
i = 1
While i / 1000 <= Range("B2").Value
Cells(i, 3).Value = i / 1000
'In column D, count entries in column A that are smaller than the counter, then divide by the number of trials
Cells(i, 4).FormulaR1C1 = "=COUNTIF(C[-3],""<""&RC[-1])/10000"
'In column E, calculate the true value of 1-e^(-lambda*x)
Cells(i, 5).FormulaR1C1 = "=(1-EXP(0-R1C2*RC[-2]))"
i = i + 1
Wend
Range("B2").Clear
End Sub

After the execution of this procedure, the user can perform further analysis. Graphing columns C through E does reveal that values in columns D and E are similar, as the points almost completely overlap. Error calculations from those two columns illustrate a similar result that the inverse transform method takes a function F(x), exponential function in this case, to produce a function whose cdf is F(x).

Wednesday, January 23, 2013

Monte Carlo Simulation of Pi with MATLAB

Using Monte Carlo simulation, the value of π can be approximated as (1/n) * Σ(4*sqrt(1-U_i^2)), where n is a large number representing the number of simulation trials, and U_i represents the i-th trial result from a uniform distribution in [0,1], for 1 ≤ i ≤ n. The MATLAB codes to run this simulation is quite straightforward:

numTrials = 1000000000;    %number of simulation trials
count = 0;
for trial = 1:numTrials
    roll = rand;    %uniform distribution [0,1]
    count = count + 4*sqrt(1-roll^2);
end
sum(count)/numTrials

In this simulation, n = 1,000,000,000. The output of 3.1416 matches the value of pi in all 4 displayed decimal places.

Monday, January 21, 2013

Intricacies of VLOOKUP

In Microsoft Excel, VLOOKUP is a very useful function that "searches for a value in the first column of a table array and returns a value in the same row from another column in the table array." The basic syntax of the function is =VLOOKUP(lookup_value, table_array, col_index_num, range_lookup), with the range_lookup being optional.

If range_lookup is FALSE, the function will look for only exact matches. In this case, the values in the first column of table_array do not need to be sorted. However, this is not the case if range_lookup is TRUE, which is the selection by default if omitted. Then the values in the first column must be in ascending order, as the function will use the largest value smaller than the lookup_value, if an exact match is not found.

Finally, two wildcard characters allow flexibility in the lookup_value when it is a text and the range_lookup is FALSE. A question mark (?) matches any single character, while asterisk (*) matches any sequence of characters. Use a tilde (~) before either the question mark or asterisk if they are the actual characters in the lookup_value. So as an example, the text value "abc" can be matched by both "a??" and "a*".

Source:

Tuesday, January 15, 2013

Seattle at a Glance

The largest city of the Pacific Northwest region, Seattle is home to over 600,000 people in the city proper and with 3.7 million residents, the Seattle-Tacoma-Bellevue Metropolitan Statical Area (Seattle MSA) is the 15th largest in the nation. The population grew by 8% between the 2000 and 2010 Census. A notable characteristic of the Seattle demographics is the level of education. For people over the age of 25, 56% of the residents have Bachelor's Degree or higher, which ranks Seattle among the top of major cities in the United States. University of Washington, the largest employer in the city proper, has over 40,000 students.

Seattle MSA accounts for 1.93 million jobs and generates an estimated gross metropolitan product of $218 billion. Aerospace, largely due to The Boeing Company, accounts over 80,000 jobs and $32 billion in revenue. Information technology is another important sector to the economy of Seattle MSA, employing over 100,000 and with companies such as Microsoft and Amazon.com headquartered in the region. Other notable companies headquartered in Seattle MSA include Costco, Starbucks, and Nordstrom.

International commerce is crucial to Seattle, which is situated roughly equidistant from Tokyo and London. Washington State ranks first in exports per capita, with Mainland China, Canada, and Japan as the top trading partners. Seattle-Tacoma International Airport is the closest airport on the continental US to Asia and served 31.2 million passengers in 2009. Internally, King County Metro and Sound Transit combine to offer express bus, light rail, and commuter rail services for the region.

In terms of the cost of living, Seattle trails major Northeastern and California cities from Boston to Los Angeles, and is comparable to that of Chicago. In 2010, 2 bedroom /1 bath apartment rental on average cost slightly over $1,100. Average high temperature in July is around 75F, while average low in winter hovers around the freezing point. At 36.2 inches, the average annual precipitation is actually lower than that of New York; summer is the dry season, while light precipitation and partly cloudy skies dominate the winter months.

Sources:

Monday, January 14, 2013

Currency Movements and Global Macro Calls

2013 Year Ahead Report published by Bank of America Merrill Lynch Global Research features 10 macro calls on the world economy. One call regarding interest rates and currencies states that "the U.S. dollar and euro could rally on the global recovery and greater fiscal clarity, pushing the yen lower and emerging market currencies higher." Another recall, regarding the crisis in Europe, states that "the big tail risk of a eurozone breakup has likely passed."

With this theme in mind, it was announced on Monday that Japan would further devalue its currency. It has already fallen 14% since October, and this has been helping the country's exports. Prime Minister Abe has "[stepped] up the pressure for the Bank of Japan to ease monetary policy" and increase the inflation target to 2%. The yen currently trades at 88.95 on the US dollar, which is near its 52-week high. While the yen has been pushed lower, the euro has hit an 11-month high against the US dollar, at $1.3382. Yields on Italian and Spanish 10-year bonds have stabilized to 4.19% and 5.03%, respectively. These numbers do go along the macro call from report that "European economy should stabilize as the year progresses."

Sources:

Friday, January 11, 2013

Principle World Cities by Time Zones

Here are the principle cities by time zones. This is not a comprehensive list of the time zones, as it excludes certain zones in the Pacific and Atlantic that do not have major cities. Most of North America and Europe utilize daylight saving time, as well as some of the major cities in South America and Australia. During the summer months, daylight saving time adds one hour forward.
  • UTC -1000: Honolulu
  • UTC -0900: Anchorage
  • UTC -0800: Los Angeles, Vancouver, Tijuana
  • UTC -0700: Denver, Calgary
  • UTC -0600: Chicago, Mexico City, Winnipeg
  • UTC -0500: New York, Toronto, Lima
  • UTC -0430: Caracas
  • UTC -0400: Santiago, San Juan, Halifax
  • UTC -0330: St. John's
  • UTC -0300: Sao Paulo, Buenos Aires
  • UTC ±0000: London, Lisbon, Casablanca, Accra
  • UTC +0100: Paris, Rome, Berlin, Madrid, Lagos, Zurich, Stockholm
  • UTC +0200: Cairo, Johannesburg, Athens, Istanbul, Helsinki, Jerusalem
  • UTC +0300: Nairobi, Baghdad, Riyadh
  • UTC +0330: Tehran
  • UTC +0400: Moscow, Dubai
  • UTC +0430: Kabul
  • UTC +0500: Karachi
  • UTC +0530: Delhi, Colombo
  • UTC +0545: Kathmandu
  • UTC +0600: Almaty, Dhaka
  • UTC +0630: Yangon
  • UTC +0700: Jakarta, Bangkok
  • UTC +0800: Beijing, Hong Kong, Perth, Singapore
  • UTC +0900: Tokyo, Seoul
  • UTC +0930: Adelaide
  • UTC +1000: Sydney
  • UTC +1100: Vladivostok
  • UTC +1200: Auckland, Suva