Estimating class birth year

Functions

elementcount[list_] :=
Module[{elements,i},
elements = Union[list];
Table[
{elements[[i]],Count[list,elements[[i]]]},
{i,1,Length[elements]}
]
];

Import and format class names

(*
names = 
Import[
"/Users/dmeyer/Teaching/111fall11/names/namesF.txt",
"Table"
];
Do[
Print[names[[i,1]]," ",names[[i,2]]],
{i,1,Length[names]}
];
Fnames = Sort[Table[names[[i,2]],{i,1,Length[names]}]];
Fnamescounts = elementcount[Fnames] 
*)

Fnamescounts = 
{{"Abbey",1},{"Anila",1},{"Jillian",1},{"Kendra",1},{"Kimberly",1},{"Kristan",1},{"Lourdes",1},{"Madelena",1},{"Merna",1},{"Natalie",1},{"Olga",1},{"Rachel",1},{"Rozhyar",1},{"Sarah",1},{"Valerie",1},{"Yuling",1}}

(*
names = 
Import[
"/Users/dmeyer/Teaching/111fall11/names/namesM.txt",
"Table"
];
Do[
Print[names[[i,1]]," ",names[[i,2]]],
{i,1,Length[names]}
];
Mnames = Sort[Table[names[[i,2]],{i,1,Length[names]}]];
Mnamescounts = elementcount[Mnames]
*)

Mnamescounts = 
{{"Adolfo",1},{"Ali",1},{"Austin",1},{"Bobby",1},{"Bryan",1},{"Carlos",1},{"Chi",1},{"Christian",1},{"Clayton",1},{"Evan",1},{"Geoffrey",1},{"Gevork",1},{"Gylmar",1},{"Henry",1},{"John",1},{"Joseph",1},{"Justin",2},{"Kevin",1},{"Lawrence",1},{"Lucas",1},{"Mason",1},{"Matthew",1},{"Michael",1},{"Mike",1},{"Robert",1},{"Rodrigo",1},{"Rohan",1},{"Ryan",2},{"Shaun",1},{"Shawn",1},{"Steven",1},{"Sumit",1},{"Tri",1},{"Yang",1}}

Import SSA CA names

(* to run, replace file path with file path on your computer *)
names =
Import[
"/Users/dmeyer/Teaching/111fall11/names/CA.txt",
"CSV"
];

Length[names]
names[[1]]
Length[names[[1]]]

Format SSA CA names

Do[
totalpop[year,"F"] = 0;
totalpop[year,"M"] = 0,
{year,1910,2010}
];
namecount[year_,sex_,name_]=0;
counts[year_,sex_]={};
Do[
{namestring,count} = names[[i]];
namelength = StringLength[namestring]-8;
year = ToExpression[StringTake[namestring,{5,8}]];
sex =StringTake[namestring,{4}];
AppendTo[counts[year,sex],count];
totalpop[year,sex] = totalpop[year,sex] + count;
name =  StringTake[namestring,-namelength];
namecount[year,sex,name]=count,
{i,1,Length[names]}
];

Look up class name counts in SSA CA names

Do[
Fcounts = 
Table[
namecount[year,"F",Fnamescounts[[i,1]]],
{i,1,Length[Fnamescounts]}
];
Print[year," ",Position[Fcounts,0]];
Mcounts = 
Table[
namecount[year,"M",Mnamescounts[[i,1]]],
{i,1,Length[Mnamescounts]}
];
Print[year," ",Position[Mcounts,0]],
{year,1910,2010}
]

Remove names with 0 counts in SSA CA names in some date range

Fcommonnamescounts = {};
Do[
If[!MemberQ[{2,8,9,13,16,1,6},i], (*2,8,9,13,16;;1,6;*)
AppendTo[Fcommonnamescounts,Fnamescounts[[i]]]
],
{i,1,Length[Fnamescounts]}
];
Fcommonnamescounts

Mcommonnamescounts = {};
Do[
If[!MemberQ[{13,32,12,34,7,27,33},i], (*13,32;12,34;7,27,33;*)
AppendTo[Mcommonnamescounts,Mnamescounts[[i]]]
],
{i,1,Length[Mnamescounts]}
];
Mcommonnamescounts

Clear[Fcounts];Clear[Mcounts];
Do[
Fcounts[year] = 
Table[
namecount[year,"F",Fcommonnamescounts[[i,1]]],
{i,1,Length[Fcommonnamescounts]}
];
Print[year," ",Position[Fcounts[year],0]];
Mcounts[year] = 
Table[
namecount[year,"M",Mcommonnamescounts[[i,1]]],
{i,1,Length[Mcommonnamescounts]}
];
Print[year," ",Position[Mcounts[year],0]],
{year,1910,2010}
]

Compute Pr(year|class names) and plot

y1 = 1988;y2 = 1996;
y1 = 1981;y2 = 2001;
y1 = 1970;y2 = 2010;
prob[year_] := 
Which[
y1≤year≤y2,1/(y2-y1+1),
True,0
];

probNgY[year_] := 
(Product[
Binomial[Fcounts[year][[i]],Fcommonnamescounts[[i,2]]],
{i,1,Length[Fcommonnamescounts]}
]/
Binomial[
totalpop[year,"F"],
Sum[Fcommonnamescounts[[i,2]],{i,1,Length[Fcommonnamescounts]}]
]
)*
(Product[
Binomial[Mcounts[year][[i]],Mcommonnamescounts[[i,2]]],
{i,1,Length[Mcommonnamescounts]}
]/
Binomial[
totalpop[year,"M"],
Sum[Mcommonnamescounts[[i,2]],{i,1,Length[Mcommonnamescounts]}]
]
);

probYgN[year_] :=
probNgY[year]*prob[year]/
Sum[probNgY[y]*prob[y],{y,y1,y2}];

plotdata =
Table[{{y},N[probYgN[y]]},{y,y1,y2}]

DateListPlot[
plotdata,
PlotStyle->{Red,Thick},
FrameTicksStyle->Directive[18],
Joined->True,
PlotRange->All
]