-
Notifications
You must be signed in to change notification settings - Fork 9
Kilgore-McCown Exercise 9 Submission #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kkilgoreND
wants to merge
16
commits into
lyy005:master
Choose a base branch
from
kkilgoreND:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
908b37d
Ex9Q1 initial Python script
pjlmac 489b59e
Progress on Exercise 9, Q1 and Q2
pjlmac 0b13bfd
Fixed a minor typo
pjlmac d630685
Fixed one bug
pjlmac 7abf2e2
Reworking the def's
pjlmac 8b69fb2
updated part 1... this should be fine now
kkilgoreND 46abff7
part 3 script...should be good
kkilgoreND 7a13331
updated part 1
kkilgoreND 89472e2
update-scipy imported
kkilgoreND 47d69e5
Exercise 9, Q1 and Q2 are done
pjlmac c43a5e7
Fixed commit conflict, I hope, for Ex9Q1 and uploaded Ex9Q2
pjlmac 0a9cb54
Got rid of practice python script for Q1
pjlmac 54ae3a7
Deleted one more file I erroneously made
pjlmac f71f8fa
Cleaned most of Q3; getting an odd error message though
pjlmac a9d93d9
Hopefully final answers to Q3
pjlmac 8b0cd39
Full script for parts 1-3
kkilgoreND File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| #---Part 1--- | ||
| import numpy | ||
| import pandas | ||
| import scipy | ||
| from scipy.optimize import minimize | ||
| from scipy.stats import norm | ||
| data=pandas.read_csv("ponzr1.csv", sep=',') | ||
| data.shape | ||
| data.columns | ||
| #likelihood function for null | ||
| def null(p,obs): | ||
| B0=p[0] | ||
| sigma=p[1] | ||
| expected=B0 | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
| #likelihood function for when mutation effects expression | ||
| def mut(p,obs): | ||
| B0=p[0] | ||
| B1=p[1] | ||
| sigma=p[2] | ||
| expected=B0+B1*obs.x | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
| #t-test between control and mutations | ||
| #data for control vs first mutation (M124K) | ||
| data1=data.loc[(data.mutation == "WT") | (data.mutation == "M124K")] | ||
| data1.columns=['x', 'y'] | ||
| data1['x'] = data1['x'].map({'WT': 0, 'M124K': 1}) | ||
| #data for control vs second mutation (V456D) | ||
| data2=data.loc[(data.mutation == "WT") | (data.mutation == "V456D")] | ||
| data2.columns=['x', 'y'] | ||
| data2['x'] = data2['x'].map({'WT': 0, 'V456D': 1}) | ||
| #data for control vs third mutation (I213N) | ||
| data3=data.loc[(data.mutation == "WT") | (data.mutation == "I213N")] | ||
| data3.columns=['x', 'y'] | ||
| data3['x'] = data3['x'].map({'WT': 0, 'I213N': 1}) | ||
|
|
||
| #parameters with null model; could we have used a for loop here? | ||
| initialGuess=numpy.array([2000,1]) | ||
| null1=minimize(null, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data1) | ||
| null2=minimize(null, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data2) | ||
| null3=minimize(null, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data3) | ||
| #print parameters | ||
| print(null1.x) | ||
| print(null2.x) | ||
| print(null3.x) | ||
| #print nll | ||
| print(null1.fun) | ||
| print(null2.fun) | ||
| print(null3.fun) | ||
|
|
||
| #parameters with mutation model; ditto about the for loop? | ||
| initialGuess=numpy.array([2000,1000, 1]) | ||
| mut1=minimize(mut, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data1) | ||
| mut2=minimize(mut, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data2) | ||
| mut3=minimize(mut, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data3) | ||
|
|
||
| #print parameters | ||
| print(mut1.x) | ||
| print(mut2.x) | ||
| print(mut3.x) | ||
|
|
||
| #print nll | ||
| print(mut1.fun) | ||
| print(mut2.fun) | ||
| print(mut3.fun) | ||
|
|
||
| #difference in nll calculation | ||
| D1=2*(null1.fun-mut1.fun) | ||
| D2=2*(null2.fun-mut2.fun) | ||
| D3=2*(null3.fun-mut3.fun) | ||
|
|
||
| #test for statistical significance | ||
| print ("The effect of M124K", 1-scipy.stats.chi2.cdf(x=D1,df=1)) | ||
| print ("The effect of V456D", 1-scipy.stats.chi2.cdf(x=D2,df=1)) | ||
| print ("The effect of I213N", 1-scipy.stats.chi2.cdf(x=D3,df=1)) | ||
|
|
||
| #---Part 2--- | ||
| #load packages | ||
| import numpy | ||
| import pandas | ||
| import scipy | ||
| from scipy.optimize import minimize | ||
| from scipy.stats import norm | ||
|
|
||
| #load dataset | ||
| data=pandas.read_csv("MmarinumGrowth.csv") | ||
| data.shape | ||
| data.columns | ||
|
|
||
| #define that funky function | ||
| def Monod(p,obs): | ||
| max=p[0] | ||
| K=p[1] | ||
| sigma=p[2] | ||
| expected=max*(obs.S/(obs.S+K)) | ||
| nll=-1*norm(expected,sigma).logpdf(obs.u).sum() | ||
| return nll | ||
| #run the fit function | ||
| initialGuess=numpy.array([1,1,1]) | ||
| fit=minimize(Monod,initialGuess,method="Nelder-Mead",options={'disp':True},args=data) | ||
| print(fit.x) | ||
| #oh yeah, thanks YY! | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job |
||
| #---Part 3--- | ||
| import numpy | ||
| import pandas | ||
| import scipy | ||
| from scipy.optimize import minimize | ||
| from scipy.stats import norm | ||
| #add'leafDecomp.csv' | ||
| ld=pandas.read_csv("leafDecomp.csv",header=0) | ||
|
|
||
| #make new dataframe with x and y as the headers | ||
| leaves=ld | ||
| leaves.columns=['x', 'y'] | ||
| leaves.head() | ||
|
|
||
| #liklihood func. for constant decomp | ||
| def constant(p,obs): | ||
| B0=p[0] | ||
| sigma=p[1] | ||
| expected=B0 | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| #set initial guess | ||
| constantGuess=numpy.array([600,1]) | ||
|
|
||
| #estimate parameters | ||
| constantFit=minimize(constant,constantGuess,method="Nelder-Mead",options={'disp':True},args=leaves) | ||
| print(constantFit.x) | ||
| #print nll | ||
| print(constantFit.fun) | ||
|
|
||
| #liklihood func. for linear decomp | ||
| def linear(p,obs): | ||
| B0=p[0] | ||
| B1=p[1] | ||
| sigma=p[2] | ||
| expected=B0+B1*obs.x | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| #set initial guesses | ||
| linearGuess=numpy.array([10,6,1]) | ||
| #estimate parameters | ||
| linearFit=minimize(linear,linearGuess,method="Nelder-Mead",options={'disp':True},args=leaves) | ||
| print(linearFit.x) | ||
| #print nll | ||
| print(linearFit.fun) | ||
|
|
||
| #liklihood function for hump decomp. | ||
| def hump(p,obs): | ||
| B0=p[0] | ||
| B1=p[1] | ||
| B2=p[2] | ||
| sigma=p[3] | ||
| expected=B0+B1*obs.x+B2*((obs.x)**2) | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| #set initial guesses | ||
| humpGuess=numpy.array([200,10,-0.2,1]) | ||
|
|
||
| #estimate parameters | ||
| humpFit=minimize(hump,humpGuess,method="Nelder-Mead",options={'disp':True},args=leaves) | ||
| print(humpFit.x) | ||
| #print nll | ||
| print(humpFit.fun) | ||
|
|
||
| #difference in nll(constant vs linear) | ||
| first_D=2*(constantFit.fun-linearFit.fun) | ||
|
|
||
| #test for statistical significance | ||
| 1-scipy.stats.chi2.cdf(x=first_D,df=1) | ||
|
|
||
| #difference in nll(linear vs hump) | ||
| second_D=2*(linearFit.fun-humpFit.fun) | ||
|
|
||
| #test for statistical significance | ||
| 1-scipy.stats.chi2.cdf(x=second_D,df=1) | ||
|
|
||
| #difference in nll(constant vs hump) | ||
| third_D=2*(constantFit.fun-humpFit.fun) | ||
|
|
||
| #test for statistical significance | ||
| 1-scipy.stats.chi2.cdf(x=third_D,df=2) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job |
||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import numpy | ||
| import pandas | ||
| import scipy | ||
| from scipy.optimize import minimize | ||
| from scipy.stats import norm | ||
| data=pandas.read_csv("ponzr1.csv", sep=',') | ||
| data.shape | ||
| data.columns | ||
| #likelihood function for null | ||
| def null(p,obs): | ||
| B0=p[0] | ||
| sigma=p[1] | ||
| expected=B0 | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
| #likelihood function for when mutation effects expression | ||
| def mut(p,obs): | ||
| B0=p[0] | ||
| B1=p[1] | ||
| sigma=p[2] | ||
| expected=B0+B1*obs.x | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
| #t-test between control and mutations | ||
| #data for control vs first mutation (M124K) | ||
| data1=data.loc[(data.mutation == "WT") | (data.mutation == "M124K")] | ||
| data1.columns=['x', 'y'] | ||
| data1['x'] = data1['x'].map({'WT': 0, 'M124K': 1}) | ||
| #data for control vs second mutation (V456D) | ||
| data2=data.loc[(data.mutation == "WT") | (data.mutation == "V456D")] | ||
| data2.columns=['x', 'y'] | ||
| data2['x'] = data2['x'].map({'WT': 0, 'V456D': 1}) | ||
| #data for control vs third mutation (I213N) | ||
| data3=data.loc[(data.mutation == "WT") | (data.mutation == "I213N")] | ||
| data3.columns=['x', 'y'] | ||
| data3['x'] = data3['x'].map({'WT': 0, 'I213N': 1}) | ||
|
|
||
| #parameters with null model; could we have used a for loop here? | ||
| initialGuess=numpy.array([2000,1]) | ||
| null1=minimize(null, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data1) | ||
| null2=minimize(null, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data2) | ||
| null3=minimize(null, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data3) | ||
| #print parameters | ||
| print(null1.x) | ||
| print(null2.x) | ||
| print(null3.x) | ||
| #print nll | ||
| print(null1.fun) | ||
| print(null2.fun) | ||
| print(null3.fun) | ||
|
|
||
| #parameters with mutation model; ditto about the for loop? | ||
| initialGuess=numpy.array([2000,1000, 1]) | ||
| mut1=minimize(mut, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data1) | ||
| mut2=minimize(mut, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data2) | ||
| mut3=minimize(mut, initialGuess, method="Nelder-Mead", options={'disp': True}, args=data3) | ||
|
|
||
| #print parameters | ||
| print(mut1.x) | ||
| print(mut2.x) | ||
| print(mut3.x) | ||
|
|
||
| #print nll | ||
| print(mut1.fun) | ||
| print(mut2.fun) | ||
| print(mut3.fun) | ||
|
|
||
| #difference in nll calculation | ||
| D1=2*(null1.fun-mut1.fun) | ||
| D2=2*(null2.fun-mut2.fun) | ||
| D3=2*(null3.fun-mut3.fun) | ||
|
|
||
| #test for statistical significance | ||
| print ("The effect of M124K", 1-scipy.stats.chi2.cdf(x=D1,df=1)) | ||
| print ("The effect of V456D", 1-scipy.stats.chi2.cdf(x=D2,df=1)) | ||
| print ("The effect of I213N", 1-scipy.stats.chi2.cdf(x=D3,df=1)) |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #load packages | ||
| import numpy | ||
| import pandas | ||
| import scipy | ||
| from scipy.optimize import minimize | ||
| from scipy.stats import norm | ||
|
|
||
| #load dataset | ||
| data=pandas.read_csv("MmarinumGrowth.csv") | ||
| data.shape | ||
| data.columns | ||
|
|
||
| #define that funky function | ||
| def Monod(p,obs): | ||
| max=p[0] | ||
| K=p[1] | ||
| sigma=p[2] | ||
| expected=max*(obs.S/(obs.S+K)) | ||
| nll=-1*norm(expected,sigma).logpdf(obs.u).sum() | ||
| return nll | ||
| #run the fit function | ||
| initialGuess=numpy.array([1,1,1]) | ||
| fit=minimize(Monod,initialGuess,method="Nelder-Mead",options={'disp':True},args=data) | ||
| print(fit.x) | ||
| #oh yeah, thanks YY! |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import numpy | ||
| import pandas | ||
| import scipy | ||
| from scipy.optimize import minimize | ||
| from scipy.stats import norm | ||
| #add'leafDecomp.csv' | ||
| ld=pandas.read_csv("leafDecomp.csv",header=0) | ||
|
|
||
| #make new dataframe with x and y as the headers | ||
| leaves=ld | ||
| leaves.columns=['x', 'y'] | ||
| leaves.head() | ||
|
|
||
| #liklihood func. for constant decomp | ||
| def constant(p,obs): | ||
| B0=p[0] | ||
| sigma=p[1] | ||
| expected=B0 | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| #set initial guess | ||
| constantGuess=numpy.array([600,1]) | ||
|
|
||
| #estimate parameters | ||
| constantFit=minimize(constant,constantGuess,method="Nelder-Mead",options={'disp':True},args=leaves) | ||
| print(constantFit.x) | ||
| #print nll | ||
| print(constantFit.fun) | ||
|
|
||
| #liklihood func. for linear decomp | ||
| def linear(p,obs): | ||
| B0=p[0] | ||
| B1=p[1] | ||
| sigma=p[2] | ||
| expected=B0+B1*obs.x | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| #set initial guesses | ||
| linearGuess=numpy.array([10,6,1]) | ||
| #estimate parameters | ||
| linearFit=minimize(linear,linearGuess,method="Nelder-Mead",options={'disp':True},args=leaves) | ||
| print(linearFit.x) | ||
| #print nll | ||
| print(linearFit.fun) | ||
|
|
||
| #liklihood function for hump decomp. | ||
| def hump(p,obs): | ||
| B0=p[0] | ||
| B1=p[1] | ||
| B2=p[2] | ||
| sigma=p[3] | ||
| expected=B0+B1*obs.x+B2*((obs.x)**2) | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| #set initial guesses | ||
| humpGuess=numpy.array([200,10,-0.2,1]) | ||
|
|
||
| #estimate parameters | ||
| humpFit=minimize(hump,humpGuess,method="Nelder-Mead",options={'disp':True},args=leaves) | ||
| print(humpFit.x) | ||
| #print nll | ||
| print(humpFit.fun) | ||
|
|
||
| #difference in nll(constant vs linear) | ||
| first_D=2*(constantFit.fun-linearFit.fun) | ||
|
|
||
| #test for statistical significance | ||
| 1-scipy.stats.chi2.cdf(x=first_D,df=1) | ||
|
|
||
| #difference in nll(linear vs hump) | ||
| second_D=2*(linearFit.fun-humpFit.fun) | ||
|
|
||
| #test for statistical significance | ||
| 1-scipy.stats.chi2.cdf(x=second_D,df=1) | ||
|
|
||
| #difference in nll(constant vs hump) | ||
| third_D=2*(constantFit.fun-humpFit.fun) | ||
|
|
||
| #test for statistical significance | ||
| 1-scipy.stats.chi2.cdf(x=third_D,df=2) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job