Muennighoff/xP3x-sample huggingface dataset:
inputs: "<img src="image/Mean_Variance_Image.png" style="height: 75%;width: 75%; position: relative; right: 5%">
Problem 1
The first problem involves normalizing the features for your training and test data.
Implement Min-Max scaling in the normalize_grayscale() function to a range of a=0.1 and b=0.9. After scaling, the values of the pixels in the input data should range from 0.1 to 0.9.
Since the raw notMNIST image data is in grayscale, the current values range from a min of 0 to a max of 255.
Min-Max Scaling:
$
X'=a+{\frac {\left(X-X_{\min }\right)\left(b-a\right)}{X_{\max }-X_{\min }}}
$
If you're having trouble solving problem 1, you can view the solution here."
Please write code following the instructions in jupyter notebook style.

targets: 
# Problem 1 - Implement Min-Max scaling for grayscale image data
def normalize_grayscale(image_data):
    """
    Normalize the image data with Min-Max scaling to a range of [0.1, 0.9]
    :param image_data: The image data to be normalized
    :return: Normalized image data
    """
    # TODO: Implement Min-Max scaling for grayscale image data
    a, b = 0.1, 0.9
    min_value, max_value = 0, 255
    
    return a + ((image_data - min_value) * (b - a)) / (max_value - min_value)

### DON'T MODIFY ANYTHING BELOW ###
# Test Cases
np.testing.assert_array_almost_equal(
    normalize_grayscale(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 255])),
    [0.1, 0.103137254902, 0.106274509804, 0.109411764706, 0.112549019608, 0.11568627451, 0.118823529412, 0.121960784314,
     0.125098039216, 0.128235294118, 0.13137254902, 0.9],
    decimal=3)
np.testing.assert_array_almost_equal(
    normalize_grayscale(np.array([0, 1, 10, 20, 30, 40, 233, 244, 254,255])),
    [0.1, 0.103137254902, 0.13137254902, 0.162745098039, 0.194117647059, 0.225490196078, 0.830980392157, 0.865490196078,
     0.896862745098, 0.9])

if not is_features_normal:
    train_features = normalize_grayscale(train_features)
    test_features = normalize_grayscale(test_features)
    is_features_normal = True

print('Tests Passed!')

if not is_labels_encod:
    # Turn labels into numbers and apply One-Hot Encoding
    encoder = LabelBinarizer()
    encoder.fit(train_labels)
    train_labels = encoder.transform(train_labels)
    test_labels = encoder.transform(test_labels)

    # Change to float32, so it can be multiplied against the features in TensorFlow, which are float32
    train_labels = train_labels.astype(np.float32)
    test_labels = test_labels.astype(np.float32)
    is_labels_encod = True

print('Labels One-Hot Encoded')

assert is_features_normal, 'You skipped the step to normalize the features'
assert is_labels_encod, 'You skipped the step to One-Hot Encode the labels'

# Get randomized datasets for training and validation
train_features,...
inputs: I am working on the file "Lesson14/Twitter assignment.ipynb".
The first task is:
Twitter authentication (fill this!)
Can you write Python code for it?

targets: 
APP_KEY = ""
APP_SECRET = ""
OAUTH_TOKEN = ""
OAUTH_TOKEN_SECRET = ""
inputs: %time model.train(train_corpus)
Given the above code, generate some markdown instructions for it.

targets: 
Essentially, the vocabulary is a dictionary (accessible via model.wv.vocab) of all of the unique words extracted from the training corpus along with the count (e.g., model.wv.vocab['penalty'].count for counts for the word penalty).
Time to Train
If the BLAS library is being used, this should take no more than 3 seconds.
If the BLAS library is not being used, this should take no more than 2 minutes, so use BLAS if you value your time.
inputs: Compute the invariant mass
This computes the 4-vectors sum for the 4-lepton system
using formulas from special relativity.
See also http://edu.itp.phys.ethz.ch/hs10/ppp1/2010_11_02.pdf
and https://en.wikipedia.org/wiki/Invariant_mass

targets: # This computes the 4-vectors sum for the 4-lepton system

df_4lep = df_lep_filtered.selectExpr(
"lep.lep_pt[0] * cos(lep.lep_phi[0]) + lep.lep_pt[1] * cos(lep.lep_phi[1]) + lep.lep_pt[2] * cos(lep.lep_phi[2]) + lep.lep_pt[3] * cos(lep.lep_phi[3]) as Px",
"lep.lep_pt[0] * sin(lep.lep_phi[0]) + lep.lep_pt[1] * sin(lep.lep_phi[1]) + lep.lep_pt[2] * sin(lep.lep_phi[2]) + lep.lep_pt[3] * sin(lep.lep_phi[3]) as Py",
"lep.lep_pt[0] * sinh(lep.lep_eta[0]) + lep.lep_pt[1] * sinh(lep.lep_eta[1]) + lep.lep_pt[2] * sinh(lep.lep_eta[2]) + lep.lep_pt[3] * sinh(lep.lep_eta[3]) as Pz",
"lep.lep_E[0] + lep.lep_E[1] + lep.lep_E[2] + lep.lep_E[3] as E"  
)

df_4lep.show(5)

df_4lep_invmass = df_4lep.selectExpr("sqrt(E * E - ( Px * Px + Py * Py + Pz * Pz))/1e3 as invmass_GeV")

df_4lep_invmass.show(5)

# This defines the DataFrame transformation to compute the histogram of invariant mass
# The result is a histogram with (energy) bin values and event counts foreach bin

# Requires sparkhistogram
# See https://github.com/LucaCanali/Miscellaneous/blob/master/Spark_Notes/Spark_DataFrame_Histograms.md 
from sparkhistogram import computeHistogram

# histogram parameters
min_val = 80
max_val = 250
num_bins = (max_val - min_val) / 5.0

# use the helper function computeHistogram in the package sparkhistogram
histogram_data = computeHistogram(df_4lep_invmass, "invmass_GeV", min_val, max_val, num_bins) 


# The action toPandas() here triggers the computation.
# Histogram data is fetched into the driver as a Pandas Dataframe.

%time histogram_data_pandas=histogram_data.toPandas()


import numpy as np

# Computes statistical error on the data (histogram)
histogram_data_stat_errors = np.sqrt(histogram_data_pandas)

# This plots the data histogram with error bars

import matplotlib.pyplot as plt 

plt.style.use('seaborn-darkgrid')
plt.rcParams.update({'font.size': 20, 'figure.figsize': [14,10]})

f, ax = plt.subplots()

x = histogram_data_pandas["value"]
y = histogram_data_pandas["count"]
err = histogram_data_stat_errors["count"]

# scatter...
inputs: # Show the plot.
pl.plot(setosa_petal_length, m * setosa_petal_length + c, 'b-')
pl.plot(setosa_petal_length, setosa_petal_width, 'k.')
pl.xlabel('setosa_petal_length')
pl.ylabel('setosa_petal_width')
pl.show()
Given the above code, generate some markdown instructions for it.

targets: 
Compare to values of $m$ & $c$ we got earlier:
m is 0.189262 and c is -0.033080.
(almost equivalent)
inputs: <b>You can check the status of the job and other information on <a href="https://console.cloud.google.com/mlengine/jobs">GCP CloudML page</a> </b>
<b> 2h. View the prediction (output) generated by the inference job </b>

targets: %%bash
gsutil cat ${INFERENCE_PATH}/prediction.results-00000-of-00001
inputs: "<span style="color:red">Assignment (2 min):</span> In the cell below add the code to knockout the gene and see what happens. Does the result make sense?"
Please write code following the instructions in jupyter notebook style.

targets: 
### KNOCKOUT THE GENE HERE

fbasol = pfba(model)
fvasol = cobra.flux_analysis.flux_variability_analysis(model,reaction_list=rxnsOfInterest,fraction_of_optimum=0,remove_cycles=True)
fvasol

b = show_map(fbasol,map_loc)
b.display_in_notebook()
inputs: "14.3. Aerosol Species
Is Required: FALSE&nbsp;&nbsp;&nbsp;&nbsp;Type: ENUM&nbsp;&nbsp;&nbsp;&nbsp;Cardinality: 0.N
Aerosol species included in the stratospheric heterogeneous chemistry scheme."
Please write code following the instructions in jupyter notebook style.

targets: 
# PROPERTY ID - DO NOT EDIT !  
DOC.set_id('cmip6.atmoschem.stratospheric_heterogeneous_chemistry.aerosol_species')  

# PROPERTY VALUE(S): 
# Set as follows: DOC.set_value("value")  
# Valid Choices: 
#      "Sulphate"  
#      "Polar stratospheric ice"  
#      "NAT (Nitric acid trihydrate)"  
#      "NAD (Nitric acid dihydrate)"  
#      "STS (supercooled ternary solution aerosol particule))"  
DOC.set_value("NAT (Nitric acid trihydrate)")  
DOC.set_value("Polar stratospheric ice")
inputs: "Clean up, Clean up...
Use of low level segmentation algorithms such as region growing is often followed by a clean up step. In this step we fill holes and remove small connected components. Both of these operations are achieved by using binary morphological operations, opening (BinaryMorphologicalOpening) to remove small connected components and closing (BinaryMorphologicalClosing) to fill holes.
SimpleITK supports several shapes for the structuring elements (kernels) including:
* sitkAnnulus
* sitkBall
* sitkBox
* sitkCross
The size of the kernel can be specified as a scalar (same for all dimensions) or as a vector of values, size per dimension.
The following code cell illustrates the results of such a clean up, using closing to remove holes in the original segmentation."
Please write code following the instructions in jupyter notebook style.

targets: 
vectorRadius = (1, 1, 1)
kernel = sitk.sitkBall
seg_implicit_thresholds_clean = sitk.BinaryMorphologicalClosing(
    seg_implicit_thresholds, vectorRadius, kernel
)
inputs: % ll dadiExercises/

% cat dadiExercises/ERY.FOLDED.sfs.dadi_format
Given the above code, generate some markdown instructions for it.

targets: 
Load data
inputs: I am working on the file "01-Container.ipynb".
The first task is:
some functions about array
Can you write Python code for it?

targets: 
a.ndim      # rank of an array

a.shape       # dimension or shape of an array (result is a tuple)

a.size       # total number of items

a.dtype       # type of item (rq. array里面所有item都是同样的类型)
inputs: # Colors for the bar plot
COLORS = ['orange', 'green', 'orange', 'cyan', 'cyan', 'blue', 'silver', 'orange', 'red', 'green']

# Plotting market_cap_usd as before but adding the colors and scaling the y-axis  
ax = cap10.plot.bar(x='id',y='market_cap_perc',title=TOP_CAP_TITLE, color=COLORS, log=True)

# Annotating the y axis with 'USD'
ax.set_ylabel("USD")

# Final touch! Removing the xlabel as it is not very informative
ax.set_xlabel('')
Given the above code, generate some markdown instructions for it.

targets: 
4. Making the plot easier to read and more informative
<p>While the plot above is informative enough, it can be improved. Bitcoin is too big, and the other coins are hard to distinguish because of this. Instead of the percentage, let's use a log<sup>10</sup> scale of the "raw" capitalization. Plus, let's use color to group similar coins and make the plot more informative<sup>1</sup>. </p>
<p>For the colors rationale: bitcoin-cash and bitcoin-gold are forks of the bitcoin <a href="https://en.wikipedia.org/wiki/Blockchain">blockchain</a><sup>2</sup>. Ethereum and Cardano both offer Turing Complete <a href="https://en.wikipedia.org/wiki/Smart_contract">smart contracts</a>. Iota and Ripple are not minable. Dash, Litecoin, and Monero get their own color.</p>
<p><sup>1</sup> <em>This coloring is a simplification. There are more differences and similarities that are not being represented here.</em></p>
<p><sup>2</sup> <em>The bitcoin forks are actually <strong>very</strong> different, but it is out of scope to talk about them here. Please see the warning above and do your own research.</em></p>
inputs: I am working on the file "05_2D_acoustic_FD_modelling/lecture_notebooks/4_fdac2d_absorbing_boundary.ipynb".
The first task is:
Homogeneous block model without absorbing boundary frame
As a reference, we first model the homogeneous block model, defined in the function model, without an absorbing boundary frame:
Can you write Python code for it?

targets: 
# Homogeneous model
def model(nx,nz,vp,dx,dz):
    
    vp += vp0 
    
    return vp
inputs: #coordinate transformation
origin_new_x=0            # A point in Fig 1
origin_new_y=0            

X_local=X-origin_new_x
Y_local=Y-origin_new_y
Given the above code, generate some markdown instructions for it.

targets: 
Line segment source integration
Line source solution for pressure and velocity (Datta-Gupta, 2007)
\begin{equation}
P(x,y)=B{{Q}{w}}=-\frac{70.60\mu }{h\sqrt{{{k}{x}}{{k}{y}}}}\ln \left{ {{(x-{{x}{w}})}^{2}}+\frac{{{k}{x}}}{{{k}{y}}}{{(y-{{y}{w}})}^{2}} \right}{{Q}{w}}+{{P}_{avg}}
\end{equation}
\begin{equation}
\frac{\partial P}{\partial x}=u=\frac{0.8936}{h\phi }\sqrt{\frac{{{k}{x}}}{{{k}{y}}}}\sum\limits_{k=1}^{{{N}{w}}}{{{Q}{k}}}\frac{x-{{x}{k}}}{{{\left( x-{{x}{k}} \right)}^{2}}+\frac{{{k}{x}}}{{{k}{y}}}{{(y-{{y}_{k}})}^{2}}}
\end{equation}
\begin{equation}
\frac{\partial P}{\partial y}=v=\frac{0.8936}{h\phi }\sqrt{\frac{{{k}{x}}}{{{k}{y}}}}\sum\limits_{k=1}^{{{N}{w}}}{{{Q}{k}}}\frac{y-{{y}{k}}}{{{\left( x-{{x}{k}} \right)}^{2}}+\frac{{{k}{x}}}{{{k}{y}}}{{(y-{{y}_{k}})}^{2}}}
\end{equation}
Local coordinate transformation
<img src="./resources/PanelLocal.png" width="300">
<center>Figure 1. Nomenclature of the boundary element in the local coordinates</center>
Based on the nomenclatures in Fig. 1, a boundary element could be determined as its start point (A), end point (B), length (lj) and inclination (αj). The coordinate transformation from global system to local system can be given as follows:
\begin{matrix}
   {x}'=x-{{x}{A}} & {y}'=y-{{y}{A}}  \
\end{matrix}
Where (xA,yA) is the location of start point (point A in Fig. A-1) of a BE in global coordinate system. Trigonometric sine and cosine function for a BE could be determined from start point (A) and end point (B) of the boundary elements, which can be expressed as follows:
\begin{matrix}
   \sin {{\alpha }{j}}=\frac{{{y}{B}}-{{y}{A}}}{{{l}{j}}} & \cos {{\alpha }{j}}=\frac{{{x}{B}}-{{x}{A}}}{{{l}{j}}}  \
\end{matrix}
In local coordinate system, line source are placed along the boundary element at a point (x’j,y’j). The location can be easily given as follows:
\begin{equation}
\begin{matrix}
   \begin{matrix}
   {{{{x}'}}{j}}=t\cos {{\alpha }{j}} & {{{{y}'}}{j}}=t\sin {{\alpha }{j}}  \
\end{matrix} & 0\le t  \
\end{matrix}\le {{l}_{j}}
\end{equation}
inputs: # Below, we create our smarter index (based on a subset of the documents for demonstration purposes)
smarter_index = defaultdict(set)

# Here we define the subset (somewhat arbitrary):
subset_of_ids = set(key for key in Abstracts.keys() if 24100000 <= key < 24200000)
subset_of_abstracts = ((key, Abstracts[key]) for key in subset_of_ids)

# Uncomment this line to process the whole corpus (might take a long time):
#subset_of_abstracts = Abstracts.items()

# Building our smarter index:
for (id, abstract) in subset_of_abstracts:
    for term in smarter_preprocess(smarter_tokenize(abstract)):
        smarter_index[term].add(id)
Given the above code, generate some markdown instructions for it.

targets: 
Now we can make a smarter index based on these functions. For practical purposes, the code below generates the smarter index on a subset of the data, as generating an index with stemming on the entire set would take too much time. (You don't need to change or add anything here.)
inputs: Also, we can write the game out in the XML format used by Game Theory Explorer:

targets: print g.write('gte')
inputs: (2) _config.yml

targets: %%writefile _config.yml
name: Ruxi
markdown:

Apprently karmdown is the only flavor of
inputs: ### Type your commands in this cell and then run using SHIFT-ENTER.
Given the above code, generate some markdown instructions for it.

targets: 
2) Simulate 5 sequences of 3 coin flips each.
inputs: I am working on the file "Mention/Mention_example.ipynb".
The first task is:
A scatterplot is used to get how an article is positioned according to these two variables. The plot shows:
* IT: when the mentions are equal to 1 the number of page views is spread between 0 and ~20k. Where the number of mentions increases the number of page visualizations belongs to a smaller range. 
* PT: also for Portuguese article the same is observed.
Can you write Python code for it?

targets: 
# def scatter_plot(df_it_mension_pageview, df_pt_mension_pageview, 'Number of mentions', 'Pageviews', 'Italian', 'Portuguese')
tls.embed('https://plot.ly/~crimenghini/36')
inputs: # Macro average, with min mae.

calculate_mape_apply_fn_args = {
    'average_type': 'macro', 
    'expected_num_locations': 47,
    'min_mae': 10.0,
    'min_count': None,
    'value_type': 'daily',
}

# Prospective.
jp_prosp_confirmed_xs, jp_prosp_confirmed_ys = calculate_mape(
    jp_prosp_confirmed_df, calculate_mape_apply_fn_args)
jp_prosp_deaths_xs, jp_prosp_deaths_ys = calculate_mape(
    jp_prosp_deaths_df, calculate_mape_apply_fn_args)

plt.figure(figsize=(16, 6))
# Plot prospective.
plt.plot(jp_prosp_confirmed_xs, jp_prosp_confirmed_ys, label='confirmed, prospective', 
         marker='o', color='b')
plt.plot(jp_prosp_deaths_xs, jp_prosp_deaths_ys, label='deaths, prospective', 
          marker='o', color='g')

# Formatting.
plt.grid()
plt.legend()
plt.xlabel('train date')
plt.ylabel('MAPE')
plt.title(f'MAPE vs training date (>= {calculate_mape_apply_fn_args["min_mae"]} MAE)')
plt.show()

print_mape_stats(
    avg_type='Macro average, with mae threshold', 
    locale='Japan', 
    prosp_confirmed_ys=jp_prosp_confirmed_ys, 
    prosp_deaths_ys=jp_prosp_deaths_ys)
Given the above code, generate some markdown instructions for it.

targets: 
Plot macro average, with min mae.
inputs: And, a convenience method for constructing Scheme-like lists of multiple items:

targets: List(1, 2, 3, 4)
inputs: # set the mutation rate
ga.mutationRate = 0.01 # this means 1% of the elements will get it
Given the above code, generate some markdown instructions for it.

targets: 
Large values of $\tau$ will cause elements with low fitness to be almost never selected. This will make the GA "converge" faster, halting evolution.
Small values of $\tau$ will eventually give equal chance to all elements regardless of their fitness. This will slow down the evolution.
You will have to find a good compromise!
Mutating elements
Occasionally, mutation can appear when mixing two elements. There are several ways to implement mutations, but here we stick to simplicity. A mutation adds a random value to one random gene of the element. <br>
Mutations should be rare or they will mess up the evolution!
inputs: Conclusion
As can be seen from the visualization and also from the dataframe table above - 1st Class passengers had highest rate of survival, then 2nd class passengers, and the least survival rates was of 3rd class passengers. A large number of passengers were travelling in 3rd class (491), but only 24.24% survived.
Question 2 - Which gender had more survival?

targets: ## CALCULATE SURVIVED AND TOTAL BY SEX

# groupby Sex
group_by_sex = titanic_df.groupby('Sex')

# calculate survived by sex
survived_by_sex = group_by_sex['Survived'].sum()
survived_by_sex.name = 'Survived'
display(survived_by_sex)

# calculate total by sex
total_by_sex = group_by_sex['Survived'].size()
total_by_sex.name = 'Total'
display(total_by_sex)

# concat the separate results into one dataframe
survived_total_by_sex = pd.concat([survived_by_sex, total_by_sex], axis=1)
survived_total_by_sex

percent_survived = (survived_total_by_sex['Survived'] / survived_total_by_sex['Total']) * 100
survived_total_by_sex['Percentage'] = percent_survived

survived_total_by_sex
inputs: "2.4 Answer
That eigenvector results in all derivatives being $0$, meaning the concentrations do not change"
Please write code following the instructions in jupyter notebook style.

targets: 
print(c_mat.dot(e_v[:,2]))
inputs: "Classifier #1 Perceptron"
Please write code following the instructions in jupyter notebook style.

targets: 
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score

# Training
ppn = Perceptron(n_iter=800, eta0=0.1, random_state=0)
ppn.fit(X_train, y_train)
# Testing
y_pred = ppn.predict(X_test)
# Results
print('Misclassified samples: %d out of %d' % ((y_test != y_pred).sum(), y_test.shape[0]))
print('Accuracy: %.3f' % accuracy_score(y_test, y_pred))
inputs: %%javascript
require(["widgets/js/widget", "widgets/js/manager"], function(widget, manager){
    
});
Given the above code, generate some markdown instructions for it.

targets: 
sync=True traitlets
Traitlets is an IPython library for defining type-safe properties on configurable objects.  For this tutorial you do not need to worry about the configurable piece of the traitlets machinery.   The sync=True keyword argument tells the widget framework to handle synchronizing that value to the front-end.  Without sync=True, the front-end would have no knowledge of _view_name.
Other traitlet types
Unicode, used for _view_name, is not the only Traitlet type, there are many more some of which are listed below:  

Any
Bool
Bytes
CBool
CBytes
CComplex
CFloat
CInt
CLong
CRegExp
CUnicode
CaselessStrEnum
Complex
Dict
DottedObjectName
Enum
Float
FunctionType
Instance
InstanceType
Int
List
Long
Set
TCPAddress
Tuple
Type
Unicode
Union

Not all of these traitlets can be synchronized across the network, only the JSON-able traits and Widget instances will be synchronized.
Front-end (JavaScript)
Models and views
The IPython widget framework front-end relies heavily on Backbone.js.  Backbone.js is an MVC (model view controller) framework.  Widgets defined in the back-end are automatically synchronized with generic Backbone.js models in the front-end.  The traitlets are added to the front-end instance automatically on first state push.  The _view_name trait that you defined earlier is used by the widget framework to create the corresponding Backbone.js view and link that view to the model.
Import the WidgetManager
You first need to import the widget and manager modules.  You will use the manager later to register your view by name (the same name you used in the back-end).  To import the modules, use the require method of require.js (as seen below).
inputs: I am working on the file "12. 추정 및 검정/06. MLE 모수 추정.ipynb".
The first task is:
다변수 정규 분포의 모수 추정
MLE for Multivariate Gaussian Normal Distribution

각각의 시도 $x_i$에 대한 확률은 다변수 정규 분포

$$ p(x ; \theta ) = N(x ; \mu, \Sigma) = \dfrac{1}{(2\pi)^{D/2} |\Sigma|^{1/2}} \exp \left( -\dfrac{1}{2} (x-\mu)^T \Sigma^{-1} (x-\mu) \right) $$


$N$개의 독립 샘플 $x_{1:N}$ 이 있는 경우, 
$$ L(\theta;x_{1:N}) = p(x_{1:N};\theta) = \prod_{i=1}^N  \dfrac{1}{(2\pi)^{D/2} |\Sigma|^{1/2}} \exp \left( -\dfrac{1}{2} (x_i-\mu)^T \Sigma^{-1} (x_i-\mu) \right)$$


Log-Likelihood
$$ 
\begin{eqnarray}
\log L 
&=& \log P(x_{1:N};\theta)  \
&=& \sum_{i=1}^N \left{ -\log((2\pi)^{D/2} |\Sigma|^{1/2}) - \dfrac{1}{2} (x-\mu)^T \Sigma^{-1} (x-\mu) \right} \
&=& C -\dfrac{N}{2} \log|\Sigma| - \dfrac{1}{2} \sum (x-\mu)^T \Sigma^{-1} (x-\mu) 
\end{eqnarray}
$$


precision matrix $\Lambda = \Sigma^{-1}$


$$ 
\begin{eqnarray}
\log L 
&=& C + \dfrac{N}{2} \log|\Lambda| - \dfrac{1}{2} \sum(x-\mu)^T \Lambda (x-\mu) 
\end{eqnarray}
$$
$$ \dfrac{\partial L}{\partial \mu} = -  \dfrac{\partial}{\partial \mu}  \sum_{i=1}^N (x_i-\mu)^T \Lambda (x_i-\mu) =  \sum_{i=1}^N 2\Lambda (x_i - \mu) = 0 $$
$$ \mu = \dfrac{1}{N}\sum_{i=1}^N x_i $$
$$ \dfrac{\partial L}{\partial \Lambda} = \dfrac{\partial}{\partial \Lambda} \dfrac{N}{2} \log|\Lambda| - \dfrac{\partial}{\partial \Lambda}  \dfrac{1}{2} \sum_{i=1}^N \text{tr}( (x_i-\mu)(x_i-\mu)^T\Lambda) =0  $$
$$ \dfrac{N}{2} \Lambda^{-T} = \dfrac{1}{2}\sum_{i=1}^N (x_i-\mu)(x_i-\mu)^T $$ 
$$ \Sigma = \dfrac{1}{N}\sum_{i=1}^N (x_i-\mu)(x_i-\mu)^T $$
Can you write Python code for it?

targets: 
np.random.seed(0)
mu0 = np.array([0, 1])
sigma0 = np.array([[1, 0.2], [0.2, 4]])
x = sp.stats.multivariate_normal(mu0, sigma0).rvs(1000)
xbar = x.mean(axis=0)
S2 = np.cov(x, rowvar=0)
print(xbar)
print(S2)
inputs: "$\Delta^{MTE}$ is constant in the absence of essential heterogeneity whereas decreasing respectively increasing values indicate the presence of essential heterogeneity. Why?


Additionally whether $\Delta^{MTE}$ is increasing or decreasing along $u_D$ provides information about which kind of selection on gains take place


In addition it is also possible to derive all conventional effects from $\Delta^{MTE}$ by applying effect specific weights along the distribution of $V$:


\begin{align}
\omega^{ATE}(x,u_D) &= 1.0\
\omega^{TT}(x,u_D) &= \frac{\int_{u_D}^{1}f(p|X=x)dp}{E[P|X=x]}\
\omega^{TUT}(x,u_D) &= \frac{\int_{0}^{u_D}f(p|X=x)dp}{E[1-P|X=x]}\
\end{align}
Formally:
\begin{align}
\Delta^j = \int_0^1 \omega_j(x,u_D) \Delta^{MTE}(x,u_D)du_D\
\text{ for } j = \text{ ATE, TT, TUT}
\end{align}"
Please write code following the instructions in jupyter notebook style.

targets: 
# assign parameter and choice covariates
gamma = spec_eh['CHOICE']['params']
Z = df_eh[spec_eh['CHOICE']['order']]

# compute average propensity score as well as individual propensity score
propensity_mean = np.mean(norm.cdf(np.dot(gamma, Z.T)))
auxiliary = norm.cdf(np.dot(gamma, Z.T))

# compute the integral
integral_TT = np.array([sum(auxiliary > quantiles[i])/ Z.shape[0]  for i in range(len(quantiles))]) 
integral_TUT = np.array( [1 - (sum(auxiliary > quantiles[i])/ Z.shape[0])  for i in range(len(quantiles))]) 

# compute the weights
omega_TT = integral_TT / propensity_mean
omega_TUT = integral_TUT / ( 1- propensity_mean)
omega_ATE = [1.0] * len(quantiles)


# plot
ax1 = plt.figure(figsize=(15, 10)).add_subplot(111)

plt.ylabel(r"$\Delta^{MTE}$", fontsize=24)
plt.xlabel("$u_D$", fontsize=24)
ax1.plot(quantiles, mte, color='blue', label=' $\Delta^{MTE}$', linewidth=3.0)
ax2 = ax1.twinx()


ax2.plot(quantiles, omega_TT, color='red', linestyle='--', label=' $\omega^{TT}$', linewidth=3.0)

ax2.plot(quantiles, omega_TUT, color='green', linestyle='--', label=' $\omega^{TUT}$', linewidth=3.0)

ax2.plot(quantiles, omega_ATE, color='orange', linestyle= '-.', label=' $\omega^{ATE}$', linewidth=3.0)

plt.legend(prop={"size": 20})

string = "      True Values  Weighted Values\n\nATE:{0:>10.5f}{3:>15.5f}\nTT:{1:>11.5f}{4:>15.5f}\nTUT:{2:>10.5f}{5:>15.5f}"

# Apply weights to the mte
ATE_w = np.mean(np.multiply(np.array(mte), omega_ATE))
TT_w = np.mean(np.multiply(np.array(mte), omega_TT))
TUT_w = np.mean(np.multiply(np.array(mte), omega_TUT))
effects = [ATE, TT, TUT, ATE_w, TT_w, TUT_w]

# print comparison to the true values
print(string.format(*effects))

print('\n\n\nNaive comparison: {:.5f}'.format(effect_estimate))
inputs: I am working on the file "2.3/tutorials/fti.ipynb".
The first task is:
As always, let's do imports and initialize a logger and a new bundle.
Can you write Python code for it?

targets: 
import phoebe
from phoebe import u # units
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary()

b.add_dataset('lc', times=np.linspace(0,1,101), dataset='lc01')
inputs: "2.A.2. Automated Collection
Procedure: Scripted search of filesystem for document and media files using PowerShell
Criteria: powershell.exe executing (Get-)ChildItem
Detection Type:Telemetry(Correlated)
Query ID:F96EA21C-1EB4-4988-8F98-BD018717EE2D"
Please write code following the instructions in jupyter notebook style.

targets: 
df = spark.sql(
'''
SELECT b.ScriptBlockText
FROM apt29Host a
INNER JOIN (
  SELECT d.ParentProcessGuid, d.ProcessId, c.ScriptBlockText
  FROM apt29Host c
  INNER JOIN (
      SELECT ParentProcessGuid, ProcessGuid, ProcessId
      FROM apt29Host
      WHERE Channel = "Microsoft-Windows-Sysmon/Operational"
          AND EventID = 1
      ) d
  ON c.ExecutionProcessID = d.ProcessId
  WHERE c.Channel = "Microsoft-Windows-PowerShell/Operational"
          AND c.EventID = 4104
          AND LOWER(c.ScriptBlockText) LIKE "%childitem%"
) b
ON a.ProcessGuid = b.ParentProcessGuid
WHERE a.Channel = "Microsoft-Windows-Sysmon/Operational"
          AND a.EventID = 1
          AND LOWER(a.ParentImage) RLIKE '.*\\â€Ž|â€|â€ª|â€«|â€¬|â€|â€®.*'

'''
)
df.show(100,truncate = False, vertical = True)
inputs: "Check data exists
Verify that you previously created CSV files we'll be using for training and evaluation."
Please write code following the instructions in jupyter notebook style.

targets: 
%%bash
gsutil ls gs://${BUCKET}/babyweight/data/*000000000000.csv
inputs: #Calculate the correlation matrix
result.corr()
Given the above code, generate some markdown instructions for it.

targets: 
Question 1: Is there a relationship between GDP and Household
consumption? Is it the same across continents?
When comparing the data sets without grouping the data into continents, the correlation matrix below shows that there is no correlation between years GDP and Electricity consumption, hence there is no inflation effect on the variables. There is a strnger correlation between the standard GDP and the years due to the impact of population.
The table below highlights the positive and strong relationship between electricity consumption and GDP. Furthermore, it evidences the relationship between our standardized variables: both vary in the same direction but at a lower rate due to the impact in the variable “Population” used to standardize them. It also evidences the positive relationship between population and electricity consumption. As expected, it shows that only half of the increase is due to an increase in population leaving the other half unexplained.
inputs: And here's the cosine of the angle between the output and the ideal output

targets: ideal = sim.data[p_in1]
actual = sim.data[p_unbind_out]
ideal = ideal / np.linalg.norm(ideal, axis=1)[:,None]
actual = actual / np.linalg.norm(actual, axis=1)[:,None]
prod = ideal*actual
cos_a = np.sum(prod, axis=1)
cos_a[np.isnan(cos_a)] = 0
pylab.plot(sim.trange(), nengo.synapses.Lowpass(10.0).filt(cos_a, dt=0.001))
pylab.show()
inputs: "Bootstrap to fill all interfaces
Now we actually run the bootstrapping calculation. The full_bootstrap function requires an initial snapshot in the state, and then it will generate trajectories satisfying TIS ensemble for the given interfaces. To fill all the ensembles in the MSTIS network, we need to do this once for each initial state."
Please write code following the instructions in jupyter notebook style.

targets: 
initA = toys.Snapshot(
    coordinates=np.array([[-0.5, -0.5]]), 
    velocities=np.array([[1.0,0.0]]),
)
bootstrapA = paths.FullBootstrapping(
    transition=mstis.from_state[stateA],
    snapshot=initA,
    engine=toy_eng,
    extra_interfaces=[ms_outers.volume_for_interface_set(interfacesA)]
)
gsA = bootstrapA.run()

initB = toys.Snapshot(
    coordinates=np.array([[0.5, -0.5]]), 
    velocities=np.array([[-1.0,0.0]]),
)

bootstrapB = paths.FullBootstrapping(
    transition=mstis.from_state[stateB], 
    snapshot=initB, 
    engine=toy_eng, 
)
gsB = bootstrapB.run()

initC = toys.Snapshot(
    coordinates=np.array([[0.0, 0.4]]), 
    velocities=np.array([[0.0,-0.5]]),
)
bootstrapC = paths.FullBootstrapping(
    transition=mstis.from_state[stateC], 
    snapshot=initC, 
    engine=toy_eng, 
)
gsC = bootstrapC.run()
inputs: "10 ethnic groups with the largest overall populations (sum of best/latest estimates over all countries)"
Please write code following the instructions in jupyter notebook style.

targets: 
pd.DataFrame(sorted(ethnic_population.items(), key=lambda x:x[1], reverse=True)[:10], columns=['Ethnic_Groups', 'Population'])
inputs: Alternatively tensors can be indexed using a dictionary of the form tensor[{'dim': slice or int}].

targets: data[{'x': 0, 'y': slice(1, -1), 'vector': 'x'}]
inputs: I am working on the file "Coursera/Machine-learning-data-analysis/Course 2/Week_03/Preprocessing_LR.ipynb".
The first task is:
Видно, что тогда мы выбросим почти все данные, и такой метод решения в данном случае не сработает.
Пропущенные значения можно так же интерпретировать, для этого существует несколько способов, они различаются для категориальных и вещественных признаков.
Для вещественных признаков:
- заменить на 0 (данный признак давать вклад в предсказание для данного объекта не будет)
- заменить на среднее (каждый пропущенный признак будет давать такой же вклад, как и среднее значение признака на датасете)
Для категориальных:
- интерпретировать пропущенное значение, как ещё одну категорию (данный способ является самым естественным, так как в случае категорий у нас есть уникальная возможность не потерять информацию о наличии пропущенных значений; обратите внимание, что в случае вещественных признаков данная информация неизбежно теряется)
Задание 0. Обработка пропущенных значений.

Заполните пропущенные вещественные значения в X нулями и средними по столбцам, назовите полученные датафреймы X_real_zeros и X_real_mean соответственно. Для подсчёта средних используйте описанную ниже функцию calculate_means, которой требуется передать на вход вешественные признаки из исходного датафрейма.
Все категориальные признаки в X преобразуйте в строки, пропущенные значения требуется также преобразовать в какие-либо строки, которые не являются категориями (например, 'NA'), полученный датафрейм назовите X_cat.

Для объединения выборок здесь и далее в задании рекомендуется использовать функции
np.hstack(...)
np.vstack(...)
Can you write Python code for it?

targets: 
def calculate_means(numeric_data):
    means = np.zeros(numeric_data.shape[1])
    for j in range(numeric_data.shape[1]):
        to_sum = numeric_data.iloc[:,j]
        indices = np.nonzero(~numeric_data.iloc[:,j].isnull())[0]
        correction = np.amax(to_sum[indices])
        to_sum /= correction
        for i in indices:
            means[j] += to_sum[i]
        means[j] /= indices.size
        means[j] *= correction
    return pd.Series(means, numeric_data.columns)

means = calculate_means(X[numeric_cols])
X_real_zeros = X[numeric_cols]
X_real_mean = X[numeric_cols]

for idx, column in enumerate(numeric_cols):
    X_real_zeros[column].fillna(0, inplace=True)
    X_real_mean[column].fillna(means.iloc[idx], inplace=True)
    
X_cat = X[categorical_cols]
for column in categorical_cols:
    X_cat[column] =  X_cat[column].apply(lambda x: str(x))
    X_cat[column] = X_cat[column].fillna('NA')
X_cat.info()
inputs: # %load _solutions/visualization_01_matplotlib4.py
Given the above code, generate some markdown instructions for it.

targets: 
<div class="alert alert-success">

**EXERCISE 4**

Pandas supports different types of charts besides line plots, all available from `.plot.xxx`, e.g. `.plot.scatter`, `.plot.bar`,... Make a bar chart to compare the mean discharge in the three measurement stations L06_347, LS06_347, LS06_348. Add a y-label 'mean discharge'. To do so, prepare a Figure and Axes with Matplotlib and add the chart to the created Axes.

<details><summary>Hints</summary>

* You can either use Pandas `ylabel` parameter to set the label or add it with Matploltib `ax.set_ylabel()`
* To link an Axes object with Pandas output, pass the Axes created by `fig, ax = plt.subplots()` as parameter to the Pandas plot function.
</details>

</div>
inputs: %sql -pl values 1,2,3,4,5
Given the above code, generate some markdown instructions for it.

targets: 
And finally a line chart.
inputs: 3: unit/systems stripe-docs-test
Three terms, A,B,C and their corresponding stripe-docs of co-occurring terms

DocA {X:20, Y:30, Z:5}
DocB {X:100, Y:20}
DocC {M:5, N:20, Z:5}

targets: ############################################
# Stripes for systems test 1 (predefined)
############################################

with open("mini_stripes.txt", "w") as f:
    f.writelines([
        '"DocA"\t{"X":20, "Y":30, "Z":5}\n',
        '"DocB"\t{"X":100, "Y":20}\n',  
        '"DocC"\t{"M":5, "N":20, "Z":5, "Y":1}\n'
    ])
!cat mini_stripes    

### Major steps to consider in you

###############################################
# Make Stripes from ngrams for systems test 2
###############################################

!aws s3 rm --recursive s3://ucb261-hw5/hw5-4-stripes-mj
!python buildStripes.py -r emr mj_systems_test.txt \
    --cluster-id=j-2WHMJSLZDGOY5 \
    --output-dir=s3://ucb261-hw5/hw5-4-stripes-mj \
    --file=stopwords.txt \
    --file=mostFrequent/part-00000 \
# Output suppressed
inputs: # %load solutions/15A_ridge_grid.py
Given the above code, generate some markdown instructions for it.

targets: 
<div class="alert alert-success">
    <b>EXERCISE</b>:
     <ul>
      <li>
      Create a pipeline out of a StandardScaler and Ridge regression and apply it to the Boston housing dataset (load using ``sklearn.datasets.load_boston``). Try adding the ``sklearn.preprocessing.PolynomialFeatures`` transformer as a second preprocessing step, and grid-search the degree of the polynomials (try 1, 2 and 3).
      </li>
    </ul>
</div>
inputs: "Adding genome-wide significant line, and suggestive lines"
Please write code following the instructions in jupyter notebook style.

targets: 
manhattan(     hip_m['f3'], hip_m['f1'], hip_m['f0'].astype(str), 'Hip men',
               plot_type='single',
               chrs_plot=[str(i) for i in range(1,23)],
               chrs_names=chrs_names,
               cut = 0,
               title='Anthropometric traits',
               xlabel='chromosome',
               ylabel='-log10(p-value)',
               lines= [6, 8],
               lines_colors=['b', 'r'],
               lines_styles=['-','--'],
               lines_widths=[1,2],
               colors = colors)
plt.savefig('Manhattan_HipMen.png', dpi=300)
inputs: "$$
\frac{d\mathrm{C}}{dt}=0.01\frac{\mathrm{A}}{V}\frac{\mathrm{B}}{V}-0.3\frac{\mathrm{C}}{V}=0\
0.01\left(60-\mathrm{C}\right)^2=0.3\mathrm{C}\times V\
\mathrm{C}=30
$$
You will obtain almost the same result with ODE or Gillespie (may take longer time than ODE or Gillespie).
This is not surprising because meso module is almost same with Gillespie unless you give additional spatial parameter.
Next we will decompose run_simulation."
Please write code following the instructions in jupyter notebook style.

targets: 
from ecell4 import *

with reaction_rules():
    A + B == C | (0.01, 0.3)

m = get_model()

w = meso.MesoscopicWorld(Real3(1, 1, 1), Integer3(1, 1, 1))  # XXX: Point2
w.bind_to(m)  # XXX: Point1
w.add_molecules(Species('C'), 60)

sim = meso.MesoscopicSimulator(w)  # XXX: Point1
obs = FixedIntervalNumberObserver(0.1, ('A', 'B', 'C'))
sim.run(10, obs)

viz.plot_number_observer(obs)
inputs: Build the Neural Network
You'll build the components necessary to build a Sequence-to-Sequence model by implementing the following functions below:
- model_inputs
- process_decoder_input
- encoding_layer
- decoding_layer_train
- decoding_layer_infer
- decoding_layer
- seq2seq_model
Input
Implement the model_inputs() function to create TF Placeholders for the Neural Network. It should create the following placeholders:

Input text placeholder named "input" using the TF Placeholder name parameter with rank 2.
Targets placeholder with rank 2.
Learning rate placeholder with rank 0.
Keep probability placeholder named "keep_prob" using the TF Placeholder name parameter with rank 0.
Target sequence length placeholder named "target_sequence_length" with rank 1
Max target sequence length tensor named "max_target_len" getting its value from applying tf.reduce_max on the target_sequence_length placeholder. Rank 0.
Source sequence length placeholder named "source_sequence_length" with rank 1

Return the placeholders in the following the tuple (input, targets, learning rate, keep probability, target sequence length, max target sequence length, source sequence length)

targets: def model_inputs():
    """
    Create TF Placeholders for input, targets, learning rate, and lengths of source and target sequences.
    :return: Tuple (input, targets, learning rate, keep probability, target sequence length,
    max target sequence length, source sequence length)
    """
    # TODO: Implement Function
    inputs = tf.placeholder(tf.int32, shape=[None, None], name='input')
    targets = tf.placeholder(tf.int32, shape=[None, None], name='targets')
    learning_rate = tf.placeholder(tf.float32, name='learning_rate')
    keep_prob = tf.placeholder(tf.float32, name='keep_prob')
    target_sequence_length = tf.placeholder(tf.int32, [None], name='target_sequence_length')
    max_target_sequence_length = tf.reduce_max(target_sequence_length)
    source_sequence_length = tf.placeholder(tf.int32, [None], name='source_sequence_length')
    
    return (inputs, targets, learning_rate, keep_prob, target_sequence_length, 
            max_target_sequence_length, source_sequence_length)


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_inputs(model_inputs)
inputs: # Import skymap and some of it's basic map classes
import skymap
Given the above code, generate some markdown instructions for it.

targets: 
We start by importing several classes from the skymap module and setting a few constants that we will use in this example.
inputs: "A single-process, univariate example
First we need a process model. In this case it will be a single stochastic process,"
Please write code following the instructions in jupyter notebook style.

targets: 
process = proc.WienerProcess.create_from_cov(mean=3., cov=25.)
inputs: "And we finally have something that looks looks like the tropopause, with temperature increasing above at about the correct rate. Though the tropopause temperature is off by 15 degrees or so.
Greenhouse warming in the RCE model with ozone"
Please write code following the instructions in jupyter notebook style.

targets: 
oz_col2 = climlab.process_like( oz_col )

oz_col2.subprocess['LW'].absorptivity *= 1.2 

oz_col2.integrate_years(2.)

fig = plt.figure( figsize=(10,8) )
ax = fig.add_subplot(111)
ax.plot( Tglobal + const.tempCtoK, np.log(level/const.ps), 'b-' )
ax.plot( oz_col.Tatm, np.log( pozcol / const.ps ), 'c-' )
ax.plot( oz_col.Ts, 0, 'co', markersize=16 )
ax.plot( oz_col2.Tatm, np.log( pozcol / const.ps ), 'c--' )
ax.plot( oz_col2.Ts, 0, 'co', markersize=16 )
ax.invert_yaxis()
ax.set_xlabel('Temperature (K)', fontsize=16)
ax.set_ylabel('Pressure (hPa)', fontsize=16 )
ax.set_yticks( np.log(level/const.ps) )
ax.set_yticklabels( level.values )
ax.set_title('Temperature profiles: observed (blue), RCE with ozone (cyan)', fontsize = 18)
ax.grid()
inputs: # PROPERTY ID - DO NOT EDIT !  
DOC.set_id('cmip6.land.soil.hydrology.freezing.permafrost')  

# PROPERTY VALUE: 
# Set as follows: DOC.set_value("value")  
# TODO - please enter value(s)

Given the above code, generate some markdown instructions for it.

targets: 
12.3. Permafrost
Is Required: TRUE&nbsp;&nbsp;&nbsp;&nbsp;Type: STRING&nbsp;&nbsp;&nbsp;&nbsp;Cardinality: 1.1
Describe the treatment of permafrost, if any, within the land surface scheme
inputs: I am working on the file "Notebooks/archive/Trying geopandas to assign NERC regions.ipynb".
The first task is:
Drop plants without lat/lon data
From 2001-mid 2017, 9 facilities (out of 8,435) don't have valid lat/lon data. All of them are from 2010 or earlier. Drop these rows or the spatial join fails. The amount of generation from these plants is tiny - well under 0.05% in all years.
Can you write Python code for it?

targets: 
print(len(facility_df['plant id'].unique()), 'total plants')
print(len(facility_df.loc[facility_df['lat'].isnull(), 'plant id'].unique()),
      'plants without lat/lon')

years = facility_df.loc[facility_df['lat'].isnull(), 'year'].unique()
for year in years:
    total_gen = facility_df.loc[facility_df['year'] == year, 'generation (MWh)'].sum()
    
    # Plant ids with no 'lat' in year
    no_loc_plants = facility_df.loc[(facility_df['lat'].isnull()) & 
                                    (facility_df['year'] == year), 'plant id'].unique()
    
    no_loc_gen = facility_df.loc[(facility_df['year'] == year) &
                                 (facility_df['plant id'].isin(no_loc_plants)),
                                  'generation (MWh)'].sum()
    
    percent_dropped = no_loc_gen / total_gen * 100
    
    print('In {}, {:.3f}% of generation is from plants without lat/lon'.format(year, percent_dropped))

facility_df.dropna(inplace=True, subset=['lat', 'lon'])

facility_df.columns
inputs: I am working on the file "Exploring_Domestic_Flights.ipynb".
The first task is:
This notebook will be describing an analysis of US domestic flight data. We will first take a dive into a single year, 2008 and do some basic analysis. Then we may expand this into multiple years to try and see trends. Finally we will use a single year to build a predictive model to predict weather or not a flight will be delayed or not.
Can you write Python code for it?

targets: 
df_flights_2008 = pd.read_csv('/home/hakim/Documents/Airline_delays-/flight_data_historical/2008.csv')
print(df_flights_2008.tail())

df_flights_2008.shape

df_flights_2008.isnull().sum(axis=0)
inputs: # take the first tweet of the list
tweet = tweet_list[2]

# each tweet is a python dictionary
type(tweet)

# all the 'entries' of the dictionary
tweet.keys()
Given the above code, generate some markdown instructions for it.

targets: 
Let's look at the informations contained in a tweet
inputs: I am working on the file "17-09-17-Python-for-Financial-Analysis-and-Algorithmic-Trading/.ipynb_checkpoints/2 - Numpy Exercises-checkpoint.ipynb".
The first task is:
Use NumPy to generate a random number between 0 and 1
Can you write Python code for it?

targets: 
# CODE HERE
np.random.randn(1)
inputs: 2. Uso de Pandas para descargar datos de precios de cierre
Bajar datos en forma de función

targets: def get_historical_closes(ticker, start_date, end_date):
    p = web.DataReader(ticker, "yahoo", start_date, end_date).sort_index('major_axis')
    d = p.to_frame()['Adj Close'].reset_index()
    d.rename(columns={'minor': 'Ticker', 'Adj Close': 'Close'}, inplace=True)
    pivoted = d.pivot(index='Date', columns='Ticker')
    pivoted.columns = pivoted.columns.droplevel(0)
    return pivoted
inputs: I am working on the file "Chapter2_MorePyMC/Ch2_MorePyMC_PyMC3.ipynb".
The first task is:
Later we will see how we use this to make predictions and test the appropriateness of our models.
Example: Bayesian A/B testing
A/B testing is a statistical design pattern for determining the difference of effectiveness between two different treatments. For example, a pharmaceutical company is interested in the effectiveness of drug A vs drug B. The company will test drug A on some fraction of their trials, and drug B on the other fraction (this fraction is often 1/2, but we will relax this assumption). After performing enough trials, the in-house statisticians sift through the data to determine which drug yielded better results. 
Similarly, front-end web developers are interested in which design of their website yields more sales or some other metric of interest. They will route some fraction of visitors to site A, and the other fraction to site B, and record if the visit yielded a sale or not. The data is recorded (in real-time), and analyzed afterwards. 
Often, the post-experiment analysis is done using something called a hypothesis test like difference of means test or difference of proportions test. This involves often misunderstood quantities like a "Z-score" and even more confusing "p-values" (please don't ask). If you have taken a statistics course, you have probably been taught this technique (though not necessarily learned this technique). And if you were like me, you may have felt uncomfortable with their derivation -- good: the Bayesian approach to this problem is much more natural. 
A Simple Case
As this is a hacker book, we'll continue with the web-dev example. For the moment, we will focus on the analysis of site A only. Assume that there is some true $0 \lt p_A \lt 1$ probability that users who, upon shown site A, eventually purchase from the site. This is the true effectiveness of site A. Currently, this quantity is unknown to us. 
Suppose site A was shown to $N$ people, and $n$ people purchased from the site. One might conclude hastily that $p_A = \frac{n}{N}$. Unfortunately, the observed...
Can you write Python code for it?

targets: 
import pymc3 as pm

# The parameters are the bounds of the Uniform.
with pm.Model() as model:
    p = pm.Uniform('p', lower=0, upper=1)