import streamlit as st
import math
from scipy.stats import *
import pandas as pd
import numpy as np
from plotnine import *

def app():
    # title of the app
    st.subheader("Proportions")
    st.sidebar.subheader("Proportion Settings")
    prop_choice = st.sidebar.radio("",["One Proportion","Two Proportions"])
    
    if prop_choice == "One Proportion":
        c1,c2,c3 = st.columns(3)
        with c1:
            x = int(st.text_input("Hits",20))
            n = int(st.text_input("Tries",25))
        with c2:
            nullp = float(st.text_input("Null:",.7))
            alpha = float(st.text_input("Alpha",.05))
        with c3:
            st.markdown("Pick a test:")
            tail_choice = st.radio("",["Left Tail","Two Tails","Right Tail"])
    
        one = st.columns(1)
        with one[0]:
            p_hat = x/n
            tsd = math.sqrt(nullp*(1-nullp)/n)
            cise = math.sqrt(p_hat*(1-p_hat)/n)
            z = (p_hat - nullp)/tsd
            x = np.arange(-4,4,.1)
            y = norm.pdf(x)
            ndf = pd.DataFrame({"x":x,"y":y})
            normp = ggplot(ndf)  + coord_fixed(ratio = 4) 
            if tail_choice == "Left Tail":
                pv = norm.cdf(z)
                cz = norm.ppf(alpha)
                rcz = cz
                cl = 1 - 2*alpha
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "steelblue", xlim = (-4,z))
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "orange", xlim = (-4,cz))
            if tail_choice == "Two Tails":
                pv = 2*(1-norm.cdf(abs(z)))
                cz = abs(norm.ppf(alpha/2))
                rcz = "±" + str(abs(norm.ppf(alpha/2)))
                cl = 1 - alpha
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "steelblue", xlim = (-4,-1*abs(z)))
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "steelblue", xlim = (abs(z),4))
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "orange", xlim = (-4,-1*abs(cz)))
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "orange", xlim = (abs(cz),4))
            if tail_choice == "Right Tail":
                pv = 1 - norm.cdf(z)
                cz = -1 * norm.ppf(alpha)
                rcz = cz
                cl = 1 - 2*alpha
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "steelblue", xlim = (z,4))
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "orange", xlim = (cz,4))
            me = cz * cise
            rme = "±" + str(abs(me))
            data = pd.DataFrame({"p-Hat":p_hat,"z-Score":z,"p-Value":pv,"CV":rcz,"Test SD":tsd,"C-Level":cl,"CI SE":cise,"ME":rme},index = [0])  
            st.write(data)
            normp = normp + geom_segment(aes(x = z, y = 0, xend = z, yend = norm.pdf(z)),color="red")
            normp = normp + geom_line(aes(x=x,y=y))
            st.pyplot(ggplot.draw(normp))
            lower = p_hat - abs(me)
            upper = p_hat + abs(me) 
            st.write(str(100*cl) + "'%' confidence interval is (" + str(lower) +", "+str(upper)+")") 
        
    if prop_choice == "Two Proportions":
        c1,c2,c3 = st.columns(3)
        with c1:
            x1 = int(st.text_input("Hits 1",20))
            n1 = int(st.text_input("Tries 1",25))
            
        with c2:
            x2 = int(st.text_input("Hits 2",30))
            n2 = int(st.text_input("Tries 2",50))
        with c3:
            alpha = float(st.text_input("Alpha",.05))
            st.markdown("Pick a test:")
            tail_choice = st.radio("",["Left Tail","Two Tails","Right Tail"])
    
        one = st.columns(1)
        with one[0]:
            p_hat1 = x1/n1
            q_hat1 = 1 -p_hat1
            p_hat2 = x2/n2
            q_hat2 = 1 - p_hat2
            pp_hat = (x1+x2)/(n1+n2)
            dp_hat = p_hat1 - p_hat2
            pq_hat = 1-pp_hat
            tsd = math.sqrt(pp_hat*pq_hat*(1/n1+1/n2))
            cise = math.sqrt(p_hat1*q_hat1/n1+p_hat2*q_hat2/n2)
            z = (p_hat1 - p_hat2)/tsd
            
            x = np.arange(-4,4,.1)
            y = norm.pdf(x)
            ndf = pd.DataFrame({"x":x,"y":y})
            
            normp = ggplot(ndf) + coord_fixed(ratio = 4) 
            if tail_choice == "Left Tail":
                pv = norm.cdf(z)
                cz = norm.ppf(alpha)
                rcz = cz
                cl = 1 - 2*alpha
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "steelblue", xlim = (-4,z))
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "orange", xlim = (-4,cz))
            if tail_choice == "Two Tails":
                pv = 2*(1-norm.cdf(abs(z)))
                cz = abs(norm.ppf(alpha/2))
                rcz = "±" + str(abs(norm.ppf(alpha/2)))
                cl = 1 - alpha
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "steelblue", xlim = (-4,-1*abs(z)))
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "steelblue", xlim = (abs(z),4))
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "orange", xlim = (-4,-1*abs(cz)))
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "orange", xlim = (abs(cz),4))
            if tail_choice == "Right Tail":
                pv = 1 - norm.cdf(z)
                cz = -1 * norm.ppf(alpha)
                rcz = cz
                cl = 1 - 2*alpha
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "steelblue", xlim = (z,4))
                normp = normp + stat_function(fun = norm.pdf, geom = "area",fill = "orange", xlim = (cz,4))
            me = cz * cise
            rme = "±" + str(abs(me))
            data = pd.DataFrame({"p-Hat 1":p_hat1,"p-Hat 2":p_hat2,"Pooled p-Hat":pp_hat,"Diff p-Hat":dp_hat,"z-Score":z,"p-Value":pv,"CV":rcz,"Test SD":tsd,"C-Level":cl,"CI SE":cise,"ME":rme},index = [0])  
            st.write(data)
            normp = normp + geom_segment(aes(x = z, y = 0, xend = z, yend = norm.pdf(z)),color="red")
            normp = normp + geom_line(aes(x=x,y=y))
            st.pyplot(ggplot.draw(normp))
            lower = dp_hat - abs(me)
            upper = dp_hat + abs(me) 
            st.write(str(100*cl) + "'%' confidence interval is (" + str(lower) +", "+str(upper)+")") 

[((10, 4, 10, 31), 'streamlit.subheader', 'st.subheader', ({(10, 17, 10, 30): '"""Proportions"""'}, {}), "('Proportions')", True, 'import streamlit as st\n'), ((11, 4, 11, 47), 'streamlit.sidebar.subheader', 'st.sidebar.subheader', ({(11, 25, 11, 46): '"""Proportion Settings"""'}, {}), "('Proportion Settings')", True, 'import streamlit as st\n'), ((12, 18, 12, 75), 'streamlit.sidebar.radio', 'st.sidebar.radio', ({(12, 35, 12, 37): '""""""', (12, 38, 12, 74): "['One Proportion', 'Two Proportions']"}, {}), "('', ['One Proportion', 'Two Proportions'])", True, 'import streamlit as st\n'), ((15, 19, 15, 32), 'streamlit.columns', 'st.columns', ({(15, 30, 15, 31): '3'}, {}), '(3)', True, 'import streamlit as st\n'), ((26, 14, 26, 27), 'streamlit.columns', 'st.columns', ({(26, 25, 26, 26): '1'}, {}), '(1)', True, 'import streamlit as st\n'), ((71, 19, 71, 32), 'streamlit.columns', 'st.columns', ({(71, 30, 71, 31): '3'}, {}), '(3)', True, 'import streamlit as st\n'), ((84, 14, 84, 27), 'streamlit.columns', 'st.columns', ({(84, 25, 84, 26): '1'}, {}), '(1)', True, 'import streamlit as st\n'), ((23, 12, 23, 39), 'streamlit.markdown', 'st.markdown', ({(23, 24, 23, 38): '"""Pick a test:"""'}, {}), "('Pick a test:')", True, 'import streamlit as st\n'), ((24, 26, 24, 77), 'streamlit.radio', 'st.radio', ({(24, 35, 24, 37): '""""""', (24, 38, 24, 76): "['Left Tail', 'Two Tails', 'Right Tail']"}, {}), "('', ['Left Tail', 'Two Tails', 'Right Tail'])", True, 'import streamlit as st\n'), ((29, 18, 29, 46), 'math.sqrt', 'math.sqrt', ({(29, 28, 29, 45): 'nullp * (1 - nullp) / n'}, {}), '(nullp * (1 - nullp) / n)', False, 'import math\n'), ((30, 19, 30, 47), 'math.sqrt', 'math.sqrt', ({(30, 29, 30, 46): 'p_hat * (1 - p_hat) / n'}, {}), '(p_hat * (1 - p_hat) / n)', False, 'import math\n'), ((32, 16, 32, 34), 'numpy.arange', 'np.arange', ({(32, 26, 32, 28): '-4', (32, 29, 32, 30): '4', (32, 31, 32, 33): '0.1'}, {}), '(-4, 4, 0.1)', True, 'import numpy as np\n'), ((34, 18, 34, 45), 'pandas.DataFrame', 'pd.DataFrame', ({(34, 31, 34, 44): "{'x': x, 'y': y}"}, {}), "({'x': x, 'y': y})", True, 'import pandas as pd\n'), ((61, 19, 61, 143), 'pandas.DataFrame', 'pd.DataFrame', (), '', True, 'import pandas as pd\n'), ((62, 12, 62, 26), 'streamlit.write', 'st.write', ({(62, 21, 62, 25): 'data'}, {}), '(data)', True, 'import streamlit as st\n'), ((81, 12, 81, 39), 'streamlit.markdown', 'st.markdown', ({(81, 24, 81, 38): '"""Pick a test:"""'}, {}), "('Pick a test:')", True, 'import streamlit as st\n'), ((82, 26, 82, 77), 'streamlit.radio', 'st.radio', ({(82, 35, 82, 37): '""""""', (82, 38, 82, 76): "['Left Tail', 'Two Tails', 'Right Tail']"}, {}), "('', ['Left Tail', 'Two Tails', 'Right Tail'])", True, 'import streamlit as st\n'), ((93, 18, 93, 54), 'math.sqrt', 'math.sqrt', ({(93, 28, 93, 53): 'pp_hat * pq_hat * (1 / n1 + 1 / n2)'}, {}), '(pp_hat * pq_hat * (1 / n1 + 1 / n2))', False, 'import math\n'), ((94, 19, 94, 63), 'math.sqrt', 'math.sqrt', ({(94, 29, 94, 62): 'p_hat1 * q_hat1 / n1 + p_hat2 * q_hat2 / n2'}, {}), '(p_hat1 * q_hat1 / n1 + p_hat2 * q_hat2 / n2)', False, 'import math\n'), ((97, 16, 97, 34), 'numpy.arange', 'np.arange', ({(97, 26, 97, 28): '-4', (97, 29, 97, 30): '4', (97, 31, 97, 33): '0.1'}, {}), '(-4, 4, 0.1)', True, 'import numpy as np\n'), ((99, 18, 99, 45), 'pandas.DataFrame', 'pd.DataFrame', ({(99, 31, 99, 44): "{'x': x, 'y': y}"}, {}), "({'x': x, 'y': y})", True, 'import pandas as pd\n'), ((127, 19, 127, 205), 'pandas.DataFrame', 'pd.DataFrame', (), '', True, 'import pandas as pd\n'), ((128, 12, 128, 26), 'streamlit.write', 'st.write', ({(128, 21, 128, 25): 'data'}, {}), '(data)', True, 'import streamlit as st\n'), ((17, 20, 17, 44), 'streamlit.text_input', 'st.text_input', ({(17, 34, 17, 40): '"""Hits"""', (17, 41, 17, 43): '20'}, {}), "('Hits', 20)", True, 'import streamlit as st\n'), ((18, 20, 18, 45), 'streamlit.text_input', 'st.text_input', ({(18, 34, 18, 41): '"""Tries"""', (18, 42, 18, 44): '25'}, {}), "('Tries', 25)", True, 'import streamlit as st\n'), ((20, 26, 20, 51), 'streamlit.text_input', 'st.text_input', ({(20, 40, 20, 47): '"""Null:"""', (20, 48, 20, 50): '0.7'}, {}), "('Null:', 0.7)", True, 'import streamlit as st\n'), ((21, 26, 21, 52), 'streamlit.text_input', 'st.text_input', ({(21, 40, 21, 47): '"""Alpha"""', (21, 48, 21, 51): '0.05'}, {}), "('Alpha', 0.05)", True, 'import streamlit as st\n'), ((73, 21, 73, 47), 'streamlit.text_input', 'st.text_input', ({(73, 35, 73, 43): '"""Hits 1"""', (73, 44, 73, 46): '20'}, {}), "('Hits 1', 20)", True, 'import streamlit as st\n'), ((74, 21, 74, 48), 'streamlit.text_input', 'st.text_input', ({(74, 35, 74, 44): '"""Tries 1"""', (74, 45, 74, 47): '25'}, {}), "('Tries 1', 25)", True, 'import streamlit as st\n'), ((77, 21, 77, 47), 'streamlit.text_input', 'st.text_input', ({(77, 35, 77, 43): '"""Hits 2"""', (77, 44, 77, 46): '30'}, {}), "('Hits 2', 30)", True, 'import streamlit as st\n'), ((78, 21, 78, 48), 'streamlit.text_input', 'st.text_input', ({(78, 35, 78, 44): '"""Tries 2"""', (78, 45, 78, 47): '50'}, {}), "('Tries 2', 50)", True, 'import streamlit as st\n'), ((80, 26, 80, 52), 'streamlit.text_input', 'st.text_input', ({(80, 40, 80, 47): '"""Alpha"""', (80, 48, 80, 51): '0.05'}, {}), "('Alpha', 0.05)", True, 'import streamlit as st\n')]