I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_professor.ipynb".
The first task is:
[3] Define a function likelihood(we, Z, s, sigma_eps) that computes the likelihood of parameter vector we for a given dataset in matrix Z and vector s, assuming Gaussian noise with varianze $\sigma_\epsilon^2$. Note that this function can use the sse function defined above. Using this function, compute the likelihood of the true parameter vector in true_w.
Can you write Python code for it?

# <SOL>
# The plot: LHS is the data, RHS will be the cost function.
def likelihood(w, Z, s, sigma_eps):
    K = len(s)
    lw = 1.0 / (np.sqrt(2*np.pi)*sigma_eps)**K * np.exp(- sse(Z, s, w)/(2*sigma_eps**2))
    return lw

L_w_true = likelihood(true_w, Z, s, sigma_eps)
# </SOL>

print("The likelihood of the true parameter vector is {0}".format(L_w_true))


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_professor.ipynb".
The first task is:
[3] Generate an input matrix ${\bf X}$ (in this case, a single column) containing 20 samples with equally spaced values between 0 and 2 in the second column (method linspace from numpy can be useful for this)
Can you write Python code for it?

# <SOL>
# Parameter settings
x_min = 0
x_max = 2
n_points = 21

# Training datapoints
X = np.linspace(x_min, x_max, n_points)[:,np.newaxis]
# </SOL>


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_professor.ipynb".
The first task is:
[4] Finally, generate the output vector ${\mbox s}$ as the product $\mbox{X} \ast \mbox{true_w}$ plus Gaussian noise of pdf ${\cal N}(0,\sigma_\varepsilon^2)$ at each element.
Can you write Python code for it?

# Expand input matrix with an all-ones column
col_1 = np.ones((n_points, 1))
# Z = <FILL IN>
Z = np.hstack((col_1,X))

# Generate values of the target variable
# s = <FILL IN>
s = np.dot(Z, true_w) + sigma_eps * np.random.randn(n_points, 1)


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
1. Introduction
In this exercise the student will review several key concepts of Maximum Likelihood and Bayesian regression. To do so, we will assume the regression model
$$s = f({\bf x}) + \varepsilon$$
where $s$ is the output corresponding to input ${\bf x}$, $f({\bf x})$ is an unobservable latent function, and $\varepsilon$ is white zero-mean Gaussian noise, i.e.,
$$\varepsilon \sim {\cal N}(0,\sigma_\varepsilon^2).$$
In addition, we will assume that the latent function is linear in the parameters
$$f({\bf x}) = {\bf w}^\top {\bf z}$$
where ${\bf z} = T({\bf x})$ is a possibly non-linear transformation of the input. Along this notebook, we will explore different types of transformations.
Also, we will assume an <i>a priori</i> distribution for ${\bf w}$ given by
$${\bf w} \sim {\cal N}({\bf 0}, \sigma_p^2~{\bf I})$$
Practical considerations

Though sometimes unavoidable, it is recommended not to use explicit matrix inversion whenever possible. For instance, if an operation like ${\mathbf A}^{-1} {\mathbf b}$ must be performed, it is preferable to code it using python $\mbox{numpy.linalg.lstsq}$ function (see http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html), which provides the LS solution to the overdetermined system ${\mathbf A} {\mathbf w} = {\mathbf b}$.

Sometimes, the computation of $\log|{\mathbf A}|$ (where ${\mathbf A}$ is a positive definite matrix) can overflow available precision, producing incorrect results. A numerically more stable alternative, providing the same result is $2\sum_i \log([{\mathbf L}]{ii})$, where $\mathbf L$ is the Cholesky decomposition of $\mathbf A$ (i.e., ${\mathbf A} = {\mathbf L}^\top {\mathbf L}$), and $[{\mathbf L}]{ii}$ is the $i$th element of the diagonal of ${\mathbf L}$.

Non-degenerate covariance matrices, such as the ones in this exercise, are always positive definite. It may happen, as a consequence of chained rounding errors, that a matrix which was mathematically expected to be positive definite, turns out not to be so. This...
Can you write Python code for it?


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
2. Data generation with a linear model
During this section, we will assume affine transformation
$${\bf z} = T({\bf x}) = (1, {\bf x}^\top)^\top$$.
The <i>a priori</i> distribution of ${\bf w}$ is assumed to be
$${\bf w} \sim {\cal N}({\bf 0}, \sigma_p^2~{\bf I})$$
2.1. Synthetic data generation
First, we are going to generate synthetic data (so that we have the ground-truth model) and use them to make sure everything works correctly and our estimations are sensible.

[1] Set parameters $\sigma_p^2 = 2$ and $\sigma_{\varepsilon}^2 = 0.2$. To do so, define variables sigma_p and sigma_eps containing the respectiv standard deviations.
Can you write Python code for it?

# Parameter settings
# sigma_p = <FILL IN>
# sigma_eps = <FILL IN>


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
2.2. Data visualization

Plot the generated data. You will notice a linear behavior, but the presence of noise makes it hard to estimate precisely the original straight line that generated them (which is stored in $\mbox{true_w}$).
Can you write Python code for it?


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
3. Maximum Likelihood (ML) regression
3.1. Likelihood function

[1] Define a function predict(we, Z) that computes the linear predictions for all inputs in data matrix Z (a 2-D numpy arry), for a given parameter vector we (a 1-D numpy array). The output should be a 1-D array. Test your function with the given dataset and we = [0.4, 0.7]
Can you write Python code for it?

# <SOL>
# </SOL>

# Print predictions
print(p)


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
3.2. ML estimate

[1] Compute the ML estimate of $w_e$ given the data.
Can you write Python code for it?


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
4. ML estimation with real data. The stocks dataset.
Once our code has been tested on synthetic data, we will use it with real data.
4.1. Dataset

[1] Load data corresponding to the evolution of the stocks of 10 airline companies. This data set is an adaptation of the Stock dataset from http://www.dcc.fc.up.pt/~ltorgo/Regression/DataSets.html, which in turn was taken from the StatLib Repository, http://lib.stat.cmu.edu/
Can you write Python code for it?


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
4.2. Polynomial ML regression with a single variable
In this first part, we will work with the first component of the input only.

[1] Take the first column of Xtrain and Xtest into arrays X0train and X0test, respectively.

[2] Visualize, in a single scatter plot, the target variable (in the vertical axes) versus the input variable)

[3] Since the data have been taken from a real scenario, we do not have any true mathematical model of the process that generated the data. Thus, we will explore different models trying to take the one that fits better the training data.

Assume a polinomial model given by
  $$
  {\bf z} = T({\bf x}) = (1, x_0, x_0^2, \ldots, x_0^{g-1})^\top.
  $$
Built a method
 `    Ztrain, Ztest = T_poly(Xtrain, Xtest, g)`

that, for a given value of $g$, computes normailzed data matrices Ztrain and Ztest that result from applying the polinomial transformation to the inputs in X0train and X0test for an arbitrary value of $g$.
Note that, despite X0train and X0test where normalized, you will need to re-normalize the transformed variables.
Can you write Python code for it?


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
5.2. Sampling regression curves from the posterior
In this section we will plot the functions corresponding to different samples drawn from the posterior distribution of the weight vector.
To this end, we will first generate an input dataset of equally spaced samples. We will compute the functions at these points
Can you write Python code for it?

# Definition of the interval for representation purposes
x2_min = -1
x2_max = 3
n_points = 100   # Only two points are needed to plot a straigh line

# Build the input data matrix:
# Input values for representation of the regression curves
X2 = np.linspace(x2_min, x2_max, n_points)
col_1 = np.ones((n_points,))
X2e = np.vstack((col_1, X2)).T


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
5.3. Model assessment
[OPTIONAL. You can skip this section]
In order to verify the performance of the resulting model, compute the posterior mean and variance of each of the test outputs from the posterior over ${\bf w}$. I.e, compute ${\mathbb E}\left{s({\bf x}^\ast)\mid{\bf s}\right}$ and $\sqrt{{\mathbb V}\left{s({\bf x}^\ast)\mid{\bf s}\right}}$ for each test sample ${\bf x}^\ast$ contained in each row of Xtest. Be sure not to use the outputs Ytest at any point during this process.
Store the predictive mean and variance of all test samples in two column vectors called m_y and v_y, respectively.
Can you write Python code for it?


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
Compute now the mean square error (MSE) and the negative log-predictive density (NLPD) with the following code:
Can you write Python code for it?


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
Generate random vectors ${\bf w}_l$ with $l = 1,\dots, 50$, from the posterior density of the weights, $p({\bf w}\mid{\bf s})$, and use them to generate 50 polinomial regression functions, $f({\bf x}^\ast) = {{\bf z}^\ast}^\top {\bf w}_l$, with ${\bf x}^\ast$ between $-1.2$ and $1.2$, with step $0.1$.
Plot the line corresponding to the model with the posterior mean parameters, along with the $50$ generated straight lines and the original samples, all in the same plot. As you can check, the Bayesian model is not providing a single answer, but instead a density over them, from which we have extracted 50 options.
Can you write Python code for it?

# Drawing weights from the posterior
# First, compute the cholesky decomposition of the covariance matrix
# L = <FILL IN>

for l in range(50):
    # Generate a random sample from the posterior distribution
    # w_l = <FILL IN>

    # Compute predictions for the inputs in the data matrix
    # p_l = <FILL IN>

    # Plot prediction function
    # plt.plot(<FILL IN>, 'c:');

# Compute predictions for the inputs in the data matrix and using the true model
# p_truew = <FILL IN>

# Plot the true model
plt.plot(X2, p_truew, 'b', label='True model', linewidth=2);

# Plot the training points
plt.plot(X,s,'r.',markersize=12);

plt.xlim((x2_min,x2_max));
plt.legend(loc='best')
plt.xlabel('$x$',fontsize=14);
plt.ylabel('$s$',fontsize=14);


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
Just as an illustration, the code below generates a set of points in a two dimensional grid going from $(-\sigma_p, -\sigma_p)$ to $(\sigma_p, \sigma_p)$, computes the log-likelihood for all these points and visualize them using a 2-dimensional plot. You can see the difference between the true value of the parameter ${\bf w}$ (black) and the ML estimate (red).
Can you write Python code for it?

# First construct a grid of (theta0, theta1) parameter pairs and their
# corresponding cost function values.
N = 200    # Number of points along each dimension.
w0_grid = np.linspace(-2.5*sigma_p, 2.5*sigma_p, N)
w1_grid = np.linspace(-2.5*sigma_p, 2.5*sigma_p, N)

Lw = np.zeros((N,N))
# Fill Lw with the likelihood values
for i, w0i in enumerate(w0_grid):
    for j, w1j in enumerate(w1_grid):
        we = np.array((w0i, w1j))
        Lw[i, j] = LL(we, Z, s, sigma_eps)

WW0, WW1 = np.meshgrid(w0_grid, w1_grid, indexing='ij')
contours = plt.contour(WW0, WW1, Lw, 20)

plt.figure
plt.clabel(contours)
plt.scatter([true_w[0]]*2, [true_w[1]]*2, s=[50,10], color=['k','w'])
plt.scatter([w_ML[0]]*2, [w_ML[1]]*2, s=[50,10], color=['r','w'])
plt.xlabel('$w_0$')
plt.ylabel('$w_1$')
plt.show()


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
Plot now ${\mathbb E}\left{s({\bf x}^\ast)\mid{\bf s}\right} \pm 2 \sqrt{{\mathbb V}\left{s({\bf x}^\ast)\mid{\bf s}\right}}$ (note that the posterior means of $f({\bf x}^\ast)$ and $s({\bf x}^\ast)$ are the same, so there is no need to plot it again). Notice that $95.45\%$ of observed data lie now within the newly designated region. These new limits establish a confidence range for our predictions. See how the uncertainty grows as we move away from the interpolation region to the extrapolation areas.
Can you write Python code for it?

# Plot sample functions confidence intervals and sampling points
# Note that you can simply copy and paste most of the code used in the cell above.

# <SOL>
# </SOL>

# Compute the standad deviations for s and plot the confidence intervals
# <SOL>
# </SOL>

plt.legend(loc='best')
plt.xlabel('$x$',fontsize=14);
plt.ylabel('$s$',fontsize=14);


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
Results should be:
Can you write Python code for it?

print('MSE = {0}'.format(MSE))
print('NLPD = {0}'.format(NLPD))


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
[2] Compute the maximum likelihood, and the maximum log-likelihood.
Can you write Python code for it?

# <SOL>
# </SOL>

print('Maximum likelihood: {0}'.format(L_w_ML))
print('Maximum log-likelihood: {0}'.format(LL_w_ML))


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
[2] Define a function sse(we, Z, s) that computes the sum of squared errors (SSE) for the linear prediction with parameters we (1D numpy array), inputs Z  (2D numpy array) and targets s (1D numpy array). Using this function, compute the SSE of the true parameter vector in true_w.
Can you write Python code for it?

# <SOL>
# </SOL>

print(" The SSE is: {0}".format(SSE))


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
[2] Generate a weight vector $\mbox{true_w}$ with two elements from the <i>a priori</i> distribution of the weights. This vector determines the regression line that we want to find (i.e., the optimum unknown solution).
Can you write Python code for it?

# Data dimension:
dim_x = 2

# Generate a parameter vector taking a random sample from the prior distributions
# (the np.random module may be usefull for this purpose)
# true_w = <FILL IN>

print('The true parameter vector is:')
print(true_w)


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
[3] Define a function likelihood(we, Z, s, sigma_eps) that computes the likelihood of parameter vector we for a given dataset in matrix Z and vector s, assuming Gaussian noise with varianze $\sigma_\epsilon^2$. Note that this function can use the sse function defined above. Using this function, compute the likelihood of the true parameter vector in true_w.
Can you write Python code for it?

# <SOL>
# </SOL>

print("The likelihood of the true parameter vector is {0}".format(L_w_true))


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
[3] Define a function log_gauss_pdf(we, mean_w, iCov_w) that computes the log of the Gaussian pdf with mean mean_w and precision matrix iCov_w. Use this function to compute and compare the log of the posterior pdf value of the true coefficients, the ML estimate and the MSE estimate, given the dataset.

5.2. Hyperparameter selection
Since the values $\sigma_p$ and $\sigma_\varepsilon$ are no longer known, we have to select them in some way. To see their influence, assume $g=3$ and plot the regression function for different values of $\sigma_p$
To this end, we will adjust them using the LS solution to the regression problem:

$\sigma_p^2$ will be taken as the average of the square values of ${\hat {\bf w}}_{LS}$
$\sigma_\varepsilon^2$ will be taken as two times the average of the square of the residuals when using ${\hat {\bf w}}_{LS}$

5.1. Hyperparameter selection
Since the values $\sigma_p$ and $\sigma_\varepsilon$ are no longer known, a first rough estimation is needed (we will soon see how to estimate these values in a principled way).
To this end, we will adjust them using the LS solution to the regression problem:

$\sigma_p^2$ will be taken as the average of the square values of ${\hat {\bf w}}_{LS}$
$\sigma_\varepsilon^2$ will be taken as two times the average of the square of the residuals when using ${\hat {\bf w}}_{LS}$
Can you write Python code for it?

# w_LS, residuals, rank, s = <FILL IN>
# sigma_p = <FILL IN>
# sigma_eps = <FILL IN>

print(sigma_eps)


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
[3] Generate an input matrix ${\bf X}$ (in this case, a single column) containing 20 samples with equally spaced values between 0 and 2 in the second column (method linspace from numpy can be useful for this)
Can you write Python code for it?


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
[4] Define a function LL(we, Xe, s) that computes the log-likelihood of parameter vector we for a given dataset in matrix Z and vector s. Note that this function can use the likelihood function defined above. However, for a highe numerical precission, implemening a direct expression for the log-likelihood is recommended.

Using this function, compute the likelihood of the true parameter vector in true_w.
Can you write Python code for it?

# <SOL>
# </SOL>

print("The log-likelihood of the true parameter vector is {0}".format(LL_w_true))


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
[4] Finally, generate the output vector ${\mbox s}$ as the product $\mbox{X} \ast \mbox{true_w}$ plus Gaussian noise of pdf ${\cal N}(0,\sigma_\varepsilon^2)$ at each element.
Can you write Python code for it?

# Expand input matrix with an all-ones column
col_1 = np.ones((n_points, 1))
# Z = <FILL IN>

# Generate values of the target variable
# s = <FILL IN>


I am working on the file "R_lab1_ML_Bay_Regresion/old/Pract_regression_student.ipynb".
The first task is:
[4] Fit a polynomial model with degree $g$ for $g$ ranging from 0 to 10. Store the weights of all models in a list of weight vectors, named models, such that models[g] returns the parameters estimated for the polynomial model with degree $g$.

We will use these models in the following sections.

[5] Plot the polynomial models with degrees 1, 3 and 10, superimposed over a scatter plot of the training data (in blue) and the test data (in red).

[6] Show, in the same plot:

The log-likelihood function corresponding to each model, as a function of $g$, computed over the training set.

The log-likelihood function corresponding to each model, as a function of $g$, computed over the test set.

[7] [OPTIONAL] You may have seen the the likelihood function grows with the degree of the polynomial. However, large values of $g$ produce a strong data overfitting. For this reasong, $g$ cannot be selected with the same data used to fit the model.

This kind of parameters, like $g$ are usually called hyperparameters and need to be selected by cross validation.
Another hyperparameter is $\sigma_\epsilon^2$. Plot the log-likelihood function corresponding to the polynomial model with degree 3 for different values of $\sigma_\epsilon^2$, for the training set and the test set. What would be the optimal value of this hyperparameters according to the training set?
In any case, not that  the model coefficients do not depend on $\sigma_eps^2$. Therefore, we do not need to estimat its value for ML regression.

[8] Select the optimal value of $g$ by cross-validation. To do so, the cross validation methods provided by sklearn will simplify this task.

[9] For the selected model:

Plot the regresion function over the scater plot of the data.
Compute the log-likelihood and the SSE over the test set.

5. Bayesian regression. The stock dataset.
In this section we will keep using the first component of the data from the stock dataset, assuming the same kind of plolynomial model. We will explore the potential advantages of using a...
Can you write Python code for it?

# <SOL>
# </SOL>

mean_w, Cov_w, iCov_w = posterior_stats(Z, s, sigma_eps, sigma_p)

print('true_w = {0}'.format(true_w))
print('mean_w = {0}'.format(mean_w))
print('Cov_w = {0}'.format(Cov_w))
print('iCov_w = {0}'.format(iCov_w))


I am working on the file "R_lab2_GP/Pract_regression_professor.ipynb".
The first task is:
1. Introduction
In this exercise the student will review several key concepts of Bayesian regression and Gaussian processes.
For the purpose of this exercise, the regression model is
$${s}({\bf x}) = f({\bf x}) + \varepsilon$$
where ${s}({\bf x})$ is the output corresponding to input ${\bf x}$, $f({\bf x})$ is the unobservable latent function, and $\varepsilon$ is white zero-mean Gaussian noise, i.e., $\varepsilon \sim {\cal N}(0,\sigma_\varepsilon^2)$.
Practical considerations

Though sometimes unavoidable, it is recommended not to use explicit matrix inversion whenever possible. For instance, if an operation like ${\mathbf A}^{-1} {\mathbf b}$ must be performed, it is preferable to code it using python $\mbox{numpy.linalg.lstsq}$ function (see http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html), which provides the LS solution to the overdetermined system ${\mathbf A} {\mathbf w} = {\mathbf b}$.

Sometimes, the computation of $\log|{\mathbf A}|$ (where ${\mathbf A}$ is a positive definite matrix) can overflow available precision, producing incorrect results. A numerically more stable alternative, providing the same result is $2\sum_i \log([{\mathbf L}]{ii})$, where $\mathbf L$ is the Cholesky decomposition of $\mathbf A$ (i.e., ${\mathbf A} = {\mathbf L}^\top {\mathbf L}$), and $[{\mathbf L}]{ii}$ is the $i$th element of the diagonal of ${\mathbf L}$.

Non-degenerate covariance matrices, such as the ones in this exercise, are always positive definite. It may happen, as a consequence of chained rounding errors, that a matrix which was mathematically expected to be positive definite, turns out not to be so. This implies its Cholesky decomposition will not be available. A quick way to palliate this problem is by adding a small number (such as $10^{-6}$) to the diagonal of such matrix.

Reproducibility of computations
To guarantee the exact reproducibility of the experiments, it may be useful to start your code initializing the seed of the random numbers generator, so that you can...
Can you write Python code for it?


I am working on the file "R_lab2_GP/Pract_regression_professor.ipynb".
The first task is:
2. The stocks dataset.
Load and properly normalize data corresponding to the evolution of the stocks of 10 airline companies. This data set is an adaptation of the Stock dataset from http://www.dcc.fc.up.pt/~ltorgo/Regression/DataSets.html, which in turn was taken from the StatLib Repository, http://lib.stat.cmu.edu/
Can you write Python code for it?

# Load data from matlab file DatosLabReg.mat
# matvar = <FILL IN>
matvar = scipy.io.loadmat('DatosLabReg.mat')

# Take main variables, Xtrain, Xtest, Ytrain, Ytest from the corresponding dictionary entries in matvar:
# <SOL>
Xtrain = matvar['Xtrain']
Xtest = matvar['Xtest']
Ytrain = matvar['Ytrain']
Ytest = matvar['Ytest']
# </SOL>

# Data normalization
# <SOL>
mean_x = np.mean(Xtrain,axis=0)
std_x = np.std(Xtrain,axis=0)
Xtrain = (Xtrain - mean_x) / std_x
Xtest = (Xtest - mean_x) / std_x
# </SOL>


I am working on the file "R_lab2_GP/Pract_regression_professor.ipynb".
The first task is:
3.2. Unidimensional regression
Use now only the first company to compute the non-linear regression. Obtain the posterior
distribution of $f({\mathbf x}^\ast)$ evaluated at the test values ${\mathbf x}^\ast$, i.e, $p(f({\mathbf x}^\ast)\mid {\mathbf s})$.
This distribution is Gaussian, with mean ${\mathbb E}\left{f({\bf x}^\ast)\mid{\bf s}\right}$ and a covariance matrix $\text{Cov}\left[f({\bf x}^\ast)\mid{\bf s}\right]$. Sample 50 random vectors from the distribution and plot them vs. the values $x^\ast$, together with the test samples.
The Bayesian model does not provide a single function, but a pdf over functions, from which we extracted 50 possible functions.
Can you write Python code for it?

# <SOL>
X_1d = np.matrix(Xtrain[:,0]).T
Xt_1d = np.matrix(Xtest[:,0]).T
Xt_1d = np.sort(Xt_1d,axis=0) #We sort the vector for representational purposes

dist = spatial.distance.cdist(X_1d,X_1d,'euclidean')
dist_ss = spatial.distance.cdist(Xt_1d,Xt_1d,'euclidean')
dist_s = spatial.distance.cdist(Xt_1d,X_1d,'euclidean')

K = (sigma_0**2)*np.exp(-dist/l)
K_ss = (sigma_0**2)*np.exp(-dist_ss/l)
K_s = (sigma_0**2)*np.exp(-dist_s/l)

m_y = K_s.dot(np.linalg.inv(K + sigma_eps**2 * np.eye(K.shape[0]))).dot((Ytrain))
v_f = K_ss - K_s.dot(np.linalg.inv(K + sigma_eps**2 * np.eye(K.shape[0]))).dot(K_s.T)

L = np.linalg.cholesky(v_f+1e-10*np.eye(v_f.shape[0]))

for iter in range(50):
    f_ast = L.dot(np.random.randn(len(Xt_1d),1)) + m_y
    plt.plot(np.array(Xt_1d)[:,0],f_ast[:,0],'c:');

# Plot as well the test points
plt.plot(np.array(Xtest[:,0]),Ytest[:,0],'r.',markersize=12);
plt.plot(np.array(Xt_1d)[:,0],m_y[:,0],'b-',linewidth=3,label='Predictive mean');

plt.legend(loc='best')
plt.xlabel('x',fontsize=18);
plt.ylabel('s',fontsize=18);
# </SOL>


I am working on the file "R_lab2_GP/Pract_regression_professor.ipynb".
The first task is:
After running this code, you will have inside matrix Xtrain the evolution of (normalized) price for 9 airlines, whereas vector Ytrain will contain a single column with the price evolution of the tenth airline. The objective of the regression task is to estimate the price of the tenth airline from the prices of the other nine.
3. Non-linear regression with Gaussian Processes
3.1. Multidimensional regression
Rather than using a parametric form for $f({\mathbf x})$, in this section we will use directly the values of the latent function that we will model with a Gaussian process
$$f({\mathbf x}) \sim {\cal GP}\left(0,k_f({\mathbf x}_i,{\mathbf x}_j)\right),$$
where we are assuming a zero mean, and where we will use the Ornstein-Uhlenbeck covariance function, which is defined as:
$$k_f({\mathbf x}_i,{\mathbf x}_j) = \sigma_0^2 \exp \left( -\frac{1}{l}\|{\mathbf x}_i-{\mathbf x}_j\|\right)$$
First, we will use the following gross estimation for the hyperparameters:
Can you write Python code for it?

sigma_0 = np.std(Ytrain)
sigma_eps = sigma_0 / np.sqrt(10)
l = 8

print('sigma_0 = {0}'.format(sigma_0))
print('sigma_eps = {0}'.format(sigma_eps))


I am working on the file "R_lab2_GP/Pract_regression_professor.ipynb".
The first task is:
As we studied in a previous session, the joint distribution of the target values in the training set, ${\mathbf s}$, and the latent values corresponding to the test points, ${\mathbf f}^\ast$, is given by
$$\left[\begin{array}{c}{\bf s}\{\bf f}^\ast\end{array}\right]~\sim~{\cal N}\left({\bf 0},\left[\begin{array}{cc}{\bf K} + \sigma_\varepsilon^2 {\bf I}& {\bf K}\ast^\top \ {\bf K}\ast & {\bf K}_{\ast\ast} \end{array}\right]\right)$$
Using this model, obtain the posterior of ${\mathbf s}^\ast$ given ${\mathbf s}$. In particular, calculate the <i>a posteriori</i> predictive mean and standard deviations, ${\mathbb E}\left{s({\bf x}^\ast)\mid{\bf s}\right}$ and $\sqrt{{\mathbb V}\left{s({\bf x}^\ast)\mid{\bf s}\right}}$ for each test sample ${\bf x}^\ast$.
Obtain the MSE and NLPD.
Can you write Python code for it?

# Compute Kernel matrices.
# You may find spatial.distance.cdist() usefull to compute the euclidean distances required by Gaussian kernels.
# <SOL>
# Compute appropriate distances
dist = spatial.distance.cdist(Xtrain, Xtrain, 'euclidean')
dist_ss = spatial.distance.cdist(Xtest, Xtest, 'euclidean')
dist_s = spatial.distance.cdist(Xtest, Xtrain, 'euclidean')

# Compute Kernel matrices
K = (sigma_0**2)*np.exp(-dist/l)
K_ss = (sigma_0**2)*np.exp(-dist_ss/l)
K_s = (sigma_0**2)*np.exp(-dist_s/l)
# </SOL>

# Compute predictive mean
# m_y = <FILL IN>
m_y = K_s.dot(np.linalg.inv(K + sigma_eps**2 * np.eye(K.shape[0]))).dot((Ytrain))

# Compute predictive variance
# v_y = <FILL IN>
v_y = np.diagonal(K_ss - K_s.dot(np.linalg.inv(K + sigma_eps**2 * np.eye(K.shape[0]))).dot(K_s.T)) + sigma_eps**2

# Compute MSE
# MSE = <FILL IN>
MSE = np.mean((m_y - Ytest)**2)

# Compute NLPD
# NLPD = <FILL IN>
NLPD = 0.5 * np.mean(((Ytest - m_y)**2)/(np.matrix(v_y).T) + 0.5*np.log(2*np.pi*np.matrix(v_y).T))

print(m_y.T)


I am working on the file "R_lab2_GP/Pract_regression_student.ipynb".
The first task is:
1. Introduction
In this exercise the student will review several key concepts of Bayesian regression and Gaussian processes.
For the purpose of this exercise, the regression model is
$${s}({\bf x}) = f({\bf x}) + \varepsilon$$
where ${s}({\bf x})$ is the output corresponding to input ${\bf x}$, $f({\bf x})$ is the unobservable latent function, and $\varepsilon$ is white zero-mean Gaussian noise, i.e., $\varepsilon \sim {\cal N}(0,\sigma_\varepsilon^2)$.
Practical considerations

Though sometimes unavoidable, it is recommended not to use explicit matrix inversion whenever possible. For instance, if an operation like ${\mathbf A}^{-1} {\mathbf b}$ must be performed, it is preferable to code it using python $\mbox{numpy.linalg.lstsq}$ function (see http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html), which provides the LS solution to the overdetermined system ${\mathbf A} {\mathbf w} = {\mathbf b}$.

Sometimes, the computation of $\log|{\mathbf A}|$ (where ${\mathbf A}$ is a positive definite matrix) can overflow available precision, producing incorrect results. A numerically more stable alternative, providing the same result is $2\sum_i \log([{\mathbf L}]{ii})$, where $\mathbf L$ is the Cholesky decomposition of $\mathbf A$ (i.e., ${\mathbf A} = {\mathbf L}^\top {\mathbf L}$), and $[{\mathbf L}]{ii}$ is the $i$th element of the diagonal of ${\mathbf L}$.

Non-degenerate covariance matrices, such as the ones in this exercise, are always positive definite. It may happen, as a consequence of chained rounding errors, that a matrix which was mathematically expected to be positive definite, turns out not to be so. This implies its Cholesky decomposition will not be available. A quick way to palliate this problem is by adding a small number (such as $10^{-6}$) to the diagonal of such matrix.

Reproducibility of computations
To guarantee the exact reproducibility of the experiments, it may be useful to start your code initializing the seed of the random numbers generator, so that you can...
Can you write Python code for it?


I am working on the file "R_lab2_GP/Pract_regression_student.ipynb".
The first task is:
3.2. Unidimensional regression
Use now only the first company to compute the non-linear regression. Obtain the posterior
distribution of $f({\mathbf x}^\ast)$ evaluated at the test values ${\mathbf x}^\ast$, i.e, $p(f({\mathbf x}^\ast)\mid {\mathbf s})$.
This distribution is Gaussian, with mean ${\mathbb E}\left{f({\bf x}^\ast)\mid{\bf s}\right}$ and a covariance matrix $\text{Cov}\left[f({\bf x}^\ast)\mid{\bf s}\right]$. Sample 50 random vectors from the distribution and plot them vs. the values $x^\ast$, together with the test samples.
The Bayesian model does not provide a single function, but a pdf over functions, from which we extracted 50 possible functions.
Can you write Python code for it?


I am working on the file "R_lab2_GP/Pract_regression_student.ipynb".
The first task is:
After running this code, you will have inside matrix Xtrain the evolution of (normalized) price for 9 airlines, whereas vector Ytrain will contain a single column with the price evolution of the tenth airline. The objective of the regression task is to estimate the price of the tenth airline from the prices of the other nine.
3. Non-linear regression with Gaussian Processes
3.1. Multidimensional regression
Rather than using a parametric form for $f({\mathbf x})$, in this section we will use directly the values of the latent function that we will model with a Gaussian process
$$f({\mathbf x}) \sim {\cal GP}\left(0,k_f({\mathbf x}_i,{\mathbf x}_j)\right),$$
where we are assuming a zero mean, and where we will use the Ornstein-Uhlenbeck covariance function, which is defined as:
$$k_f({\mathbf x}_i,{\mathbf x}_j) = \sigma_0^2 \exp \left( -\frac{1}{l}\|{\mathbf x}_i-{\mathbf x}_j\|\right)$$
First, we will use the following gross estimation for the hyperparameters:
Can you write Python code for it?


I am working on the file "R_lab2_GP/Pract_regression_student.ipynb".
The first task is:
As we studied in a previous session, the joint distribution of the target values in the training set, ${\mathbf s}$, and the latent values corresponding to the test points, ${\mathbf f}^\ast$, is given by
$$\left[\begin{array}{c}{\bf s}\{\bf f}^\ast\end{array}\right]~\sim~{\cal N}\left({\bf 0},\left[\begin{array}{cc}{\bf K} + \sigma_\varepsilon^2 {\bf I}& {\bf K}\ast^\top \ {\bf K}\ast & {\bf K}_{\ast\ast} \end{array}\right]\right)$$
Using this model, obtain the posterior of ${\mathbf s}^\ast$ given ${\mathbf s}$. In particular, calculate the <i>a posteriori</i> predictive mean and standard deviations, ${\mathbb E}\left{s({\bf x}^\ast)\mid{\bf s}\right}$ and $\sqrt{{\mathbb V}\left{s({\bf x}^\ast)\mid{\bf s}\right}}$ for each test sample ${\bf x}^\ast$.
Obtain the MSE and NLPD.
Can you write Python code for it?

# Compute Kernel matrices.
# You may find spatial.distance.cdist() usefull to compute the euclidean distances required by Gaussian kernels.
# <SOL>
# </SOL>

# Compute predictive mean
# m_y = <FILL IN>

# Compute predictive variance
# v_y = <FILL IN>

# Compute MSE
# MSE = <FILL IN>

# Compute NLPD
# NLPD = <FILL IN>

print(m_y.T)


I am working on the file "R_lab2_GP/Pract_regression_student.ipynb".
The first task is:
Compute now the MSE and NLPD of the model. The correct results are given below:
Can you write Python code for it?

# <SOL>
# </SOL>

print('MSE = {0}'.format(MSE))
print('NLPD = {0}'.format(NLPD))


I am working on the file "R_lab2_GP/Pract_regression_student.ipynb".
The first task is:
Plot again the previous figure, this time including in your plot the confidence interval delimited by two standard deviations of the prediction. You can observe how $95.45\%$ of observed data fall within the designated area.
Can you write Python code for it?


I am working on the file "RaD/SCat.ipynb".
The first task is:
Before training, let's test the whole network.
Can you write Python code for it?

print(train_x[0])

test_vec = Variable(vectorizer(train_x[0]).view(1, len(tokenizer(train_x[0]))))
test_net = SCatNetwork(len(vocabulary), 20, 10)

print(test_vec)
print(str(test_net))
print()

test_net(test_vec)


I am working on the file "RaD/SCat.ipynb".
The first task is:
It's now time to write our network as described before.
Can you write Python code for it?

class SCatNetwork(nn.Module):
    def __init__(self, vocabulary_size, embedding_dimension, hidden_size):
        super(SCatNetwork, self).__init__()

        self.embeddings = nn.Embedding(vocabulary_size, embedding_dimension)
        self.encoder = nn.LSTM(  # a LSTM layer to encode features
            embedding_dimension,
            hidden_size,
            batch_first=True,
        )
        self.decoder = nn.Linear(hidden_size, 2)

    def forward(self, inputs):
        hc = (Variable(torch.ones(1, 1, 10)), Variable(torch.ones(1, 1, 10)))

        outputs = self.embeddings(inputs)
        outputs, _ = self.encoder(outputs, hc)
        outputs = self.decoder(outputs)
        return outputs


I am working on the file "RaD/SCat.ipynb".
The first task is:
Now let's test the model with this sentence : Give me the latest weather forecast in Los Angeles
Can you write Python code for it?

x = "Give me the latest weather forecast in Los Angeles"
tokens = tokenizer(x)
x_vec = Variable(vectorizer(x).view(1, len(tokens)))

pred = F.softmax(model(x_vec), dim=2)[0, :,1].data


I am working on the file "RaD/SCat.ipynb".
The first task is:
We will also need a function to transform a sentence into a list (LongTensor) of index.
Can you write Python code for it?

def vectorizer(sentence):
    tokens = tokenizer(sentence)
    vector = [vocabulary.index(token) if token in vocabulary else vocabulary.index('<unknown>') for token in tokens]

    return torch.LongTensor(vector)


I am working on the file "RaD/new_word_vectorization.ipynb".
The first task is:
Let's run the new vectorizer into a CProfiler.
Can you write Python code for it?

import cProfile

cProfile.run("""
from scipy import sparse as S
import numpy as np

vocabulary = sorted(set(test_tokens))
len_vocabulary = len(vocabulary)

voc_indices = [vocabulary.index(word) for word in test_tokens]
len_document = len(test_tokens)
cooc_matrix = S.lil_matrix((len_vocabulary, len_vocabulary))

window_size = 5

for word_pos, row_index in enumerate(voc_indices):
    window = voc_indices[max(0, word_pos - window_size) : word_pos] +\
             voc_indices[word_pos+1 : min(len_document+1, word_pos+1+window_size)]

    for col_index in window:
        cooc_matrix[row_index, col_index] += 1
""")


I am working on the file "Racial Discrimination in the US Job Market/sliderule_dsi_inferential_statistics_exercise_2.ipynb".
The first task is:
1) What test is appropriate for this problem? Does CLT apply?
we can perform a permutation test with the two samples. The Central Limit does apply.
2) What are the null and alternate hypotheses?
$H_0$: The race of a person has no bearing on whether he/she receives a callback.
$H_A$: The race of a person gives affects the callback rate.
3) Compute margin of error, confidence interval, and p-value.
Can you write Python code for it?

white = data[data['race'] == 'w']['call']
black = data[data['race'] == 'b']['call']

diff_of_means = np.abs(np.mean(white) - np.mean(black))

# assuming alpha = 0.05
SE = np.sqrt(np.std(white) ** 2 / len(white) + np.std(black) ** 2 / len(black))

margin_of_error = 1.96 * SE

confidence_interval = [diff_of_means - margin_of_error, diff_of_means + margin_of_error]

print('The margin of error is', margin_of_error)
print('The 95% confidence interval is', confidence_interval)

permutation_replicates = np.empty(100000)

for i in range(len(permutation_replicates)):
    permutation_samples = np.random.permutation(np.concatenate((white, black)))

    white_perm = permutation_samples[:len(white)]
    black_perm = permutation_samples[len(white):]

    permutation_replicates[i] = np.abs(np.mean(white_perm) - np.mean(black_perm))

p = np.sum(permutation_replicates > diff_of_means) / len(permutation_replicates)
print('p =', p)


I am working on the file "RadiationTransportCoding/NeutronDiffusion_Python/NDE_AlphaEigenvalue.ipynb".
The first task is:
Algorithm
Using Hill's (LANL) algorithm, we calculate the $\alpha$-eigenvalue as follows:

Guess $\alpha_0$ (usually $\alpha_0$ = 0)
Solve for $k_0$
Modify the initial $\alpha_0$ using some modifier ($\alpha_1 = \alpha_0 + \xi$)
Solve for $k_1$
Using the points $(k_i,\alpha_i)$ and $(k_{i-1},\alpha_{i-1})$, linearly extrapolate $\alpha_{i+1}$ such that $k_{i+1} = 0$.
Repeat until $k_i = 1$.

Problems:
1) If the system is subcritical enough
\begin{equation}
\frac{\alpha}{v} - \Sigma_{a} < 0
\end{equation}
which causes instabilities (confirm by looking at the condition number of matrix iterations). Need to add parameter to prevent this according to Warsa and Fichtl.
2) Bad guesses of $\alpha_0$ causes algorithm to fail and does not converge.
Can you write Python code for it?

#Generate flux vector

#Tolerance, k-effective and alpha initial guesses, alpha-eigenvalue modifier
tol = 1e-10
k = 1.00
alpha = 0.0
evm = 0.01

#Alpha-eigenvalue outer iteration
for j in range(1,100):

    kprev = k

    A = np.zeros((N,N))
    phi0 = np.ones((N,1))
    phi0[0] = 0
    phi0[N-1] = 0
    k = 1.0

    for i in range(N):
        A[i][i] = A[i][i] + siga + alpha

    M = L + A
    M[0][0] = 1
    M[0][1] = 0
    M[N-1][N-1] = 1
    M[N-1][N-2] = 0

    #k-effective inner iteration
    for i in range(100):

        kold = k
        psi = np.linalg.solve(M,nusigf*phi0)

        k = sum(nusigf*psi)/sum(nusigf*phi0)
        phi0 = (1/k)*psi
        phi0[0] = 0
        phi0[N-1] = 0

        residual = np.abs(k-kold)

        if residual <= tol:
            break

    #Modify alpha-eigenvalue after first iteration, or linearly extrapolate new guess such that k equals 1
    #Reminder: does not calculate both k and alpha
    if j == 2:

        alpha_prev = alpha
        alpha = alpha + evm

    elif j > 2:

        #print "alpha-alpha_prev = ", alpha-alpha_prev
        #print "alpha = ", alpha
        #print "alpha_prev = ", alpha_prev
        #print "j = ", j

        if abs(alpha - alpha_prev) < tol:

            break

        alpha_new = alpha_prev + (1-kprev)/(k-kprev)*(alpha-alpha_prev)
        alpha_prev = alpha
        alpha = alpha_new

    else:

        continue

    #if abs(k-1) < tol:
    #    print "alpha = ", alpha
    #    print "k-effective = ", k
    #    break

#print("abs(k-1) = %.20f" % abs(k-1))

print("alpha = %.15f" % alpha)
print('k-effective = %.15f' % k)

#plt.plot(x,phi0)
#plt.xlabel('Slab (cm)')
#plt.ylabel('Neutron Flux')
#plt.grid()

#print " alpha = ", (k-1)/k * sum(nusigf*phi0)/sum(phi0)


I am working on the file "RadiationTransportCoding/NeutronDiffusion_Python/NDE_AlphaEigenvalue.ipynb".
The first task is:
Generation of Leakage Matrix
Can you write Python code for it?


I am working on the file "RadiationTransportCoding/NeutronDiffusion_Python/NDE_AlphaEigenvalue.ipynb".
The first task is:
Material Properties
Can you write Python code for it?


I am working on the file "RadiationTransportCoding/NeutronDiffusion_Python/NDE_CriticalityEigenvalue.ipynb".
The first task is:
Boundary Conditions $(\phi(0) = \phi(L) = 0)$
Can you write Python code for it?


I am working on the file "RadiationTransportCoding/NeutronDiffusion_Python/NDE_CriticalityEigenvalue.ipynb".
The first task is:
Slab Geometry Width and Discretization
Can you write Python code for it?

#Lx = np.pi*((nusigf-siga)/D)**(-0.5)
Lx = 15.0

N = 50;
h = Lx/(N-1)

x = np.zeros(N)

for i in range(N-1):
    x[i+1] = x[i] + h


I am working on the file "Random Binding.ipynb".
The first task is:
And here's the cosine of the angle between the output and the ideal output
Can you write Python code for it?

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()


I am working on the file "Random Binding.ipynb".
The first task is:
First, let's make a random binding network.  We use the same structure as the circular convolution network: a hidden layer optimized to do pairwise products, and linear transforms into and out of it.  But, instead of using the DFT matrix, we randomly generate the matrices.
Can you write Python code for it?

D = 16
D_bind = 32
scaling_fudge_factor = 2.0

model = spa.Network()
model.config[nengo.Ensemble].neuron_type=nengo.LIFRate()
with model:
    in1 = spa.State(D)
    in2 = spa.State(D)
    out = spa.State(D)
    bind = nengo.networks.Product(n_neurons=50, dimensions=D_bind)

    T1 = np.random.normal(size=(D_bind, D))
    T2 = np.random.normal(size=(D_bind, D))
    T3 = np.random.normal(size=(D, D_bind))
    T1 = T1 / np.linalg.norm(T1, axis=1)[:, None]*np.sqrt(D)
    T2 = T2 / np.linalg.norm(T2, axis=1)[:, None]*np.sqrt(D)
    T3 = T3 / np.linalg.norm(T3, axis=1)[:, None]*scaling_fudge_factor/np.sqrt(D)

    nengo.Connection(in1.output, bind.input_a, transform=T1)
    nengo.Connection(in2.output, bind.input_b, transform=T2)
    nengo.Connection(bind.output, out.input, transform=T3)

    p_in1 = nengo.Probe(in1.output, synapse=0.01)
    p_in2 = nengo.Probe(in2.output, synapse=0.01)
    p_bind = nengo.Probe(bind.output, synapse=0.01)
    p_bind_in1 = nengo.Probe(bind.input_a, synapse=0.01)
    p_out = nengo.Probe(out.output, synapse=0.01)

    stim1 = nengo.Node(nengo.processes.WhiteSignal(high=0.5, period=10.0, rms=1.0/np.sqrt(D)), size_out=D)
    nengo.Connection(stim1, in1.input)
    stim2 = nengo.Node(nengo.processes.WhiteSignal(high=0.5, period=10.0, rms=1.0/np.sqrt(D)), size_out=D)
    nengo.Connection(stim2, in2.input)

sim = nengo.Simulator(model)
sim.run(10)

pylab.plot(sim.trange(), np.linalg.norm(sim.data[p_in1], axis=1), label='in1')
pylab.plot(sim.trange(), np.linalg.norm(sim.data[p_in2], axis=1), label='in2')
pylab.plot(sim.trange(), np.linalg.norm(sim.data[p_out], axis=1), label='out')
pylab.legend(loc='best')
pylab.show()


I am working on the file "Random Binding.ipynb".
The first task is:
Here are the norms of the vectors, just to make sure we're in the right ranges
Can you write Python code for it?

pylab.plot(sim.trange(), np.linalg.norm(sim.data[p_in1], axis=1), label='in1')
pylab.plot(sim.trange(), np.linalg.norm(sim.data[p_error], axis=1), label='error')
pylab.plot(sim.trange(), np.linalg.norm(sim.data[p_unbind_out], axis=1), label='unbind_out')
pylab.legend(loc='best')
pylab.xlim(450,500)
pylab.show()


I am working on the file "Random Binding.ipynb".
The first task is:
This seems to give us something like binding.  But, can we now unbind?
To do this, we build anothe binding network, feed in the bound result and one of the two inputs, and then do PES to learn the function that decodes out the other input.
Can you write Python code for it?

D = 2
D_bind = 16
scaling_fudge_factor = 2.0
T = 500.0

learning_rate = 1e-5 / D_bind

model = spa.Network()
model.config[nengo.Ensemble].neuron_type=nengo.LIFRate()
with model:
    in1 = spa.State(D, subdimensions=D)
    in2 = spa.State(D, subdimensions=D)
    out = spa.State(D, subdimensions=D)
    bind = nengo.networks.Product(n_neurons=50, dimensions=D_bind)
    unbind = nengo.networks.Product(n_neurons=50, dimensions=D_bind)
    unbind_out = nengo.Node(None, size_in=D)
    error = nengo.Node(None, size_in=D)

    T1 = np.random.normal(size=(D_bind, D))
    T2 = np.random.normal(size=(D_bind, D))
    T3 = np.random.normal(size=(D, D_bind))
    T1 = T1 / np.linalg.norm(T1, axis=1)[:, None]*np.sqrt(D)
    T2 = T2 / np.linalg.norm(T2, axis=1)[:, None]*np.sqrt(D)
    T3 = T3 / np.linalg.norm(T3, axis=1)[:, None]*scaling_fudge_factor/np.sqrt(D)

    nengo.Connection(in1.output, bind.input_a, transform=T1)
    nengo.Connection(in2.output, bind.input_b, transform=T2)
    nengo.Connection(bind.output, out.input, transform=T3)

    T4 = np.random.normal(size=(D_bind, D))
    T5 = np.random.normal(size=(D_bind, D))
    T4 = T4 / np.linalg.norm(T4, axis=1)[:, None]*np.sqrt(D)
    T5 = T5 / np.linalg.norm(T5, axis=1)[:, None]*np.sqrt(D)
    nengo.Connection(out.output, unbind.input_a, transform=T4)
    nengo.Connection(in2.output, unbind.input_b, transform=T5)
    for ens in unbind.all_ensembles:
        c = nengo.Connection(ens, unbind_out, learning_rule_type=nengo.PES(learning_rate=learning_rate),
                             function=lambda x: np.zeros(D))
        nengo.Connection(error, c.learning_rule)
    nengo.Connection(unbind_out, error)
    nengo.Connection(in1.output, error, transform=-1)

    p_in1 = nengo.Probe(in1.output, synapse=0.01)
    p_error = nengo.Probe(error, synapse=0.01)
    p_unbind_out = nengo.Probe(unbind_out, synapse=0.01)

    stim1 = nengo.Node(nengo.processes.WhiteSignal(high=0.5, period=T, rms=1.0/np.sqrt(D)), size_out=D)
    nengo.Connection(stim1,...


I am working on the file "Random Fields.ipynb".
The first task is:
It appears the two states are equivalent, which means this is a single state. This is the spacetime equivalent of a fair coin, so this is the desired result. Which makes me feel better about the local epsilon machine constructed from the lightcone equivalence relation.
*** This has been fixed by excluding the present from future light cones. This eliminates the state splitting issue in the reconstruction algorithm ***
Can you write Python code for it?

state_overlay_diagram(field, random_states.get_causal_field(), t_max = 50, x_max = 50)

for state in random_states.causal_states():
    print state.plc_configs()

for state in random_states.causal_states():
    print state.morph()

t_trans = random_states.all_transitions(zipped = False)[1]
print np.unique(t_trans)

print np.log(8)/np.log(2)

print random_states.entropy_rate('forward')

print random_states.entropy_rate('right')

print random_states.entropy_rate('left')


I am working on the file "Random Fields.ipynb".
The first task is:
Would like that the value of intrinsic randomness of this field be 1 bit in both time and space. Here we have three bits for both with past depth 1. (still need to change code to have correct value of depth)
Can you write Python code for it?

random_states = epsilon_field(random_field(600,600))
random_states.estimate_states(3,2,1)
random_states.filter_data()

t_trans = random_states.all_transitions(zipped = False)[1]
print np.unique(t_trans)

print np.log(32)/np.log(2)
print np.log(8)/np.log(2)


I am working on the file "Random Variable and its Distribution.ipynb".
The first task is:
5. 均匀分布 Uniform Distribution
若连续型随机变量 X 具有概率密度
$$ f(x) =\left{
\begin{aligned}
& \frac{1}{b-a}, & a < x < b, \
& 0, & 其它 \
\end{aligned}
\right.
$$
则称 X 在区间(a, b)上服从均匀分布，记为$X \sim U(a, b)$
numpy.random.uniform函数可以根据均匀分布进行抽样：
Can you write Python code for it?

# 取a = -1, b = 0, 样本数10000
a, b = -1, 0
s = np.random.uniform(a, b, 10000)

# 所有样本的值均大于a
np.all(s >= a)

# 所有样本的值均小于b
np.all(s < b)

# 绘制样本直方图及密度函数
count, bins, ignored = plt.hist(s, 15, normed=True)
plt.plot(bins, np.ones_like(bins) / (b - a), linewidth=2, color='r')
plt.show()


I am working on the file "Random Variable and its Distribution.ipynb".
The first task is:
6. 指数分布 Exponential Distribution
若连续型随机变量 X 具有概率密度
$$ f(x) =\left{
\begin{aligned}
& \frac{1}{\theta}e^{-\frac{x}{\theta}}, & x > 0, \
& 0, & 其它 \
\end{aligned}
\right.
$$
其中$\theta > 0$为常数，则称 X 服从参数为$\theta$的指数分布。
numpy.random.exponential函数可以根据均匀分布进行抽样：
Can you write Python code for it?

# 取theta = 1，绘制样本直方图及密度函数
theta = 1
f = lambda x: math.e ** (-x / theta) / theta

s = np.random.exponential(theta, 10000)
count, bins, ignored = plt.hist(s, 100, normed=True)
plt.plot(bins, f(bins), linewidth=2, color='r')
plt.show()


I am working on the file "Random Variable and its Distribution.ipynb".
The first task is:
7. 正态分布 Normal Distribution
若连续型随机变量 X 的概率密度为
$$ f(x) = \frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{(x-\mu)^2}{2\sigma^2}}, -\infty < x < \infty $$
其中$\mu, \sigma(\sigma > 0)$为常数，则称 X 服从参数为$\mu, \sigma$的正态分布或高斯（Gauss）分布，记为$X \sim N(\mu, \sigma^2)$。
f(x)的图形具有以下性质：

曲线关于$x = \mu$对称。这表明对于任意$h > 0$有
$$ P{\mu - h < X \leq \mu } = P{\mu < X \leq \mu + h} $$

当$x = \mu$时取到最大值
$$ f(\mu) = \frac{1}{\sqrt{2\pi}\sigma} $$
x离$\mu$越远，f(x)的值越小。这表明对于同样长度的区间，当区间离$\mu$越远，X落在这个区间上的概率越小。

在$x = \mu \pm \sigma$处曲线有拐点。曲线以Ox轴为渐近线。
如果固定$\sigma$，改变$\mu$的值，则图形沿着Ox轴平移，而不改变其形状，可见正太分布的概率密度曲线y=f(x)的位置完全由参数$\mu$所确定。$\mu$称为位置参数。
如果固定$\mu$，改变$\sigma$，由于最大值$f(\mu)=\frac{1}{\sqrt{2\pi}\sigma}$，可知当$\sigma$越小时图形变得越尖，因为 X 落在$\mu$附近的概率越大。
特别，当$\mu = 0, \sigma = 1$时称随机变量 X 服从标准正态分布。其概率密度和分布函数分别用$\varphi(x), \Phi(x)$表示，即有
$$ \varphi(x) = \frac{1}{\sqrt{2\pi}}e^{-\frac{x^2}{2}} $$
$$ \Phi(x) = \frac{1}{\sqrt{2\pi}}\int_{-\infty}^{x} e^{-\frac{x^2}{2}}dx $$
易知
$$ \Phi(-x) = 1 - \Phi(x) $$
numpy.random.normal函数可以根据正态分布进行抽样：
Can you write Python code for it?

# 取均值0，标准差0.1
mu, sigma = 0, 0.1
s = np.random.normal(mu, sigma, 1000)

# 验证均值
abs(mu - np.mean(s)) < 0.01

# 验证标准差
abs(sigma - np.std(s, ddof=1)) < 0.01

# 绘制样本直方图及密度函数
count, bins, ignored = plt.hist(s, 30, normed=True)
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (bins - mu)**2 / (2 * sigma**2) ), linewidth=2, color='r')
plt.show()


I am working on the file "Random Variable and its Distribution.ipynb".
The first task is:
一个现实生活中的例子。一家钻井公司探索九个矿井，预计每个开采成功率为0.1；九个矿井全部开采失败的概率是多少？
根据公式，$n = 9, p = 0.1, P{X = 0} = \binom{9}{0} \cdot 0.1^{0} \cdot 0.9^{9} \approx 0.3874$
我们对该模型进行20000次试验，计算其中得到0的概率：
Can you write Python code for it?


I am working on the file "Random Variable and its Distribution.ipynb".
The first task is:
将试验次数增加，可以模拟出更加逼近准确值的结果。
4. 泊松分布 Poisson Distribution
设随机变量 X 所有可能取的值为0, 1, 2, ..., 而取各个值的概率为
$$P{X=k} = \frac{\lambda^ke^{-\lambda}}{k!}, k=0,1,2,...,$$
其中 $\lambda > 0$ 是常数，则称 $X$ 服从参数为 $\lambda$ 的泊松分布，记为 $X \sim \pi(\lambda)$。
易知，$P{X=k}\geq0,k=0,1,2,...$，且有
$$ \sum_{k=0}^\infty P{X=k} = \sum_{k=0}^\infty \frac{\lambda^{k}e^{-\lambda}}{k!} = e^{-\lambda}\sum_{k=0}^\infty \frac{\lambda^k}{k!} = e^{-\lambda} \cdot e^{\lambda} = 1 $$
具有泊松分布的随机变量在实际应用中是非常多的。例如，一本书一页中的印刷错误数、某地区在一天内邮递遗失的信件数、某一医院在一天内的急诊病人数、某一地区一个时间间隔内发生交通事故的次数、在一个时间间隔内某种放射性物质发出的、经过计数器的 $\alpha$ 粒子数等都服从泊松分布。
numpy.random.poisson函数可以根据泊松分布进行抽样：
Can you write Python code for it?

lb = 5
s = np.random.poisson(lb, 10000)
count, bins, ignored = plt.hist(s, 14, normed=True)


I am working on the file "RandomForest.ipynb".
The first task is:
Data description
Below are the summary for all variables distribution. One can see that after dropping for record missing data, the dataset contains 42314 records.
Can you write Python code for it?


I am working on the file "RandomForest.ipynb".
The first task is:
Dividiamo ora il dataset in training set e test set, mantenendo un rapporto 0,75 0,25 tra i due. Il training set verrà usato per fare cross validation, mentre il test set per misurare le performance del modello.
Can you write Python code for it?

from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectKBest
from sklearn.cross_validation import StratifiedKFold
from sklearn.grid_search import GridSearchCV
from sklearn.ensemble.gradient_boosting import GradientBoostingClassifier
from sklearn.cross_validation import cross_val_score
from sklearn.cross_validation import train_test_split

# Generate training set and test set
targets = dataset['TipoLesione']
dataset.drop('TipoLesione', inplace=True, axis=1)
X_train, X_test, y_train, y_test = train_test_split(dataset, targets, test_size=0.25, random_state=42)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)


I am working on the file "RandomForest.ipynb".
The first task is:
Here above is the most interesting results. It shows the relative importance of the explanatory variables. The main message here is that I forgot to consider the grade completed variable when running the classification tree last week... And the second one is the preponderant role of age in the trees.
Influence of number of tree on accuracy
Can you write Python code for it?

trees=range(25)
accuracy=np.zeros(25)

for idx in range(len(trees)):
   classifier=RandomForestClassifier(n_estimators=idx + 1)
   classifier=classifier.fit(pred_train,tar_train)
   predictions=classifier.predict(pred_test)
   accuracy[idx]=sklearn.metrics.accuracy_score(tar_test, predictions)

plt.plot(trees, accuracy)
plt.xlabel("Number of trees")
plt.ylabel("Accuracy score");


I am working on the file "RandomForest.ipynb".
The first task is:
La tecnica del one-hot encoding aumenta notevolmente in numero di features con le quali l'algoritmo deve avere a che fare, appesantendolo di conseguenza e introducendo, in alcuni casi, troppa varianza. Infatti, arrivati a questo punto abbiamo ottenuto 79 features, e sarebbe preferibile lavorare con molte meno. Utilizziamo perciò la tecnica degli extra-tree per selezionare, all'interno del training set le feature più importanti e, successivamente, rimappare training e test in due nuovi set, più facili da trattare.
Can you write Python code for it?

from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel

# Feature selection
clf = ExtraTreesClassifier(n_estimators=200, class_weight='balanced')
clf = clf.fit(X_train, y_train)

# Show feature importance
features = pd.DataFrame()
features['feature'] = X_train.columns
features['importance'] = clf.feature_importances_
features.sort(['importance'], ascending=False)

model = SelectFromModel(clf, prefit=True)
X_train_new = model.transform(X_train)
X_test_new = model.transform(X_test)
print(X_train_new.shape)
print(X_test_new.shape)


I am working on the file "RandomForest.ipynb".
The first task is:
Modeling and prediction
Can you write Python code for it?

# Selection of explanatory variables
features = ['sex', 'AGE', 'native', 'asian', 'black', 'pacific', 'white', 'family_income',
            'smoked_100cigarettes',
            'bioparent', 'adoptiveparent', 'byrelative', 'byfoster', 'ininstitution',
            'otherraised', 'divorced', 'grade', 'fulltime', 'parttime',
            'unemployedsearch', 'unemployed']

predictors = subnesarc[features]

# Reponse variable
targets = subnesarc['used_canabis']

# Split the data in test and training test
pred_train, pred_test, tar_train, tar_test = train_test_split(predictors, targets, test_size=.4)


I am working on the file "RandomForest.ipynb".
The first task is:
Per prima cosa, creiamo la colonna delle label. Questo passaggio è necessario perchè la maggior parte delle librerie si aspetta le label in formato [0, 1] nei casi di classificazione binaria e in formato [0, 1, ..., N] nel caso di classificazione a N classi. Inoltre, per trattare i dati di tipo categorico, scegliamo la tecnica del one-hot encoding, anche detta tecnica delle dummy variables. Per fare un esempio, la colonna "TipoPersona", che ha i valori "Conducente", "Passeggero" e "Pedone", viene trasformata in 3 colonne, ovvero "TipoPersona_Conducente", "TipoPersona_Passeggero" e "TipoPersona_Pedone"; le colonne avranno un 1 in corrispondenza della categoria indicata dal record in esame.
Can you write Python code for it?

from sklearn.preprocessing import LabelEncoder

# Create labels for classification
number = LabelEncoder()
dataset['TipoLesione'] = number.fit_transform(dataset['TipoLesione'].astype('str'))
print(list(number.classes_))
print(number.transform(number.classes_))
dataset.head()

# NaturaIncidente dummies
NaturaIncidente_dummies = pd.get_dummies(dataset['NaturaIncidente'], prefix='NaturaIncidente')
dataset = pd.concat([dataset, NaturaIncidente_dummies], axis=1)
dataset.drop('NaturaIncidente', inplace=True, axis=1)

# ParticolaritaStrade dummies
ParticolaritaStrade_dummies = pd.get_dummies(dataset['ParticolaritaStrade'], prefix='ParticolaritaStrade')
dataset = pd.concat([dataset, ParticolaritaStrade_dummies], axis=1)
dataset.drop('ParticolaritaStrade', inplace=True, axis=1)

# TipoStrada dummies
TipoStrada_dummies = pd.get_dummies(dataset['TipoStrada'], prefix='TipoStrada')
dataset = pd.concat([dataset, TipoStrada_dummies], axis=1)
dataset.drop('TipoStrada', inplace=True, axis=1)

# FondoStradale dummies
FondoStradale_dummies = pd.get_dummies(dataset['FondoStradale'], prefix='FondoStradale')
dataset = pd.concat([dataset, FondoStradale_dummies], axis=1)
dataset.drop('FondoStradale', inplace=True, axis=1)

# Pavimentazione dummies
Pavimentazione_dummies = pd.get_dummies(dataset['Pavimentazione'], prefix='Pavimentazione')
dataset = pd.concat([dataset, Pavimentazione_dummies], axis=1)
dataset.drop('Pavimentazione', inplace=True, axis=1)

# Segnaletica dummies
Segnaletica_dummies = pd.get_dummies(dataset['Segnaletica'], prefix='Segnaletica')
dataset = pd.concat([dataset, Segnaletica_dummies], axis=1)
dataset.drop('Segnaletica', inplace=True, axis=1)

# CondizioneAtmosferica dummies
CondizioneAtmosferica_dummies = pd.get_dummies(dataset['CondizioneAtmosferica'], prefix='CondizioneAtmosferica')
dataset = pd.concat([dataset, CondizioneAtmosferica_dummies], axis=1)
dataset.drop('CondizioneAtmosferica', inplace=True, axis=1)

# Traffico dummies
Traffico_dummies = pd.get_dummies(dataset['Traffico'],...


I am working on the file "RandomForest.ipynb".
The first task is:
The following commands run the Random Forest test on 60% of the sample.
Can you write Python code for it?

from sklearn.ensemble import RandomForestClassifier

classifier = RandomForestClassifier(n_estimators=25)
classifier = classifier.fit(pred_train, tar_train)

predictions = classifier.predict(pred_test)

cm = sklearn.metrics.confusion_matrix(tar_test, predictions)
nice_cm = pd.DataFrame({'Never used cannabis' : cm[:, 0], 'Used cannabis' : cm[:, 1]},
                       index=('Never used cannabis', 'Used cannabis'))
nice_cm.index.name = 'True/predicted value'
nice_cm


I am working on the file "RandomForest.ipynb".
The first task is:
Tutto è pronto per lanciare l'algoritmo!!! Andiamo ad impostare i parametri per la cross validation, lanciamo l'algoritmo e aspettiamo che il Random Forrest faccia il suo lavoro.
Can you write Python code for it?

# Random Forest with grid search Cross Validation
forest = RandomForestClassifier(max_features='sqrt', class_weight='balanced')

parameter_grid = {
                     'max_depth' : [6, 8, 10, 12, 14],
                     'n_estimators': [200, 220, 240, 260],
                     'criterion': ['gini', 'entropy'],
                 }

cross_validation = StratifiedKFold(y_train, n_folds=5)

grid_search = GridSearchCV(forest,
                           param_grid=parameter_grid,
                           cv=cross_validation)

grid_search.fit(X_train_new, y_train)

print('Best score: {}'.format(grid_search.best_score_))
print('Best parameters: {}'.format(grid_search.best_params_))


I am working on the file "RandomVariables.ipynb".
The first task is:
Let's now estimate the frequency of the event roll even number in different ways.
First let's directly count the number of even numbers in the generated samples. Then let's
take the sum of the counts of the individual estimated probabilities.
Can you write Python code for it?

# we can also write the predicates directly using lambda notation
est_even = len([x for x in samples if x%2==0]) / len(samples)
est_2 = len([x for x in samples if x==2]) / len(samples)
est_4 = len([x for x in samples if x==4]) / len(samples)
est_6 = len([x for x in samples if x==6]) / len(samples)
print(est_even)
# Let's print some estimates
print('Estimates of 2,4,6 = ', (est_2, est_4, est_6))
print('Direct estimate = ', est_even)
print('Sum of estimates = ', est_2 + est_4 + est_6)
print('Theoretical value = ', 0.5)


I am working on the file "RandomVariables.ipynb".
The first task is:
Let's now plot the cumulative histogram of the samples. By observing the cumulative histogram you can see a possible way to sample any discrete probability distribution. The idea is to use a random number between 0.0 and 1.0 (programming languages typically provide a uniform random number generator that can be used for this purpose) and use it to "index" the y-axis of the histogram. If the number is between 0.0 and the height of the first bar then output 1, if it is between the height of the first bar and the second bar output 2, and so on. This is called the inverse transform method for sampling a distribution. It also works for arbitrary continuous probability densities for which the cumulative distribution can be computed either analytically or approximated numerically. (Note: there are other more efficient ways of sampling continuous densities)
Can you write Python code for it?


I am working on the file "RandomVariables.ipynb".
The first task is:
Notice that we can always estimate the probability of an event by simply counting how many times it occurs in the samples of an experiment. However if we have multiple events we are interested in then it can be easier to calculate the probabilities of the values of invdividual random variables and then use the rules of probability to estimate the probabilities of more complex events.
Generating a random melody
Just for fun let's generate a random melody. We will assume all notes have equal duration, there are no rests, and each note is equally likely. Using music21, a python package for symbolic music processing, we can generate a simple "score" using a simplified notation called tiny notation and then render it as a MIDI file that we can listen.
First let's check that we can hear a melody specified in tiny notation.
Can you write Python code for it?

import music21 as m21
from music21 import midi

littleMelody = m21.converter.parse("tinynotation: 3/4 c4 d8 f g16 a g f#")
sp = midi.realtime.StreamPlayer(littleMelody)
littleMelody.show()
sp.play()


I am working on the file "RandomVariables.ipynb".
The first task is:
Now let's look at a numeric random variable corresponding to a dice so that we can more easily make plots and histograms
Can you write Python code for it?

values = np.arange(1,7)
probabilities = [1/6.] * 6
dice = Random_Variable('dice', values, probabilities)
samples = dice.sample(200)
plt.stem(samples, markerfmt= ' ')


I am working on the file "RandomVariables.ipynb".
The first task is:
Now we can create a random variable with all 12 notes in a chromatic scale and generate a random melody of 10 notes by sampling it.
Can you write Python code for it?

values = ['c4','c#4','d4','d#4', 'e4', 'f4','f#4', 'g4','g#4','a4','a#4','b4']
probabilities = [1/12.]*12
note_rv = Random_Variable('note', values, probabilities)
print(note_rv.sample(10))
note_string = 'tinynotation: 4/4 ; ' + " ".join(note_rv.sample(10))
print(note_string)
randomMelody = m21.converter.parse(note_string)
sp = midi.realtime.StreamPlayer(randomMelody)
randomMelody.show()
sp.play()


I am working on the file "RandomVariables.ipynb".
The first task is:
Using music21 it is possible to load a particular Bach Chorale and play it using MIDI.
Can you write Python code for it?

bachPiece = m21.corpus.parse('bwv66.6')
sp = midi.realtime.StreamPlayer(bachPiece)
bachPiece.show()
sp.play()


I am working on the file "RandomVariables.ipynb".
The first task is:
We can also define a Markov Chain based on the transition probabilities we have calculated. This provides a better modeling of sequencing information as can be heard when listening/viewing the randomly generated melody. Markov chains and other probabilistic models for sequences are described in more detail in another notebook.
Can you write Python code for it?

def markov_chain(transmat, state, state_names, samples):
    (rows, cols) = transmat.shape
    rvs = []
    values = list(np.arange(0,rows))

    # create random variables for each row of transition matrix
    for r in range(rows):
        rv = Random_Variable("row" + str(r), values, transmat[r])
        rvs.append(rv)

    # start from initial state given as an argument and then sample the appropriate
    # random variable based on the state following the transitions
    states = []
    for n in range(samples):
        state = rvs[state].sample(1)[0]
        states.append(state_names[state])
    return states

note_string = 'tinynotation: 4/4 ; ' + " ".join(markov_chain(transition_matrix,0,values, 16))
print(note_string)
markovChoraleMelody = m21.converter.parse(note_string)
markovChoraleMelody.show()
sp = midi.realtime.StreamPlayer(markovChoraleMelody)
sp.play()


I am working on the file "RandomVariables.ipynb".
The first task is:
We can parse a whole bunch of chorales (in this case all the ones that are in C major) and derive probabilities for each note of the chromatic scale. That way we can estimate the probability density of notes and make a very simple "chorale" like random melody generator. Listen to how it sounds compared to the uniform random atonal melodies we heard before. I calculate the probabilities of each pitch class in chorale_probs and also compute a transition matrix that we will use later when exploring Markov Chains. This takes a a few minutes to process so be patient. There 405 chorales and 23 are in C major. You will see them slowly printed at the output. Wait until you see that the processing is finished - takes about a minute.
Can you write Python code for it?

chorales = m21.corpus.search('bach', fileExtensions='xml')
print(chorales)

chorale_probs = np.zeros(12)
totalNotes = 0
transition_matrix = np.ones([12,12])     # hack of adding one to avoid 0 counts
j = 0

for (i,chorale) in enumerate(chorales):
    score = chorale.parse()
    analyzedKey = score.analyze('key')
    # only consider C major chorales
    if (analyzedKey.mode == 'major') and (analyzedKey.tonic.name == 'C'):
        j = j + 1
        print(j,chorale)
        score.parts[0].pitches
        for (i,p) in enumerate(score.parts[0].pitches):
            chorale_probs[p.pitchClass] += 1
            if i < len(score.parts[0].pitches)-1:
                transition_matrix[p.pitchClass][score.parts[0].pitches[i+1].pitchClass] += 1
            totalNotes += 1

chorale_probs /= totalNotes
transition_matrix /= transition_matrix.sum(axis=1,keepdims=1)
print('Finished processing')


I am working on the file "RandomVariables.ipynb".
The first task is:
We have calculated the probabilities of each pitch class in our data collection (chorales in C Major) as a 12-element vector (chorale_probs). We also have calculated the transition matrix that calculates the frequency of each transition from one note to another for a total of 12 by 12.
Can you write Python code for it?

print("Note probabilities:")
print(chorale_probs)
print("Transition matrix:")
print(transition_matrix)


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
A quick analysis of the newly created "karma" features highlights the fact that people who received pizza have a median karma value of over 660, compared to those who did not with a median value of only 480. This large difference will likely have strong predictve power for our model.  As indicated in the code snippet below, roughly one-third of all observations who did not receive a pizza have a karma value below 100.
Can you write Python code for it?

low_losers= len(df[df['requester_received_pizza'] == 0][df['karma'] < 15])
low_winners=len(df[df['requester_received_pizza'] == 1][df['karma'] < 15])
print low_losers, low_losers/3046.
print low_winners, low_winners/994.

high_losers = len(df[df['requester_received_pizza'] == 0][df['karma'] > 5000])
high_winners = len(df[df['requester_received_pizza'] == 1][df['karma'] > 5000])
print high_losers, high_losers/3046.
print high_winners, high_winners/994.

df['karma_low'] = np.where(df['karma'] < 15, 1, 0)


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
I. Background:
This project aims to predict whether a reddit post asking for pizzas would get funded. Since it's a binary classification problem, we will explore several algorithms with a focus on logistic regression. In particular, we will look into details how to extract features from text.
II. Data Pre-Processing:
The data in its raw form consists of 4040 observations of 31 features.  The original columns consist of 19 integer values, 4 floats, and 8 objects (there is one boolean column which is the outcome variable).  In order to extract predictive value from the dataset a good deal of pre-processing and feature engineering was required.  A walkthrough of the various steps taken follows below in a narrative format:
Can you write Python code for it?

#load json training data into pandas dataframe
df = pd.read_json('train.json')
df.info()


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
III. Baseline Modeling:
With pre-processing out of the way, we decided to run a baseline text analysis model using the "request_text_n_title" feature alone.  This model serves as our basis against which we can compare the success or failure of all futue feature engineering efforts.
IIIa. Train and Dev set creation
We begin by forming our training and dev sets.  Given that the success rate for receiving a pizza in the original dataset is ~25%, we'll check our randomized training and dev sets to ensure a similar success rate is preserved.
Can you write Python code for it?

np.random.seed(0)

#separate into features and labels
text_features = df['request_text_n_title'].values # text features
success_rate = sum(df['requester_received_pizza'])/4040.
print "Original success rate: {}".format(round(success_rate, 4))

# Create target field for received pizza are only 1's and 0's
target = df['requester_received_pizza'].values

#shuffle our data to ensure randomization
shuffle = np.random.permutation(len(text_features))
text_features, target = text_features[shuffle], target[shuffle]

#separate into training and dev groups
train_data, train_labels = text_features[:3200], target[:3200]
dev_data, dev_labels = text_features[3200:], target[3200:]

#check to ensure success rate is roughly preserved across sets
train_success_rate = sum(train_labels)/3200.
dev_success_rate = sum(dev_labels)/840.
print "Training success rate: {}".format(round(train_success_rate, 4))
print "Dev success rate: {}".format(round(dev_success_rate, 4))

#check to ensure we've got the right datasets
print '\n\nTraining Data shape: \t{}'.format(train_data.shape)
print 'Training Labels shape: \t{}'.format(train_labels.shape)
print 'Dev data shape: \t{}'.format(dev_data.shape)
print 'Dev Labels shape: \t{}'.format(dev_labels.shape)


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
IIIb. Logistic Regression
Our initial model will make use of the sklearn TfidfVectorizer function to encapsulate predictive power from the training data.  After some initial trial runs, we realized that we needed to limit the number of features to make a useful model.  In this case, a predictive "sweet spot" emerged in the 350+ - 400 range, so we limited our feature number to this amount.
Can you write Python code for it?

#fit logistic classifier to training data
print "BASELINE LOGISTIC REGRESSION"
print "----------------------------"
vec = TfidfVectorizer()
train_matrix = vec.fit_transform(train_data)
dev_matrix = vec.transform(dev_data)
lr= LogisticRegression(n_jobs=-1, class_weight='balanced').fit(train_matrix, train_labels)
predictions = lr.predict(dev_matrix)
score = round(roc_auc_score(dev_labels, predictions, average='weighted'), 4)
print "Baseline ROC AUC score: {}".format(score)

print "\n\nRESTRICTED FEATURES LOGISTIC REGRESSION"
print "---------------------------------------"

model_scores = []
range_features = np.arange(385,400)

for features in range_features:
    vec = TfidfVectorizer(stop_words='english',sublinear_tf=1, ngram_range=(1,1),max_features=features)
    train_matrix = vec.fit_transform(train_data)
    dev_matrix = vec.transform(dev_data)
    lr= LogisticRegression(n_jobs=-1, class_weight='balanced').fit(train_matrix, train_labels)
    predictions = lr.predict(dev_matrix)
    model_scores.append(round(metrics.roc_auc_score(dev_labels, predictions, average = 'weighted'), 4))
    best_score = round(max(model_scores), 4)
print "Max number of features: {}".format(range_features[np.argmax(model_scores)])
print "Best ROC AUC Score: {}".format(best_score)

#best_max_feature
best_max_feature = range_features[np.argmax(model_scores)]

#fit logistic classifier to training data
vec = TfidfVectorizer(stop_words='english',sublinear_tf=1, ngram_range=(1,1), max_features=best_max_feature)
train_matrix = vec.fit_transform(train_data)
dev_matrix = vec.transform(dev_data)

print "\n"
print "LOGISTIC REGRESSION with tuning c value and restricted # of features"
print "--------------------------------------------------------------------"
c_values = np.logspace(.0001, 2, 200)
c_scores = []
for value in c_values:
    lr = LogisticRegression(C=value, n_jobs=-1, class_weight='balanced', penalty='l2')
    lr.fit(train_matrix, train_labels)
    predictions = lr.predict(dev_matrix)
   ...


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
In the end, we added a total of 16 additional features, 3 continuous and 13 binary.
Can you write Python code for it?


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
Model Three: Unigrams predict_probability combined with engineered features
Can you write Python code for it?

#fit logistic classifier to training data
vec = TfidfVectorizer(stop_words='english',sublinear_tf=1, max_features=best_max_feature)
train_matrix = vec.fit_transform(train_data)

lr1 = LogisticRegression(C=best_c_value, n_jobs=-1).fit(train_matrix, train_labels)

text_features = df['request_text_n_title'].values.copy()

text_transformed = vec.transform(text_features)
# create pizza_predict field for all records based on features array
pizza_predict = lr1.predict_proba(text_transformed)[:,1][:, np.newaxis]

# Concatenate numeric_features with pizza_predict to create pizza_predict + numeric features in ens_features
combined_features = np.append(features.values, pizza_predict, axis = 1)

X, y = combined_features, target.copy()
#shuffle = np.random.permutation(len(X))
X, y = X[shuffle], y[shuffle]
Xtrain, Xdev, ytrain, ydev = X[:3200], X[3200:], y[:3200], y[3200:]
Xtrain.shape, Xdev.shape, ytrain.shape, ydev.shape

#####
c_values = np.logspace(.0001, 2, 200)
c_scores = []
for value in c_values:
    lr = LogisticRegression(C=value, n_jobs=-1, class_weight='balanced', penalty='l2')
    lr.fit(Xtrain, ytrain)
    predictions = lr.predict(Xdev)
    c_scores.append(round(metrics.roc_auc_score(ydev, predictions, average = 'weighted'), 4))

best_C_value = c_values[np.argmax(c_scores)]
print "Best C-value: {}".format(best_C_value)
print "Best ROC AUC Score: {}".format(c_scores[np.argmax(c_scores)])


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
Removing the "at_retrieval" columns and the other six columns, reduces our dataset by 17 total features (leaving us with 14 features, not including the outcome variable ).
During the EDA phase of this project we found that several (104 to be exact), observations had a "request_text" length of zero, some of these observations actually ended up being given a pizza.  After looking through the data, we discovered that some people had left their request in the "request_title" field of the RAOP Reddit page and had left their request_text field blank.  In order to clean this discrepancy up, we decided to combine these two fields together, as it is unclear if the benefactors (those who ended up giving pizzas away),  were responding to the '"request_title" field, the "request_text" field, or both when they made their altruistic decision.
Can you write Python code for it?

#Show that 104 observations have a blank "request_text" field
len(df[df['request_text'].str.len() == 0])

#1. Combine request_text and request_title fields
#2. Lowercase all words

df['request_text_n_title'] = (df['request_title'] + ' ' + df['request_text_edit_aware'])
df['request_text_n_title'] = [ text.split(" ",1)[1].lower() for text in df['request_text_n_title']]
print df['request_text_n_title'].head()

#3. Add a total length feature to the dataset
df['total_length'] =df['request_text_n_title'].apply(lambda x: len(x.split(' ')))

#4. Ensure there are no zero length requests in the new feature/column
print '\nAfter combining request_title and request_text, number of requests with length of \
"zero": {}'.format(len(df[df['request_text_n_title'] == 0]))

#Showcase of new added features to dataset
df.loc[:5,['request_text_n_title', 'total_length']]


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
SVM performs no better than previous models.
IV. Feature Engineering
Setting aside text processing for the time being, we decided to focus on specific features within the dataset that could possibly lead to increased predictive power.  For example, we noted that users who included pictues in their request (presumably as a means of validating their story), were disporportionately more likely to receive a pizza.  Similarly, users who wrote longer requests, or who had spent more time in the RAOP community were also more likely to recieve a pizza.  Therefore, what follows is an explanation of our attempts at engineering features to build the best predictive model possible.  With a few exceptions all features were binarized.
a. Including an image
The inclusion of an image in the request indicates that the requester is providing some evidence of their need for a pizza. A photo might be a screenshot of a bank account balance, a large bill, an injury, or other misfortune. Adding such evidence increases the odds of receiving a pizza.
Can you write Python code for it?

#Create feature where in image is included in the request
df['image_incl'] = np.where(df['request_text_n_title'].str.contains("imgur"), int(1), int(0))


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
To fit our model, we combined all engineered features into a single dataframe.  And after several iterations, decided to truncate some of the added features to improve predictive accuracy ('time' and 'karma' specifically).
Can you write Python code for it?

continuous_list = ['total_length']#, 'time', 'karma']
binary_list = [
                                               u'image_incl',
                                                u'karma_low',
                                            #        u'month',
                                             #         u'day',
                                                     u'time',
                                               u'first_half',
                                       u'requester_grateful',
                                        u'requester_payback',
                                          u'narrative_money',
                                            u'narrative_job',
                                         u'narrative_family',
                                        u'narrative_student',
                                        u'narrative_craving'
             ]

#create new DataFrame using previously defined "numeric_features" object to determine all columns in the DF

numeric_features = df.copy().loc[:,continuous_list]
numeric_features_norm = pd.DataFrame(data=preprocessing.normalize(numeric_features, axis=0),\
                                     columns=numeric_features.columns.values)

# combine to contious and binary
numeric_features_norm = pd.concat([df[binary_list],numeric_features_norm],axis=1)
numeric_features_norm.head()

features = numeric_features_norm.copy()
target = df['requester_received_pizza']
features.info()


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
Using Bernoulli NB we get similar results to LR, again making use of restricting the number of features in our training matrix.
IIId. Support Vector Machine
Can you write Python code for it?

print "\nSupport Vector Machine"
print "------------------------"
vec = TfidfVectorizer(max_features=best_max_feature, stop_words='english')
train_matrix = vec.fit_transform(train_data)
dev_matrix = vec.transform(dev_data)
svc = LinearSVC().fit(train_matrix, train_labels)
predictions = svc.predict(dev_matrix)
print round(roc_auc_score(dev_labels, predictions, average='weighted'),4)


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
V. Final Modeling
We decided to create three final models.

One model using just our engineered features (without unigram text)
One combined model using engineered features and unigram text
One model using predicted probabilities as model features
Can you write Python code for it?

X, y = features.values, target.copy()
#shuffle = np.random.permutation(len(X))
X, y = X[shuffle], y[shuffle]
Xtrain, Xdev, ytrain, ydev = X[:3200], X[3200:], y[:3200], y[3200:]
Xtrain.shape, Xdev.shape, ytrain.shape, ydev.shape


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
VI. Results and Analysis
Our best model ended up being model Two which was a combination of engineered features and unigram text.  This result is not surprising, especially considering the amount of work required to engineer the given features.  We submitted different variants of model two to the Kaggle website with varying degrees of success.
FOR SUBMISSION WITH FINAL MODELS
Can you write Python code for it?

"""Our first attempt is a brute force method with logistics regression that
use the best parameters from developing the model"""

df_test = pd.read_json('test.json')

df_test['request_text_n_title'] = (df_test['request_title'] + ' ' + df_test['request_text_edit_aware'])
df_test['request_text_n_title'] = [ text.split(" ",1)[1].lower() for text in df_test['request_text_n_title']]
# print df_test['request_text_n_title'].head()

#create test set
test_features = df_test['request_text_n_title'].values # text features

#create new training set using all 4040 training samples
train_features = df['request_text_n_title'].values
train_labels = df['requester_received_pizza'].values

#shuffle our data to ensure randomization
shuffle = np.random.permutation(len(train_features))
train_features, train_labels = train_features[shuffle], train_labels[shuffle]

#transform raw data into Tfdif vector
vec = TfidfVectorizer(stop_words='english',sublinear_tf=1, max_features=387)
train_matrix = vec.fit_transform(train_features)
test_matrix = vec.transform(test_features)

#fit Logistic Regression model
lr= LogisticRegression(n_jobs=-1, class_weight='balanced').fit(train_matrix, train_labels)
test_predictions = lr.predict(test_matrix)[:, np.newaxis]

# print type(predictions_test)
sub_1 = np.append(df_test['request_id'].values[:, np.newaxis], test_predictions, axis = 1)
sub_1_df = pd.DataFrame(data=sub_1, columns=['request_id', 'requester_received_pizza'])  # 1st row as the column names
sub_1_df.to_csv("submission_1.csv", sep=',', header=True,  mode='w', index=0)

"""With this submission, we get  a AUC score of 0.59386"""

print "Kaggle submission 1, result AUC  = 0.59386"

"""The 2nd submission, we define a function process
to process the train and test json files

This function use the file name and the max number of feature tuned previously.
The output of process function depends on if it's train or test set
"""

def process(filename,max_feature_length,train=True):

    df = pd.read_json(filename)

   ...


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
a. Model One: Engineered Features only
Can you write Python code for it?

scores = []
c_values = np.linspace(.001, 100, 20)
for c in c_values:
    lr_8 = LogisticRegression(C=c, class_weight='balanced', n_jobs=-1).fit(Xtrain, ytrain)
    predictions = lr_8.predict(Xdev)
    scores.append(round(roc_auc_score(ydev, predictions, average='weighted'), 4))
print "Best C-value: {}".format(c_values[np.argmax(scores)])
print 'Best AUC score based on metrics.roc_auc_score = {}'.format(max(scores))


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
b. Community standing/status
Can you write Python code for it?

#Create feature that is an aggregate of all indicators of community status including seniority

df['karma'] = df['requester_account_age_in_days_at_request'] + df['requester_days_since_first_post_on_raop_at_request']\
+ df['requester_number_of_comments_at_request'] + df['requester_number_of_comments_in_raop_at_request'] + \
df['requester_number_of_posts_at_request'] + df['requester_number_of_posts_on_raop_at_request'] + \
df['requester_number_of_subreddits_at_request'] + df['requester_upvotes_minus_downvotes_at_request']

karma_winners = df['karma'][df['requester_received_pizza'] == 1].describe()
karma_losers = df['karma'][df['requester_received_pizza'] == 0].describe()
karma_comparison = pd.concat([karma_winners, karma_losers], axis=1)
karma_comparison.columns = ['winners', 'losers']
karma_comparison


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
b. Model Two: Engineered Features combined with Unigram text
Can you write Python code for it?

#Initialize our vectorizer using hyperparameters from previous models
vec = TfidfVectorizer(stop_words='english',sublinear_tf=1, max_features=best_max_feature)
train_matrix = vec.fit_transform(train_data)

#Transform our text features
text_features = df['request_text_n_title'].values.copy()
text_transformed = vec.transform(text_features)

# Concatenate egineered numeric_features with vectorized text features
combined_features = np.append(features.values, text_transformed.toarray(), axis = 1)
print combined_features.shape

#prep data for modeling
X, y = combined_features, target.copy()
X, y = X[shuffle], y[shuffle]
Xtrain, Xdev, ytrain, ydev = X[:3200], X[3200:], y[:3200], y[3200:]
Xtrain.shape, Xdev.shape, ytrain.shape, ydev.shape

#Fit logistic model to data
c_values = np.logspace(.0001, 2, 200)
c_scores = []
for value in c_values:
    lr = LogisticRegression(C=value, n_jobs=-1, class_weight='balanced', penalty='l2')
    lr.fit(Xtrain, ytrain)
    predictions = lr.predict(Xdev)
    c_scores.append(round(metrics.roc_auc_score(ydev, predictions, average = 'weighted'), 4))

best_C_value = c_values[np.argmax(c_scores)]
print "Best C-value: {}".format(best_C_value)
print "Best ROC AUC Score: {}".format(c_scores[np.argmax(c_scores)])


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
e. Extracting gratitude and "pay it forward" sentiment
Can you write Python code for it?

#Create binary variables that show whether requester is grateful or willing to "pay it forward"
df['requester_grateful'] = np.where(df['request_text_n_title'].str.contains('thanks' or 'advance' or 'guy'\
                    or 'reading' or 'anyone' or 'anything' or'story'or 'tonight'or 'favor'or'craving'), int(1), int(0))

df['requester_payback'] = np.where(df['request_text_n_title'].str.contains('return' or 'pay it back'\
                                                                     or 'pay it forward' or 'favor'), int(1), int(0))


I am working on the file "Random_Acts_of_Pizza_Kaggle_Competition_Project.ipynb".
The first task is:
f. Extracting request narratives
We extract the narrative out of each request. Based on our EDA, we picked 5 narratives: Money, Job, Student, Family and Craving. 5 new columns will be added for the narratives which are coded in binary 0 or 1.  The terms that specify each narrative are given in the narratives' dictionary.  We counted the total occurent of the words in each post, normalize this count by the word count in the post, and use median-threshold to determin whether the post fall into the respective narrative.
Can you write Python code for it?

# Define narrative categories
narratives = {
            'money':['money','now','broke','week','until','time',
                      'last','day','when','today','tonight','paid',
                      'next','first','night','night','after','tomorrow',
                      'while','account','before','long','friday','rent',
                      'buy','bank','still','bills','ago','cash','due',
                      'soon','past','never','paycheck','check','spent',
                      'year','years','poor','till','yesterday','morning',
                      'dollars','financial','hour','bill','evening','credit',
                      'budget','loan','bucks','deposit','dollar','current','payed'],
             'job':['work','job','paycheck','unemployment','interviewed',
                   'fired','employment','hired','hire'],
             'student':['college','student','school','roommate','studying',
                       'study','university','finals','semester','class','project',
                       'dorm','tuition'],
             'family': ['family','mom','wife','parents','mother','husband','dad','son',
                     'daughter','father','parent','mum','children','starving','hungry'],
             'craving': ['friend','girlfriend','birthday','boyfriend','celebrate',
                      'party','game','games','movie','movies','date','drunk',
                      'beer','celebrating','invited','drinks','crave','wasted','invited']
            }

# function to extract word count for each narrative from one text post, normalize by the word count of that post
def single_extract(text):
    count = {'money':0.,
            'job':0.,
            'student':0.,
            'family':0.,
            'craving':0.}
    words = text.split(' ')
    length = 1./len(words)
    for word in text.split(' '):
        for i,k in narratives.items():
            if word in k:
                count[i] += length
    return count.values()

# Extract request_text_n_title field
texts =...


I am working on the file "Random_Forest.ipynb".
The first task is:
Cantidad de árboles entrenados:
Can you write Python code for it?


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

df_forest = preprocessing.rforest_preproc(df_all)

# Separate test and train data
X_train, X_test, y_train, y_test = train_test_split(df_forest.drop('volveria', 1), df_forest['volveria'])

model_rfr = RandomForestClassifier(max_depth=5)

model_rfr.fit(X_train, y_train)

preprocessing.report(model_rfr, X_train, y_train, X_test, y_test)

pred_rfr = model_rfr.predict(X_test)


I am working on the file "Random_Forest.ipynb".
The first task is:
Veamos el primero de ellos:
Can you write Python code for it?

with plt.style.context("classic"):
    plt.figure(figsize=(20, 10))
    plot_tree(model_rfr.estimators_[0], filled=True)


I am working on the file "Random_Forest.ipynb".
The first task is:
Y sus métricas:
Can you write Python code for it?

pred_first_estimator = model_rfr.estimators_[0].predict(X_test)
accuracy_score(y_test, pred_first_estimator)

precision_score(y_test, pred_first_estimator)

recall_score(y_test, pred_first_estimator)

f1_score(y_test, pred_first_estimator)

pred_estimators = [
    estimator.predict(X_test) for estimator in model_rfr.estimators_
]
acc_estimators = [accuracy_score(y_test, pred) for pred in pred_estimators]

plt.figure(figsize=(10, 10))
plt.hist(acc_estimators)
plt.xlabel("Accuracy", weight="bold", fontsize=15)
plt.ylabel("Frecuencia", weight="bold", fontsize=15)
plt.title(
    "Histograma de accuracy de los arboles del RF model", weight="bold", fontsize=16
)

max(acc_estimators)

min(acc_estimators)


I am working on the file "Random_walk_1D.ipynb".
The first task is:

For example, we can see that the variance of the distribution $p(x,t)$ grows linearly with time:$$\langle x^2 \rangle = t $$

Can you write Python code for it?


I am working on the file "Random_walk_1D.ipynb".
The first task is:

Let's take a look at the trajectory of the fly for a large number of steps.

Can you write Python code for it?

Nsteps = 10000
x = np.zeros(Nsteps)
for t in range(1,Nsteps):
    if np.random.rand() <= p:
        x[t] = x[t - 1] + 1
    else:
        x[t] = x[t - 1] - 1
print "mean: %.2f" % np.mean(x)
print "std. dev.: %.2f" % np.std(x)
plt.plot(x)
plt.title("Random Walk")
plt.xlabel("time $t$")
plt.ylabel("position $x(t)$")
plt.show()


I am working on the file "Random_walk_1D.ipynb".
The first task is:

Many processes in physics and chemistry happen randomly or stochastically, whic. This is in contrast to deterministic problems where we can exactly predict the future based if we now the current state of the system. For example, we can predict the velocity of object if we know the initial velocity and the forces it feels. However, in many cases we only know the probabilities of different outcomes. For example, when we roll a die, we don't know which face of the die will end up facing the ground, we only know the *probability* of seeing any of of the faces.

 In the case of the die, there are six different outcomes and they all have equal probability (if it is an unbiased die). In general, if there are $N$ outcomes, each with probability $p_i$ then the probabilities must be normalized, $$\sum\limits_{i}^N p_i = 1$$ In other words, the probability that something happens is $1$.

A random walk in one dimension

As a first example, consider the trajectory of a speck of dust floating in a cup of water. For the moment let's imagine that the dust can only move in one dimension. Since the dust is bombarded by water molecules in a random way we cannot know it's trajectory exactly. The trajectory is random or stochastic: the dust takes a step to the right $x > 0$ with probability $p$ and to the left $x < 0$ with probabilty $q$.

Can you write Python code for it?


I am working on the file "RandomizedAlgorithms/CW2-2 Shuffling.ipynb".
The first task is:
Another shuffling algorithm based on sorting, but uniform
Can you write Python code for it?

# Permute by sorting: Assign a random priority from 1 to n^3 to every element
# Cormen page 125
def random_sort_shuffle_uniform(a):
    n = len(a)
    a.sort(key=lambda _: random.randint(1, n ** 3))


I am working on the file "RandomizedAlgorithms/CW2-2 Shuffling.ipynb".
The first task is:
Empirical analysis
Can you write Python code for it?

# Returns the swap matrix generated by running the
# shuffle_function on [0, 1, 2, ..., n_of_elems] 'times' times
# Column: index before shuffling
# Row: Index after shuffling
def swap_matrix(shuffle_function, n_of_elems, times):
    matrix = np.zeros((n_of_elems, n_of_elems), dtype=np.int)

    for _ in range(times):
        l_copy = [x for x in range(0, n_of_elems)]
        shuffle_function(l_copy)

        # Keep track of how many times index j is at position i
        for i in range(n_of_elems):
            matrix[l_copy[i]][i] += 1

    return matrix

def plot_matrixes(matrixes):
    columns = len(matrixes)
    rows = 1
    fig, axs = plt.subplots(rows, columns, figsize=(15, 10))

    for j in range(columns):
        axs[j].imshow(eval(matrixes[j]))
        axs[j].set_title(matrixes[j])

    plt.show()

times = 10000
n_of_elems = 50

random_sort_swap_matrix = swap_matrix(random_sort_shuffle, n_of_elems, times)

fisher_yates_swap_matrix = swap_matrix(fisher_yates_shuffle, n_of_elems, times)

fisher_yates_buggy1_swap_matrix = swap_matrix(fisher_yates_shuffle_buggy1, n_of_elems, times)

fisher_yates_buggy2_swap_matrix = swap_matrix(fisher_yates_shuffle_buggy2, n_of_elems, times)

matrixes = [
            'random_sort_swap_matrix',
            'fisher_yates_swap_matrix',
            'fisher_yates_buggy1_swap_matrix',
            'fisher_yates_buggy2_swap_matrix',
           ]

plot_matrixes(matrixes)