Trivial birthday code

My daughter asked yesterday what an “application” was, which led to a discussion of computer programming. I demonstrated by writing a one-line program in Mathematica that computed the day of the week her birthday landed on every year since she was born. She was not impressed.

Code below; birthday changed for privacy protection.

Module[{bday},
 TableForm[
  Table[{i, bday = DatePlus["August 1, 2005", {i, "Year"}];
    DateString[bday, {"MonthName", " ", "DayShort", ", ", "Year"}],
    DateString[bday, "DayName"]}, {i, 0, 7}],
  TableHeadings -> {None, {"Birthday", "Date", "Day of Week"}}]]

table of birthdays

For extra credit, it’s not hard to generate a histogram showing the distribution of days of the week.

daysOfTheWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",  "Friday", "Saturday"};

numboHits[dayOfWeek_] :=
 Module[{holder},
  holder =
   Select[
    Gather[
     Sort[
      Table[
       DateString[DatePlus["August 1, 2005", {i, "Year"}], "DayName"], {i, 0,
        25(*years to check*)}]]], #[[1]] == dayOfWeek &];
  If[Length[holder] == 1, Length[holder[[1]]], 0]]

BarChart[Map[numboHits[#] &, daysOfTheWeek], ChartLabels -> daysOfTheWeek]

birthday histogram

Updated statistics — Jesus does not care for gay marriage at all

Updated (final?) death statistics came out today, and they revolutionize our understanding of God’s view towards the Republican party and its most cherished principles. Evidently, God would prefer that you vote Republican.

 

And he takes a dim view towards gay marriage.

 

 

and if you vote for a Democratic candidate, or marry somebody the same sex you are, Jesus is going to arrange for heavier than usual rains in your vicinity.

 

[boring details: Irene deaths from http://www.google.com/hostednews/ap/article/ALeqM5jpulHUHeBUW4RhBETMtrl3Mw93RQ?docId=ee2cb4f8e6224ad797d81bc73fbe442e
, population data from http://quickfacts.census.gov/qfd/index.html, % voting from McCain from http://en.wikipedia.org/wiki/United_States_presidential_election_in_Connecticut,_2008 and equivalent pages for the other states with Irene deaths, and gay marriage stats from http://en.wikipedia.org/wiki/Same-sex_marriage_law_in_the_United_States_by_state using a dummy variable of 1 if gay marriage is allowed, 0 if it isn’t, and 0.5 if some kind of intermediate right is granted.]

Jesus loves gay marriage

There was an earthquake near Washington DC last week, and a hurricane tore through New York City this morning, for the first time in many years. Naturally, some took this as a sign of God’s wrath. The same sort of thing was said about Hurricane Katrina. Republican presidential candidate Rick Perry regularly treats changes in weather as indicia of Jesus’s favor.

For what it’s worth, the numbers suggest that if Jesus is in fact using hurricanes to guide our behavior, he’s trying to guide us to approve gay marriage

and to vote for democrats.

[boring details: Irene deaths from http://www.ksro.com/news/article.aspx?id=3182755, population data from http://quickfacts.census.gov/qfd/index.html, % voting from McCain from http://en.wikipedia.org/wiki/United_States_presidential_election_in_Connecticut,_2008 and equivalent pages for the other states with Irene deaths, and gay marriage stats from http://en.wikipedia.org/wiki/Same-sex_marriage_law_in_the_United_States_by_state using a dummy variable of 1 if gay marriage is allowed, 0 if it isn’t, and 0.5 if some kind of intermediate right is granted.]

Futures conversion factors in Mathematica

Sovereign debt futures in the U.S. and elsewhere are designed to accept multiple different bonds for delivery. The exchange specifies a delivery factor algorithm to normalize the prices of these bonds (or notes) to make them roughly of equal value upon delivery. Since they’re not precisely of equal value, traders can make money trading around the cheapest to deliver bond.

The conversion factor in the U.S. is designed to convert the yield on any bond or note to 6%. Considering the semi-annual or monthly coupon payments on bonds and notes, the formula to do this looks as follows.

Options[conversionfactor] = {type -> "bond"};
(* versus "10yr note", "5yr note", or "2yr note" *)

conversionfactor[coupon_Real, wholeYearsToMaturity_Integer,
stubMonthsToMatury_Integer, OptionsPattern[]] :=
Module[{v, a, b, c, d},
v = If[stubMonthsToMatury < 7, stubMonthsToMatury, If[OptionValue[type] == "bond" || OptionValue[type] == "10yr note", 3, stubMonthsToMatury - 6]](* for 10 year, other options possible for other instruments *); a = 1/1.03^(v/6); b = coupon/2*(6 - v)/6; c = If[stubMonthsToMatury < 7, 1/1.03^(2*wholeYearsToMaturity), 1/ 1.03^(2*wholeYearsToMaturity + 1)]; d = coupon/.06*(1 - c); a*(coupon/2 + c + d) - b]

wholeYearsToMaturity represents the number of whole years from the first day of the delivery month to the maturity (or call) date of the bond or note.

stubMonthsToMatury represents the number of whole months between wholeYearsToMaturity and the maturity (or call) date rounded down to the nearest quarter for Treasury Bonds and 10 Year Note futures, or to the nearest month for 5-year and 2-year note futures.

For a 2 year note with 1 year, 10 months remaining and a coupon of .015,

In[]:= conversionfactor[.015, 1, 10, type -> "2yr note"]
Out[]= 0.922939

For a 5 year note with 4 years, 10 months remaining and a coupon of .0275

In[]:= conversionfactor[.0275, 4, 10, type -> "5yr note"]
Out[]= 0.86533

Memory conformity

There was a nice article in Science this week about Memory Conformity, the phenomenon whereby people will alter their own memories to conform with versions of events claimed by others. In the study, even people who correctly remembered a test event would persuade themselves that they had seen something different if their peers claimed to have seen it.

The study used brain imaging to try to figure out how this happens, and their results seem so vague as to be of little use. However the phenomenon itself is fascinating, and I wonder if it explains anomalies like the Angel of Mons, a fairy tale about angels appearing in the sky over British troops in WWI, subsequently believed by some of the people who were there.

The theory seems to me relevant to the financial markets, as the number of opinions in the market tend to collapse to a much narrower range than the breadth of experience and knowledge present would seem to justify. Most people abandon their own memory of precedents, and their own intuitive or formal models, and buy into the consensus narrative for why housing prices should be high, or internet stocks, or whatever.