Last active
August 29, 2015 14:20
-
-
Save alstat/cfb927fffea2cc15afe2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##------------------## | |
# POWER FUNCTION # | |
##------------------## | |
import plotly.plotly as py | |
from plotly.graph_objs import * | |
import numpy as np | |
from scipy.stats import norm | |
def f1(x, c, th0, sigma, n): | |
return c + (th0 - x)/(sigma/np.sqrt(n)) | |
trace1 = Scatter( | |
x = np.linspace(5.2, 6.2, num = 100), | |
y = 1 - norm.cdf(f1(x = np.linspace(5.2, 6.2, num = 100), c = 1.645, sigma = 1, n = 30, th0 = 5.4)), | |
name = "$1-\\Phi\\left[c'+\\frac{\\theta_0-\\theta}{\\sigma/\\sqrt{n}}\\right]$" | |
) | |
trace2 = Scatter( | |
x = [5.2, 5.4], | |
y = [1 - norm.cdf(f1(x = 5.4, c = 1.645, sigma = 1, n = 30, th0 = 5.4)), 0], | |
name = "Critical Value", | |
mode = "lines+text", | |
text = ["$\\;\\alpha$", "$\\;\\theta_{0}$"], | |
textposition = 'top right', | |
textfont = Font( | |
family = "sans serif", | |
), | |
line = Line( | |
shape = 'hv' | |
) | |
) | |
layout = Layout( | |
title = "Power Function", | |
xaxis = XAxis( | |
title = "$\\theta$" | |
), | |
yaxis = YAxis( | |
title = "$\\beta(\\theta)$" | |
), | |
legend = Legend( | |
x = 0.6, | |
y = 0.3, | |
traceorder = 'normal', | |
font = Font( | |
family = 'sans-serif', | |
size = 12, | |
color = '#000' | |
) | |
) | |
) | |
data = Data([trace1, trace2]) | |
fig = Figure(data = data, layout = layout) | |
plot_url = py.plot(fig, filename = 'betatheta') | |
##-----------------## | |
# TYPE II ERROR # | |
##-----------------## | |
import plotly.plotly as py | |
from plotly.graph_objs import * | |
import numpy as np | |
from scipy.stats import norm | |
def f1(x, c, th0, sigma, n): | |
return c + (th0 - x)/(sigma/np.sqrt(n)) | |
trace1 = Scatter( | |
x = np.linspace(5.4, 6.2, num = 100), | |
y = norm.cdf(f1(x = np.linspace(5.4, 6.2, num = 100), c = 1.645, sigma = 1, n = 30, th0 = 5.4)) | |
) | |
layout = Layout( | |
title = "Type II Error", | |
xaxis = XAxis( | |
title = "$\\theta$" | |
), | |
yaxis = YAxis( | |
title = "$1 - \\beta(\\theta)$" | |
), | |
legend=Legend( | |
x = 0.6, | |
y = 0.3, | |
traceorder = 'normal', | |
font = Font( | |
family = 'sans-serif', | |
size = 12, | |
color = '#000' | |
) | |
) | |
) | |
data = Data([trace1]) | |
fig = Figure(data = data, layout = layout) | |
plot_url = py.plot(fig, filename = 'type2') | |
##-----------------------------------------## | |
# POWER FUNCTION WITH SAMPLE SIZE N = 35 # | |
##-----------------------------------------## | |
import plotly.plotly as py | |
from plotly.graph_objs import * | |
import numpy as np | |
from scipy.stats import norm | |
def f1(x, c, th0, sigma, n): | |
return c + (th0 - x)/(sigma/np.sqrt(n)) | |
trace1 = Scatter( | |
x = np.linspace(5.2, 6.2, num = 100), | |
y = 1 - norm.cdf(f1(x = np.linspace(5.2, 6.2, num = 100), c = 1.645, sigma = 1, n = 34.25878, th0 = 5.4)), | |
name = "$1-\\Phi\\left[1.645+\\frac{5.4-\\theta}{1/\\sqrt{34.25878}}\\right]$" | |
) | |
trace2 = Scatter( | |
x = [5.2, 5.4], | |
y = [1 - norm.cdf(f1(x = 5.4, c = 1.645, sigma = 1, n = 34.25878, th0 = 5.4)), 0], | |
name = "Condition 1", | |
mode = "lines+text", | |
text = ["$\\;\\beta(\\theta_0)=0.05$", "$\\;\\theta_{0}$"], | |
textposition = 'top right', | |
textfont = Font( | |
family = "sans serif", | |
), | |
line = Line( | |
shape = 'hv' | |
) | |
) | |
trace3 = Scatter( | |
x = [5.2, 5.9], | |
y = [1 - norm.cdf(1.645 - np.sqrt(34.25878) / 2), 0], | |
name = "Condition 2", | |
mode = "lines+text", | |
text = ["$\\;\\beta(\\theta_0+\\sigma/2)=0.9$", "$\\;\\theta_0+\\sigma/2$"], | |
textposition = 'top right', | |
textfont = Font( | |
family = "sans serif", | |
), | |
line = Line( | |
shape = 'hv' | |
) | |
) | |
layout = Layout( | |
title = "Power Function with Sample Size, n ≈ 35", | |
xaxis = XAxis( | |
title = "$\\theta$" | |
), | |
yaxis = YAxis( | |
title = "$\\beta(\\theta)$" | |
), | |
legend = Legend( | |
x = 0.02, | |
y = 0.85, | |
traceorder = 'normal', | |
font = Font( | |
family = 'sans-serif', | |
size = 12, | |
color = '#000' | |
) | |
) | |
) | |
data = Data([trace1, trace2, trace3]) | |
fig = Figure(data = data, layout = layout) | |
plot_url = py.plot(fig, filename = 'betatheta2') | |
##-----------------------------------------------## | |
# EFFECT OF SAMPLE SIZE ON THE POWER FUNCTION # | |
##-----------------------------------------------## | |
import plotly.plotly as py | |
from plotly.graph_objs import * | |
import numpy as np | |
from scipy.stats import norm | |
def f1(x, c, th0, sigma, n): | |
return c + (th0 - x)/(sigma/np.sqrt(n)) | |
trace1 = Scatter( | |
x = np.linspace(5.2, 6.2, num = 100), | |
y = 1 - norm.cdf(f1(x = np.linspace(5.2, 6.2, num = 100), c = 1.645, sigma = 1, n = 10, th0 = 5.4)), | |
name = "$n=10$" | |
) | |
trace2 = Scatter( | |
x = np.linspace(5.2, 6.2, num = 100), | |
y = 1 - norm.cdf(f1(x = np.linspace(5.2, 6.2, num = 100), c = 1.645, sigma = 1, n = 30, th0 = 5.4)), | |
name = "$n=30$" | |
) | |
trace3 = Scatter( | |
x = np.linspace(5.2, 6.2, num = 100), | |
y = 1 - norm.cdf(f1(x = np.linspace(5.2, 6.2, num = 100), c = 1.645, sigma = 1, n = 50, th0 = 5.4)), | |
name = "$n=50$" | |
) | |
trace4 = Scatter( | |
x = np.linspace(5.2, 6.2, num = 100), | |
y = 1 - norm.cdf(f1(x = np.linspace(5.2, 6.2, num = 100), c = 1.645, sigma = 1, n = 70, th0 = 5.4)), | |
name = "$n=70$" | |
) | |
trace5 = Scatter( | |
x = np.linspace(5.2, 6.2, num = 100), | |
y = 1 - norm.cdf(f1(x = np.linspace(5.2, 6.2, num = 100), c = 1.645, sigma = 1, n = 100, th0 = 5.4)), | |
name = "$n=100$" | |
) | |
trace6 = Scatter( | |
x = np.linspace(5.2, 6.2, num = 100), | |
y = 1 - norm.cdf(f1(x = np.linspace(5.2, 6.2, num = 100), c = 1.645, sigma = 1, n = 200, th0 = 5.4)), | |
name = "$n=200$" | |
) | |
trace7 = Scatter( | |
x = np.linspace(5.2, 6.2, num = 100), | |
y = 1 - norm.cdf(f1(x = np.linspace(5.2, 6.2, num = 100), c = 1.645, sigma = 1, n = 500, th0 = 5.4)), | |
name = "$n=500$" | |
) | |
trace8 = Scatter( | |
x = np.linspace(5.2, 6.2, num = 100), | |
y = 1 - norm.cdf(f1(x = np.linspace(5.2, 6.2, num = 100), c = 1.645, sigma = 1, n = 1000, th0 = 5.4)), | |
name = "$n=1000$" | |
) | |
layout = Layout( | |
title = "Effect of Sample Size on Power Function", | |
xaxis = XAxis( | |
title = "$\\theta$" | |
), | |
yaxis = YAxis( | |
title = "$\\beta(\\theta)$" | |
), | |
legend = Legend( | |
x = 0.85, | |
y = 0.4, | |
traceorder = 'normal', | |
font = Font( | |
family = 'sans-serif', | |
size = 12, | |
color = '#000' | |
) | |
) | |
) | |
data = Data([trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8]) | |
fig = Figure(data = data, layout = layout) | |
plot_url = py.plot(fig, filename = 'effectofn') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment