clear all
close all force
% Binomial Estimation and Testing 
dna = ['gatctcttcttcgtggttagtattctttgtgttctgctaaagaactctttgcctaccctc'... 
 'aggtttgcttaggtctacaatctactgtgagttgatttttttaatggacagtgagatgta'...
 'ggaattgtttctttcttatggatttccagtcattcagcaccatttattaaaaagagcccc'...
 'ctgtgagcccctggtcttggtgccttctgtgttataaatcaggtgttcctgtgtgtctat'...
 'gcgtgggcccatatgtatatttctggactctcttttgccccactggtctctttatctatc'...
 'caagtgataacccctaccaccatcactccttcagctttataatgtcttgatagtcaatag'...
 'agtatatatttcaactttgttcttcaagagtgtcttgataattctcagtcctttgcattt'...
 'tcacataaaacttaaaatctgccttccatttgcatacacgcacacacacaaaaagaaaac'...
 'tctgctagaaatttgatagggatcactttgaatctctctatcaatttggggagactttgc'...
 'aatgctgagtcttccaatccatgaacatgatatatgcttccatttatgcttcaatttttc'...
 'ttaataacttttaaagctttcagtgtcaagttcttatacatattttattagataaattac'...
 'aggtgtttggttttctactataaatagaatcattttgaaattatcttatttgttgctgat'...
 'atatggaaatactgactttgtaaatatgcagtcttactcaatgcacttattagttctggt'...
 'tatttatttgtagaataaacaaaatcatatcatttgtaaaaaaacaggggggttgggttt'...
 'tgtttgtttgtttgtttgtttgagacagggtctcactctgtcatgcagactggagtggag'...
 'tggcacagtcatggctcactgcagccttgacctcttgggctcaagcctcctgagtagctg'...
 'cgaccacaggaatacaccatgaccaagccttgcctgatttttttttttttcccagaaaca'...
 'gggtttctctatgttggccaggctggtcttgaactcctgtgctcaagcagtcctcccgcc'...
 'ctggcctcccaaagtgctgggattacaggcatgagccaccgcacctggtccagttttatt'...
 'tattctctttcagttcttactggctttatttcatgtttgcttttactgaactggacagga'...
 'ttttgagcataatgctgatgtcataatagtagacattaaaagaaaagcacgtatattctt'...
 'accactaaattttatgtttggccaggcatggtggctcatgcctgtaatcccagcactttg'...
 'ggaggccgaggcaggtggatcacctgaggtcaggagcttgagaccagcctggccaacatg'...
 'gtgaaaccctgtctctactaaaaatacaaaaactagctaggtgtgttagcacatgcctgt'...
 'aatctcagctactagggaggctgaggcagcagaattgcttgaacccaggggtcagaggtt'...
 'gcagtgacccaagattgcactgctgcacttgaacctgggcaacaaagcgagactctgtct'...
 'caaaaataaatttaaaaaaaagttatgtttgctatggagttgtttttgttgcttctgctg'...
 'cttcatacatttcctttatcaggttatagaattttccttctatttttctttttatctttt'...
 'ttttttttttgagacaaagtcttgctctgttgcccaggctggagcgcagtggagtgatct'...
 'cagctcaccataacctctgcctcccaggttcaagcgattctcatgcctcagcctcccaag'...
 'tagctgggactacagcatgcgccaccacacccagctaatttttgcatttttagtagacac'...
 'agggtttcaccatgttggccagcatggccttgaactcctggcctcaggtgatctgcccgc'...
 'ctcgacctcccaaagtgttgggattacaggcgtgagccactgtggctgaccccttctatt'...
 'tttatttgccaagagtttttattatgaatagatggcagattttatcaaatgctttttctg'...
 'catctgttgaaataatcatgtgactttcctcctttattcagttattgtagtgaattaaaa'...
 'ttattttattttcaaatgttacaccaaacttgtattccttgaataaatctaattcagctg'...
 'tgatatattagcattttatacaagtatggctaggttctatttgctaatattttgcttaga'...
 'acttttgtatctatgacataaacgagactggcttattatttactctgcttataatatcct'...
 'tattttggtatcaaggttattcttggcctcataaatcaagttgaaaagcattttctcatc'...
 'tctggaaagttttaataagattggcagttttccttaaatgtcagatagaatccactggtg'...
 'aagccaaatgggtctggaaatttctttattggaaagtttcaaattaaatattccatttat'...
 'ttaacctagaagactattcatattttctatttctttttttgtcatttttgctaagttgtg'...
 'tttttctaggaacttgcctgttatacttaggctttcgcatatatctgcatgatcgcttgg'...
 'gtttttacagctgtaggctctgtaataatgtcacccaccctttcctgattctgattacct'...
 'gcacttctctcttattccttgaacattcttcaacaggggctgggtttcttagattcccag'...
 'gaactgtttctccctctctctctctccctctttctccccacttcctctatctgcagggat'...
 'ttaactggactctgttcatacaaactgtgctatcctctgtcaaaatcaagctgctgccag'...
 'atgaggaagtggtggtctatggcatcccctacctgcagaaccttgaaaacatcatcgaca'...
 'cctactcagccaggtgaggcccagggagggtctccaaaggcctcagcatctttgcccctg'...
 'ctctcccatcctccccaacacagcttctgctcccaggaggggaccctatgaagcatctgc'...
 'cctgaggggtgtgggacatgaggggttaagctttagtggccagtggagtcttacattaaa'...
 'aatgtgtcacatcccaagcacccggggcagatgggctggatcttggaggtggttgtcctg'...
 'gaggttttctccagcacccataactctctggggtggaacgtgctggccaagggccctaag'...
 'ccatatctcctcccttgcaaagacctctcacctgttctcccctcaccctctaccttctgc'...
 'ccccaggaccatacagaactacctggtctggcgcctggtgctggaccgcattggtagcct'...
 'aagccagagattcaaggacacacgagtgaactaccgcaaggtgagccccttccctgccca'...
 'gagcctggccgggcactcattcattcaggccacgatgctgagcataagctgtaggcaaac'...
 'accgaggtccacaccagggtcacagccatggacgaggcagggcgaggcgcccaccccagt'...
 'ggggagtggaccatgacagtcaacaaggcagaacgtagctctgccaaaaccgcagagtat'...
 'gatgtggcggggaggcagggagggacgaggacagggagcactgctccggggaggcgacat'...
 'ttgagaccttaaaggcctgaagaggcggcggggtgatttcccgggagcagaaacagccag'...
 'tgccagtgcagaggccagaggcaggagcaggcaggggccggacccgtggtcagcagcgga'...
 'caggaaggagaagatggcagggagggaggcgaggtgaccacgggccacagcagccggttt'...
 'ggattgtactccctgtctgaaagccgcaggggtgctaagagcagagacgcggtcagatat'...
 'gctgctcctgtagtcactgtggctgctccagggctggggtgagggtggagacaaggaacc'...
 'gcagggaggccctagggggattctggctgttctggggagagtgagttgagtagggggcgg'...
 'ggtcagcatggaggccgatgtggtcacccaggtgaggtgacaggcttggccatggtgctg'...
 'gcagacacacagacgcaggggaggtgttgaccgcagcagctgagggattgggtatagggg'...
 'ctaagggcagaggggagtcgggactctcaagacctttgttagagtaatgggagggacaga'...
 'ggtccccactgcaatggggagactgaggcagaggcaagctgggggcaagtcaagcctcct'...
 'gttgccatgggggggtaggtcagagacatctgtgagtcaagcgtcaggggggcactgagt'...
 'ctaggagctttggggaggaagaaagggctgcctccctttcctaaagggcaggaagtcctg'...
 'cagcccagaaggtgtggcaggacgctgggcagagtgctccctgggctttgcctgagtctg'...
 'gggcccagagtctcgtgctcctgtgtccctggggacagatgagcaggtgttctcacagct'...
 'gcttcaacaatggagagaccagggccctgggagctggtggctgagagggacgtggtccca'...
 'ggccttgctctggggtcagcgccagccctgtgccttgagactggggctggagtcctggag'...
 'agcagagtccagccagacaaccaggacaggggccggggaggaacccccccaactcctcag'...
 'ggtaaaggcccagctctcagtctgcggaagttccccaatcacaataccatgcccatcctt'...
 'ccccccgccgcagcagcacccagaggtgaggctgagatcatcccgtgcccccgctagccc'...
 'cggggcaccctgaggctgaggataggtgggtctgctcaccccgtaagcccagggccccac'...
 'gccaggcctgggaaggctgacgtgggggcccgtgggatgatcgggtggccccaccaggcg'...
 'ctgtttggcacaatggtggaggaggtgcgctggcgtgaatgtgtgggctacgtcaacagc'...
 'aacatggagaacgccgtgggctccctctacgtcagggaggcgttccctggagacagcaag'...
 'agcatggtgggcgcccctcatccatggcccacgcggggccagccttccacagggacaccg'...
 'agcctccctgccctgagactcccaccccgcccaggctcaggggcacctgccccagctcac'...
 'cagacagagggaggctaagggaagccgtgcccaccccatgcccctgaccccagccggcca'...
 'ctgggccccagcctgagccctgcaggtccagctgggaaaggcacaggtggtacaagttgc'...
 'ccctggcccacgaggacccacagcccagcagggaggaccctgaagttgggttgtcatctc'...
 'atccccccaccggccccacacctcacctgagaggcccacagctgggcagccctgggcctt'...
 'gccccggcggggctcagaccagacctccccaaagcccttgaggctggtcattcgccctcc'...
 'agatgagtctgggacacgagggccatgcccgattcccaggtcagagaactcattgacaag'...
 'gtgcggacagtgtttgtggagacgctggacgagctgggctggatggacgaggagtccaag'...
 'aagaaggcgcaggagaaggtatgtggctcgcagaggtcagggcagccccgactggatctg'...
 'ggcacgggcagcaccgccccgacaaagcctcctgacggtccccacggggagaaggcagcc'...
 'ccgggaggcgcagacaggaccccacggggagaaggcagccccgggaggcgcagacaggac'...
 'cccacggggagaaggcagccccgggaggcgcagacaggaccactcgcccactgtggagct'...
 'gaggaaaagcaggcaggggcaggaaccaggcctgggacatccccagcaccccccatgagc'...
 'cagcgggggtagcggtggcctcgggagcaagggtcagacccaccagaagctcctctggac'...
 'ccaagtgacagacggcagccgagtgacccctccagggcccctgcccccacccggacccct'...
 'ggcgggctgttgggctctaggtcaggtgggcggagccttccatattctgtccccattaaa'...
 'cctccccagagcctgcaaggtgggggccagatcccggagccccccagcccctcaccacag'...
 'gacagggagagcacagccaggatgccccgggctgacccctcccacagtgtggcactcact'...
 'gaccggcccccgggctgactccatccgcagtggggcacccactgaccagtccctcctccc'...
 'cactcaggccatgagcatccgggagcagatcgggcaccctgactacatcctggaggagat'...
 'gaacaggcgcctggacgaggagtactccaatgtgcgcccccaaccccagccctaaactgc'...
 'cccccacaccgtggacatgggccctgattgcctgggggctcctctgtgccaagctctgtg'...
 'ccctcctcccaggcgctaacacagctgtcctcaccgtgccctagggtggtggctccagca'...
 'ctaccctcagtgtacagatggggaaactgaggcctggagggacgagggacttgctcagag'...
 'tcatgtaaatgcccacagccagggctgccccgttcacctgcacccacagccgactgtcca'...
 'cgtgcggacgcctggcaaggcttgaggtccttccccaaccacccaggacctctccccttg'...
 'ctcctctgagggcccggccccagcatctggcagtgactgcccctctcccgtagctgaact'...
 'tctcagaggacctgtactttgagaacagtctgcagaacctcaaggtgggcgcccagcgga'...
 'gcctcaggaagcttcgggaaaaggtggacccaaatctgtgagtggcatcgctggtgcggc'...
 'atgggcggggggggccagggagtggccagtaaggcgtgcagggctgcctgccaggcgggg'...
 'gactggatgcgtggctggggtccagtgccagctgaggggtgcagtcactgggcctatctt'...
 'ggacagagtcccccatcaccctctgctttctggggcccaagtcgcctcctcctcagggac'...
 'cccaggaaggaaccagatcttgggataggcctgggcccttgctctggtgtggccagggta'...
 'ggccctggtccatcaaggcagagagagcccagagggagggtccccggagaggcggctccc'...
 'agagaccggcttgtctatcacagctggatcatcggggcggcggtggtcaatgcgttctac'...
 'tccccaaaccgaaaccagattggtaagttcctccccctccctgcctgcccaggccctttg'...
 'gtgccagagttgaaggggtggcttgcagccccctgcccatcccttcctcctgggggcacc'...
 'ccagcctgttcttggaacattctcctatgtcaccccttgctgcccccgtggggaacactc'...
 'actcccgtgcccatgagcggcttcgggagggacttggcaggcggcccagagcctttcctg'...
 'caggcagaacaggatggtgggaggaggccggactgaggtcctgggtctgccaggcacgag'...
 'cccgcccagcctctgaacctccgtgtcttcatctgtaaattgagggaatcctgacctctg'...
 'cctcccggtctgtgggatcaagtgtcagaacccctggcagcgccatccctgagcctggca'...
 'cacaggagatgtgtccactcacagcttattctttgtcccgcccttggggacagtattccc'...
 'tgccgggatcctccagccccccttcttcagcaaggagcagccacaggccttgaactttgg'...
 'aggcattgggatggtgatcgggcacgagatcacgcacggctttgacgacaatggtgagcg'...
 'gccgcccctccaatcacacacgccctcccctctccgcggagaagcagccaggaaggggcc'...
 'tgggggcgctccattcaaccccttcacggtttcagtggggacaccgaggccaggtcctca'...
 'gggagctgtgggggcagagcaggctttgaatgccaggctggggccctgtctgggggcact'...
 'gactcccagcatggaggaggggacccagggactccagcccatgaccctgactccacctgg'...
 'cactgggcacttccctcccacggcctgtgacatggggacgacaacactgcccaatacagc'...
 'agcgaagcccagatcccagcctgggagactctccagacggccagcatcctccaccggggg'...
 'gtgtgctacccccgccatggccatgctgacctccgcccctcggtggggactgccctgcct'...
 'gggacaggggccccagctggttttcccctgtg']; 

% Interested in proportion of 'a';
%-----------------------------------------------
% Point estimate
n = length(dna)
p_hat = sum( dna == 'a')/n
%-----------------------------------------------
% Interval Estimate
alpha = 0.01; % 99-percent CI
z_alpha2 = norminv(1 - alpha/2);  
add = z_alpha2 * sqrt( p_hat * (1-p_hat)/ n );
int = [p_hat - add    p_hat + add]
%-----------------------------------------------
%%
% Testing H_o: p = 0.2  vs H_1: p > 0.2
zstat = (p_hat - 0.2)/sqrt( 0.2 * (1-0.2)/n )
pvalue = 1 - normcdf(zstat) 

%
%-----------------------------------------------
sprintf(' p_hat= %7.5f', p_hat)
sprintf('%d Percent Confidence interval= [%7.5f, %7.5f]', ...
         round((1-alpha)*100), int)
sprintf('Test statistics Z= %7.5f   with p-value %7.5f', zstat, pvalue)

% --------------------------------
