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)+")") 

[((162, 189), 'streamlit.subheader', 'st.subheader', (['"""Proportions"""'], {}), "('Proportions')\n", (174, 189), True, 'import streamlit as st\n'), ((194, 237), 'streamlit.sidebar.subheader', 'st.sidebar.subheader', (['"""Proportion Settings"""'], {}), "('Proportion Settings')\n", (214, 237), True, 'import streamlit as st\n'), ((256, 315), 'streamlit.sidebar.radio', 'st.sidebar.radio', (['""""""', "['One Proportion', 'Two Proportions']"], {}), "('', ['One Proportion', 'Two Proportions'])\n", (272, 315), True, 'import streamlit as st\n'), ((378, 391), 'streamlit.columns', 'st.columns', (['(3)'], {}), '(3)\n', (388, 391), True, 'import streamlit as st\n'), ((780, 793), 'streamlit.columns', 'st.columns', (['(1)'], {}), '(1)\n', (790, 793), True, 'import streamlit as st\n'), ((3287, 3300), 'streamlit.columns', 'st.columns', (['(3)'], {}), '(3)\n', (3297, 3300), True, 'import streamlit as st\n'), ((3754, 3767), 'streamlit.columns', 'st.columns', (['(1)'], {}), '(1)\n', (3764, 3767), True, 'import streamlit as st\n'), ((655, 682), 'streamlit.markdown', 'st.markdown', (['"""Pick a test:"""'], {}), "('Pick a test:')\n", (666, 682), True, 'import streamlit as st\n'), ((709, 763), 'streamlit.radio', 'st.radio', (['""""""', "['Left Tail', 'Two Tails', 'Right Tail']"], {}), "('', ['Left Tail', 'Two Tails', 'Right Tail'])\n", (717, 763), True, 'import streamlit as st\n'), ((857, 891), 'math.sqrt', 'math.sqrt', (['(nullp * (1 - nullp) / n)'], {}), '(nullp * (1 - nullp) / n)\n', (866, 891), False, 'import math\n'), ((905, 939), 'math.sqrt', 'math.sqrt', (['(p_hat * (1 - p_hat) / n)'], {}), '(p_hat * (1 - p_hat) / n)\n', (914, 939), False, 'import math\n'), ((986, 1007), 'numpy.arange', 'np.arange', (['(-4)', '(4)', '(0.1)'], {}), '(-4, 4, 0.1)\n', (995, 1007), True, 'import numpy as np\n'), ((1051, 1081), 'pandas.DataFrame', 'pd.DataFrame', (["{'x': x, 'y': y}"], {}), "({'x': x, 'y': y})\n", (1063, 1081), True, 'import pandas as pd\n'), ((2693, 2835), 'pandas.DataFrame', 'pd.DataFrame', (["{'p-Hat': p_hat, 'z-Score': z, 'p-Value': pv, 'CV': rcz, 'Test SD': tsd,\n    'C-Level': cl, 'CI SE': cise, 'ME': rme}"], {'index': '[0]'}), "({'p-Hat': p_hat, 'z-Score': z, 'p-Value': pv, 'CV': rcz,\n    'Test SD': tsd, 'C-Level': cl, 'CI SE': cise, 'ME': rme}, index=[0])\n", (2705, 2835), True, 'import pandas as pd\n'), ((2832, 2846), 'streamlit.write', 'st.write', (['data'], {}), '(data)\n', (2840, 2846), True, 'import streamlit as st\n'), ((3629, 3656), 'streamlit.markdown', 'st.markdown', (['"""Pick a test:"""'], {}), "('Pick a test:')\n", (3640, 3656), True, 'import streamlit as st\n'), ((3683, 3737), 'streamlit.radio', 'st.radio', (['""""""', "['Left Tail', 'Two Tails', 'Right Tail']"], {}), "('', ['Left Tail', 'Two Tails', 'Right Tail'])\n", (3691, 3737), True, 'import streamlit as st\n'), ((4028, 4074), 'math.sqrt', 'math.sqrt', (['(pp_hat * pq_hat * (1 / n1 + 1 / n2))'], {}), '(pp_hat * pq_hat * (1 / n1 + 1 / n2))\n', (4037, 4074), False, 'import math\n'), ((4084, 4138), 'math.sqrt', 'math.sqrt', (['(p_hat1 * q_hat1 / n1 + p_hat2 * q_hat2 / n2)'], {}), '(p_hat1 * q_hat1 / n1 + p_hat2 * q_hat2 / n2)\n', (4093, 4138), False, 'import math\n'), ((4196, 4217), 'numpy.arange', 'np.arange', (['(-4)', '(4)', '(0.1)'], {}), '(-4, 4, 0.1)\n', (4205, 4217), True, 'import numpy as np\n'), ((4261, 4291), 'pandas.DataFrame', 'pd.DataFrame', (["{'x': x, 'y': y}"], {}), "({'x': x, 'y': y})\n", (4273, 4291), True, 'import pandas as pd\n'), ((5915, 6129), 'pandas.DataFrame', 'pd.DataFrame', (["{'p-Hat 1': p_hat1, 'p-Hat 2': p_hat2, 'Pooled p-Hat': pp_hat, 'Diff p-Hat':\n    dp_hat, 'z-Score': z, 'p-Value': pv, 'CV': rcz, 'Test SD': tsd,\n    'C-Level': cl, 'CI SE': cise, 'ME': rme}"], {'index': '[0]'}), "({'p-Hat 1': p_hat1, 'p-Hat 2': p_hat2, 'Pooled p-Hat': pp_hat,\n    'Diff p-Hat': dp_hat, 'z-Score': z, 'p-Value': pv, 'CV': rcz, 'Test SD':\n    tsd, 'C-Level': cl, 'CI SE': cise, 'ME': rme}, index=[0])\n", (5927, 6129), True, 'import pandas as pd\n'), ((6116, 6130), 'streamlit.write', 'st.write', (['data'], {}), '(data)\n', (6124, 6130), True, 'import streamlit as st\n'), ((429, 454), 'streamlit.text_input', 'st.text_input', (['"""Hits"""', '(20)'], {}), "('Hits', 20)\n", (442, 454), True, 'import streamlit as st\n'), ((475, 501), 'streamlit.text_input', 'st.text_input', (['"""Tries"""', '(25)'], {}), "('Tries', 25)\n", (488, 501), True, 'import streamlit as st\n'), ((545, 572), 'streamlit.text_input', 'st.text_input', (['"""Null:"""', '(0.7)'], {}), "('Null:', 0.7)\n", (558, 572), True, 'import streamlit as st\n'), ((598, 626), 'streamlit.text_input', 'st.text_input', (['"""Alpha"""', '(0.05)'], {}), "('Alpha', 0.05)\n", (611, 626), True, 'import streamlit as st\n'), ((3339, 3366), 'streamlit.text_input', 'st.text_input', (['"""Hits 1"""', '(20)'], {}), "('Hits 1', 20)\n", (3352, 3366), True, 'import streamlit as st\n'), ((3388, 3416), 'streamlit.text_input', 'st.text_input', (['"""Tries 1"""', '(25)'], {}), "('Tries 1', 25)\n", (3401, 3416), True, 'import streamlit as st\n'), ((3468, 3495), 'streamlit.text_input', 'st.text_input', (['"""Hits 2"""', '(30)'], {}), "('Hits 2', 30)\n", (3481, 3495), True, 'import streamlit as st\n'), ((3517, 3545), 'streamlit.text_input', 'st.text_input', (['"""Tries 2"""', '(50)'], {}), "('Tries 2', 50)\n", (3530, 3545), True, 'import streamlit as st\n'), ((3589, 3617), 'streamlit.text_input', 'st.text_input', (['"""Alpha"""', '(0.05)'], {}), "('Alpha', 0.05)\n", (3602, 3617), True, 'import streamlit as st\n')]