I am working on the file "Projects/Project2/Project2_Prashant.ipynb".
The first task is:
The films need to be grouped by franchise so that franchise-related data can be included as featured for each observation.
- The Average Adjusted Gross of all previous films in the franchise
- The Adjusted Gross of the very first film in the franchise
- The Release Date of the previous film in the franchise
- The Release Date of the very first film in the franchise
- The Series Number of the film in that franchise
-- I considered using the film's number in the franchise as a rank value that could be split into indicator variables, but it's useful as a linear value because the total accrued sum of $ earned by the franchise is a linear combination of "SeriesNum" and "PrevAvgGross"
Can you write Python code for it?

df = df.sort_values(['Franchise','Release'])
df['CumGross'] = df.groupby(['Franchise'])['AdjGross'].apply(lambda x: x.cumsum())
df['SeriesNum'] = df.groupby(['Franchise'])['Release'].apply(lambda x: x.rank())
df['PrevAvgGross'] = (df['CumGross'] - df['AdjGross'])/(df['SeriesNum'] - 1)


I am working on the file "Projects/Project2/Project2_Prashant.ipynb".
The first task is:
Try Polynomial Regression
Can you write Python code for it?

polyX=PolynomialFeatures(2).fit_transform(X)

polymodel = sm.OLS(y, polyX)
polyfit = polymodel.fit()
polyfit.rsquared

polyfit.resid.plot(style='o');

polyfit.rsquared_adj


I am working on the file "Projects/Project3/Project3_Prashant.ipynb".
The first task is:
Aggregate manager_id to get features representing manager performance
Note: Need to aggregate manager performance ONLY over a training subset in order to validate against test subset. So the train-test split is being performed in this step before creating the columns for manager performance.
Can you write Python code for it?

# Choose features for modeling (and sorting)
df = df.sort_values('listing_id')
X = df[['bathrooms','bedrooms','price','latitude','longitude','days_old','num_words','num_features','num_photos','listing_id','manager_id']]
y = df['interest_level']

# Merge indicators to X dataframe and sort again to match sorting of y
X = X.merge(X_dummies, how='outer', left_index=True, right_index=True).fillna(0)
X = X.sort_values('listing_id')

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

# compute ratios and count for each manager
mgr_perf = pd.concat([X_train.manager_id,pd.get_dummies(y_train)], axis=1).groupby('manager_id').mean()

mgr_perf.head(2)

mgr_perf['manager_count'] = X_train.groupby('manager_id').count().iloc[:,1]
mgr_perf['manager_skill'] = mgr_perf['high']*1 + mgr_perf['medium']*0 + mgr_perf['low']*-1

# for training set
X_train = X_train.merge(mgr_perf.reset_index(), how='left', left_on='manager_id', right_on='manager_id')

# for test set
X_test = X_test.merge(mgr_perf.reset_index(), how='left', left_on='manager_id', right_on='manager_id')

# Fill na's with mean skill and median count
X_test['manager_skill'] = X_test.manager_skill.fillna(X_test.manager_skill.mean())
X_test['manager_count'] = X_test.manager_count.fillna(X_test.manager_count.median())

# Delete unnecessary columns before modeling
del X_train['listing_id']
del X_train['manager_id']
del X_test['listing_id']
del X_test['manager_id']
del X_train['high']
del X_train['medium']
del X_train['low']
del X_test['high']
del X_test['medium']
del X_test['low']


I am working on the file "Projects/Project3/Project3_Prashant.ipynb".
The first task is:
KNN
Can you write Python code for it?

for i in [95,100,105]:
    knn = KNeighborsClassifier(n_neighbors=i)
    knn.fit(X_train, y_train)
    y_test_predicted_proba = knn.predict_proba(X_test)
    print log_loss(y_test, y_test_predicted_proba)


I am working on the file "Projects/Project3/Project3_Prashant.ipynb".
The first task is:
Logistic Regression
Can you write Python code for it?

lr = LogisticRegression()
lr.fit(X_train, y_train)

y_test_predicted_proba = lr.predict_proba(X_test)
log_loss(y_test, y_test_predicted_proba)

lr = LogisticRegression(solver='newton-cg', multi_class='multinomial')
lr.fit(X_train, y_train)

y_test_predicted_proba = lr.predict_proba(X_test)
log_loss(y_test, y_test_predicted_proba)


I am working on the file "Projects/Project3/Project3_Prashant.ipynb".
The first task is:
Naive Bayes
Can you write Python code for it?

bnb = BernoulliNB()
bnb.fit(X_train, y_train)

y_test_predicted_proba = bnb.predict_proba(X_test)
log_loss(y_test, y_test_predicted_proba)

gnb = GaussianNB()
gnb.fit(X_train, y_train)

y_test_predicted_proba = gnb.predict_proba(X_test)
log_loss(y_test, y_test_predicted_proba)


I am working on the file "Projects/Project3/Project3_Prashant.ipynb".
The first task is:
Random Forest
Can you write Python code for it?

rf = RandomForestClassifier(n_estimators=1000, n_jobs=-1)
rf.fit(X_train, y_train)

y_test_predicted_proba = rf.predict_proba(X_test)
log_loss(y_test, y_test_predicted_proba)


I am working on the file "Projects/Project3/Project3_Prashant.ipynb".
The first task is:
Step 1: Load Data
Can you write Python code for it?

# Load the training dataset from Kaggle.
df = pd.read_json('data/raw/train.json')
print df.shape

df.head(2)


I am working on the file "Projects/Project3/Project3_Prashant.ipynb".
The first task is:
Step 3: Modeling, First Pass
Can you write Python code for it?

X = df[['bathrooms','bedrooms','price','latitude','longitude','days_old','num_words','num_features','num_photos']]
y = df['interest_level']

# Scaling is necessary for Logistic Regression and KNN
X_scaled = pd.DataFrame(preprocessing.scale(X))
X_scaled.columns = X.columns

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, random_state=42)


I am working on the file "Projects/Project3/Project3_Prashant.ipynb".
The first task is:
Total number of columns is 14 + 1 target:
- 1 target variable (interest_level), with classes low, medium, high
- 1 photo link
- lat/long, street address, display address
- listing_id, building_id, manager_id
- numerical (price, bathrooms, bedrooms)
- created date
- text (description, features)
Features for modeling:
- bathrooms
- bedrooms
- created date (calculate age of posting in days)
- description (number of words in description)
- features (number of features)
- photos (number of photos)
- price
- features (split into category indicators)
- manager_id (with manager skill level)
Further opportunities for modeling:
- description (need to text parse)
- building_id (possibly with a building popularity level)
- photos (quality)
Can you write Python code for it?

# Distribution of target, interest_level

s = df.groupby('interest_level')['listing_id'].count()
s.plot.bar();

df_high = df.loc[df['interest_level'] == 'high']
df_medium = df.loc[df['interest_level'] == 'medium']
df_low = df.loc[df['interest_level'] == 'low']

plt.figure(figsize=(6,10))
plt.scatter(df_low.longitude, df_low.latitude, color='yellow', alpha=0.2, marker='.', label='Low')
plt.scatter(df_medium.longitude, df_medium.latitude, color='green', alpha=0.2, marker='.',  label='Medium')
plt.scatter(df_high.longitude, df_high.latitude, color='purple', alpha=0.2, marker='.',  label='High')

plt.xlim(-74.04,-73.80)
plt.ylim(40.6,40.9)
plt.legend(loc=2);


I am working on the file "Projects/Project4/Project4_Prashant.ipynb".
The first task is:
Repeat above process with new tweets from API in March 2017
Can you write Python code for it?

with open("data/new_tweets.pkl", 'rb') as picklefile:
    new_tweets = pickle.load(picklefile)

new_tweets

new_tweets.columns = ['text']

%time tfidf2 = tfidf_vectorizer.fit_transform(new_tweets['text']) #fit the vectorizer to tweets

print tfidf2.shape

from sklearn.metrics.pairwise import cosine_similarity
dist = 1 - cosine_similarity(tfidf2)

km = KMeans(n_clusters=3)
km.fit(tfidf2)
clusters = km.labels_.tolist()

pca = PCA(n_components=2)
km_pca = pca.fit_transform(dist)
xs, ys = km_pca[:,0], km_pca[:,1]

fig, ax = plt.subplots()
plt.scatter(xs,ys,c=clusters, cmap='Accent')
plt.colorbar(ticks=[0,1,2])
ax.tick_params(axis='x',bottom='off',labelbottom='off')
ax.tick_params(axis='y',left='off',labelleft='off')

order_centroids = km.cluster_centers_.argsort()[:, ::-1]

terms = tfidf_vectorizer.get_feature_names()

for i in range(3):
    print('Cluster----------',i)
    for x in order_centroids[i,:10]:
        print(terms[x])

collections.Counter(km.labels_)

new_tweets['cluster'] = clusters

new_tweets[new_tweets.cluster == 1]

new_tweets['text'][1137]

new_tweets['sentiment'] = new_tweets['text'].apply(lambda x: TextBlob(x).sentiment[0])

fig, ax = plt.subplots()
plt.scatter(xs,ys, c=new_tweets['sentiment'], cmap='cool')
plt.colorbar()
ax.tick_params(axis='x',bottom='off',labelbottom='off')
ax.tick_params(axis='y',left='off',labelleft='off')


I am working on the file "Projects/Project4/Project4_Prashant.ipynb".
The first task is:
Step 1: Load Data
Can you write Python code for it?

df = pd.read_csv('data/raw/demonetization-tweets.csv')

df.head()

# RESTful search API
all_tweets = []
search_url = "https://api.twitter.com/1.1/search/tweets.json"
parameters = {"q": "#demonetisation", "count":100, "lang": "en"}
response = requests.get(search_url,
                        params = parameters,
                        auth=oauth)

for tweet in response.json()['statuses']:
    all_tweets.append(tweet['text'])

for _ in range(99):
    if 'next_results' in response.json()['search_metadata'].keys():
        next_page_url = search_url + response.json()['search_metadata']['next_results']
        response = requests.get(next_page_url, auth=oauth)
        for tweet in response.json()['statuses']:
            all_tweets.append(tweet['text'])
    else:
        break

X = pd.DataFrame(all_tweets)

with open('data/new_tweets.pkl', 'wb') as picklefile:
    pickle.dump(X, picklefile)


I am working on the file "Projects/Project4/Project4_Prashant.ipynb".
The first task is:
Step 2: Apply TF/IDF
Can you write Python code for it?

def tokenize_func(text):
    if text[:2] == 'RT':
        text = text.partition(':')[2]
    tokens = nltk.word_tokenize(text)
    filtered_tokens = []
    for token in tokens:
        token = re.sub('[^A-Za-z]', '', token).strip()
        if token not in ['demonetization','demonetisation','https','amp','rt','']:
            filtered_tokens.append(token)
    return filtered_tokens

tfidf_vectorizer = TfidfVectorizer(max_df=0.97, min_df=0.03, max_features=200000,
                                 stop_words='english', decode_error='ignore',
                                 tokenizer=tokenize_func, ngram_range=(1,3))

%time tfidf_matrix = tfidf_vectorizer.fit_transform(df['text']) #fit the vectorizer to tweets

print tfidf_matrix.shape

from sklearn.metrics.pairwise import cosine_similarity
dist = 1 - cosine_similarity(tfidf_matrix)


I am working on the file "Projects/Project4/Project4_Prashant.ipynb".
The first task is:
Step 3: Kmeans Clustering
Can you write Python code for it?

Inertia = []
Sil_coefs = []
for k in range(2,10):
    km = KMeans(n_clusters=k, random_state=42)
    km.fit(tfidf_matrix)
    labels = km.labels_
    Sil_coefs.append(silhouette_score(tfidf_matrix, labels, metric='euclidean'))
    Inertia.append(km.inertia_)

fig, (ax1, ax2) = plt.subplots(1,2, figsize=(15,5), sharex=True)
k_clusters = range(2,10)
ax1.plot(k_clusters, Sil_coefs)
ax1.set_xlabel('number of clusters')
ax1.set_ylabel('silhouette score')

# plot here on ax2
ax2.plot(k_clusters, Inertia)
ax2.set_xlabel('number of clusters')
ax2.set_ylabel('Inertia');

km = KMeans(n_clusters=3)
km.fit(tfidf_matrix)
clusters = km.labels_.tolist()

collections.Counter(km.labels_)

pca = PCA(n_components=2)
km_pca = pca.fit_transform(dist)
xs, ys = km_pca[:,0], km_pca[:,1]

fig, ax = plt.subplots()
plt.scatter(xs,ys,c=clusters, cmap='Accent')
plt.colorbar(ticks=[0,1,2])
ax.tick_params(axis='x',bottom='off',labelbottom='off')
ax.tick_params(axis='y',left='off',labelleft='off')

order_centroids = km.cluster_centers_.argsort()[:, ::-1]

terms = tfidf_vectorizer.get_feature_names()

for i in range(3):
    print('Cluster----------',i)
    for x in order_centroids[i,:10]:
        print(terms[x])

df['clusters'] = clusters

df[df.clusters == 1]['screenName'][4340]


I am working on the file "Projects/Project4/Project4_Prashant.ipynb".
The first task is:
Step 4: Sentiment
Can you write Python code for it?

df['sentiment'] = df['text'].apply(lambda x: TextBlob(unicode(x, errors='ignore')).sentiment[0])

fig, ax = plt.subplots()
plt.scatter(xs,ys, c=df['sentiment'], cmap='cool')
plt.colorbar()
ax.tick_params(axis='x',bottom='off',labelbottom='off')
ax.tick_params(axis='y',left='off',labelleft='off')

df.sort_values('sentiment')['screenName'][6402]


I am working on the file "Projects/Project5/Neural Networks Summary.ipynb".
The first task is:
We can use gradient descent to minimize a cost function, thereby optimizing our weights (backpropagation).
ANNs in Sklearn
Multi-layer Perceptron (MLP) models in sklearn
The advantages of MLP are:
- Capability to learn non-linear models.
- Capability to learn models in real-time (on-line learning) using partial_fit.
The disadvantages of MLP include:
- MLP with hidden layers have a non-convex loss function where there exists more than one local minimum. Therefore different random weight initializations can lead to different validation accuracy.
- MLP requires tuning a number of hyperparameters such as the number of hidden neurons, layers, and iterations.
- MLP is sensitive to feature scaling.
Can you write Python code for it?

# build simple neural net with sklearn: An "OR" gate
from sklearn.neural_network import MLPClassifier

X = [[0., 0.], [1., 1.], [1., 0.], [0., 1.]]
y = [0, 1, 1, 1]

clf = MLPClassifier(hidden_layer_sizes=(5,2),
                    solver='lbfgs',
                    random_state=42)
clf.fit(X,y)

# predict new observations
clf.predict([[0,1]])

# find parameters
print([coef.shape for coef in clf.coefs_])
clf.coefs_


I am working on the file "Projects/Project5/NeuralNetSum.ipynb".
The first task is:
We can use gradient descent to minimize a cost function, thereby optimizing our weights.
ANNs in Sklearn
Multi-layer Perceptron (MLP) models in sklearn
The advantages of MLP are:
- Capability to learn non-linear models.
- Capability to learn models in real-time (on-line learning) using partial_fit.
The disadvantages of MLP include:
- MLP with hidden layers have a non-convex loss function where there exists more than one local minimum. Therefore different random weight initializations can lead to different validation accuracy.
- MLP requires tuning a number of hyperparameters such as the number of hidden neurons, layers, and iterations.
- MLP is sensitive to feature scaling.
Can you write Python code for it?

# build simple neural net with sklearn: An "OR" gate
from sklearn.neural_network import MLPClassifier

X = [[0., 0.], [1., 1.], [1., 0.], [0., 1.]]
y = [0, 1, 1, 1]

clf = MLPClassifier(hidden_layer_sizes=(5,2),
                    solver='lbfgs',
                    random_state=42)
clf.fit(X,y)

# predict new observations
clf.predict([[0,1]])

# find parameters
print([coef.shape for coef in clf.coefs_])
clf.coefs_

clf.predict([[2,2]])

clf.predict([[-2,2]])

clf.predict([[-2,-2]])


I am working on the file "Projects/Summer_15/PISA_SusanChen_Aug_15.ipynb".
The first task is:
Creating the Dataset
PISA 2012 scores are downloaded as an excel file from the statlink on page 21 of the published PISA key findings. I deleted the explanatory text surrounding the table. I kept only the "Mean Score in PISA 2012" column for each subject and then saved the file as a csv. Then, I read the file into pandas and renamed the columns.
Can you write Python code for it?

file1 = '/users/susan/desktop/PISA/PISA2012clean.csv' # file location
df1 = pd.read_csv(file1)

#pandas remote data access API for World Bank GDP per capita data
df2 = wb.download(indicator='NY.GDP.PCAP.PP.KD', country='all', start=2012, end=2012)

df1

#drop multilevel index
df2.index = df2.index.droplevel('year')

df1.columns = ['Country','Math','Reading','Science']
df2.columns = ['GDPpc']

#combine PISA and GDP datasets based on country column
df3 = pd.merge(df1, df2, how='left', left_on = 'Country', right_index = True)

df3.columns = ['Country','Math','Reading','Science','GDPpc']

#drop rows with missing GDP per capita values
df3 = df3[pd.notnull(df3['GDPpc'])]

print (df3)


I am working on the file "Projects/Summer_15/PISA_SusanChen_Aug_15.ipynb".
The first task is:
Excluding Outliers
I initially plotted the data and ran the regression without excluding any outliers. The resulting r-squared values for reading, math, and science were 0.29, 0.32, and 0.27, respectively. Looking at the scatter plot, there seem to be two obvious outliers, Qatar and Vietnam. I decided to exclude the data for these two countries because the remaining countries do seem to form a trend. I found upon excluding them that the correlation between GDP per capita and scores was much higher.
Qatar is an outlier as it placed relatively low, 63rd out of the 65 countries, with a relatively high GDP per capita at about $131000. Qatar has a high GDP per capita for a country with just 1.8 million people, and only 13% of which are Qatari nationals. Qatar is a high income economy as it contains one of the world's largest natural gas and oil reserves.
Vietnam is an outlier because it placed relatively high, 17th out of the 65 countries, with a relatively low GDP per capita at about $4900. Reasons for Vietnam's high score may be due to the investment of the government in education and the uniformity of classroom professionalism and discipline found across countries. At the same time, rote learning is much more emphasized than creative thinking, and it is important to note that many disadvantaged students are forced to drop out, reasons which may account for the high score.
Can you write Python code for it?

df3.index = df3.Country #set country column as the index
df3 = df3.drop(['Qatar', 'Vietnam']) # drop outlier


I am working on the file "Projects/Summer_15/PISA_SusanChen_Aug_15.ipynb".
The first task is:
Plotting the Data
I use the log of the GDP per capita to plot against each component of the PISA on a scatter plot.
Can you write Python code for it?

Reading = df3.Reading
Science = df3.Science
Math = df3.Math
GDP = np.log(df3.GDPpc)

#PISA reading vs GDP per capita
plt.scatter(x = GDP, y = Reading, color = 'r')
plt.title('PISA 2012 Reading scores vs. GDP per capita')
plt.xlabel('GDP per capita (log)')
plt.ylabel('PISA Reading Score')
plt.show()

#PISA math vs GDP per capita
plt.scatter(x = GDP, y = Math, color = 'b')
plt.title('PISA 2012 Math scores vs. GDP per capita')
plt.xlabel('GDP per capita (log)')
plt.ylabel('PISA Math Score')
plt.show()

#PISA science vs GDP per capita
plt.scatter(x = GDP, y = Science, color = 'g')
plt.title('PISA 2012 Science scores vs. GDP per capita')
plt.xlabel('GDP per capita (log)')
plt.ylabel('PISA Science Score')
plt.show()


I am working on the file "Projects/Summer_15/PISA_SusanChen_Aug_15.ipynb".
The first task is:
Regression Analysis
The OLS regression results indicate that the there is a 0.57 correlation betweeen reading scores and GDP per capita, 0.63 between math scores and GDP per capita, and 0.57 between science scores and GDP per capita.
Can you write Python code for it?

lm = smf.ols(formula='Reading ~ GDP', data=df3).fit()
lm2.params
lm.summary()

lm2 = smf.ols(formula='Math ~ GDP', data=df3).fit()
lm2.params
lm2.summary()

lm3 = smf.ols(formula='Science ~ GDP', data=df3).fit()
lm3.params
lm3.summary()


I am working on the file "Projects/chargeNoiseMaskDesign.ipynb".
The first task is:
$\lambda/4$ readout resonators
IMPAs from Google will be good in the 4-6GHz range.  We will aim for resonators near 6GHz, but have a spread from 5-6.5GHz on the mask.  They should be spread every 30MHz or so.
We are changing to fixed resonator frequencies (non variable) for this mask.
The frequency is brought down significantly by the capacitance through to ground through the qubit, as well as the self-capacitance of the coupling cap to ground.  These capacitances pull down the transmon frequency more, so we will set Q3 to have no extension, and set the other qubit frequencies around it.
Can you write Python code for it?

quarter_curve = 2*pi*50/4
coupling_length = 200   # this coupling length varies depending on desired Qc.
tot_length = quarter_curve*(1+2+2+2+2) + 2*850 + (1335-700) + 200 + 350 + coupling_length

def L4FromQubit(q):
    L4 = cpwtools.QuarterLResonator(cpw, tot_length)
    seriesCap = q.C_g*q.C_q/(q.C_g+q.C_q)
    L4.addCapacitiveCoupling('g', seriesCap, Z0 = 0)
    L4.addCapacitiveCoupling('c_coupler', q.C_resToGnd, Z0 = 0)
    return L4

f0 = 6.0e9

print('How much do the capacitors and change in res length of the qubits pull the resonance?')
print('{:>11}   {:>9}  {:>8}  {:>8}'.format('', 'length', 'f_l', 'C_r'))
L4 = L4FromQubit(qubits[1])
l0 = L4.setLengthFromFreq(f0) - qubits[1].cpw_length
for q in qubits:
    L4 = L4FromQubit(q)
    L4.l = l0 + q.cpw_length
    q.C_r = L4.C()
    q.omega_r = L4.wl()
    q.omega_q = 2*pi*(f0-1e9)
    print('{:>11}:  {:>7.2f}um  {:>5.3f}GHz  {:>6.2f}fF'.format(
        q.name, 1e6*l0, L4.fl()/1e9, 1e15*L4.C()))

print
base_length = quarter_curve*(1+2+2+2+2) + (1335-700) + 2*600 + (533-392) + coupling_length
res_lengths = [base_length + 2*l for l in (728, 684, 641, 599)]
Deltas = (1, 1., 1, 1.)
for i,q in enumerate(selectedQubits):
    L4 = L4FromQubit(q)
    L4.l = 1e-6*res_lengths[i] + q.cpw_length
    q.C_r = L4.C()
    q.omega_r = L4.wl()
    q.omega_q = 2*pi*(L4.fl()-Deltas[i]*1e9)
    print L4.fl()/1e9, q.omega_r/2e9/pi, q.omega_q/2e9/pi

# Add in measured frequencies if we know them
# D5
f_meas = (5.80, 5.89, 6.01, 6.14)
f_q_meas = (4.58, 5.31, 4.17, 4.77)
# E5
#f_meas = (5.81, 5.91, 6.03, 6.13)
#f_q_meas = (4.76, 6.26, 4.75, 4.83)
for i,q in enumerate(selectedQubits):
    q.omega_r = 2*pi*1e9*f_meas[i]
    q.omega_q = 2*pi*1e9*f_q_meas[i]


I am working on the file "Projects/chargeNoiseMaskDesign.ipynb".
The first task is:
CPW
We want to use the same cpw dimensions for resonator and feedline/purcell filter cpw's so the kinetic inductance correction is the same for everything.
Can you write Python code for it?

cpw = cpwtools.CPW(material='al', w=10., s=7.)
print cpw


I am working on the file "Projects/chargeNoiseMaskDesign.ipynb".
The first task is:
Charge Noise Mask Design
General Notes:
 * Looking at the chip left to right, top to bottom, we have Q1-Q4.
 * Q4 is the charge sensitive qubit.  All others are normal transmons.
 * Resonators spaced by ~100MHz, lowest to highest
Still to do:
 * simulate mutual
 * bandaids - notches in ground plane?
 * figure out where junctions go
 * loops at end of inductive coupling?
 * dielectric liftoff now?
40  40.7    46.3    1.5 39.0    d=15 t=90 m=1.5
50  52.9    47.0    1.44    30.9    d=15 t=70 m=1
60  69  46.9    1.47    32.8    d=15 t=55 m=1
70  90.5    47.0    1.45    35.6    d=15 t=45 m=1
80  123 47  1.46    41.9    d=15 t=40 m=1
90  182 47  1.49    54.1    d=15 t=38 m=1
100 305 46.9    1.42    74.0    d=15 t=35 m=1
108 500 46.9    1.48    108 d=15 t=35 m=1
Can you write Python code for it?


I am working on the file "Projects/chargeNoiseMaskDesign.ipynb".
The first task is:
Feedline with and without crossovers
Can you write Python code for it?

cpw.setKineticInductanceCorrection(False)
print cpw
cpwx = cpwtools.CPWWithBridges(material='al', w=1e6*cpw.w, s=1e6*cpw.s, bridgeSpacing = 250, bridgeWidth = 3, t_oxide=0.16)
cpwx.setKineticInductanceCorrection(False)
print cpwx


I am working on the file "Projects/chargeNoiseMaskDesign.ipynb".
The first task is:
Inductive Coupling
From [1], we have the dephasing of a qubit:
$\Gamma_\phi = \eta\frac{4\chi^2}{\kappa}\bar{n}$, where $\eta=\frac{\kappa^2}{\kappa^2+4\chi^2}$, $\bar{n}=\left(\frac{\Delta}{2g}\right)^2$
$\Gamma_\phi = \frac{4\chi^2\kappa}{\kappa^2+4\chi^2}\left(\frac{\Delta}{2g}\right)^2$
To maximize the efficiency of readout, we want to maximize the rate of information leaving the system (into the readout chain), or equivilently, maximize dephasing.
$\partial_\kappa\Gamma_\phi = 0 = -\frac{4\chi^2(\kappa^2-4\chi^2)}{(\kappa^2+4\chi^2)^2}$ when $2\chi=\kappa$.
$2\chi = \kappa_r = \omega_r/Q_r$
$ Q_{r,c} = \frac{8Z_0^2}{\pi(\omega M)^2}$  [2]
We want a $Q_c$ of 3k-30k
[1] Yan et al. The flux qubit revisited to enhance coherence and reproducibility. Nature Communications, 7, 1–9. http://doi.org/10.1038/ncomms12964
[2] Matt Beck's Thesis, p.39
Can you write Python code for it?

d = 5
MperL = inductiveCoupling.inductiveCoupling.CalcMutual(cpw.w*1e6, cpw.w*1e6, cpw.s*1e6, cpw.s*1e6, d, 10*cpw.w*1e6)[0]

MperL = 9e-8 # experimentally measured

print( '{:>11}   {:>7}   {:>15}'.format('', 'M', 'coupling length') )
for q in selectedQubits:
    M = 1/(np.sqrt(q.Q_r()*pi/8/cpw.z0()**2)*q.omega_r)
    print "{:>11}:  {:>5.2f}pH   {:>13.2f}um".format(q.name, M*1e12, (M-1e-12)/MperL*1e6)

print( '{:>5}  {:>8}'.format('Q_c', 'l_c') )
for q in [3000,6000,9000,15000,21000,27000,33000,99000]:
    print "{:>5}  {:>6.2f}um".format(q,1/(np.sqrt(q*pi/8/cpw.z0()**2)*qubits[2].omega_r)/MperL*1e6)


I am working on the file "Projects/chargeNoiseMaskDesign.ipynb".
The first task is:
Loss from XY line
From Thorbeck's notes, we have
$R_p = R_s(1+Q_s^2)$ and $C_p = C_s\left(\frac{Q_s^2}{1+Q_s^2}\right)$
where $Q_s = \frac{1}{\omega R_s C_s}$
and the "s" and "p" subscript refer to just the coupling capacitor and Z0 of the line in a series or parallel configuration.  Combining this with the normal LC of the qubit, we can find the loss
Can you write Python code for it?

C_q = qubits[2].C_q
L_q = 1/(qubits[2].omega_q**2 * C_q)
R_s = 50
C_s = 0.1e-15
Q_s = 1/(qubits[2].omega_q * R_s * C_s)
R_p = R_s*(1 + Q_s**2)
C_p = C_s * Q_s**2/(1 + Q_s**2)
omega = 1/np.sqrt((C_q+C_p)*L_q)
Q_xy = omega*R_p*(C_q+C_p)
print("f: {:.3f}GHz --> {:.3f}GHz".format( 1e-9/np.sqrt(C_q*L_q)/2/pi, 1e-9*omega/2/pi))
print("Q = {:.2f}".format(Q_xy))
print("1/kappa = {:.2f}us".format(1e6*Q_xy/omega))


I am working on the file "Projects/chargeNoiseMaskDesignCorr.ipynb".
The first task is:
$\lambda/4$ readout resonators
IMPAs from Google will be good in the 4-6GHz range.  We will aim for resonators near 6GHz, but have a spread from 5-6.5GHz on the mask.  They should be spread every 30MHz or so.
We are changing to fixed resonator frequencies (non variable) for this mask.
The frequency is brought down significantly by the capacitance through to ground through the qubit, as well as the self-capacitance of the coupling cap to ground.  These capacitances pull down the transmon frequency more, so we will set Q3 to have no extension, and set the other qubit frequencies around it.
Can you write Python code for it?

quarter_curve = 2*pi*50/4
coupling_length = 200   # this coupling length varies depending on desired Qc.
tot_length = quarter_curve*(1+2+2+2+2) + 2*850 + (1335-700) + 200 + 350 + coupling_length

def L4FromQubit(q):
    L4 = cpwtools.QuarterLResonator(cpw, tot_length)
    seriesCap = q.C_g*q.C_q/(q.C_g+q.C_q)
    L4.addCapacitiveCoupling('g', seriesCap, Z0 = 0)
    L4.addCapacitiveCoupling('c_coupler', q.C_resToGnd, Z0 = 0)
    return L4

f0 = 6.0e9
f_correction = 0.0e9 # correction for extra 700um length

print('How much do the capacitors and change in res length of the qubits pull the resonance?')
print('{:>11}   {:>9}  {:>8}  {:>8}'.format('', 'length', 'f_l', 'C_r'))
L4 = L4FromQubit(qubits[1])
l0 = L4.setLengthFromFreq(f0+f_correction) - qubits[1].cpw_length
for q in qubits:
    L4 = L4FromQubit(q)
    L4.l = l0 + q.cpw_length
    q.C_r = L4.C()
    q.omega_r = L4.wl()
    q.omega_q = 2*pi*(f0-1e9)
    print('{:>11}:  {:>7.2f}um  {:>5.3f}GHz  {:>6.2f}fF'.format(
        q.name, 1e6*l0, L4.fl()/1e9, 1e15*L4.C()))

print
base_length = quarter_curve*(1+2+2+2+2) + (1335-700) + 2*600 + (533-392) + coupling_length
res_lengths = [base_length + 2*l for l in (728, 684, 641, 599)]
qubit_list = [qubits[i] for i in selectedQubits]
Deltas = (1, 1,1,1)
for i in range(len(res_lengths)):
    L4 = L4FromQubit(qubit_list[i])
    L4.l = 1e-6*res_lengths[i] + qubit_list[i].cpw_length
    qubit_list[i].omega_r = L4.wl()
    qubit_list[i].omega_q = 2*pi*(L4.fl()-Deltas[i]*1e9)
    print L4.fl()/1e9, qubit_list[i].omega_r/2e9/pi, qubit_list[i].omega_q/2e9/pi

# Add in measured frequencies if we know them
# D5
f_meas = (5.80, 5.89, 6.01, 6.14)
f_q_meas = (4.58, 5.31, 4.17, 4.77)
# E5
#f_meas = (5.81, 5.91, 6.03, 6.13)
#f_q_meas = (4.76, 6.26, 4.75, 4.83)
for j,i in enumerate(selectedQubits):
    qubits[i].omega_r = 2*pi*1e9*f_meas[j]
    qubits[i].omega_q = 2*pi*1e9*f_q_meas[j]


I am working on the file "Projects/chargeNoiseMaskDesignCorr.ipynb".
The first task is:
CPW
We want to use the same cpw dimensions for resonator and feedline/purcell filter cpw's so the kinetic inductance correction is the same for everything.
Can you write Python code for it?


I am working on the file "Projects/chargeNoiseMaskDesignCorr.ipynb".
The first task is:
Inductive Coupling
From [1], we have the dephasing of a qubit:
$\Gamma_\phi = \eta\frac{4\chi^2}{\kappa}\bar{n}$, where $\eta=\frac{\kappa^2}{\kappa^2+4\chi^2}$, $\bar{n}=\left(\frac{\Delta}{2g}\right)^2$
$\Gamma_\phi = \frac{4\chi^2\kappa}{\kappa^2+4\chi^2}\left(\frac{\Delta}{2g}\right)^2$
To maximize the efficiency of readout, we want to maximize the rate of information leaving the system (into the readout chain), or equivilently, maximize dephasing.
$\partial_\kappa\Gamma_\phi = 0 = -\frac{4\chi^2(\kappa^2-4\chi^2)}{(\kappa^2+4\chi^2)^2}$ when $2\chi=\kappa$.
$2\chi = \kappa_r = \omega_r/Q_r$
$ Q_{r,c} = \frac{8Z_0^2}{\pi(\omega M)^2}$  [2]
We want a $Q_c$ of 3k-30k
[1] Yan et al. The flux qubit revisited to enhance coherence and reproducibility. Nature Communications, 7, 1–9. http://doi.org/10.1038/ncomms12964
[2] Matt Beck's Thesis, p.39
Can you write Python code for it?


I am working on the file "Projects/chargeNoiseMaskDesignCorr.ipynb".
The first task is:
Loss from XY line
From Thorbeck's notes, we have
$R_p = R_s(1+Q_s^2)$ and $C_p = C_s\left(\frac{Q_s^2}{1+Q_s^2}\right)$
where $Q_s = \frac{1}{\omega R_s C_s}$
and the "s" and "p" subscript refer to just the coupling capacitor and Z0 of the line in a series or parallel configuration.  Combining this with the normal LC of the qubit, we can find the loss
Can you write Python code for it?


I am working on the file "Projects/chargeNoiseMaskDesignCorr.ipynb".
The first task is:
Purcell Filter
Do we even need a purcell filter? [3]
Without purcell filter: $\kappa_r T_1 \le \left(\frac{\Delta}{g}\right)^2$
With purcell filter: $\kappa_r T_1 \le \left(\frac{\Delta}{g}\right)^2 \left(\frac{\omega_r}{\omega_q}\right) \left(\frac{2\Delta}{\omega_r/Q_{pf}}\right)^2$
$\kappa_r = \omega_r/Q_r$
With the readout resonators spaced ~30MHz appart, we need a bandwidth of at least 4*30MHz=120MHz.
We have a range of readout resonators from 5-6GHz.
[3] Jeffrey et al. Fast accurate state measurement with superconducting qubits. Physical Review Letters, 112(19), 1–5. http://doi.org/10.1103/PhysRevLett.112.190504
Can you write Python code for it?

l_curve = 2*pi*50/4
tot_length = l_curve*(1+2+2+2+1)*2 + 4*750 + 2569 + 4*450 + 2*106

purcell = cpwtools.HalfLResonator(cpw,tot_length)
purcell.addCapacitiveCoupling('in', 40e-15)
purcell.addCapacitiveCoupling('out', 130e-15)

print( "f_max = {:.3f}GHz  Q_in = {:.2f}  Q_out = {:.2f}".format( 1e-9*purcell.fl(), purcell.Qc('in'), purcell.Qc('out') ) )
purcell.l = (tot_length + 503*4)*1e-6
print( "f_min = {:.3f}GHz  Q_in = {:.2f}  Q_out = {:.2f}".format( 1e-9*purcell.fl(), purcell.Qc('in'), purcell.Qc('out') ) )
print
print('The measured purcell filter (no crossovers) seems to be 150-200MHz below expected.  This has been accounted for below.')
f0 = (qubits[1].omega_r + qubits[2].omega_r)/2/2/pi
purcell.setLengthFromFreq(f0 + 175e6)   # The measured purcell filter (no crossovers) seems to be 150-200MHz below expected.
print "f = {:.2f}GHz  l = {:.3f}um  offset = {:.3f}um  Q_in = {:.2f}  Q_out = {:.2f}".format( 1e-9*purcell.fl(), purcell.l*1e6, (purcell.l*1e6-tot_length)/4, purcell.Qc('in'), purcell.Qc('out') )
print "V_out/V_in =", (purcell.Qc('in')/purcell.Qc('out'))**0.5
print "{:.2f}% power lost through input".format( 100*purcell.Ql()/purcell.Qc('in') )
print "{:.2f}% power lost through output".format( 100*purcell.Ql()/purcell.Qc('out') )
print "{:.2f}% power lost internally".format( 100*purcell.Ql()/purcell.Qint() )
print
print "The purcell filter frequency goes up by 310MHz when crossovers are added:"
purcellx = deepcopy(purcell)
purcellx.cpw = cpwx
print "f = {:.2f}GHz  l = {:.3f}um  Q_in = {:.2f}  Q_out = {:.2f}".format( 1e-9*purcellx.fl(), purcellx.l*1e6, purcellx.Qc('in'), purcellx.Qc('out') )

print "Purcell Filter FWHM = {:.2f}MHz".format(2*pi*f0/purcell.Ql()/2/pi/1e6)
print "Purcell Filter Q_l = {:.2f}".format(purcell.Ql())
print
print('T1 Limits:')
print('{:>11}   {:>10}  {:>11}'.format('', 'no purcell', 'yes purcell'))
for q in qubits:
    kappa_r = q.omega_r/q.Q_r()
    Delta = q.omega_q - q.omega_r
    #print "{}: T1 limit (no purcell) = {:.2f}us  T1 limit (purcell) = {:.2f}us".format(
    print...


I am working on the file "Projects/chargeNoiseMaskDesignCorr.ipynb".
The first task is:
Qubit parameters
From Ted Thorbeck's notes:
$E_c = \frac{e^2}{2C}$, $E_c/\hbar=\alpha=\text{anharmonicity}$
$E_J = \frac{I_o \Phi_0}{2 \pi} $
$\omega_q = \sqrt{8E_JE_c}/\hbar $
$g = \frac{1}{2} \frac{C_g}{\sqrt{(C_q+C_g)(C_r+C_g)}}\sqrt{\omega_r\omega_q}$
We want g in the range 25-200MHz for an ideal anharmonicity $\alpha$=250MHz
Can you write Python code for it?


I am working on the file "Projects/dynam_pressure/mlt_velocity_profile.ipynb".
The first task is:
A convective velocity profile for solar-mass star at 5 Gyr looks like this:
Can you write Python code for it?

fig, ax = plt.subplots(1, 1, figsize=(8., 5.))

ax.set_xlabel('log(P / [dyne/cm^2])', size=16.)
ax.set_ylabel('convective velocity', size=16.)
ax.set_xlim(7.0, 4.5)

ax.plot(env[:, 1], env[:, 14]/1.0e5, '-', lw=3, c='black')


I am working on the file "Projects/dynam_pressure/mlt_velocity_profile.ipynb".
The first task is:
If we define the dyanmic pressure as a turbulent pressure within the convection zone, then a first approximation is to use the mixing length theory velocity as a basis for the turbulent velocity. That is, we can define
\begin{equation}
    P_{\rm dyn} \approx P_{\rm turb} \approx C_{\rm turb}\rho u_{\rm mlt}^2,
\end{equation}
where $C_{\rm turb} = 1/3$ for isotropic turbulence. However, it's well known that mixing length theory is not valid in super-adiabatic regions. Mixing length theory is derived assuming adibatic convection. That is, assuming radiation does not carry a significnat fraction of energy away from convecting element and assuming that convective velocities are sub-sonic.
Let's load an envelope structure for a solar-mass star at 5 Gyr.
Can you write Python code for it?


I am working on the file "Projects/dynam_pressure/mlt_velocity_profile.ipynb".
The first task is:
Now linearly combine it with the profile generate from MLT.
Can you write Python code for it?

mlt_dyn_pr = np.append(np.zeros(len(atm)), (10**env[:, 5]*env[:, 14]**2/3.0)/10**env[:, 1])
tot_dyn_pr = all_dyn_pr + mlt_dyn_pr

fig, ax = plt.subplots(1, 1, figsize=(8., 5.))

ax.set_xlabel('log(rho / [g/cm^3])', size=20.)
ax.set_ylabel('P_dynamic / P_gas', size=20.)
ax.set_xlim(-5.0, -9.0)

ax.grid()

ax.plot(all_logd, tot_dyn_pr, '-', lw=3, c='black')
fig.savefig('Pdyn_rho_ext.pdf')


I am working on the file "Projects/dynam_pressure/mlt_velocity_profile.ipynb".
The first task is:
Now, let's load the atmospheric profile for this model.
Can you write Python code for it?


I am working on the file "Projects/dynam_pressure/mlt_velocity_profile.ipynb".
The first task is:
This produces the following profile for the dynamical pressure contribution,
Can you write Python code for it?


I am working on the file "Projects/facebook-api.ipynb".
The first task is:
Define Facebook functions
Create some fuctions that will scrape Facebook pages.
It is a lot of things here, but you'll only interact with these functions:

getme() will get information about yourself.
getpage(id) will get information about a page by its page ID, slug name or URL, and return page information.
getstatuses(pageid) will get status updates from a page by its page ID, and return a list of statuses.
Can you write Python code for it?

# Install facepy package to connect o Facebook API. If it doesn't work, test with pip3 instead.
!pip install facepy

# Import the "facepy" library that talks to Facebooks API.
from facepy.exceptions import OAuthError
from facepy import GraphAPI
import datetime

# Function that connects to Faceboko GraphAPI and returns information about you.
def getme():
    print("Fetching yourself...")
    graph = GraphAPI(accesstoken, version="2.11")
    melist = graph.get("me?fields=id,name,email,birthday", page=True, retry=2, limit=1)
    for me in melist:
        print("Done.")
        return(me)
    print("Couldn't find you...")

# Function that gets information about a Facebook page.
def getpage(id):
    graph = GraphAPI(accesstoken, version="2.11")
    page = graph.get(str(id) + "?fields=id,name,link,fan_count", page=False, retry=2, limit=1)
    return(page)

# Function to get statuses from a Facebook page.
def getstatuses(id, limit=0):
    fields = "permalink_url,message,link,created_time,type,from,name,id,likes.limit(1).summary(true),comments.limit(1).summary(true),shares"
    if limit == 0:
        print("Fetching statuses (this might take some while, consider changing the limit to speed things up)...")
    else:
        print("Fetching statuses (limited to latest {0})...".format(limit))
    graph = GraphAPI(accesstoken, version="2.11")
    pages = graph.get(str(id) + "/feed?fields=" + fields, page=True, retry=2, limit=1)
    l = process_pager(pages, limit)
    print("Done.")
    print("Got {0} statuses.".format(len(l)))
    return(l)

# Function that process pager from facepy and cycle through each status message.
def process_pager(pages, limit):
    l = []
    i = 0
    for page in pages:
        for status in page["data"]:
            l.append(process_status(status))
            i = i + 1
            if i >= limit and limit != 0:
                break
        if i >= limit and limit != 0:
            break
    return(l)

# Function that processes a status message into a more easy-to-use...


I am working on the file "Projects/facebook-api.ipynb".
The first task is:
Get information about Facebook page
Can you write Python code for it?

guardian = getpage("http://facebook.com/theguardian")

guardian

guardian["fan_count"]

print("{0} ({1}) has {2} fans and ID {3}.".format(guardian["name"], guardian["link"], guardian["fan_count"], guardian["id"]))


I am working on the file "Projects/fluxNoiseMaskDesignV1.ipynb".
The first task is:
$\lambda/4$ readout resonators
IMPAs from Google will be good in the 4-6GHz range.  We will aim for resonators near 6GHz, but have a spread from 5-6GHz on the mask.  They should be spread every 30MHz or so.
Can you write Python code for it?

l_curve = 2*pi*50/4
coupling_length = 287
tot_length = l_curve*(1+1+2+2+2+2) + 2*1000 + 1156 + 350 + 500 + coupling_length # this coupling length ranges from 45-150 depending on desired Qc.
                                                                                # Plan for 45, can always trombone down
L4 = cpwtools.QuarterLResonator(cpw, tot_length)
print('The frequency is brought down significantly by the capacitance through to ground through the qubit, ' +
      'as well as the self-capacitance of the coupling cap to ground.  These capacitances pull down the transmon ' +
      'frequency more, so we will set Q3 to have no extension, and set the other qubit frequencies around it.')
print('Bare resonator frequency = {:.3f} GHz'.format(L4.fl()/1e9))
print

def L4FromQubit(q):
    L4 = cpwtools.QuarterLResonator(cpw, tot_length)
    seriesCap = q.C_g*q.C_q/(q.C_g+q.C_q)
    L4.addCapacitiveCoupling('g', seriesCap, Z0 = 0)
    L4.addCapacitiveCoupling('c_coupler', q.C_resToGnd, Z0 = 0)
    return L4

L4 = L4FromQubit(qubits[2])
f0 = L4.fl()

for i,q in enumerate(qubits):
    L4 = L4FromQubit(q)
    length = L4.setLengthFromFreq(f0 + 0.04e9*[-2, -1, 0, 1][i])
    q.C_r = L4.C()
    q.omega_r = L4.wl()
    q.omega_q = 2*pi*(f0-1e9)
    print("{}: l = {:.2f}um  f_l = {:.3f}GHz  C_r = {:.2f}fF  extension = {:.2f}um".format(
        q.name, 1e6*q.res_length, L4.fl()/1e9, 1e15*L4.C(), (1e6*length - tot_length)/2))


I am working on the file "Projects/fluxNoiseMaskDesignV1.ipynb".
The first task is:
CPW
We want to use the same cpw dimensions for resonator and feedline/purcell filter cpw's so the kinetic inductance correction is the same for everything.
Can you write Python code for it?


I am working on the file "Projects/fluxNoiseMaskDesignV1.ipynb".
The first task is:
Flux Noise Mask Design
General Notes:
 * Looking at the chip left to right, top to bottom, we have Q1-Q4.
 * Q4 is the charge sensitive qubit.  All others are normal transmons.
Still to do:
 * simulate mutual
Problems with mask:
 * One of the XY (Q2) lines is messed up
 * capacitances change resonator frequencies so nothing matches
 * Alex thinks flux bias line might be bad
 * Resonators need to be moved closer (Z0 -> Z0^2)
 * Should probably change the test pattern to have less substrate contribution
 * Purcell filter changes resonator coupling, so Q was wrong.
Transmon Selected params:
+    w = 34, l_c = 90, w_c = 150:
    + C_q = 75.6fF
    + C_g = 3.39fF
    + C_resToGnd = 79.1fF
+    d_xy = 80: C_xy = 99.5fF
Charge Sensitive Selected params:
+    w = 14, l_c = 90, w_c = 200:
    + C_q = 48.5fF
    + C_g = 2.94fF
    + C_resToGnd = 107fF
+    d_xy = 50: C_xy = 95.5fF
Can you write Python code for it?

qubits = []
for i in range(3):
    q = qubit.Qubit('Transmon')
    q.C_g = 3.87e-15
    q.C_q = 75.1e-15
    q.C_resToGnd = 79.1e-15
    qubits.append(q)
q = qubit.Qubit('OCSQubit')
q.C_g = 2.94e-15
q.C_q = 48.5e-15
q.C_resToGnd = 51.5e-15
qubits.append(q)


I am working on the file "Projects/fluxNoiseMaskDesignV1.ipynb".
The first task is:
Loss from XY line
From Thorbeck's notes, we have
$R_p = R_s(1+Q_s^2)$ and $C_p = C_s\left(\frac{Q_s^2}{1+Q_s^2}\right)$
where $Q_s = \frac{1}{\omega R_s C_s}$
and the "s" and "p" subscript refer to just the coupling capacitor and Z0 of the line in a series or parallel configuration.  Combining this with the normal LC of the qubit, we can find the loss
Can you write Python code for it?


I am working on the file "Projects/fluxNoiseMaskDesignV1.ipynb".
The first task is:
Qubit parameters
From Ted Thorbeck's notes:
$E_c = \frac{e^2}{2C}$, $E_c/\hbar=\alpha=\text{anharmonicity}$
$E_J = \frac{I_o \Phi_0}{2 \pi} $
$\omega_q = \sqrt{8E_JE_c}/\hbar $
$g = \frac{1}{2} \frac{C_g}{\sqrt{(C_q+C_g)(C_r+C_g)}}\sqrt{\omega_r\omega_q}$
We want g in the range 25-200MHz for an ideal anharmonicity $\alpha$=250MHz
Can you write Python code for it?

qb = deepcopy(qubits[2])
g = 2*pi*50e6     # qubit-resonator coupling in Hz

print('Range of C_q on the mask:')
print "C_q = 30fF: E_c = {:.2f}MHz".format( qb.E_c(30e6)/(2*pi*hbar)*1e15 )
print "C_q = 95fF: E_c = {:.2f}MHz".format( qb.E_c(95e6)/(2*pi*hbar)*1e15 )
print
print('Ideal:')
print "Transmon:          E_c = 250MHz: C_sigma = C_q + C_g = {:.2f}fF".format( e**2/2/250e6/(2*pi*hbar)*1e15 )
print "Charge Sensitive:  E_c = 385MHz: C_sigma = C_q + C_g = {:.2f}fF".format( e**2/2/385e6/(2*pi*hbar)*1e15 )

# With caps chosen from the mask:
for q in qubits:
    print "{}:  C_q = {:.2f}fF  E_c = {:.2f}MHz  E_j = {:.2f}GHz  alpha = {:.2f}MHz  g = {:.2f}MHz  C_g = {:.2f}fF".format(
        q.name, 1e15*q.C_q, -q.E_c()/(2*pi*hbar)/1e6, q.E_j()/2/pi/hbar/1e9, q.alpha(q.E_c(),q.E_j())/(2*pi)/1e6, g/2/pi/1e6, 1e15*q.cap_g(g))

# We choose the closest g capacitance from the mask
for q in qubits:
    print "{}:  C_g = {:.2f}fF  g = {:.2f}MHz  Chi_0/2pi = {:.2f}MHz  Chi/2pi = {:.2f}MHz  Q_r = {:.0f}  kappa = {:.2f}MHz  1/kappa = {:.0f}ns  I_c={:.2f}nA  n_crit={:.0f}".format(
        q.name, 1e15*q.cap_g(q.g()), q.g()/2/pi/1e6, 1e-6*q.Chi_0()/2/pi, 1e-6*q.Chi()/2/pi, q.Q_r(), q.omega_r/q.Q_r()*1e-6/2/pi, q.Q_r()/q.omega_r*1e9, q.I_c()*1e9, ((q.omega_q-q.omega_r)/2/q.g())**2)

delta = 380e-6; #2\Delta/e in V
Jc = 1e8*673e-9 # A/cm^2
nJJs = [2,1,1,2]
for i,q in enumerate(qubits):
    print("{}:  I_c = {:.2f}nA  R_N = {:.2f}k  width = {} x {:.3f}nm".format(q.name, q.I_c()*1e9, 1e-3*pi/4*delta/q.I_c(), nJJs[i], 1e9*q.I_c()/(1e4*Jc)/100e-9/nJJs[i] ))

for q in qubits:
    print "{}: Ej/Ec = {:.3f}  Charge dispersion = {:.3f}MHz".format(q.name, q.E_j()/q.E_c(), q.charge_dispersion()/2/pi/hbar/1e6)

# What variation in C_g should be included on mask for the C_q variation we have?
for C_q_ in [85e-15, 29e-15, e**2/2/250e6]:
    for g_ in [2*pi*25e6, 2*pi*50e6, 2*pi*200e6]:
        qb.C_q = C_q_
        print "C_q = {:.2f}fF  g = {:.2f}MHz  C_g = {:.2f}fF".format(
            1e15*C_q_, g_/2/pi/1e6, 1e15*qb.cap_g(g_))


I am working on the file "Projects/fluxNoiseMaskDesignV2.ipynb".
The first task is:
$\lambda/4$ readout resonators
IMPAs from Google will be good in the 4-6GHz range.  We will aim for resonators near 6GHz, but have a spread from 5-6.5GHz on the mask.  They should be spread every 30MHz or so.
We are changing to fixed resonator frequencies (non variable) for this mask.
The frequency is brought down significantly by the capacitance through to ground through the qubit, as well as the self-capacitance of the coupling cap to ground.  These capacitances pull down the transmon frequency more, so we will set Q3 to have no extension, and set the other qubit frequencies around it.
Can you write Python code for it?

l_curve = 2*pi*50/4
coupling_length = 287
tot_length = l_curve*(1+1+2+2+2+2) + 2*850 + 1156 + 200 + 350 + coupling_length # this coupling length ranges from 45-150 depending on desired Qc.
                                                                                # Plan for 45, can always trombone down
L4 = cpwtools.QuarterLResonator(cpw, tot_length)
print('Highest resonator frequency = {:.3f} GHz'.format(L4.fl()/1e9))
print
length = L4.setLengthFromFreq(5.5e9)
print('Highest resonator frequency = {:.3f} GHz for extension = {:.2f}um'.format( L4.fl()/1e9, (1e6*length-tot_length)/2 ))
length = L4.setLengthFromFreq(5e9)
print('Lowest resonator frequency = {:.3f} GHz for extension = {:.2f}um'.format( L4.fl()/1e9, (1e6*length-tot_length)/2 ))
print

def L4FromQubit(q):
    L4 = cpwtools.QuarterLResonator(cpw, tot_length)
    seriesCap = q.C_g*q.C_q/(q.C_g+q.C_q)
    L4.addCapacitiveCoupling('g', seriesCap, Z0 = 0)
    L4.addCapacitiveCoupling('c_coupler', q.C_resToGnd, Z0 = 0)
    return L4

L4 = L4FromQubit(qubits[2])
f0 = 6e9 #L4.fl()

print('{:>8}   {:>9}  {:>8}  {:>8}  {:>9}'.format('', 'length', 'f_l', 'C_r', 'extension'))
for i,q in enumerate(qubits):
    L4 = L4FromQubit(q)
    q.res_length = L4.setLengthFromFreq(f0 + 0.04e9*[-2, -1, 0, 1][i])
    if i==len(qubits)-1:
        q.res_length = qubits[i-1].res_length - 40e-6
        L4.l = q.res_length
    q.C_r = L4.C()
    q.omega_r = L4.wl()
    q.omega_q = 2*pi*(f0-1e9)
    print('{:>8}:  {:>7.2f}um  {:>5.3f}GHz  {:>6.2f}fF  {:>7.3f}um'.format(
        q.name, 1e6*q.res_length, L4.fl()/1e9, 1e15*L4.C(), (1e6*q.res_length - tot_length)/2))


I am working on the file "Projects/fluxNoiseMaskDesignV2.ipynb".
The first task is:
Loss from XY line
From Thorbeck's notes, we have
$R_p = R_s(1+Q_s^2)$ and $C_p = C_s\left(\frac{Q_s^2}{1+Q_s^2}\right)$
where $Q_s = \frac{1}{\omega R_s C_s}$
and the "s" and "p" subscript refer to just the coupling capacitor and Z0 of the line in a series or parallel configuration.  Combining this with the normal LC of the qubit, we can find the loss
Can you write Python code for it?


I am working on the file "Projects/fluxNoiseMaskDesignV2.ipynb".
The first task is:
Purcell Filter
Do we even need a purcell filter? [3]
Without purcell filter: $\kappa_r T_1 \le \left(\frac{\Delta}{g}\right)^2$
With purcell filter: $\kappa_r T_1 \le \left(\frac{\Delta}{g}\right)^2 \left(\frac{\omega_r}{\omega_q}\right) \left(\frac{2\Delta}{\omega_r/Q_{pf}}\right)^2$
$\kappa_r = \omega_r/Q_r$
With the readout resonators spaced ~30MHz appart, we need a bandwidth of at least 4*30MHz=120MHz.
We have a range of readout resonators from 5-6GHz.
[3] Jeffrey et al. Fast accurate state measurement with superconducting qubits. Physical Review Letters, 112(19), 1–5. http://doi.org/10.1103/PhysRevLett.112.190504
Can you write Python code for it?

l_curve = 2*pi*50/4
tot_length = l_curve*(1+2+2+2+1)*2 + 4*750 + 2569 + 4*450 + 2*106

purcell = cpwtools.HalfLResonator(cpw,tot_length)
purcell.addCapacitiveCoupling('in', 40e-15)
purcell.addCapacitiveCoupling('out', 130e-15)

print( "f_max = {:.3f}GHz  Q_in = {:.2f}  Q_out = {:.2f}".format( 1e-9*purcell.fl(), purcell.Qc('in'), purcell.Qc('out') ) )
purcell.l = (tot_length + 503*4)*1e-6
print( "f_min = {:.3f}GHz  Q_in = {:.2f}  Q_out = {:.2f}".format( 1e-9*purcell.fl(), purcell.Qc('in'), purcell.Qc('out') ) )
print
print('The measured purcell filter (no crossovers) seems to be 150-200MHz below expected.  This has been accounted for below.')
f0 = (qubits[1].omega_r + qubits[2].omega_r)/2/2/pi
purcell.setLengthFromFreq(f0 + 175e6)   # The measured purcell filter (no crossovers) seems to be 150-200MHz below expected.
print "f = {:.2f}GHz  l = {:.3f}um  offset = {:.3f}um  Q_in = {:.2f}  Q_out = {:.2f}".format( 1e-9*purcell.fl(), purcell.l*1e6, (purcell.l*1e6-tot_length)/4, purcell.Qc('in'), purcell.Qc('out') )
print "V_out/V_in =", (purcell.Qc('in')/purcell.Qc('out'))**0.5
print "{:.2f}% power lost through input".format( 100*purcell.Ql()/purcell.Qc('in') )
print "{:.2f}% power lost through output".format( 100*purcell.Ql()/purcell.Qc('out') )
print "{:.2f}% power lost internally".format( 100*purcell.Ql()/purcell.Qint() )
print
print "The purcell filter frequency goes up by 310MHz when crossovers are added:"
purcellx = deepcopy(purcell)
purcellx.cpw = cpwx
print "f = {:.2f}GHz  l = {:.3f}um  Q_in = {:.2f}  Q_out = {:.2f}".format( 1e-9*purcellx.fl(), purcellx.l*1e6, purcellx.Qc('in'), purcellx.Qc('out') )

print "Purcell Filter FWHM = {:.2f}MHz".format(2*pi*f0/purcell.Ql()/2/pi/1e6)
print "Purcell Filter Q_l = {:.2f}".format(purcell.Ql())
print
print('T1 Limits:')
print('{:>8}   {:>10}  {:>11}'.format('', 'no purcell', 'yes purcell'))
for q in qubits:
    kappa_r = q.omega_r/q.Q_r()
    Delta = q.omega_q - q.omega_r
    #print "{}: T1 limit (no purcell) = {:.2f}us  T1 limit (purcell) = {:.2f}us".format(
    print...


I am working on the file "Projects/fluxNoiseMaskDesignV2.ipynb".
The first task is:
Qubit parameters
From Ted Thorbeck's notes:
$E_c = \frac{e^2}{2C}$, $E_c/\hbar=\alpha=\text{anharmonicity}$
$E_J = \frac{I_o \Phi_0}{2 \pi} $
$\omega_q = \sqrt{8E_JE_c}/\hbar $
$g = \frac{1}{2} \frac{C_g}{\sqrt{(C_q+C_g)(C_r+C_g)}}\sqrt{\omega_r\omega_q}$
We want g in the range 25-200MHz for an ideal anharmonicity $\alpha$=250MHz
Can you write Python code for it?


I am working on the file "Projects/info-curia-sraper.ipynb".
The first task is:
Save to file
Let's save all the texts to a single file named all_texts.txt.
Can you write Python code for it?

# All texts are kept separate in the list text_list.
# This will join all texts together into one big text separated by a new line (\n).
one_big_text = "\n".join(text_list)

# Save the text to a single file.
file = open("all_texts.txt", "w", encoding="utf-8")
file.writelines(one_big_text)
file.close()
print("File saved!")


I am working on the file "Projects/mcmc/first_day.ipynb".
The first task is:
How does this data look like?
Can you write Python code for it?

import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sb


I am working on the file "Projects/mcmc/first_day.ipynb".
The first task is:
Now I can compute the loglike in the same way as for the union2.1 data. Just copy the loglike function from before, add as params
 - OmDE
 - alpha
 - beta
 - M_b
For the covariance matrix use the function from before, and instead of the "zandmu" variable use jla_dataset
Can you write Python code for it?

 def loglike_jla(params,h=0.7):
    """
    This function computes the logarithm of the likelihood. It recieves a vector
    params-> vector with three components (Omega Dark Energy, alpha, beta and M_b)
    """
    OmDE = params[0]
    alpha = params[1]
    beta = params[2]
    MB = params[3]
    covariance = mu_cov(alpha, beta)
    inv_covariance=np.linalg.inv(covariance)
# Ahora quiero calcular la diferencia entre el valor reportado y el calculado
    mu_obs = jla_dataset['mb']-(MB-alpha*jla_dataset['x1']+beta*jla_dataset['color'])
    mu_teo = 5.*np.log10(dl(jla_dataset['zcmb'],OmDE,h))+25
    delta = mu_teo - mu_obs
    chisquare = np.dot(delta,np.dot(inv_covariance,delta))
    return -chisquare/2

param = [0.65,0.13,3.1,-20]
loglike_jla(param)

param = [0.7,0.13,3.1,-19]
loglike_jla(param)


I am working on the file "Projects/mcmc/first_day.ipynb".
The first task is:
The supernovae
Can you write Python code for it?

plt.plot(zandmu[:,0],zandmu[:,1],'o')

heat = np.log(abs(covariance))

sb.set()
sb.heatmap(heat)

yerr = np.loadtxt('../data/SCPUnion2.1_mu_vs_z.txt', skiprows=5,usecols=[3])
plt.errorbar(zandmu[:,0],zandmu[:,1], yerr=yerr, fmt='o')


I am working on the file "Projects/mcmc/first_day.ipynb".
The first task is:
This function is very clever and you can use the names of the variables instead of the number of the corresponding column, like that:
Can you write Python code for it?


I am working on the file "Projects/mcmc/first_day.ipynb".
The first task is:
We now have to load the supernovae data in order to compare it with the theoretical data
Can you write Python code for it?

zandmu = np.loadtxt('../data/SCPUnion2.1_mu_vs_z.txt', skiprows=5,usecols=(1,2))
covariance = np.loadtxt('../data/SCPUnion2.1_covmat_sys.txt')

type(zandmu)

np.shape(zandmu[1])


I am working on the file "Projects/mcmc/first_day.ipynb".
The first task is:
You can ask which are the different columns or info in that file:
Can you write Python code for it?


I am working on the file "Projects/mcmc/second_day_bis.ipynb".
The first task is:
Let's check the chain
Can you write Python code for it?

import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(omega_chain,'o')


I am working on the file "Projects/mcmc/second_day_bis.ipynb".
The first task is:
Use a second parameter
Can you write Python code for it?

import numpy as np
import scipy.integrate as integrate
def E(z,OmDE,w):
    """
    This function computes the integrand for the computation of the luminosity distance for a flat universe
    z -> float
    OmDE -> float
    gives
    E -> float
    """
    return 1/np.sqrt((1-OmDE)*(1+z)**3+OmDE*(1+z)**(3.*(1.+w)))

def dl(z,OmDE,w,h=0.7):
    """
    This function computes the luminosity distance
    z -> float
    OmDE -> float
    h ->float
    returns
    dl -> float
    """
    inte=integrate.quad(E,0,z,args=(OmDE,w))
    # Velocidad del sonido en km/s
    c = 299792.458
    # Factor de Hubble
    Ho = 100*h
    return c*(1+z)/Ho * inte[0]
zandmu = np.loadtxt('../data/SCPUnion2.1_mu_vs_z.txt', skiprows=5,usecols=(1,2))
covariance = np.loadtxt('../data/SCPUnion2.1_covmat_sys.txt')
dl = np.vectorize(dl)
def loglike(params,h=0.7):
    """
    This function computes the logarithm of the likelihood. It recieves a vector
    params-> vector with one component (Omega Dark Energy,dark energy equation of state)
    """
    OmDE = params[0]
    w = params[1]
# Ahora quiero calcular la diferencia entre el valor reportado y el calculado
    delta = 5.*np.log10(dl(zandmu[:,0],OmDE,w,h))+25-zandmu[:,1]
    chisquare=np.dot(delta,np.dot(np.linalg.inv(covariance),delta))
    return -chisquare/2

chain1, likechain1 = markovchain(100,[0.06,0.05],[0.7,-1.])

plt.plot(chain1[:,1],'o')

plt.plot(chain1[:,0],'o')


I am working on the file "Projects/mcmc/second_day_bis.ipynb".
The first task is:
Use the jla dataset
Can you write Python code for it?

import pyfits
import glob
jla_dataset = np.genfromtxt('../data/jla/data/jla_lcparams.txt', names=True, dtype=None)
def mu_cov(alpha, beta):
    """ Assemble the full covariance matrix of distance modulus

    See Betoule et al. (2014), Eq. 11-13 for reference
    """
    Ceta = sum([pyfits.getdata(mat) for mat in glob.glob('../data/jla/covmat/C*.fits')])

    Cmu = np.zeros_like(Ceta[::3,::3])
    for i, coef1 in enumerate([1., alpha, -beta]):
        for j, coef2 in enumerate([1., alpha, -beta]):
            Cmu += (coef1 * coef2) * Ceta[i::3,j::3]

    # Add diagonal term from Eq. 13
    sigma = np.loadtxt('../data/jla/covmat/sigma_mu.txt')
    sigma_pecvel = (5 * 150 / 3e5) / (np.log(10.) * sigma[:, 2])
    Cmu[np.diag_indices_from(Cmu)] += sigma[:, 0] ** 2 + sigma[:, 1] ** 2 + sigma_pecvel ** 2

    return Cmu
def loglike(params,h=0.7):
    """
    This function computes the logarithm of the likelihood. It recieves a vector
    params-> vector with three components (Omega Dark Energy, alpha, beta and M_b)
    """
    OmDE = params[0]
    alpha = params[1]
    beta = params[2]
    MB = params[3]
    covariance = mu_cov(alpha, beta)
    inv_covariance=np.linalg.inv(covariance)
# Ahora quiero calcular la diferencia entre el valor reportado y el calculado
    mu_obs = jla_dataset['mb']-(MB-alpha*jla_dataset['x1']+beta*jla_dataset['color'])
    mu_teo = 5.*np.log10(dl(jla_dataset['zcmb'],OmDE,h))+25
    delta = mu_teo - mu_obs
    chisquare = np.dot(delta,np.dot(inv_covariance,delta))
    return -chisquare/2

param = [0.65,0.13,3.1,-20]
loglike(param)

chain1, likechain1 = markovchain(10,[0.06,0.1,0.1,0.1],[0.65,0.13,3.1,-20])

plt.plot(chain1[:,2],'o')


I am working on the file "Projects/mcmc/second_day_bis.ipynb".
The first task is:
We can even compute the asociated standard deviation
Can you write Python code for it?

average_of_squares = np.mean(omega_chain**2)
sigma_omega = np.sqrt(average_of_squares-average**2)
print sigma_omega


I am working on the file "Projects/mcmc/second_day_bis.ipynb".
The first task is:
We can use the previous result to obtain the average values of the sampled quantities
Can you write Python code for it?

chain=np.array(chain)

omega_chain=chain[:,0]

average = np.mean(omega_chain)
print average


I am working on the file "Projects/mcmc/third_day.ipynb".
The first task is:
How to obtain the confidence regions of a variable?
Can you write Python code for it?


I am working on the file "Projects/mcmc/third_day.ipynb".
The first task is:
Now let's analize the chains
First just an histogram
Can you write Python code for it?

import seaborn as sns

omegade=chain1[:,0]
omegadm=chain1[:,1]

sns.distplot(omegade)

sns.distplot(omegadm)

sns.jointplot(x=omegadm,y=omegade)

import corner

corner.corner(chain1)


I am working on the file "Projects/mcmc/third_day.ipynb".
The first task is:
You can use the "central credible interval"
Can you write Python code for it?

dimde=len(omegade)
points_outside = np.int((1-0.68)*dimde/2)
de_sorted=np.sort(omegade)
print de_sorted[points_outside], de_sorted[dimde-points_outside]

dimde=len(omegade)
points_outside = np.int((1-0.95)*dimde/2)
de_sorted=np.sort(omegade)
print de_sorted[points_outside], de_sorted[dimde-points_outside]


I am working on the file "Projects/mlt_calib/alpha_distributions.ipynb".
The first task is:
Mean and median values trace each other well. However, mean values tend to be larger than median values, with the difference increasing toward lower values of $\alpha$. There is a clear tendency for modal values to be smaller than both mean and median values, suggesting that the posterior distributions are typically skewed toward lower values. One notices that there is a population of stars whose modal values are around $\alpha = 3.0$. Individually checking the posterior distributions confirms that many of these have fairly flat distributions with local maximums at low and high values of $\alpha$.

Kernel Density Estimation
Can you write Python code for it?


I am working on the file "Projects/mlt_calib/alpha_distributions.ipynb".
The first task is:
Quickly, here is what the posterior distribution for $\alpha$ looks like.
Can you write Python code for it?

histogram = plt.hist(samples[:, 5], bins=100)


I am working on the file "Projects/mlt_calib/alpha_distributions.ipynb".
The first task is:
Start by plotting them directly against one another.
Can you write Python code for it?

fig, ax = plt.subplots(1, 3, figsize=(15., 5.))

for axis in ax:
    axis.tick_params(which='major', axis='both', labelsize=16., length=15.)

# Median vs mean
ax[0].set_xlabel('Mean Alpha', fontsize=16.)
ax[0].set_ylabel('Median Alpha', fontsize=16.)
ax[0].plot([0.5, 3.0], [0.5, 3.0], dashes=(20., 5.), lw=2, c="#b22222", alpha=0.8)
ax[0].plot(means[:, 5], medians[:, 5], 'o', c="#555555")

# Median vs mode
ax[1].set_xlabel('Mode Alpha', fontsize=16.)
ax[1].set_ylabel('Median Alpha', fontsize=16.)
ax[1].plot([0.5, 3.0], [0.5, 3.0], dashes=(20., 5.), lw=2, c="#b22222", alpha=0.8)
ax[1].plot(modes[:, 5], medians[:, 5], 'o', c="#555555")

# Mean vs Mode
ax[2].set_xlabel('Mode Alpha', fontsize=16.)
ax[2].set_ylabel('Mean Alpha', fontsize=16.)
ax[2].plot([0.5, 3.0], [0.5, 3.0], dashes=(20., 5.), lw=2, c="#b22222", alpha=0.8)
ax[2].plot(modes[:, 5], means[:, 5], 'o', c="#555555")

fig.tight_layout()


I am working on the file "Projects/mlt_calib/alpha_distributions.ipynb".
The first task is:
There does appear to be a constant shift of the distribution toward higher values of $\alpha$ as the simluation progresses. This could be used to suggest that we should continue to run the simluation longer, but shifts in the peak of the distributions are not significant compared to uncertainties.
Can you write Python code for it?

peaks = np.array([1.70])
for i in range(1, 201, 20):
    samples = chains[:, -i,:].reshape(-1, 9)
    kde = gaussian_kde(samples[:, 5], bw_method='silverman')
    peaks = np.append(peaks, alphas[np.argmax(kde(alphas))])

np.median(peaks)

masses = np.arange(0.75, 1.0, 0.005)
kde = gaussian_kde(sample010[:, 0], bw_method='silverman')
max010 = masses[np.argmax(kde(masses))]
plt.plot(masses, kde(masses), '-', lw=3, c='#b22222')

max010

params = np.arange(-0.5, 0.5, 0.005)
kde = gaussian_kde(sample010[:, 1], bw_method='silverman')
max010 = params[np.argmax(kde(params))]
plt.plot(params, kde(params), '-', lw=3, c='#b22222')

max010

params = np.arange(0.23, 0.33, 0.005)
kde = gaussian_kde(sample010[:, 2], bw_method='silverman')
max010 = params[np.argmax(kde(params))]
plt.plot(params, kde(params), '-', lw=3, c='#b22222')

max010

params = np.arange(8.8, 10.1, 0.005)
kde = gaussian_kde(sample010[:, 3], bw_method='silverman')
max010 = params[np.argmax(kde(params))]
plt.plot(params, kde(params), '-', lw=3, c='#b22222')

10**max010/1.0e9


I am working on the file "Projects/mlt_calib/alpha_distributions.ipynb".
The first task is:
There is a notable peak around $\alpha = 1.5$, which we commented on in our note on the solar runs. Now attempt a KDE.
Can you write Python code for it?

kde = gaussian_kde(samples[:, 5])

alphas = np.arange(0.5, 3.0, 0.005)
plt.plot(alphas, kde(alphas), '-', lw=2, c='#b22222')


I am working on the file "Projects/mlt_calib/alpha_distributions.ipynb".
The first task is:
This turns out to be precisely the technique used to compute "modal" values, which were assumed to be the peak of the probability distribution function. Note that, by taking only some of the final samples, we extract a much smoother distribution.
What if we try only the final iteration? That should, in principle, be representative of the target distribution.
Can you write Python code for it?

samples = chains[:, -10, :].reshape(-1, 9)

histogram = plt.hist(samples[:, 5], bins=50)

kde = gaussian_kde(samples[:, 5])

plt.plot(alphas, kde(alphas), '-', lw=2, c='#b22222')


I am working on the file "Projects/mlt_calib/alpha_distributions.ipynb".
The first task is:
When one does this, we find that the distribution is considerably more smooth with a peak value lying around $\alpha = 1.75$, quite close to the true value.
Choosing other snapshots of the run toward the end of the simluations should reveal similar distributions. Do they?
Can you write Python code for it?

sample100 = chains[:, -100, :].reshape(-1, 9)
kde = gaussian_kde(sample100[:, 5], bw_method='silverman')
max100 = alphas[np.argmax(kde(alphas))]
plt.plot(alphas, kde(alphas), dashes=(5., 5.), lw=3, c='#555555')

sample050 = chains[:, -50, :].reshape(-1, 9)
kde = gaussian_kde(sample050[:, 5], bw_method='silverman')
max050 = alphas[np.argmax(kde(alphas))]
plt.plot(alphas, kde(alphas), dashes=(20., 5.), lw=3, c='#1e90ff')

sample010 = chains[:, -10, :].reshape(-1, 9)
kde = gaussian_kde(sample010[:, 5], bw_method='silverman')
max010 = alphas[np.argmax(kde(alphas))]
plt.plot(alphas, kde(alphas), '-', lw=3, c='#b22222')

max100, max050, max010


I am working on the file "Projects/mlt_calib/credible_intervals.ipynb".
The first task is:
Explore how values of $f$ look as a funciton of $\theta$ and $\theta_2$.
Can you write Python code for it?


I am working on the file "Projects/mlt_calib/credible_intervals.ipynb".
The first task is:
It will be good to first evaluate the shape of the three posterior probability distributions for the mixing length, which poses particular problems.
Can you write Python code for it?

fig, ax = plt.subplots(1, 3, figsize=(12, 4))

tmp = ax[0].hist(left_samp[:, 5], bins=25, alpha=0.5)
tmp = ax[1].hist(right_samp[:, 5], bins=25, alpha=0.5)
tmp = ax[2].hist(mid_samp[:, 5], bins=25, alpha=0.5)


I am working on the file "Projects/mlt_calib/credible_intervals.ipynb".
The first task is:
Let's instead aim for a 3D visualization.
Can you write Python code for it?

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

ax.plot_trisurf(values[:, 0], values[:, 1], values[:, 2])


I am working on the file "Projects/mlt_calib/credible_intervals.ipynb".
The first task is:
Remold the flattened MCMC chains into individual chains.
Can you write Python code for it?

left_chains  = left.reshape(300, -1, 9)
right_chains = right.reshape(300, -1, 9)
mid_chains   = mid.reshape(300, -1, 9)


I am working on the file "Projects/mlt_calib/credible_intervals.ipynb".
The first task is:
We can now also extract a new flattened chain using the last 50 iterations from the sample.
Can you write Python code for it?

left_samp  = left_chains[:, -50:, :].reshape(-1, 9)
right_samp = right_chains[:, -50:, :].reshape(-1, 9)
mid_samp   = mid_chains[:, -50:, :].reshape(-1, 9)


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
As with stellar mass, there is no immediate correlation between observed effective temperatures and model inferred fundamental properties. However, it is clear that the largest errors appear at the coolest temperatures, with the presence of the same four outliers as above. We also again note that, even for the hottest stars, there appears to be a systematic offset of model errors toward hotter temperatures and smaller radii. Individual errors are not necessarily significant, often located within $1\sigma$ of the zero-point. However, the sysetmatic nature of the errors betrays the existence of possible errors among K dwarfs. A problem thought to be exclusive of M dwarfs appears to extend throughout the K dwarf regime.
Can you write Python code for it?

fig, axes = plt.subplots(2, 1, figsize=(10, 8))

# set axis properties
axes[1].set_xlabel('Observed Metallicity (dex)', fontsize=22.)
axes[0].set_ylabel('$\\Delta T_{\\rm eff} / \\sigma$', fontsize=22.)
axes[1].set_ylabel('$\\Delta \\Theta / \\sigma$', fontsize=22.)

# plot zero points
for ax in axes:
    ax.set_xlim(-0.5, 0.5)
    ax.plot([-0.5, 0.5], [0.0, 0.0], '--', lw=2, color='#333333')

# plot relative errors
axes[0].plot(FeH_strong[:, 30], dTeff_sigma_strong, 's', color='#800000', markersize=7.0)
axes[1].plot(FeH_strong[:, 30], dTheta_sigma_strong, 's', color='#800000', markersize=7.0)

axes[0].plot(FeH_weak[:, 30], dTeff_sigma_weak, 'o', color='#4682B4', markersize=9.0)
axes[1].plot(FeH_weak[:, 30], dTheta_sigma_weak, 'o', color='#4682B4', markersize=9.0)


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
Computing the mean and median offsets of bolometric flux and angular diameter from the zero-points, we find
Can you write Python code for it?

# compute mean of the offsets
print 'Weak Metallicity Prior'
print 'Mean Bolometric Flux Error:  {:+5.2f} sigma'.format(np.mean(dFbol_sigma_weak))
print 'Mean Angular Diameter Error: {:+5.2f} sigma\n'.format(np.mean(dTheta_sigma_weak))

print 'Strong Metallicity Prior'
print 'Mean Bolometric Flux Error:  {:+5.2f} sigma'.format(np.mean(dFbol_sigma_strong))
print 'Mean Angular Diameter Error: {:+5.2f} sigma\n'.format(np.mean(dTheta_sigma_strong))

# compute median of the offsets
print 'Weak Metallicity Prior'
print 'Median Bolometric Flux Error:  {:+5.2f} sigma'.format(np.median(dFbol_sigma_weak))
print 'Median Angular Diameter Error: {:+5.2f} sigma\n'.format(np.median(dTheta_sigma_weak))

print 'Strong Metallicity Prior'
print 'Median Bolometric Flux Error:  {:+5.2f} sigma'.format(np.median(dFbol_sigma_strong))
print 'Median Angular Diameter Error: {:+5.2f} sigma\n'.format(np.median(dTheta_sigma_strong))


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
Errors on stellar radii (given that we've recovered measured distances) are on the order of 2%, depending on the strength of the metallicity prior. This is consistent with mean offsets observed among low-mass stars in detached double-lined ecilpsing binaries (Feiden & Chaboyer 2012; [Spada et al. 2013). However, it's quite possible that the models are compensating for true modeling errors with older ages and higher metallicities. Let's look at the distribution of ages.
Can you write Python code for it?

fig, ax = plt.subplots(1, 1, figsize=(8, 8))

# set axis properties
ax.set_xlabel('$\\log_{10}(age / yr)$', fontsize=22.)
ax.set_ylabel('Number', fontsize=22.)

strong_hist = ax.hist(FeH_strong[:, 3], bins=20, facecolor='#800000', alpha=0.6)
weak_hist = ax.hist(FeH_weak[:, 3], bins=20, facecolor='#4682B4', alpha=0.6)


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
Exploring Correlations
Can you write Python code for it?

fig, axes = plt.subplots(2, 1, figsize=(10, 8))

# set axis properties
axes[1].set_xlabel('Mass ($M_{\\odot}$)', fontsize=22.)
axes[0].set_ylabel('$\\Delta T_{\\rm eff} / \\sigma$', fontsize=22.)
axes[1].set_ylabel('$\\Delta \\Theta / \\sigma$', fontsize=22.)

# plot zero points
for ax in axes:
    ax.plot([0.0, 1.0e0], [0.0, 0.0], '--', lw=2, color='#333333')

# plot relative errors
axes[0].plot(FeH_strong[:, 0], dTeff_sigma_strong, 's', color='#800000', markersize=7.0)
axes[1].plot(FeH_strong[:, 0], dTheta_sigma_strong, 's', color='#800000', markersize=7.0)

axes[0].plot(FeH_weak[:, 0], dTeff_sigma_weak, 'o', color='#4682B4', markersize=9.0)
axes[1].plot(FeH_weak[:, 0], dTheta_sigma_weak, 'o', color='#4682B4', markersize=9.0)


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
In general, masses estimates are consistent among all three trials. Masses inferred in the weak metallicity prior trial are systematically higher than those in the other two trials, but not by a large margin. Issues arise in the vicinity of $0.80 M_{\odot}$, with the strong metallicity prior and no $T_{\textrm{eff}}$ trials showing a propensity for stars to have masses around $0.77 M_{\odot}$. This propensity does not appear to exist under the weak metallicity prior. While the effective temperature is a derived quantity, results of trials adopting an effective temperature prior appear to derive more reliable results that are devoid of a pile up in the $0.80 M_{\odot}$ vicinity. Masses above that threshold are most certainly expected, particularly in the case of Gl 559 B ($\alpha$ Cen B), which has an expected mass of approximately $0.92±0.02 M_{\odot}$. Unfortunately, none of the various trials were able to recover this mass within the $1\sigma$ limit.
How well do the various trials recover the observed properties? Let's start with the two primary observables: angular diameter and bolometric flux. These are inextricably coupled to the star's distance when comparing with stellar evolution models, which only provide information on the stellar radius and bolometric luminosity. Nevertheless, we saw above that distances were well recovered, permitting a reliable comparison of the more fundamental observables. First we'll define these new properties in new arrays,
Can you write Python code for it?

# define in terms of (obs - model)/sigma_observed
#
# angular diameters
dTheta_sigma_weak = (FeH_weak[:, 18] - FeH_weak[:, 8])/FeH_weak[:, 19]
dTheta_sigma_strong = (FeH_strong[:, 18] - FeH_strong[:, 8])/FeH_strong[:, 19]
dTheta_sigma_noTeff = (no_Teff[:, 18] - no_Teff[:, 8])/no_Teff[:, 19]

# bolometric fluxes (note: strange units for model properties)
dFbol_sigma_weak = (FeH_weak[:, 22] - 10**(FeH_weak[:, 7] + 8.0))/FeH_weak[:, 23]
dFbol_sigma_strong = (FeH_strong[:, 22] - 10**(FeH_strong[:, 7] + 8.0))/FeH_strong[:, 23]
dFbol_sigma_noTeff = (no_Teff[:, 22] - 10**(no_Teff[:, 7] + 8.0))/no_Teff[:, 23]

# effective temperatures
dTeff_sigma_weak = (FeH_weak[:, 24] - 10**FeH_weak[:, 6])/FeH_weak[:, 25]
dTeff_sigma_strong = (FeH_strong[:, 24] - 10**FeH_strong[:, 6])/FeH_strong[:, 25]


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
Note that this does not include the trials where the effective temperature was neglected in the likelihood estimator. There is good agreement between the two samples, despite changing the metallicity prior.
Can you write Python code for it?

fig, axes = plt.subplots(2, 1, figsize=(10, 8))

# set axis properties
axes[1].set_xlabel('Angular Diameter (mas)', fontsize=22.)
axes[0].set_ylabel('$\\Delta F_{\\rm bol} / \\sigma$', fontsize=22.)
axes[1].set_ylabel('$\\Delta \\Theta / \\sigma$', fontsize=22.)

# plot zero points
for ax in axes:
    ax.semilogx([1.e-1, 1.e1], [0.0, 0.0], '--', lw=2, color='#333333')

# plot relative errors
axes[0].semilogx(FeH_strong[:, 18], dFbol_sigma_strong, 's', color='#800000', markersize=7.0)
axes[1].semilogx(FeH_strong[:, 18], dTheta_sigma_strong, 's', color='#800000', markersize=7.0)

axes[0].semilogx(FeH_weak[:, 18], dFbol_sigma_weak, 'o', color='#4682B4', markersize=9.0)
axes[1].semilogx(FeH_weak[:, 18], dTheta_sigma_weak, 'o', color='#4682B4', markersize=9.0)


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
Now we can compare results from all three trials at once, with relative discrepancies normalized to the observational uncertainties.
Can you write Python code for it?

from matplotlib.patches import Ellipse

fig, ax = plt.subplots(1, 1, figsize=(8, 8))

# set axis properties
ax.set_xlabel('$\\Delta F_{\\rm bol} / \\sigma$', fontsize=22.)
ax.set_ylabel('$\\Delta \\Theta / \\sigma$', fontsize=22.)

# plot 68% and 99% confidence intervals
ells = [Ellipse(xy=(0.0, 0.0), width=2.*x, height=2.*x, angle=0.0, lw=3, fill=False,
                linestyle='dashed', edgecolor='#333333') for x in [1.0, 3.0]]
for e in ells:
    ax.add_artist(e)

# plot results of trials
ax.plot(dFbol_sigma_weak, dTheta_sigma_weak, 'o', color='#4682B4', markersize=9.0)
ax.plot(dFbol_sigma_strong, dTheta_sigma_strong, 's', color='#800000', markersize=9.0)
ax.plot(dFbol_sigma_noTeff, dTheta_sigma_noTeff, '^', color='#444444', markersize=9.0)


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
Rephrasing these as fractional relative errors,
Can you write Python code for it?

# define in terms of (obs - model)/obs
#
# angular diameters
dTheta_weak = (FeH_weak[:, 18] - FeH_weak[:, 8])/FeH_weak[:, 18]
dTheta_strong = (FeH_strong[:, 18] - FeH_strong[:, 8])/FeH_strong[:, 18]

# bolometric fluxes (note: strange units for model properties)
dFbol_weak = (FeH_weak[:, 22] - 10**(FeH_weak[:, 7] + 8.0))/FeH_weak[:, 22]
dFbol_strong = (FeH_strong[:, 22] - 10**(FeH_strong[:, 7] + 8.0))/FeH_strong[:, 22]

# compute mean of the offsets
print 'Weak Metallicity Prior'
print 'Mean Bolometric Flux Error:  {:+5.2f}%'.format(np.mean(dFbol_weak)*100.)
print 'Mean Angular Diameter Error: {:+5.2f}%\n'.format(np.mean(dTheta_weak)*100.)

print 'Strong Metallicity Prior'
print 'Mean Bolometric Flux Error:  {:+5.2f}%'.format(np.mean(dFbol_strong)*100.)
print 'Mean Angular Diameter Error: {:+5.2f}%\n'.format(np.mean(dTheta_strong)*100.)

# compute median of the offsets
print 'Weak Metallicity Prior'
print 'Median Bolometric Flux Error:  {:+5.2f}%'.format(np.median(dFbol_weak)*100.)
print 'Median Angular Diameter Error: {:+5.2f}%\n'.format(np.median(dTheta_weak)*100.)

print 'Strong Metallicity Prior'
print 'Median Bolometric Flux Error:  {:+5.2f}%'.format(np.median(dFbol_strong)*100.)
print 'Median Angular Diameter Error: {:+5.2f}%\n'.format(np.median(dTheta_strong)*100.)


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
Results are rather consistent, at least in the morphology of the scatter. Beyond the 68% confidence interval region (inner ellipse), there is very little scatter into quadrants III and IV with only a handful of points lying in quadrant I. Most of the scatter resids in quadrant II, which can be interpretted to mean that models tend to infer radii that are too small compared to observations with bolometric fluxes that are slightly too large. While the scatter along the bolometric flux axis is reveals a general consistency with the observations, there is still a systematic offset toward the left side of the zero-point. One may interpret this as the model attempting to produce larger radii while maintaining agreement with the bolometric flux and effective temperature measurements.
Can we discern any correlations of these errors among the parameter present in the data? Let's start with potential correations with the observables themselves.
Can you write Python code for it?

fig, axes = plt.subplots(2, 1, figsize=(10, 8))

# set axis properties
axes[1].set_xlabel('Bolometric Flux (erg s$^{-1}$ cm$^{-2}$)', fontsize=22.)
axes[0].set_ylabel('$\\Delta F_{\\rm bol} / \\sigma$', fontsize=22.)
axes[1].set_ylabel('$\\Delta \\Theta / \\sigma$', fontsize=22.)

# plot zero points
for ax in axes:
    ax.semilogx([1.e-1, 1.e3], [0.0, 0.0], '--', lw=2, color='#333333')

# plot relative errors
axes[0].semilogx(FeH_strong[:, 22], dFbol_sigma_strong, 's', color='#800000', markersize=7.0)
axes[1].semilogx(FeH_strong[:, 22], dTheta_sigma_strong, 's', color='#800000', markersize=7.0)

axes[0].semilogx(FeH_weak[:, 22], dFbol_sigma_weak, 'o', color='#4682B4', markersize=9.0)
axes[1].semilogx(FeH_weak[:, 22], dTheta_sigma_weak, 'o', color='#4682B4', markersize=9.0)


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
There do not appear to be trends among model-observation discrepancies as a function of mass. Instead, models tend to systmatically under predict stellar radii and over predict effective temperatures. Below $0.5 M_{\odot}$, there is increased scatter owing to four significant outliers. One of these, Gl 388 (AD Leo), is a candidate young star, so we should perhaps not be surprised by the failure of models to reproduce its properties. It stands out in our above analysis of how well models recovered observed bolometric fluxes. Since the star is young, the models are not able to reproduce its bolometric flux; Gl 388 shows the largest disagreements when it comes to bolometric flux, adding further proof that the system may very well be young.
Can you write Python code for it?

fig, axes = plt.subplots(2, 1, figsize=(10, 8))

# set axis properties
axes[1].set_xlabel('Effective Temperature (K)', fontsize=22.)
axes[0].set_ylabel('$\\Delta T_{\\rm eff} / \\sigma$', fontsize=22.)
axes[1].set_ylabel('$\\Delta \\Theta / \\sigma$', fontsize=22.)

# plot zero points
for ax in axes:
    ax.plot([2500, 6000], [0.0, 0.0], '--', lw=2, color='#333333')

# plot relative errors
axes[0].plot(FeH_strong[:, 24], dTeff_sigma_strong, 's', color='#800000', markersize=7.0)
axes[1].plot(FeH_strong[:, 24], dTheta_sigma_strong, 's', color='#800000', markersize=7.0)

axes[0].plot(FeH_weak[:, 24], dTeff_sigma_weak, 'o', color='#4682B4', markersize=9.0)
axes[1].plot(FeH_weak[:, 24], dTheta_sigma_weak, 'o', color='#4682B4', markersize=9.0)


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
There is, in fact, a significant number of stars that are predicted to have ages comparable to the age of the Universe. While we cannot rule out the possibility that some stars have older ages consistent with the thick disk or halo populations, it is unlikely that such a large fraction of local field stars (nearly 50%) have ages older than 12 Gyr. This, in part, reflects the difficult of acquiring ages for low-mass stars, particuarly those in the M dwarf regime, where the stellar properties are not expected to evolve significantly over the stellar lifetime. A preference for older ages then only reflects that fact that some evolution does occur in the models, leading to almost negligibly larger radii that are only slightly preferred given the systematic offset observed above.
While considering final distributions, here is the final mass distribution from the weak metallicity prior trial.
Can you write Python code for it?

fig, ax = plt.subplots(1, 1, figsize=(8, 8))

# set axis properties
ax.set_xlabel('Mass ($M_{\\odot}$)', fontsize=22.)
ax.set_ylabel('Number', fontsize=22.)

weak_hist = ax.hist(FeH_weak[:, 0], bins=10, facecolor='#4682B4', alpha=0.6)


I am working on the file "Projects/mlt_calib/fixed_Y_fixed_alpha.ipynb".
The first task is:
~~The first two cases accurately reproduce the observed distances. However, the third case (no $T_{\textrm{eff}}$ constraint) does not. This may suggest that the latter MCMC runs did not fully converge.~~ Scratch that -- there was a typo in the code that lead to reasonable looking, but incorrect results. Distances are well recovered. Moving now to metallicity,
Can you write Python code for it?

fig, axes = plt.subplots(1, 3, figsize=(15, 5))

# set axis labels and plot one-to-one line
for ax in axes:
    ax.set_xlabel('Observed [Fe/H] (dex)', fontsize=22.)
    ax.set_xlim((-0.5, 0.5))
    ax.set_ylim((-0.5, 0.5))
    ax.plot( np.arange(-0.5, 0.51, 0.5), np.arange(-0.5, 0.51, 0.5), '--', lw=2, color='#444444')
axes[0].set_ylabel('Inferred [M/H] (dex)', fontsize=22.)

# plot metallicity data
axes[0].plot(FeH_weak[:, 30], FeH_weak[:, 1], 'o', color='#4682B4')
axes[1].plot(FeH_strong[:, 30], FeH_strong[:, 1], 'o', color='#4682B4')
axes[2].plot(no_Teff[:, 30], no_Teff[:, 1], 'o', color='#4682B4')


I am working on the file "Projects/mlt_calib/fixed_Y_float_alpha.ipynb".
The first task is:
And compare the overall recovery of observed fundamental properties.
Can you write Python code for it?

from matplotlib.patches import Ellipse
fig, ax = plt.subplots(1, 1, figsize=(8, 8))

# set axis labels
ax.set_xlabel('$\\Delta F_{\\rm bol} / \\sigma$', fontsize=22.)
ax.set_ylabel('$\\Delta \\Theta / \\sigma$', fontsize=22.)
ax.set_xlim(-6.0, 6.0)
ax.set_ylim(-6.0,10.0)

# plot 68% and 99% confidence intervals
ells = [Ellipse(xy=(0.0, 0.0), width=2.*x, height=2.*x, angle=0.0, lw=3, fill=False,
                linestyle='dashed', edgecolor='#333333') for x in [1.0, 3.0]]
for e in ells:
    ax.add_artist(e)

# plot recovery diagnostics (uncertainty normalized errors)
ax.plot([-6.0, 6.0], [ 0.0, 0.0], '--', lw=2, color="#444444")
ax.plot([ 0.0, 0.0], [-6.0,10.0], '--', lw=2, color="#444444")
ax.plot(dFbol_sigma, dTheta_sigma, 'o', markersize=9.0, color='#4682B4')


I am working on the file "Projects/mlt_calib/fixed_Y_float_alpha.ipynb".
The first task is:
Comments and analysis.
Compute Bonaca et al. predictions for $\alpha_{\rm MLT}$.
Can you write Python code for it?

B12_coeffs = [-12.77, 0.54, 3.18, 0.52]  # from Table 1: Trilinear analysis
B12_alphas = B12_coeffs[0] + B12_coeffs[1]*Log_g + B12_coeffs[2]*data[:,6] + B12_coeffs[3]*data[:,1]


I am working on the file "Projects/mlt_calib/fixed_Y_float_alpha.ipynb".
The first task is:
Note, will add diagnostic figures later. Beginning with $\alpha_{\rm MLT}$ values and how they vary as a function of stellar fundamental properties and how they compare to Bonaca et al. (2012, ApJL, 755, L12).
Variations of $\alpha_{\rm MLT}$ as a function of stellar properties. Note that we use the most-likely value of the mixing lenght parameter as opposed to the median value.
Can you write Python code for it?


I am working on the file "Projects/mlt_calib/float_Y_float_alpha.ipynb".
The first task is:
As before, we can confirm that distances were accurately recovered and we can check on how well metallicities were recovered compared to the measured value.
Can you write Python code for it?

fig, ax = plt.subplots(1, 2, figsize=(10, 5))

# distance recovery diagnostic
distance_limits = (0.0, 20.0)
ax[0].set_xlabel('Observed Distance (pc)', fontsize=22.)
ax[0].set_ylabel('Inferred Distance (pc)', fontsize=22.)
ax[0].set_xlim(distance_limits)
ax[0].set_ylim(distance_limits)
ax[0].plot(distance_limits, distance_limits, '--', lw=3, color="#444444")
ax[0].plot(1.0/data[:, 20], data[:, 4], 'o', markersize=9.0, color='#4682B4')

# metallicity recovery diagnostic
quoted_err = np.array([x for x in data if x[31] > 0.0])

FeH_limits = (-0.5, 0.5)
ax[1].set_xlabel('Observed [Fe/H] (dex)', fontsize=22.)
ax[1].set_ylabel('Inferred [M/H] (dex)', fontsize=22.)
ax[1].set_xlim(FeH_limits)
ax[1].set_ylim(FeH_limits)
ax[1].plot(FeH_limits, FeH_limits, '--', lw=3, color="#444444")
ax[1].plot(data[:, 30], data[:, 1], 'o', markersize=9.0, color='#4682B4')
ax[1].plot(quoted_err[:, 30], quoted_err[:, 1], 'o', markersize=9.0, color='#800000')

# auto-adjust subplot spacing
fig.tight_layout()


I am working on the file "Projects/mlt_calib/float_Y_float_alpha.ipynb".
The first task is:
Distance are well recovered, as anticipated. Metallicities are scattered about the zero-point with perhaps a tendency for predicting systematically higher metallicities between $-0.40$ and $-0.20$ dex. Typical scatter appears to be around $\pm0.2$ dex, consistent with the assumed metallicities uncertainty. Points in red are those that have a measured metallicity uncertainty. In general, those metallicities are better recovered, perhaps owing to tighter constraints.
Define relative errors and errors normalized to observational unceratinties.
Can you write Python code for it?


I am working on the file "Projects/mlt_calib/float_Y_float_alpha.ipynb".
The first task is:
NOTE: values for the Sun plotted above are drawn from our solar-calibrated model.
We can also compare how the inferred mixing length compares to those that are the result of extrapolating the Bonaca et al. (2012, ApJL, 755, L12) relation. The latter is valid for stars with $3.8 \le \log(g) \le 4.5$, $5000 \le T_{\rm eff} \le 6700$ K, and $-0.65 \le \textrm{[Fe/H]} \le +0.35$, but here we extrapolate to see how the mixing length parameter might evolve toward cooler temperatures.
Can you write Python code for it?


I am working on the file "Projects/mlt_calib/float_Y_float_alpha.ipynb".
The first task is:
Now plot the Bonaca et al values against those we derived,
Can you write Python code for it?

fig, ax = plt.subplots(1, 1, figsize=(8, 8))

ax.set_xlabel('Bonaca et al. $\\alpha_{\\rm MLT}$', fontsize=20.)
ax.set_ylabel('This work, $\\alpha_{\\rm MLT}$', fontsize=20.)
ax.tick_params(which='major', axis='both', length=10., labelsize=16.)

# one-to-one relation
ax.plot([1.0, 1.59], [1.0, 1.59], '--', lw=2, color='#444444')

# compare values
ax.errorbar(B12_alphas, data[:,5], yerr=data[:,14], fmt='o', lw=2, markersize=9.0, color='#4682B4')


I am working on the file "Projects/mlt_calib/float_Y_float_alpha.ipynb".
The first task is:
Recovery of observed fundamental properties.
Can you write Python code for it?

from matplotlib.patches import Ellipse
fig, ax = plt.subplots(1, 1, figsize=(8, 8))

# set axis labels
ax.set_xlabel('$\\Delta F_{\\rm bol} / \\sigma$', fontsize=22.)
ax.set_ylabel('$\\Delta \\Theta / \\sigma$', fontsize=22.)
ax.set_xlim(-3.0, 3.0)
ax.set_ylim(-4.0, 4.0)

# plot 68% and 99% confidence intervals
ells = [Ellipse(xy=(0.0, 0.0), width=2.*x, height=2.*x, angle=0.0, lw=3, fill=False,
                linestyle='dashed', edgecolor='#333333') for x in [1.0, 3.0]]
for e in ells:
    ax.add_artist(e)

# plot recovery diagnostics (uncertainty normalized errors)
ax.plot([-3.0, 3.0], [ 0.0, 0.0], '--', lw=2, color="#444444")
ax.plot([ 0.0, 0.0], [-4.0, 4.0], '--', lw=2, color="#444444")
ax.plot(dFbol_sigma, dTheta_sigma, 'o', markersize=9.0, color='#4682B4')


I am working on the file "Projects/mlt_calib/float_Y_float_alpha.ipynb".
The first task is:
There is a rise of the average error as one moves toward lower bolometric fluxes and smaller angular diameters. These points are effectively all M dwarfs. This illustrates, quite well, that problems for the lowest mass stars are most resiliant to variations in stellar model input parameters, thus preserving the trends present in the data where $\alpha_{MLT}$ and $Y_i$ are fixed. The growth of the trend is therefore attributable to the model's steadily increasing resistance to change resulting from modifications to input parameters.
As a function of stellar mass and effective temperature,
Can you write Python code for it?


I am working on the file "Projects/mlt_calib/kde_mle_median.ipynb".
The first task is:
Calculate correlation coefficients from Spearman r test.
Can you write Python code for it?

stat.spearmanr(10**warm_kde[:, 6], warm_kde[:, 5]), stat.spearmanr(warm_kde[:, 1], warm_kde[:, 5]), \
    stat.spearmanr(warm_kde[:, 0], warm_kde[:, 5])


I am working on the file "Projects/mlt_calib/kde_mle_median.ipynb".
The first task is:
Load data for three different approaches to estimating stellar parameters from MCMC simulations.
Can you write Python code for it?

kde = np.genfromtxt('data/run08_kde_props.txt')
mle = np.genfromtxt('data/run08_mle_props.txt')
med = np.genfromtxt('data/run08_median_props.txt')


I am working on the file "Projects/mlt_calib/kde_mle_median.ipynb".
The first task is:
Now try only a limited range of effective temperatures to mitigate effects related to the temperature sensitivity of various opacity sources.
Can you write Python code for it?

warm_kde = np.array([star for star in kde if 5000. <= 10**star[6] <= 6000. and star[1] > -0.35])

fig, ax = plt.subplots(1, 3, figsize=(12., 4.))

# KDE
ax[0].plot(10**warm_kde[:, 6], warm_kde[:, 5], 'o', markersize=7., c='#555555', alpha=0.7)
ax[1].plot(warm_kde[:, 1], warm_kde[:, 5], 'o', markersize=7., c='#555555', alpha=0.7)
ax[2].plot(warm_kde[:, 0], warm_kde[:, 5], 'o', markersize=7., c='#555555', alpha=0.7)

fig.tight_layout()


I am working on the file "Projects/mlt_calib/kde_mle_median.ipynb".
The first task is:
To permit a better comparison with 3D RHD models, we should isolate stars that have a roughly solar metallicity. Since 3D RHD simluations are only performed at solar metallicity, it may bias the comparison given that we a significantly larger spread in metallicity.
Can you write Python code for it?

solar_kde = np.array([star for star in kde if -0.1 < star[1] < 0.1])

fig, ax = plt.subplots(1, 3, figsize=(12., 4.))

# KDE
ax[0].plot(10**solar_kde[:, 6], solar_kde[:, 5], 'o', markersize=7., c='#555555', alpha=0.7)
ax[1].plot(solar_kde[:, 1], solar_kde[:, 5], 'o', markersize=7., c='#555555', alpha=0.7)
ax[2].plot(solar_kde[:, 0], solar_kde[:, 5], 'o', markersize=7., c='#555555', alpha=0.7)

fig.tight_layout()


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
A linear model performs well for fitting the subset of the data where points affected by grid boundaries have been removed ($R = 0.84$). Assuming a significance test of $p < 0.01$, we would reject the null hypothesis for all three parameters, finding plausible evidence that the correlations shown above are not the result of randomness.
Can you write Python code for it?

fig, ax = plt.subplots(4, 2, figsize=(12., 16.))

for row in ax:
    for axis in row:
        axis.tick_params(which='major', axis='both', length=14., labelsize=14.)

#=================
# All data
#
b0, b1, b2, b3 = trifit_all.params
# total residuals
ax[0, 0].set_title('All Data Points', fontsize=16.)
ax[0, 0].set_xlabel('Mass (Msun)', fontsize=14.)
ax[0, 0].plot([0.0, 1.0], [0.0, 0.0], dashes=(20., 5.), lw=2, c='#b22222', alpha=0.8)
ax[0, 0].plot(fit_data_all[:, 0], trifit_all.resid, 'o', c='#555555', alpha=0.6)

# variations with b1 (mass)
loggs  = np.arange(4.4, 5.2, 0.05)
depend = fit_data_all[:, 5] - b0 - b2*fit_data_all[:, 1] - b3*fit_data_all[:, 6]
ax[1, 0].set_xlabel('log(g)', fontsize=14.)
ax[1, 0].plot(loggs, loggs*b1, dashes=(20., 5.), lw=2, c='#b22222', alpha=0.8)
ax[1, 0].plot(logg_all, depend, 'o', c='#555555', alpha=0.6)

# variations with b2 (metallicity)
metals = np.arange(-0.6, 0.60, 0.05)
depend = fit_data_all[:, 5] - b0 - b1*logg_all - b3*fit_data_all[:, 6]
ax[2, 0].set_xlabel('[M/H] (dex)', fontsize=14.)
ax[2, 0].plot(metals, metals*b2, dashes=(20., 5.), lw=2, c='#b22222', alpha=0.8)
ax[2, 0].plot(fit_data_all[:, 1], depend, 'o', c='#555555', alpha=0.6)

# variations with b3 (logTeff)
logT   = np.arange(3.4, 3.75, 0.05)
depend = fit_data_all[:, 5] - b0 - b1*logg_all - b2*fit_data_all[:, 1]
ax[3, 0].set_xlabel('log(Teff)', fontsize=14.)
ax[3, 0].plot(logT, logT*b3, dashes=(20., 5.), lw=2, c='#b22222', alpha=0.8)
ax[3, 0].plot(fit_data_all[:, 6], depend, 'o', c='#555555', alpha=0.6)

#=================
# Outliers removed
#
b0, b1, b2, b3 = trifit_low.params
# total residuals
ax[0, 1].set_title('Subset of Data Points', fontsize=16.)
ax[0, 1].set_xlabel('Mass (Msun)', fontsize=14.)
ax[0, 1].plot([0.0, 1.0], [0.0, 0.0], dashes=(20., 5.), lw=2, c='#b22222', alpha=0.8)
ax[0, 1].plot(fit_data_low[:, 0], trifit_low.resid, 'o', c='#555555', alpha=0.6)

# variations with b1 (mass)
loggs  = np.arange(4.3, 5.2, 0.05)
depend = fit_data_low[:, 5] - b0 - b2*fit_data_low[:, 1] - b3*fit_data_low[:,...


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
And the Spearman $r$ rank-correlation coefficients from the above comparisons,
Can you write Python code for it?

stats.spearmanr(10**kde_fbr2[:, 6], kde_fbr2[:, 5]), \
    stats.spearmanr(kde_fbr2[:, 1], kde_fbr2[:, 5]), \
    stats.spearmanr(kde_fbr2[:, 0], kde_fbr2[:, 5])


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
Compare Bonaca et al. results for Run 5 with our inferred values.
Can you write Python code for it?


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
Comparing with the KDE computed from the raw posterior distributions,
Can you write Python code for it?

fig, ax = plt.subplots(3, 3, figsize=(12., 12.))

for i in range(9):
    row = i/3
    col = i%3
    axis = ax[row, col]

    axis.plot([min(kde_fbr2[:, i]), max(kde_fbr2[:, i])], [min(kde_fbr2[:, i]), max(kde_fbr2[:, i])],
                  '-', lw=3, c='#b22222', alpha=0.8)
    axis.plot(kde_fbr2[:, i], kde_pdf[:, i], 'o', markersize=5.0, c='#555555', alpha=0.6)

fig.tight_layout()


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
First we need to reorganize the data so that we have an array with only the desired quantities.
Can you write Python code for it?

fit_data_all = kde_fbr2  # include all points
logg_all = np.log10(6.67e-8*fit_data_all[:, 0]*1.988e33/(fit_data_all[:, 26]*6.955e10)**2)

# fit with all points
all_data = np.column_stack((logg_all, fit_data_all[:, 1]))
all_data = np.column_stack((all_data, fit_data_all[:, 6]))
all_data = sm.tools.tools.add_constant(all_data, prepend=True)

# remove noted outliers (high and low alphas)
fit_data_low = np.array([star for star in kde_fbr2 if 0.55 <= star[5] <= 2.5])
logg_low = np.log10(6.67e-8*fit_data_low[:, 0]*1.988e33/(fit_data_low[:, 26]*6.955e10)**2)

# fit to lower sequence
low_data = np.column_stack((logg_low, fit_data_low[:, 1]))
low_data = np.column_stack((low_data, fit_data_low[:, 6]))
low_data = sm.tools.tools.add_constant(low_data, prepend=True)


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
First, Spearman $r$ rank-order correlation coefficient. For resampled distribution:
Can you write Python code for it?

stats.spearmanr(10**kde_fbr[:, 6], kde_fbr[:, 5]), \
    stats.spearmanr(kde_fbr[:, 1], kde_fbr[:, 5]), \
    stats.spearmanr(kde_fbr[:, 0], kde_fbr[:, 5])

stats.spearmanr(10**kde_pdf[:, 6], kde_pdf[:, 5]), \
    stats.spearmanr(kde_pdf[:, 1], kde_pdf[:, 5]), \
    stats.spearmanr(kde_pdf[:, 0], kde_pdf[:, 5])


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
Herein lies a problem. How we compute the modal value can alter the results. To see what effect this has on inferred correlations, let's compare Spearman, Pearson, and Kendall correlation tests.
Can you write Python code for it?


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
It will first be instructive to see whether there is a difference between parameters estimated from the two different KDEs calculated posteriors that have been bootstrapped resampled.
Can you write Python code for it?

fig, ax = plt.subplots(3, 3, figsize=(12., 12.))

for i in range(9):
    row = i/3
    col = i%3
    axis = ax[row, col]

    axis.plot([min(kde_pbr[:, i]), max(kde_pbr[:, i])], [min(kde_pbr[:, i]), max(kde_pbr[:, i])],
                  '-', lw=3, c='#b22222', alpha=0.8)
    axis.plot(kde_pbr[:, i], kde_fbr[:, i], 'o', markersize=5.0, c='#555555', alpha=0.6)


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
Now for Pearson $\rho$ correlation coefficients.
Can you write Python code for it?

stats.pearsonr(10**kde_fbr[:, 6], kde_fbr[:, 5]), \
    stats.pearsonr(kde_fbr[:, 1], kde_fbr[:, 5]), \
    stats.pearsonr(kde_fbr[:, 0], kde_fbr[:, 5])

stats.pearsonr(10**kde_pdf[:, 6], kde_pdf[:, 5]), \
    stats.pearsonr(kde_pdf[:, 1], kde_pdf[:, 5]), \
    stats.pearsonr(kde_pdf[:, 0], kde_pdf[:, 5])


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
Now perform a direct comparison, but normalized to solar values (see above),
Can you write Python code for it?


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
Now we can compute values for $\alpha$ using our fit and those from Bonaca et al. (2012).
Can you write Python code for it?

our_alphas = trifit_low.predict(low_data)/1.884
b12_alphas = (c0 + c1*low_data[:, 1] + c2*low_data[:, 3] + c3*low_data[:, 2])/1.69

our_solar = (b0 + b1*logg + b2*Fe_H + b3*logT)/1.884
b12_solar = (c0 + c1*logg + c2*logT + c3*Fe_H)/1.69


I am working on the file "Projects/mlt_calib/resampling_tests.ipynb".
The first task is:
Now, let's compare reults for the mixing lenght parameter from Run 5 (float Y) and Run 8 (Z-scaled Y).
Can you write Python code for it?