I am working on the file "Guided+example+and+Challenge.ipynb".
The first task is:
Since we're now working with a binary outcome, we've switched to a classifier.  Now our loss function can't be the residuals.  Our options are "deviance", or "exponential".  Deviance is used for logistic regression, and we'll try that here.
Can you write Python code for it?

# We'll make 500 iterations, use 2-deep trees, and set our loss function.
params = {'n_estimators': 1000,
          'max_depth': 3,
          'loss': 'exponential',
          'learning_rate' : 0.01
         }

# Initialize and fit the model.
clf = ensemble.GradientBoostingClassifier(**params)
clf.fit(X_train, y_train)

predict_train = clf.predict(X_train)
predict_test = clf.predict(X_test)

# Accuracy tables.
table_train = pd.crosstab(y_train, predict_train, margins=True)
table_test = pd.crosstab(y_test, predict_test, margins=True)

train_tI_errors = table_train.loc[0.0,1.0] / table_train.loc['All','All']
train_tII_errors = table_train.loc[1.0,0.0] / table_train.loc['All','All']

test_tI_errors = table_test.loc[0.0,1.0]/table_test.loc['All','All']
test_tII_errors = table_test.loc[1.0,0.0]/table_test.loc['All','All']

print((
    'Training set accuracy:\n'
    'Percent Type I errors: {}\n'
    'Percent Type II errors: {}\n\n'
    'Test set accuracy:\n'
    'Percent Type I errors: {}\n'
    'Percent Type II errors: {}'
).format(train_tI_errors, train_tII_errors, test_tI_errors, test_tII_errors))


I am working on the file "Guided_Evolutionary_Strategies_Demo.ipynb".
The first task is:
As we see in the plot below, Guided ES combines the benefits of gradient descent (quick initial descent) and vanilla evolutionary strategies (converges on the true solution).
Can you write Python code for it?

COLORS = {'ges': '#7570b3', 'ves': '#1b9e77', 'sgdm': '#d95f02'}
plt.figure(figsize=(8, 6))
plt.plot(ves - f_opt, color=COLORS['ves'], label='Vanilla ES')
plt.plot(gd - f_opt, color=COLORS['sgdm'], label='Grad. Descent')
plt.plot(ges - f_opt, color=COLORS['ges'], label='Guided ES')
plt.legend(fontsize=16, loc=0)
plt.xlabel('Iteration', fontsize=16)
plt.ylabel('Loss', fontsize=16)
plt.title('Demo of Guided Evolutionary Strategies', fontsize=16);


I am working on the file "Guided_Evolutionary_Strategies_Demo.ipynb".
The first task is:
Demo
Vanilla ES
First, we run minimize the problem using vanilla evolutionary strategies.
Can you write Python code for it?

tf.reset_default_graph()
loss_fn, gav = perturbed_quadratic(1000, 2000, 2)

updates = vanilla_es(loss_fn, gav, sigma=0.1, beta=1.0)

opt = tf.train.GradientDescentOptimizer(0.2)
train_op = opt.apply_gradients(updates)

loss = loss_fn()

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

# train
fobj = []
for k in range(10000):
  f, _ = sess.run([loss, train_op])
  fobj.append(f)

# store results for plotting
ves = np.array(fobj).copy()

sess.close()


I am working on the file "Guided_Evolutionary_Strategies_Demo.ipynb".
The first task is:
Gradient descent
Our next baseline is gradient descent, applied directly to the biased gradients.
Can you write Python code for it?

tf.reset_default_graph()
loss_fn, gav = perturbed_quadratic(1000, 2000, 2)

updates = gradient_descent(loss_fn, gav)

opt = tf.train.GradientDescentOptimizer(5e-3)
train_op = opt.apply_gradients(updates)

loss = loss_fn()

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

# train
fobj = []
for k in range(10000):
  f, _ = sess.run([loss, train_op])
  fobj.append(f)

# store results for plotting
gd = np.array(fobj).copy()

sess.close()


I am working on the file "Guided_Evolutionary_Strategies_Demo.ipynb".
The first task is:
Guided ES
Finally, we will run the same problem using the guided evolutionary strategies method.
Can you write Python code for it?

tf.reset_default_graph()
loss_fn, gav = perturbed_quadratic(1000, 2000, 2)

updates = guided_es(loss_fn, gav, sigma=0.1, alpha=0.5, beta=2.0)

opt = tf.train.GradientDescentOptimizer(0.2)
train_op = opt.apply_gradients(updates)
loss = loss_fn()

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

# train
fobj = []
for k in range(10000):
  f, _ = sess.run([loss, train_op])
  fobj.append(f)

# store results for plotting
ges = np.array(fobj).copy()

sess.close()


I am working on the file "Guided_Evolutionary_Strategies_Demo.ipynb".
The first task is:
Guided ES
Guided ES is our proposed method. It uses a diagonal plus low-rank covariance matrix for drawing perturbations, where the low-rank subspace is spanned by the available gradient information.
Can you write Python code for it?

def guided_es(loss_fn, grads_and_vars, sigma=0.1, alpha=0.5, beta=1.0):

  def vardist(grad, variable):
    """Builds the sampling distribution for the given variable."""

    n = tf.cast(variable.shape[0], tf.float32)
    k = 1

    a = sigma * tf.sqrt(alpha / n)
    c = sigma * tf.sqrt((1-alpha) / k)
    b = tf.sqrt(a ** 2 + c ** 2) - a

    scale_diag = a * tf.ones(tf.cast(n, tf.int32))
    perturb_diag = b * tf.ones(1,)
    perturb_factor, _ = tf.qr(grad)

    return mvn_lowrank(scale_diag=scale_diag,
                       scale_perturb_factor=perturb_factor,
                       scale_perturb_diag=perturb_diag)

  dists = {v.op.name: vardist(g, v) for g, v in grads_and_vars}

  # antithetic getter
  updates_and_vars = evostrat_update(loss_fn, dists, grads_and_vars, beta, sigma)

  return updates_and_vars


I am working on the file "Guided_Evolutionary_Strategies_Demo.ipynb".
The first task is:
Guided ES Demo
This is a fully self-contained notebook that reproduces the toy example in Fig.1 of the guided evolutionary strategies paper.
The main code is in the 'Algorithms' section below.
Contact: nirum@google.com
Date: 6/22/18
Can you write Python code for it?

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_probability as tfp

print(f'tensorflow version: {tf.__version__}')
print(f'tensorflow_probability version: {tfp.__version__}')


I am working on the file "Guided_Evolutionary_Strategies_Demo.ipynb".
The first task is:
Noise distributions
We draw perturbations of parameters from either a diagonal covariance (the standard evolutionary strategies algorithm), or from a diagonal plus low rank covariance (guided ES).
Can you write Python code for it?

mvn_diag = tfp.distributions.MultivariateNormalDiag
mvn_lowrank = tfp.distributions.MultivariateNormalDiagPlusLowRank


I am working on the file "Guided_Evolutionary_Strategies_Demo.ipynb".
The first task is:
Plots
Can you write Python code for it?

A, b, _ = generate_problem(1000, 2000, seed=2)
xstar = np.linalg.lstsq(A, b, rcond=None)[0]
f_opt = (0.5/2000) * np.linalg.norm(np.dot(A, xstar) - b) ** 2


I am working on the file "Guided_Evolutionary_Strategies_Demo.ipynb".
The first task is:
Vanilla ES
Vanilla ES is the standard evolutionary strategies algorithm. It uses a diagonal covariance matrix for perturbing parameters.
Can you write Python code for it?

def vanilla_es(loss_fn, grads_and_vars, sigma=0.1, beta=1.0):

  def vardist(v):
    n = v.shape[0]
    scale_diag = (sigma / tf.sqrt(tf.cast(n, tf.float32))) * tf.ones(n)
    return mvn_diag(scale_diag=scale_diag)

  # build distributions
  dists = {v.op.name: vardist(v) for _, v in grads_and_vars}

  updates_and_vars = evostrat_update(loss_fn, dists, grads_and_vars, beta, sigma)

  return updates_and_vars


I am working on the file "Guided_Evolutionary_Strategies_Demo_Jax.ipynb".
The first task is:
Algorithm 3: Guided Evolutionary Strategies (Guided ES)
Guided ES is our proposed method. It uses a diagonal plus low-rank covariance matrix for drawing perturbations, where the low-rank subspace is spanned by the available gradient information.
This allows it to incorporate the biased gradient information, while still minimizing the true loss function.
Can you write Python code for it?

# Hyperparameters for Guided ES
sigma = 0.1
alpha = 0.5
beta = 1.0
k = 1        # Defines the dimensionality of the low-rank subspace.

# Defines parameters of the distribution for sampling perturbations.
a = sigma * np.sqrt(alpha / float(n))
c = sigma * np.sqrt((1. - alpha) / float(k))

# Defines the distribution for sampling parameter perturbations.
def sample(prng_key, gradient_subspace):
    keys = jax.random.split(prng_key, 2)

    epsilon_full = jax.random.normal(keys[0], shape=(n, 1))
    epsilon_subspace = jax.random.normal(keys[1], shape=(k, 1))

    Q, _ = jnp.linalg.qr(gradient_subspace)
    epsilon = a * epsilon_full + c * jnp.dot(Q, epsilon_subspace)

    return epsilon

init_opt, update_opt, get_params = optimizers.sgd(0.2)
base_key = jax.random.PRNGKey(0)

@jax.jit
def step_fun(step, opt_state):
    prng_key = jax.random.fold_in(base_key, step)

    x = get_params(opt_state)
    loss, gradient = loss_and_grad_fun(x)
    epsilon = sample(prng_key, gradient)

    # We utilize antithetic (positive and negative) samples.
    f_pos, _ = loss_and_grad_fun(x + epsilon)
    f_neg, _ = loss_and_grad_fun(x - epsilon)

    # This update is a stochastic finite difference estimate of the true gradient.
    update = (beta / (2 * sigma ** 2)) * (f_pos - f_neg) * epsilon
    opt_state = update_opt(step, update, opt_state)

    return loss, opt_state

%%time

x = jnp.zeros((n, 1))
opt_state = init_opt(x)

# Run the optmizer.
fobj = []
for step in range(10000):
    loss, opt_state = step_fun(step, opt_state)
    fobj.append(loss)

# Store training curve for plotting later.
f_ges = np.array(fobj).copy()

COLORS = {'ges': '#7570b3', 'ves': '#1b9e77', 'sgdm': '#d95f02'}
plt.figure(figsize=(8, 6))
plt.plot(f_ves - f_star, color=COLORS['ves'], label='Vanilla ES')
plt.plot(f_gd - f_star, color=COLORS['sgdm'], label='Grad. Descent')
plt.plot(f_ges - f_star, color=COLORS['ges'], label='Guided ES')
plt.legend(fontsize=16, loc=0)
plt.xlabel('Iteration', fontsize=16)
plt.ylabel('Loss',...


I am working on the file "Guided_Evolutionary_Strategies_Demo_Jax.ipynb".
The first task is:
Guided Evolutionary Strategies Demo (using Jax)
Date: 06/08/2020
This is a self-contained notebook that reproduces the toy example in Fig. 1 of the Guided ES paper.
Can you write Python code for it?

import numpy as np
import matplotlib.pyplot as plt

import jax
import jax.numpy as jnp
from jax.experimental import optimizers

print(f'Jax version: {jax.__version__}')


I am working on the file "Guided_Evolutionary_Strategies_Demo_TensorFlow2.ipynb".
The first task is:
Algorithm 1: Gradient Descent
Our first algorithm is gradient descent, applied directly to the biased gradients.
Can you write Python code for it?

opt = tf.keras.optimizers.SGD(5e-3)

@tf.function
def step_fun(x):
    loss, gradient = loss_and_grad_fun(x)
    opt.apply_gradients([(gradient, x)])
    return loss

%%time

x = tf.Variable(tf.zeros((n, 1)), dtype=tf.float32)

fobj = []
for _ in range(10000):
    fobj.append(step_fun(x))

# Store training curve for plotting later.
f_gd = tf.stack(fobj).numpy().copy()


I am working on the file "Guided_Evolutionary_Strategies_Demo_TensorFlow2.ipynb".
The first task is:
Algorithm 2: Vanilla Evolutionary Strategies (Vanilla ES)
Our next algorithm is vanilla (standard) evolutionary strategies. This is a zeroth-order optimization algorithm, which means that it only uses the function evaluation (and ignores the biased gradients).
Can you write Python code for it?

# Hyperparameters for Vanilla ES
sigma = 0.1
beta = 1.0
learning_rate = 0.2

# Defines the distribution for sampling parameter perturbations.
scale = sigma / np.sqrt(n)
def sample():
    return scale * tf.random.normal(shape=(n, 1), dtype=tf.float32)

opt = tf.keras.optimizers.SGD(learning_rate)

@tf.function
def step_fun(x):
    epsilon = sample()

    # We utilize antithetic (positive and negative) samples.
    f_pos, _ = loss_and_grad_fun(x + epsilon)
    f_neg, _ = loss_and_grad_fun(x - epsilon)

    # This update is a stochastic finite difference estimate of the true gradient.
    update = (beta / (2 * sigma ** 2)) * (f_pos - f_neg) * epsilon
    opt.apply_gradients([(update, x)])

    return loss_and_grad_fun(x)[0]

%%time

x = tf.Variable(tf.zeros((n, 1)), dtype=tf.float32)

# Run the optmizer.
fobj = []
for _ in range(10000):
    fobj.append(step_fun(x))

# Store training curve for plotting later.
f_ves = tf.stack(fobj).numpy().copy()


I am working on the file "Guided_Evolutionary_Strategies_Demo_TensorFlow2.ipynb".
The first task is:
Algorithm 3: Guided Evolutionary Strategies (Guided ES)
Guided ES is our proposed method. It uses a diagonal plus low-rank covariance matrix for drawing perturbations, where the low-rank subspace is spanned by the available gradient information.
This allows it to incorporate the biased gradient information, while still minimizing the true loss function.
Can you write Python code for it?

# Hyperparameters for Guided ES
sigma = 0.1
alpha = 0.5
beta = 1.0
k = 1        # Defines the dimensionality of the low-rank subspace.

# Defines parameters of the distribution for sampling perturbations.
a = sigma * np.sqrt(alpha / float(n))
c = sigma * np.sqrt((1. - alpha) / float(k))

def sample(gradient_subspace):
    epsilon_full = tf.random.normal(shape=(n, 1), dtype=tf.float32)
    epsilon_subspace = tf.random.normal(shape=(k, 1), dtype=tf.float32)

    Q, _ = tf.linalg.qr(gradient_subspace)
    epsilon = a * epsilon_full + c * tf.matmul(Q, epsilon_subspace)

    return epsilon

opt = tf.keras.optimizers.SGD(0.2)

@tf.function
def step_fun(x):

    # We pass the gradient to our sampling function.
    loss, gradient = loss_and_grad_fun(x)
    epsilon = sample(gradient)

    # We utilize antithetic (positive and negative) samples.
    f_pos, _ = loss_and_grad_fun(x + epsilon)
    f_neg, _ = loss_and_grad_fun(x - epsilon)

    # This update is a stochastic finite difference estimate of the true gradient.
    update = (beta / (2 * sigma ** 2)) * (f_pos - f_neg) * epsilon
    opt.apply_gradients([(update, x)])

    return loss_and_grad_fun(x)[0]

%%time

x = tf.Variable(tf.zeros((n, 1)), dtype=tf.float32)

# Run the optmizer.
fobj = []
for _ in range(10000):
    fobj.append(step_fun(x))

# Store training curve for plotting later.
f_ges = tf.stack(fobj).numpy().copy()

COLORS = {'ges': '#7570b3', 'ves': '#1b9e77', 'sgdm': '#d95f02'}
plt.figure(figsize=(8, 6))
plt.plot(f_ves - f_star, color=COLORS['ves'], label='Vanilla ES')
plt.plot(f_gd - f_star, color=COLORS['sgdm'], label='Grad. Descent')
plt.plot(f_ges - f_star, color=COLORS['ges'], label='Guided ES')
plt.legend(fontsize=16, loc=0)
plt.xlabel('Iteration', fontsize=16)
plt.ylabel('Loss', fontsize=16)
plt.title('Demo of Guided Evolutionary Strategies', fontsize=16);


I am working on the file "Guided_Evolutionary_Strategies_Demo_TensorFlow2.ipynb".
The first task is:
Guided Evolutionary Strategies Demo (using TensorFlow 2)
Date: 06/08/2020
This is a self-contained notebook that reproduces the toy example in Fig. 1 of the Guided ES paper.
Can you write Python code for it?

import numpy as np
import matplotlib.pyplot as plt

import tensorflow as tf

print(f'TensorFlow version: {tf.__version__}')


I am working on the file "Guides/algorithms/Sorting.ipynb".
The first task is:
Heapsort
Create a max heap with the input array and then swap the first and last element. Reduce sort space by 1 and continue until only one element is in the sort space.
Can you write Python code for it?

def heapify(arr, root, n):
    largest = root
    l = 2*root+1
    r = 2*root+2

    if l > len(arr)-1:
        return arr

    if l < n and arr[l] > arr[largest]:
        largest = l
    if r < len(arr)-1:
        if r < n and arr[r] > arr[largest]:
            largest = r
    if largest != root:
        arr[root], arr[largest] = arr[largest], arr[root]
        heapify(arr, root, n)

def swap(arr,i,j):
    arr[i], arr[j] = arr[j], arr[i]

def heapsort(arr):
    n = len(arr)
    while n > 1:
        for i in range(0,n):
            heapify(arr,i,n)
        swap(arr,0,n-1)
        n -= 1

    return arr


I am working on the file "Guides/algorithms/Sorting.ipynb".
The first task is:
Quicksort
Generally fastest sorting algorith. Uses a divide an conquere scheme.
Can you write Python code for it?

def quicksort(arr,low,high):
    pivot = arr[low]  # pivot on the first value in the array
    j = low  # index of smaller element
    for i in range(low,high):
        if arr[i] <= pivot:
            print('swap')
            swap(arr,i,j)
            j = j+1
    if low < high:
        quicksort(arr,low,low-1)
        quicksort(arr,low+1,high)
    return arr

arr = np.array([0, 7, 2, 4, 6, 1, 0, 9, 3, 5])
print(quicksort(arr,0,len(arr)-1))


I am working on the file "Guides/experimentalTechniquesAndSensors/Filters-FirstOrder.ipynb".
The first task is:
High Pass Filter
A first order high-pass filter similarly uses a resistor and capacitor, however, the output voltage is measured across the resistor.

The response of the high-pass filter is,
$$ V_o = V_i \frac{\omega R C}{\sqrt{1+ (\omega R C)^2}}$$
Can you write Python code for it?

Vo_hp = Vi*(omega*tau)/np.sqrt(1+(omega*tau)**2)

Gdb_hp = 20*np.log10(Vo_hp/Vi)  # Where Gdb is the power

# plot with plotly
# Create traces
legend = ['High Pass Gain']
tracehp = go.Scatter(
    x=np.log10(omega),
    y=Gdb_hp,
    mode='lines',
    name=legend[0]
)

# Edit the layout
layout = dict(title='Output Voltage of First Order High-Pass Filter vs. Time',
              xaxis=dict(title='Log[Frequency (Hz)]'),
              yaxis=dict(title='Power Gain (dB)'),
              )
data = [tracehp]  # put trace in array (plotly formatting)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename="FirstOrderHPFilter")


I am working on the file "Guides/experimentalTechniquesAndSensors/Filters-FirstOrder.ipynb".
The first task is:
Low-Pass (LP) Filter
A low pass filter uses a resistor and capacitor with the voltage read across the capacitor and the input voltage into the resistor. The reactance of the capacitor blocks low frequency signals.

The response of the low pass filter is,
$$ V_o = V_i \frac{1}{\sqrt{1+(\omega R C)^2}} $$
Where $ \omega $ is the frequency and $RC$ is $ \tau $.
Can you write Python code for it?

# inputs
Vi = 5  # volts

# define a linear space of omega that is significantly greatr that fc
omega = np.linspace(0.01*fc,fc*5,1000)
Vo_lp = Vi*1/np.sqrt(1+(omega*tau)**2)

Gdb_lp = 20*np.log10(Vo_lp/Vi)  # Where Gdb is the power

# plot with plotly
# Create traces
legend = ['Low Pass Gain']
tracelp = go.Scatter(
    x=np.log10(omega),
    y=Gdb_lp,
    mode='lines',
    name=legend[0]
)

# Edit the layout
layout = dict(title='Output Voltage of First Order Low-Pass Filter vs. Time',
              xaxis=dict(title='Log[Frequency (Hz)]'),
              yaxis=dict(title='Power Gain (dB)'),
              )
data = [tracelp]  # put trace in array (plotly formatting)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename="FirstOrderLPFilter")


I am working on the file "Guides/experimentalTechniquesAndSensors/Filters-FirstOrder.ipynb".
The first task is:
Now, overlaying the graphs
Can you write Python code for it?

# Edit the layout
layout = dict(title='Output Voltage of First Order High-Pass and Low-Pass Filter vs. Time',
              xaxis=dict(title='Log[Frequency (Hz)]'),
              yaxis=dict(title='Power Gain (dB)'),
              )
data = [tracehp, tracelp]  # put trace in array (plotly formatting)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename="FirstOrderFilters")


I am working on the file "Guides/experimentalTechniquesAndSensors/Filters-FirstOrder.ipynb".
The first task is:
Now, we can find the $-3 dB$ frequency with this discrete data set if we interoplate, or if we just look at the graph and find the point where the Gain in dB is $-3$.
Can you write Python code for it?

freq3db = 0 # logarithmic frequency at -3db
freqcut = 10**freq3db
print('Therefore, the cutoff frequency is', freqcut, 'Hz')


I am working on the file "Guides/filtering/State Estimation Using a GPS and Accelerometer.ipynb".
The first task is:
Although not immediately intuitive, the top block of the Kalman gain matrix is the identity matrix. This is because we are defining our measurements as the error between the euler integrated velocity and position and the accelerometer measured. Since the corruption in the accelerometer and GPS measurements is a zero-mean white noise process, the bias is the driving influence in our measurements and is the only term that the Kalman filter needs to correct. The orthogonality checks that follow further confirm the validity of our Kalman filter derivation.
Error Variance
The ensamble average produces the average error variance, $P^{ave}$.
$$ P^{ave}k = \frac{1}{N{ave}-1} \sum_{l=t}^{N_{ave}}[e^l_k - e^{ave}_k][e^l_k-e^{ave}_k]^T $$
where $N_{ave} - 1$ is used for an unbiased variance from sample theory. An important check for the validity of the Kalman filter is the difference between the ensamble average variance and the variance for a single Kalman Filter realization.
$$ P^{ave}_k - P_k \approx 0 $$
This is computed below, where $P_k$ is the error variance for a single realization.
Can you write Python code for it?

j = 0
k = 1

Pave = np.zeros((ns,ns,time_steps_acc))  # error variance averaged over all runs
Peach = np.zeros((ns,ns,time_steps_acc,Nsimulations))  # error variances for each simulation
error_P = np.zeros((ns,ns,time_steps_acc,Nsimulations))
error_P_mean = np.zeros((ns,ns,time_steps_acc))

for idx_t, current_time in enumerate(time):
    for idx_sim in range(0,Nsimulations):
        el = np.array([[error_x[idx_t,idx_sim],error_v[idx_t,idx_sim],
                        error_bias[idx_t,idx_sim]]])
        el_ave = np.array([[ave_error_x[idx_t],ave_error_v[idx_t],
                            ave_error_bias[idx_t]]])

        Peach[:,:,idx_t,idx_sim] = np.matmul((el-el_ave).T,(el-el_ave))
        Pave[:,:,idx_t] += 1./Nsimulations*Peach[:,:,idx_t,idx_sim]
        error_P[:,:,idx_t,idx_sim] = Pave[:,:,idx_t] - Peach[:,:,idx_t,idx_sim]
        error_P_mean[:,:,idx_t] += 1./Nsimulations*error_P[:,:,idx_t,idx_sim]


I am working on the file "Guides/filtering/State Estimation Using a GPS and Accelerometer.ipynb".
The first task is:
Filtered Position, Velocity, and Acceleration for a Single Realization
Can you write Python code for it?

fig, ax = plt.subplots(3,1,sharex=True,figsize=(15,10))

# extract the gps measurements
idx_meas = np.where(z[0,0,:] != 0)
z1 = z[0,0,idx_meas]
z2 = z[1,0,idx_meas]
time_gps = time[idx_meas]

ax[0].plot(time, x_truth-np.squeeze(xc))
ax[0].plot(time_gps, np.squeeze(z1))
ax[0].plot(time, x_truth-np.squeeze(xc)-np.squeeze(state[0,:,:]), '--g')
ax[0].set_ylabel('Position $(m)$',size=fsize)
ax[0].legend(['$p_E-p_c$', 'GPS Measurement', 'Filtered Error'], prop={'size': 13})

ax[1].plot(time, v_truth-np.squeeze(vc))
ax[1].plot(time_gps, np.squeeze(z2))
ax[1].plot(time, v_truth-np.squeeze(vc)-np.squeeze(state[1,:,:]), '--g')
ax[1].set_ylabel('Velocity $(m/s)$',size=fsize)
ax[1].legend(['$v_E-v_c$', 'GPS Measurement', 'Filtered Error'], prop={'size': 13})

ax[2].plot(time, a_truth-np.squeeze(ac))
ax[2].axhline(y=-b, color='r')
ax[2].plot(time, a_truth-np.squeeze(ac)+np.squeeze(state[2,:,:]), '--g')
ax[2].set_ylabel('Acceleration $(m/s^2)$',size=fsize)
ax[2].set_xlabel('Time $(s)$', size=fsize)
ax[2].legend(['$a_E-a_c$', 'Bias Value', 'Filtered Error'], prop={'size': 13})

fig.suptitle('State Error and Measurements for One Realization',size=22,y=0.93)

for i in range(0,3):
    ax[i].grid()

plt.show()


I am working on the file "Guides/filtering/State Estimation Using a GPS and Accelerometer.ipynb".
The first task is:
It is clear from the figure above that the ensamble averaged error is approximately zero for each of the states. However, further checks need to be evoked to evaluate the performance of the Kalman Filter.
Therefore, we have validated that $e^{ave}_k \approx 0 \: \forall \:k \:\in\: [0,30]$.
Kalman Gain Evolution
It is intesting to see how the Kalman gain evolves for a single realization.
Can you write Python code for it?

Kidx = np.where(K[0,0,:]!=0)
Kgain = K[:,:,Kidx]
t_idx = time[Kidx]

fig, ax = plt.subplots(3,2,sharex=True,figsize=(15,10))

ax[0,0].scatter(t_idx, np.squeeze(Kgain[0,0,:]))
ax[0,0].set_ylabel('K[0,0]', size=fsize)
ax[1,0].scatter(t_idx, np.squeeze(Kgain[1,0,:]))
ax[1,0].set_ylabel('K[1,0]', size=fsize)
ax[0,1].scatter(t_idx, np.squeeze(Kgain[0,1,:]))
ax[0,1].set_ylabel('K[0,1]', size=fsize)
ax[1,1].scatter(t_idx, np.squeeze(Kgain[1,1,:]))
ax[1,1].set_ylabel('K[1,1]', size=fsize)
ax[2,1].scatter(t_idx, np.squeeze(Kgain[2,1,:]))
ax[2,1].set_xlabel('Time (s)', size=fsize)
ax[2,1].set_ylabel('K[2,1]', size=fsize)
ax[2,0].scatter(t_idx, np.squeeze(Kgain[2,0,:]))
ax[2,0].set_ylabel('K[2,0]', size=fsize)
ax[2,0].set_xlabel('Time (s)', size=fsize)

fig.suptitle('Kalman Gain Matrix Elements',size=25,y=0.93)

for i in range(0,3):
    for j in range(0,2):
        ax[i,j].grid()

plt.show()


I am working on the file "Guides/filtering/State Estimation Using a GPS and Accelerometer.ipynb".
The first task is:
Monte Carlo Averaged Error
We expect the ensamble averaged estimated position, velocity, and bias error to be close to zero. The estimation error is defined as the true state less the corrupted state and the calculated error.
$$ \bar{e}k = \delta x_k - \bar{\delta x}_k $$
$$e^{ave} = \frac{1}{N{ave}} \sum^{N_{ave}}_{l=1} e^{l}_k $$
Can you write Python code for it?

fig, ax = plt.subplots(3,1,sharex=True,figsize=(15,10))

ax[0].plot(time, ave_error_x)
ax[0].set_ylabel('Position Error $(m)$', size=fsize)
ax[1].plot(time, ave_error_v)
ax[1].set_ylabel('Velocity Error $(m/s)$', size=fsize)
ax[2].plot(time, ave_error_bias)
ax[2].set_ylabel('Bias Error $(m/s^2)$', size=fsize)
ax[2].set_xlabel('Time (s)', size=fsize)

fig.suptitle('Averaged Ensamble Error',size=25,y=0.93)

for i in range(0,3):
    ax[i].grid()

plt.show()


I am working on the file "Guides/filtering/State Estimation Using a GPS and Accelerometer.ipynb".
The first task is:
The plot above indicates that the estimated state is approximately orthogonal to the error in the state estimation as expected. The significant magnitude in the (2,1) and (2,2) element of the correlation matrix is likely a result of the white noise processes. This was confirmed by varying the white noise processes and seeing a resulting increase or decrease in the magnitude of the (2,1) element.
As a last check, we need to verify the independence of the residual, or innovations process, term. This is again computed with an auto-correlation function.
$$ \frac{1}{N_{ave}-1} \sum_{l=t}^{N_{ave}}r^l_k(r^l_m)^T \: \forall \: m < k$$
The correlation of the residuals is evaluated at a single single choice of $m$ and $k$ below.
Can you write Python code for it?


I am working on the file "Guides/heatTransfer/Conduction.ipynb".
The first task is:
These values can be used to calculate $\dot{Q}$ the rate of heat transfer. This can be related to a more physically relevant value, $\dot{q}$, the heat flux. Where,
$$ \dot{q} = \frac{\dot{q}}{A} = \frac{T_1 - T_4}{RA} $$
$$ RA = \sum_{n}{AR_n} $$
Can you write Python code for it?

q = float(T1 - T4)/np.sum(AR)
print('q = ', q, 'W/m^2')  # W/m^2


I am working on the file "Guides/python/betterGraphs-bokeh.ipynb".
The first task is:
Now lets plot something pretty. To start with, we are going to plot a Lorenz System. The code below is taken from a bokeh demo
Can you write Python code for it?

sigma = 10
rho = 28
beta = 8.0/3
theta = 3 * np.pi / 4

def lorenz(xyz, t):
    x, y, z = xyz
    x_dot = sigma * (y - x)
    y_dot = x * rho - x * z - y
    z_dot = x * y - beta* z
    return [x_dot, y_dot, z_dot]

initial = (-10, -7, 35)
t = np.arange(0, 100, 0.006)

solution = odeint(lorenz, initial, t)

x = solution[:, 0]
y = solution[:, 1]
z = solution[:, 2]
xprime = np.cos(theta) * x - np.sin(theta) * y

colors = ["#C6DBEF", "#9ECAE1", "#6BAED6", "#4292C6", "#2171B5", "#08519C", "#08306B",]

p = figure(title="lorenz example")

p.multi_line(np.array_split(xprime, 7), np.array_split(z, 7),
             line_color=colors, line_alpha=0.8, line_width=1.5)
show(p)  # open a browser


I am working on the file "Guides/python/betterGraphs-bokeh.ipynb".
The first task is:
You'll notice that we didn't import the entire bokeh library, rather, we imported specific modules and methods. This is to prevent your python script from using up more computation time and memory than is necessary. Bokeh is a large library, and importing the entire package is rather reckless.

The first thing that we are going to do is allow the bokeh library to produce graphs in a python notebook.
Can you write Python code for it?


I am working on the file "Guides/python/csvToPandas.ipynb".
The first task is:
Our data is now accessible by a key value. The keys are the column headers in the dataframe. In this example case, those are 'Time (s) - Dev1/ai0' and  'Amplitude - Dev1/ai0'. For example, lets access the data in the first column.
Can you write Python code for it?


I am working on the file "Guides/python/csvToPandas.ipynb".
The first task is:
So lets say you have a large dataframe with unknown columns. There is a simple way to index them without having prior knowledge of what the dataframe columns are. Namely, the columns method in pandas.
Can you write Python code for it?

cols = df.columns
for col in cols:
    print(df[col])


I am working on the file "Guides/python/csvToPandas.ipynb".
The first task is:
Step 3
Great! Now lets read the data into a dataframe called df.
This will allow our data to be accessible by the string in the header.
Can you write Python code for it?

df = pd.read_csv(filePath,header=0)
print(df)


I am working on the file "Guides/python/excelToPandas.ipynb".
The first task is:
Data Manipulation (Plots)
Now that we have the data easily accessible in python, lets look at how to plot it. Pandas allows you to use matplotlib to plot, however it is done using methods built into pandas.
Although the methods to create an manipulate plots are built into Pandas, we will still have to import matplotlib to save and show the plots.
Can you write Python code for it?


I am working on the file "Guides/python/excelToPandas.ipynb".
The first task is:
In order to demonstrate the plotting capabilities of pandas arrays, lets use the example data that we imported earlier. The data frame contains only the two columns that were in the file; temperature and time. Because of this simplicity, we can trust pandas to properly interpret the first column as time and the second column as th measurement (temperature). Thus we can plot with the simple command.
df.plot()
Can you write Python code for it?


I am working on the file "Guides/python/excelToPandas.ipynb".
The first task is:
Note: There is probably a better way of doing this (i.e. without a loop, but I'm tired and can't think of anything right now)
Can you write Python code for it?

plt.figure(4)
plt.plot(timeArray, df[cols[1]], 'b')
plt.title('This is a graph with a better time axis')
plt.ylabel('Temperature (deg F)')
plt.xlabel('Time (s)')
plt.grid()
plt.show()


I am working on the file "Guides/python/excelToPandas.ipynb".
The first task is:
While this simplification is nice, it is generally better to specify what data you want to plot. Particularly if you are automating the plotting of a large set of dataframes. To do this, specify the x and y arrays in your dataframe as you would in a standard matplotlib plot call, however since this plotting function is a method of the dataframe, you need only specify the column.
I.e.
Can you write Python code for it?

plt.figure(2)
ax = df.plot(cols[0],cols[1])
plt.show()


I am working on the file "Guides/python/plotly-basic-plotting.ipynb".
The first task is:
Creating the plot
Plots in plotly are generated using the graph_objs object from the plotly library.
Additionally, the layout is declared in a dictionary with parameters  title, xaxis, and yaxis, where  xaxis  and  yaxis  also are declared as dictionaries with one entry,  title
Can you write Python code for it?

# Create a trace
trace = go.Scatter(
    x = df[cols[0]],
    y = df[cols[1]]
)

data = [trace]

# Edit the layout
layout = dict(title='Temperature vs. Time',
              xaxis=dict(title='Time'),
              yaxis=dict(title='Temperature (C)'),
              )

fig = dict(data=data, layout=layout)
py.iplot(fig, filename='Thermal Data')


I am working on the file "Guides/python/plotly-basic-plotting.ipynb".
The first task is:
Plotly Basics of Plotting
Importing Data
Let's start by importing our data using the methods described in the excelToPandas notebook.
Can you write Python code for it?

dirPath = os.path.realpath('.')
fileName = 'assets/coolingExample.xlsx'
filePath = os.path.join(dirPath, fileName)
df = pd.read_excel(filePath,header=0)
cols = df.columns


I am working on the file "Guides/python/timing.ipynb".
The first task is:
As we can see, currentTime is an instance of a class, struct_time as is defined in the time library.
The string that was returned when we ran print(currentTime) showed us the values associated with several of the class variables. We can access these class variables by using the dot "." notation.
Can you write Python code for it?


I am working on the file "Guides/python/timing.ipynb".
The first task is:
UTC and local time
time has methods which return the UTC time and local time in a somewhat useless structure format.

time.gmtime() returns the structure with the current gm or UTC time.

time.localtime() returns a structure with the current local time

Can you write Python code for it?

currentTime = time.gmtime()
print(currentTime)


I am working on the file "Guides/python/timing.ipynb".
The first task is:
With this knowledge, it becomes trivial to parse this data into a more usable format.
Can you write Python code for it?

def parseTime(timeObj):
    '''parseTime:
    takes time.struct_time instances
    :return  time displayed as string -  year month day hour min sec'''
    return (str(timeObj.tm_year) + str(timeObj.tm_mon) + str(timeObj.tm_mday) +
            str(timeObj.tm_hour) + str(timeObj.tm_min) + str(timeObj.tm_sec))

parseTime(currentTime)


I am working on the file "Guides/stacksGuides/Stacks_Getting_Started.ipynb".
The first task is:
If you are able to ping it successfully, great! The Stacks is now communicating with your computer.
*   If the ping failed, try assigning a static IP to your computer.
*   Assign IP: 192.168.1.40 and Subnet mask: 255.255.255.0
    +   On Windows 10, right-click on the Wi-Fi icon, and click on Open Network and Sharing Center.
    +   Click on Change adapter settings.
    +   Right-click on your Ethernet connection(the one with a cable), and select Properties.
    +   Select Internet Protocol Version 4(TCP/IPv4).
    +   Click the Properties button.
    +   When the window pops up, click on Use the following IP address, and enter the following information:
        -    IP: 192.168.1.40
        -    Subnet mast: 255.255.255.0
Run the following script to verify that everything works:
Can you write Python code for it?

import subinitial.stacks1 as stacks
core = stacks.Core(host="192.168.1.49")
core.print_console("id")


I am working on the file "Guides/stacksGuides/Stacks_Getting_Started.ipynb".
The first task is:
You should see the following output:
Stacks Library Major Version: 1

Setting up your Stacks
For best results, use the Stacks with a laptop with a Wi-Fi connection.
Connect an Ethernet cable from the Stacks to your computer. Verify that the second light from the top in the light bank
is lit.
*   If this light is not lit, verify the connection.
Open your terminal or command prompt, and type the following command:
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Pmaxian.ipynb".
The first task is:
1.13 Create distance matrix
Can you write Python code for it?

t1 = time.time()
All_Neigh_Dist = ntw.allneighbordistances(sourcepattern=ntw.pointpatterns['Rand_Points_CLIENT'],
                                             destpattern=ntw.pointpatterns['Rand_Points_SERVICE'])
All_Dist_MILES = All_Neigh_Dist * float(10000/90) * 0.6214
seconds = round(time.time()-t1, 4)
print seconds, 'seconds'
print 'Client [i] x Service [j] Matrix Shape --> ', All_Dist_MILES.shape


I am working on the file "Gurobi_v_Cplex__Pmaxian.ipynb".
The first task is:
1.2 Instantiate all graphs to be drawn
Can you write Python code for it?

# Roads and Nodes
g = nx.Graph()
# Graph of Roads and Nodes
g1 = nx.MultiGraph()
# Clients
GRAPH_client = nx.Graph()
# Snapped Clients
g_client = nx.Graph()
# Service
GRAPH_service = nx.Graph()
# Snapped Service
g_service = nx.Graph()
# Gurobi p-Maxian
GUROBI_maxian_g = nx.Graph()
# Cplex p-Maxian
CPLEX_maxian_g = nx.Graph()


I am working on the file "Gurobi_v_Cplex__Pmaxian.ipynb".
The first task is:
1.4 Create numpy arrays of random floats within a  bounding box
Can you write Python code for it?

lat_client = np.random.uniform(shp_W.bbox[0], shp_W.bbox[2], 100)
lon_client = np.random.uniform(shp_W.bbox[1], shp_W.bbox[3], 100)
lat_service = np.random.uniform(shp_W.bbox[0], shp_W.bbox[2], 50)
lon_service = np.random.uniform(shp_W.bbox[1], shp_W.bbox[3], 50)


I am working on the file "Gurobi_v_Cplex__Pmaxian.ipynb".
The first task is:
1.5 Zip the latitude and longitude lists together
Can you write Python code for it?

rand_coords_client = map(list, zip(lat_client, lon_client))
rand_coords_service = map(list, zip(lat_service, lon_service))


I am working on the file "Gurobi_v_Cplex__Pmaxian.ipynb".
The first task is:
1.7 Fill dictionaries of random roints
Can you write Python code for it?

# CLIENT
for idx, coords in enumerate(rand_coords_client):
    GRAPH_client.add_node(idx)
    points_client[idx] = coords
    GRAPH_client.node[idx] = coords
# SERVICE
for idx, coords in enumerate(rand_coords_service):
    GRAPH_service.add_node(idx)
    points_service[idx] = coords
    GRAPH_service.node[idx] = coords


I am working on the file "Gurobi_v_Cplex__Pmaxian.ipynb".
The first task is:
1.8 Draw network, simplified network, and random client & service nodes
Can you write Python code for it?

print dt.datetime.now()
#Instantiate Figure
figsize(10,10)
# Draw Graph of Actual Nodes and Roads
for e in ntw.edges:
    g.add_edge(*e)
nx.draw(g, ntw.node_coords, node_size=5, alpha=0.25, edge_color='r', width=2)
# Draw only unique edges in graph
for e in ntw.graphedges:
    g1.add_edge(*e)
    # highlights cases where start and end node are the same
    if e[0]==e[1]:
        g1.add_node(e[0])
for node_id in g1.node:
    g1.node[node_id] = ntw.node_coords[node_id]
nx.draw(g1, ntw.node_coords, node_size=10, alpha=0.5)
# Draw Graph of Random Client Points
nx.draw(GRAPH_client, points_client,
    node_size=75, alpha=1, node_color='b')
# Draw Graph of Random Service Points
nx.draw(GRAPH_service, points_service,
    node_size=100, alpha=1, node_color='c')
# Legend (Ordered Dictionary)
LEGEND = OrderedDict()
LEGEND['Network Nodes']=g
LEGEND['Roads']=g
LEGEND['Graph Vertices']=g1
LEGEND['Graph Edges']=g1
LEGEND['Client Nodes']=GRAPH_client
LEGEND['Service Nodes']=GRAPH_service
legend(LEGEND, loc='lower right', fancybox=True, framealpha=0.5)

# Title
title('Waverly Hills\n Tallahassee, Florida', family='Times New Roman',
      size=40, color='k', backgroundcolor='w', weight='bold')
# North Arrow and 'N' --> Must be changed for different spatial resolutions, etc.
arrow(-84.281, 30.507, 0.0, 0.01, width=.0003, head_width=0.0012,
          head_length=0.002, fc='k', ec='k',alpha=0.75,)
annotate('N', xy=(-84.2815, 30.52), fontstyle='italic', fontsize='xx-large',
            fontweight='heavy', alpha=0.75)


I am working on the file "Gurobi_v_Cplex__Pmaxian.ipynb".
The first task is:
2.2a  Cplex p-Maxian test [p = 2]
Can you write Python code for it?

t1 = time.time()

# Demand
Ai = Ai
# Demand Sum
AiSum = AiSum
# Travel Costs
Cij = All_Dist_MILES
# Weighted Costs
Sij = Ai * Cij
client_nodes = range(len(Sij))
service_nodes = range(len(Sij[0]))

all_nodes = len(Sij) * len(Sij[0])
ALL_nodes = range(all_nodes)

x = 'x'
cli_var = []
for i in client_nodes:
    for j in service_nodes:
        temp = x + str(i+1) + '_' + str(j+1)
        cli_var.append(temp)
client_var = np.array(cli_var)
client_var = client_var.reshape(len(Sij),len(Sij[0]))

y = 'y'
fac_var = []
for i in service_nodes:
    temp = y + str(i+1)
    fac_var.append(temp)
facility_var = np.array(fac_var)
facility_var = facility_var.reshape(1,len(Sij[0]))

#     2. Create Model and Add Variables
# Create Model
mPMaxP_CPLEX = cp.Cplex()
# Problem Name
mPMaxP_CPLEX.set_problem_name('\n -- P-Maxian -- ')
print mPMaxP_CPLEX.get_problem_name()

# Problem Type  ==>  Linear Programming
mPMaxP_CPLEX.set_problem_type(mPMaxP_CPLEX.problem_type.LP)
# Set MIP Emphasis to '2' --> Optimal
mPMaxP_CPLEX.parameters.emphasis.mip.set(2)
print mPMaxP_CPLEX.parameters.get_changed()
print '\nProblem Type\n    ' + str(mPMaxP_CPLEX.problem_type[mPMaxP_CPLEX.get_problem_type()])
# Objective Function Sense  ==>  Maximize
mPMaxP_CPLEX.objective.set_sense(mPMaxP_CPLEX.objective.sense.maximize)
print 'Objective Sense\n    ' + str(mPMaxP_CPLEX.objective.sense[mPMaxP_CPLEX.objective.get_sense()])
# Add Client Decision Variables
mPMaxP_CPLEX.variables.add(names = [cli_var[i] for i in ALL_nodes],
                        obj = [Sij[i][j] for i in client_nodes for j in service_nodes],
                        lb = [0] * all_nodes,
                        ub = [1] * all_nodes,
                        types = ['B'] * all_nodes)
# Add Service Decision Variable
mPMaxP_CPLEX.variables.add(names = [fac_var[j] for j in service_nodes],
                        lb = [0] * len(Sij[0]),
                        ub = [1] * len(Sij[0]),
                        types = ['B'] * len(Sij[0]))

#    3. Add Constraints
# Add Assignment Constraints
for...


I am working on the file "Gurobi_v_Cplex__Pmaxian.ipynb".
The first task is:
3.3 System Information
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Pmaxian.ipynb".
The first task is:
3. Selected locations
3.1 Gurobi & Cplex p-Maxian Selected locations
Can you write Python code for it?

figsize(10,10)
# Draw Network Actual Roads and Nodes
nx.draw(g, ntw.node_coords, node_size=5, alpha=0.25, edge_color='r', width=2)
# Draw Graph
nx.draw(g1, ntw.node_coords, node_size=10, alpha=0.5)

# Gurobi p-Maxian
P_Max_GUROBI = ps.open('shapefiles/Selected_Locations_Pmaxian_GUROBI.shp')
points_max_GUROBI = {}
for idx, coords in enumerate(P_Max_GUROBI):
    GUROBI_maxian_g.add_node(idx)
    points_max_GUROBI[idx] = coords
    GUROBI_maxian_g.node[idx] = coords
nx.draw(GUROBI_maxian_g, points_max_GUROBI,
        node_size=600, alpha=1, node_color='g')

# Cplex p-Maxian
P_Max_CPLEX = ps.open('shapefiles/Selected_Locations_Pmaxian_CPLEX.shp')
points_max_CPLEX = {}
for idx, coords in enumerate(P_Max_CPLEX):
    CPLEX_maxian_g.add_node(idx)
    points_max_CPLEX[idx] = coords
    CPLEX_maxian_g.node[idx] = coords
nx.draw(CPLEX_maxian_g, points_max_CPLEX,
        node_size=300, alpha=1, node_color='r')

# Draw Graph of Random Service
nx.draw(GRAPH_client, points_client,
        node_size=15, alpha=.5, node_color='k')

# Draw Graph of Random Service
nx.draw(GRAPH_service, points_service,
        node_size=50, alpha=1, node_color='k')

# Legend (Ordered Dictionary)
LEGEND = OrderedDict()
LEGEND['Network Nodes']=g
LEGEND['Roads']=g
LEGEND['Graph Vertices']=g1
LEGEND['Graph Edges']=g1
LEGEND['Gurobi Optimal p-Maxian (p=2)']=GUROBI_maxian_g
LEGEND['Cplex Optimal p-Maxian (p=2)']=CPLEX_maxian_g
LEGEND['Client Nodes']=GRAPH_client
LEGEND['Service Nodes']=GRAPH_service
legend(LEGEND, loc='lower right', fancybox=True, framealpha=0.5)

# Title
title('Waverly Hills\n Tallahassee, Florida', family='Times New Roman',
      size=40, color='k', backgroundcolor='w', weight='bold')
# North Arrow and 'N' --> Must be changed for different spatial resolutions, etc.
arrow(-84.281, 30.507, 0.0, 0.01, width=.0003, head_width=0.0012,
          head_length=0.002, fc='k', ec='k',alpha=0.75,)
annotate('N', xy=(-84.2815, 30.52), fontstyle='italic', fontsize='xx-large',
            fontweight='heavy', alpha=0.75)


I am working on the file "Gurobi_v_Cplex__Pmedian.ipynb".
The first task is:
1.3 Create Bounding Box from 'Waverly.shp'
Can you write Python code for it?

shp_W = ps.open('Waverly/Waverly.shp')
shp_W.bbox


I am working on the file "Gurobi_v_Cplex__Pmedian.ipynb".
The first task is:
1.5 Zip the latitude and longitude lists together
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Pmedian.ipynb".
The first task is:
1.8 Draw network, simplified network, and random client & service nodes
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Pmedian.ipynb".
The first task is:
2.1b Instantiate Selected Gurobi p-median shapefile
Can you write Python code for it?

SHP_Median_GUROBI = shp.Writer(shp.POINT)
# Add Points
for idy,idx,x,y in NEW_Records_PMP_GUROBI:
    SHP_Median_GUROBI.point(float(x), float(y))
# Add Fields
SHP_Median_GUROBI.field('y_ID')
SHP_Median_GUROBI.field('x_ID')
SHP_Median_GUROBI.field('LAT')
SHP_Median_GUROBI.field('LON')
# Add Records
for idy,idx,x,y in NEW_Records_PMP_GUROBI:
    SHP_Median_GUROBI.record(idy,idx,x,y)
# Save Shapefile
SHP_Median_GUROBI.save('shapefiles/Selected_Locations_Pmedian_GUROBI')


I am working on the file "Gurobi_v_Cplex__Pmedian.ipynb".
The first task is:
2.2b Instantiate Selected Cplex p-median shapefile
Can you write Python code for it?

SHP_Median_CPLEX = shp.Writer(shp.POINT)
# Add Points
for idy,idx,x,y in NEW_Records_PMP_CPLEX:
    SHP_Median_CPLEX.point(float(x), float(y))
# Add Fields
SHP_Median_CPLEX.field('y_ID')
SHP_Median_CPLEX.field('x_ID')
SHP_Median_CPLEX.field('LAT')
SHP_Median_CPLEX.field('LON')
# Add Records
for idy,idx,x,y in NEW_Records_PMP_CPLEX:
    SHP_Median_CPLEX.record(idy,idx,x,y)
# Save Shapefile
SHP_Median_CPLEX.save('shapefiles/Selected_Locations_Pmedian_CPLEX')


I am working on the file "Gurobi_v_Cplex__Pmedian.ipynb".
The first task is:
3.2 Optimized Values
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Pmedian.ipynb".
The first task is:
3.3 System Information
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Pmedian.ipynb".
The first task is:
1. Data preparation and creation
1.1 Instantiate a network
Can you write Python code for it?

ntw = ps.Network('Waverly/Waverly.shp')
print dt.datetime.now()
print 'PySAL.Network\n'
print dir(ntw)


I am working on the file "Gurobi_v_Cplex__Pmedian.ipynb".
The first task is:
2. Mathematical Optimization
2.1a  Gurobi p-Median test [p = 2]
Can you write Python code for it?

t1 = time.time()

#     1. Data
# Demand
Ai = Ai
# Demand Sum
AiSum = AiSum
# Travel Costs
Cij = All_Dist_MILES
# Weighted Costs
Sij = Ai * Cij
client_nodes = range(len(Sij))
service_nodes = range(len(Sij[0]))

mPMP_GUROBI = gbp.Model(' -- p-Median -- ')

gbp.setParam('MIPFocus', 2)

# Client IxJ
client_var = []
for orig in client_nodes:
    client_var.append([])
    for dest in service_nodes:
        client_var[orig].append(mPMP_GUROBI.addVar(vtype=gbp.GRB.BINARY,
                                            obj=Sij[orig][dest],
                                            name='x'+str(orig+1)+'_'+str(dest+1)))
#J
serv_var = []
for dest in service_nodes:
    serv_var.append([])
    serv_var[dest].append(mPMP_GUROBI.addVar(vtype=gbp.GRB.BINARY,
                                    name='y'+str(dest+1)))
mPMP_GUROBI.update()
mPMP_GUROBI.setObjective(gbp.quicksum(Sij[orig][dest]*client_var[orig][dest]
                        for orig in client_nodes for dest in service_nodes),
                        gbp.GRB.MINIMIZE)
for orig in client_nodes:
    mPMP_GUROBI.addConstr(gbp.quicksum(client_var[orig][dest]
                        for dest in service_nodes) == 1)
for orig in service_nodes:
    for dest in client_nodes:
        mPMP_GUROBI.addConstr((serv_var[orig] - client_var[dest][orig] >= 0))
mPMP_GUROBI.addConstr(gbp.quicksum(serv_var[dest][0] for dest in service_nodes) == 2)
mPMP_GUROBI.optimize()
t2P = time.time()-t1
#mPMP_GUROBI.write('LP_Files/WaverlyPMP_GUROBI.lp')
print '\n*************************************************************************'
selected = []
dbf1 = ps.open('shapefiles/RandomPoints_SERVICE.dbf')
NEW_Records_PMP_GUROBI = []
for v in mPMP_GUROBI.getVars():
    if 'x' in v.VarName:
        pass
    elif v.x > 0:
        var = '%s' % v.VarName
        selected.append(var)
        for i in range(dbf1.n_records):
            if var in dbf1.read_record(i):
                x = dbf1.read_record(i)
                NEW_Records_PMP_GUROBI.append(x)
            else:
                pass
       ...


I am working on the file "Gurobi_v_Cplex__Pmedian.ipynb".
The first task is:
3. Selected locations
3.1 Gurobi & Cplex p-Median Selected locations
Can you write Python code for it?

figsize(10,10)
# Draw Network Actual Roads and Nodes
nx.draw(g, ntw.node_coords, node_size=5, alpha=0.25, edge_color='r', width=2)
# Draw Graph
nx.draw(g1, ntw.node_coords, node_size=10, alpha=0.5)

# Gurobi p-Median
P_Med_GUROBI = ps.open('shapefiles/Selected_Locations_Pmedian_GUROBI.shp')
points_median_GUROBI = {}
for idx, coords in enumerate(P_Med_GUROBI):
    GUROBI_median_g.add_node(idx)
    points_median_GUROBI[idx] = coords
    GUROBI_median_g.node[idx] = coords
nx.draw(GUROBI_median_g, points_median_GUROBI,
        node_size=600, alpha=1, node_color='g')

# Cplex p-Median
P_Med_CPLEX = ps.open('shapefiles/Selected_Locations_Pmedian_CPLEX.shp')
points_median_CPLEX = {}
for idx, coords in enumerate(P_Med_CPLEX):
    CPLEX_median_g.add_node(idx)
    points_median_CPLEX[idx] = coords
    CPLEX_median_g.node[idx] = coords
nx.draw(CPLEX_median_g, points_median_CPLEX,
        node_size=300, alpha=1, node_color='r')

# Draw Graph of Random Service
nx.draw(GRAPH_client, points_client,
        node_size=15, alpha=.5, node_color='k')

# Draw Graph of Random Service
nx.draw(GRAPH_service, points_service,
        node_size=50, alpha=1, node_color='k')

# Legend (Ordered Dictionary)
LEGEND = OrderedDict()
LEGEND['Network Nodes']=g
LEGEND['Roads']=g
LEGEND['Graph Vertices']=g1
LEGEND['Graph Edges']=g1
LEGEND['Gurobi Optimal p-Median (p=2)']=GUROBI_median_g
LEGEND['Cplex Optimal p-Median (p=2)']=CPLEX_median_g
LEGEND['Client Nodes']=GRAPH_client
LEGEND['Service Nodes']=GRAPH_service
legend(LEGEND, loc='lower right', fancybox=True, framealpha=0.5)

# Title
title('Waverly Hills\n Tallahassee, Florida', family='Times New Roman',
      size=40, color='k', backgroundcolor='w', weight='bold')
# North Arrow and 'N' --> Must be changed for different spatial resolutions, etc.
arrow(-84.281, 30.507, 0.0, 0.01, width=.0003, head_width=0.0012,
          head_length=0.002, fc='k', ec='k',alpha=0.75,)
annotate('N', xy=(-84.2815, 30.52), fontstyle='italic', fontsize='xx-large',
            fontweight='heavy', alpha=0.75)


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
1.13 Create distance matrix
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
1.3 Create Bounding Box from 'Waverly.shp'
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
1.5 Zip the latitude and longitude lists together
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
1.6 Create Empty Random Points Dictionaries
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
1.7 Fill dictionaries of random roints
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
1.8 Draw network, simplified network, and random client & service nodes
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
2.2a  Cplex Set Cover test
Can you write Python code for it?

t1 = time.time()

#     1. Read In Data
# Cost Vector
Cij = All_Dist_MILES
# Create Aij: Determine Aij (nodes within S)
# S --> 1 = served; 0 = unserved
Cij = All_Dist_MILES
# Create Aij: Determine Aij (nodes within S)
# S --> 1 = served; 0 = unserved
Aij = []
for i in np.nditer(Cij):
    if i <= S:
        outtext = 1
    else:
        outtext = 0
    Aij.append(outtext)
Aij = np.array(Aij)
Aij = Aij.reshape(len(Cij),len(Cij[0]))

# Indices & Variable Names
nodes = len(Cij)
Nodes = range(len(Cij))

all_nodes = len(Cij) * len(Cij)
ALL_nodes = range(all_nodes)
x = 'x'
cli_var = []
for i in Nodes:
    for j in Nodes:
        temp = x + str(j+1)
        cli_var.append(temp)
client_var = np.array(cli_var)
results_var = []
for i in Nodes:
    temp = x + str(i+1)
    results_var.append(temp)

#     2. Create Model and Add Variables
# Create Model
mSCLP_CPLEX = cp.Cplex()
# Problem Name
mSCLP_CPLEX.set_problem_name('\n -- Set Cover Location Problem -- ')
print mSCLP_CPLEX.get_problem_name()
# Problem Type  ==>  Linear Programming
mSCLP_CPLEX.set_problem_type(mSCLP_CPLEX.problem_type.LP)
# Set MIP Emphasis to '2' --> Optimal
mSCLP_CPLEX.parameters.emphasis.mip.set(2)
print mSCLP_CPLEX.parameters.get_changed()
print '\nProblem Type\n    ' + str(mSCLP_CPLEX.problem_type[mSCLP_CPLEX.get_problem_type()])
# Objective Function Sense  ==>  Minimize
mSCLP_CPLEX.objective.set_sense(mSCLP_CPLEX.objective.sense.minimize)
print 'Objective Sense\n    ' + str(mSCLP_CPLEX.objective.sense[mSCLP_CPLEX.objective.get_sense()])
# Add Client Decision Variables
mSCLP_CPLEX.variables.add(names = [cli_var[i] for i in Nodes],
                        obj = [1] * nodes,
                        lb = [0] * nodes,
                        ub = [1] * nodes,
                        types = ['B'] * nodes)

#    3. Add Constraints
#Add Coverage Constraints
for orig in Nodes:
    coverage_constraints = cp.SparsePair(ind = [client_var[dest]
                                            for dest in Nodes],
          ...


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
2.2b Instantiate Selected Cplex Set Cover shapefile
Can you write Python code for it?

SHP_SCLP_CPLEX = shp.Writer(shp.POINT)
# Add Points
for idy,idx,x,y in NEW_Records_SCLP_CPLEX:
    SHP_SCLP_CPLEX.point(float(x), float(y))
# Add Fields
SHP_SCLP_CPLEX.field('y_ID')
SHP_SCLP_CPLEX.field('x_ID')
SHP_SCLP_CPLEX.field('LAT')
SHP_SCLP_CPLEX.field('LON')
# Add Records
for idy,idx,x,y in NEW_Records_SCLP_CPLEX:
    SHP_SCLP_CPLEX.record(idy,idx,x,y)
# Save Shapefile
SHP_SCLP_CPLEX.save('shapefiles/Selected_Locations_SCLP_CPLEX')


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
3.2 Optimized Values
Can you write Python code for it?

print '********************************************************'
print ' | Total Facilities to be opened for [ S =', S, ']'
print ' |  | Gurobi ------------------ ', mSCLP_GUROBI.objVal, '       '
print ' |  | CPLEX ------------------- ', solution.get_objective_value(), '       '
print '-------------------------------------------------------'
NSG = NEW_Records_SCLP_GUROBI
NSC = NEW_Records_SCLP_CPLEX
G_val = mSCLP_GUROBI.objVal
C_val = solution.get_objective_value()
if G_val == C_val:
    print ' | Gurobi and CPLEX chose the same number of facilities'
else:
    print ' | Gurobi and CPLEX chose different numbers of facilities'
if NSG == NSC:
    print ' |        ***'
    print ' | Gurobi and CPLEX chose the same facilities'
else:
    print ' |        ***'
    print ' | Gurobi and CPLEX chose different facilities'
print '-------------------------------------------------------'
print ' | Total Time to Build Model and Optimize: (seconds)  '
print ' |  | Gurobi ------------------ ', t2G, '       '
print ' |  | CPLEX ------------------- ', t2C, '       '
print '********************************************************'


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
3.3 System Information
Can you write Python code for it?


I am working on the file "Gurobi_v_Cplex__Set_Cover.ipynb".
The first task is:
2. Mathematical Optimization
2.1a  Gurobi Set Cover test
Can you write Python code for it?

t1 = time.time()

#     1. Read In Data
# Cost Vector
Cij = All_Dist_MILES
# Create Aij: Determine Aij (nodes within S)
# S --> 1 = served; 0 = unserved
Aij = []
for i in np.nditer(Cij):
    if i <= S:
        outtext = 1
    else:
        outtext = 0
    Aij.append(outtext)
rows, cols = Cij.shape
Aij = np.array(Aij)
Aij = Aij.reshape(len(Cij),len(Cij[0]))
client_nodes = range(len(Cij))
service_nodes = range(len(Cij[0]))

#     2. Create Model, Set MIP Focus, Add Variables, & Update Model
mSCLP_GUROBI = gbp.Model(" -- SCLP -- ")
# Set MIP Focus to 2 for optimality
gbp.setParam('MIPFocus', 2)

#     3. Add Service Decision Variables
serv_var = []
for dest in service_nodes:
    serv_var.append(mSCLP_GUROBI.addVar(vtype=gbp.GRB.BINARY,
                                ub = 1,
                                name='x'+str(dest+1)))
# Update Model Variables
mSCLP_GUROBI.update()

#     4. Set Objective Function
mSCLP_GUROBI.setObjective(gbp.quicksum(serv_var[dest]
                            for dest in service_nodes),
                            gbp.GRB.MINIMIZE)

#    5. Add Constraints
#Add Coverage Constraints
for orig in client_nodes:
    mSCLP_GUROBI.addConstr(gbp.quicksum(Aij[orig][dest]*serv_var[dest]
                            for dest in service_nodes) >= 1)

#     6. Optimize and Print Results
mSCLP_GUROBI.optimize()
mSCLP_GUROBI.write('GUROBI_LP.lp')
t2G = time.time()-t1
print '\n*****************************************************************************************'
selected = []
dbf1 = ps.open('shapefiles/RandomPoints_SERVICE.dbf')
NEW_Records_SCLP_GUROBI = []
for v in mSCLP_GUROBI.getVars():
    if v.x > 0:
        var = '%s' % v.VarName
        selected.append(v.x)
        for i in range(dbf1.n_records):
            if var in dbf1.read_record(i):
                x = dbf1.read_record(i)
                NEW_Records_SCLP_GUROBI.append(x)
            else:
                pass
        print '    |                                                         ', var
print '    | Selected...


I am working on the file "H750.ipynb".
The first task is:
"Our" three-loop (ČKP) model
Can you write Python code for it?

# Factors relevant for H-->gamma gamma
sigmaprime = symbols('sigmaprime')
print(tau*VVfactW(D=7, Y=0, real=True)['gg'])
print(sigma*VVfactW(D=5, Y=-2, real=False)['gg'])
print(sigmaprime*VVfactW(D=5, Y=-2, real=False, wght=True)['gg'])

# Factors relevant for H--> W+ W-  (with sqrt(2)/sw2 factor extracted)
print((tau*VVfactW(D=7, Y=0, real=True)['WW']*sw2/sqrt(2)).evalf())
print((sigma*VVfactW(D=5, Y=-2, real=False)['WW']*sw2/sqrt(2)).evalf())
print((sigmaprime*VVfactW(D=5, Y=-2, real=False, wght=True)['WW']*sw2/sqrt(2)).evalf())


I am working on the file "H750.ipynb".
The first task is:
(For check) Elllis and Ellis et al. 1512.05327
Can you write Python code for it?

# Model 1
Rtogg([smgroup.Dirac(3,1,S(4)/3), smgroup.Dirac(3,1,-S(4)/3)])

# Zg above agrees with Eq. (32) of 1512.07616
2*sw2/cw2

# Model 2
Rtogg([smgroup.Dirac(3,2,S(1)/3), smgroup.Dirac(3,2,-S(1)/3)])

# Model 3
Rtogg([smgroup.Dirac(3,1,S(4)/3), smgroup.Dirac(3,1,-S(4)/3), smgroup.Dirac(3,2,S(1)/3), smgroup.Dirac(3,2,-S(1)/3), smgroup.Dirac(3,1,-S(2)/3), smgroup.Dirac(3,1,S(2)/3)])


I am working on the file "H750.ipynb".
The first task is:
2HDM: for $m_H = 750\;{\rm GeV}$, $A$ and $H^\pm$ are also close to 750 GeV.
Signal is $10^4$ times stronger than SM-like Higgs, so pure 2HDM is hopeless

Higgs production by gluon-gluon fusion: 7 and 8 TeV by  LHC Higgs xs WG, and 13 TeV by gluon luminosity ratios (13 TeV/8 TeV) being 2.296 for 125 GeV and 4.693 for 750 GeV.
|          | 7 TeV    |  8 TeV   |  13 TeV
|  ----    | -----    |  ------  | -----
| h(125)   | 15.13 pb |  19.27 pb|  44.2 pb
| h(750)   |  93 fb   |  157 fb  |  737 fb
and we have
$$\sigma_{\gamma\gamma} = 737\,{\rm fb}\; Br(H\to\gamma\gamma)$$
Can you write Python code for it?


I am working on the file "H750.ipynb".
The first task is:
Benbrink at al have:
RGG = 40, RZg = 2.29,  RZZ = 5.59,   RWW  = 8.88
So their RWW looks factor 2 too small.
"Our" one-loop  (BPR) model
For the purposes of couplings to H(750) and gauge bosons, we have one Dirac doublet (times the number of generations, of course)
Can you write Python code for it?

bpr = [smgroup.Dirac(1,2,-1)]
smgroup.SMDynkin(bpr)

RGGbpr, RZgbpr, RZZbpr, RWWbpr = Rtogg(bpr, prt=True)

# Branching ratio to gamma gamma:
BrBPRgg = GAMHVV('gg', BSMfermions=bpr)/(GAMHVV('gg', BSMfermions=bpr)+GAMHVV('Zg', BSMfermions=bpr)+GAMHVV('ZZ', BSMfermions=bpr)+GAMHVV('WW', BSMfermions=bpr))
BrBPRgg

# OA's factor
10.8*(750/45)**2 /(64*pi.evalf()**3)**2 * 1000  # fb


I am working on the file "H750.ipynb".
The first task is:
Final expression for $H\to VV$ width
$$\Gamma(h\to\gamma\gamma) = B \left|\sum_i Q_i^2 A_{i}(\tau_i) \right|^2$$
$$ B = \frac{\alpha^2 g^2 m_h^3}{1024 \pi^3 m_W^2} = \frac{G_F \alpha^2 m_h^3}{
128\sqrt{2} \pi^3}$$
$$\tau_i = \frac{4m_i^2}{m_{H}^2} $$
$$A_{0}(\tau) = -\tau(1-\tau f(\tau)) \to \frac{1}{3}  \quad \text{for} \quad \tau\to\infty$$
$$A_{1/2}(\tau) = 2\tau\big(1+(1-\tau)f(\tau)\big) = 2 + (4 m^2 -m_{H}^2)C_0(0,0,m_{H}^2,m^2,m^2,m^2)$$
$$f(\tau) = \arcsin^2(\sqrt{\frac{1}{\tau}})  \quad \text{for} \quad \tau\ge 1$$
$$f(\tau) = -\frac{m_H^2}{2} C_0 (0,0,m_H^2; m, m, m) $$
Can you write Python code for it?

# Loop functions

def f(tau):
    return asin(1/sqrt(tau))**2

def A0(tau):
    return -tau*(1-tau*f(tau))

def A1(tau):
    return -2-3*tau-3*tau*(2-tau)*f(tau)

def A12(tau):
    return 2*tau*(1+(1-tau)*f(tau))

# numpy-approved versions
fN = lambdify(x, f(x), 'numpy')
A0N = lambdify(x, A0(x), 'numpy')
A1N = lambdify(x, A1(x), 'numpy')
A12N = lambdify(x, A12(x), 'numpy')

def tauN(m, mH=750):
    return 4*m**2/mH**2

print(" A0 --> {}".format(limit(A0(tau), tau, oo)))
print("A12 --> {}".format(limit(A12(tau), tau, oo)))
print(" A1 --> {}".format(limit(A1(tau), tau, oo)))

# Numerical check of the relation of C0 and f(tau):
# LoopTools for mH=125, m=375
# fLT = 0.028038859
f(4*375**2/125**2)


I am working on the file "H750.ipynb".
The first task is:
For production via photon fusion $pp \to \gamma \gamma \to H \to \gamma \gamma$ at 13 TeV, Harland-Lang et al. have
$$ \sigma = 4.1\,{\rm pb}\, \left(\frac{\Gamma_H}{45\, {\rm GeV}}\right) {\rm Br}(H\to\gamma\gamma)^2$$
while Csaki et al. have
$$ \sigma = 10.8 \,{\rm pb}\, \left(\frac{\Gamma_H}{45\, {\rm GeV}}\right) {\rm Br}(H\to\gamma\gamma)^2$$.
They both include elastic and inelastic contributions, the latter also mixed, and in narrow width approximation. For $\sigma = 3-9\,{\rm fb}$, we get from the second choice
branching ratio 1.7-2.9 %, or $\Gamma(H\to\gamma\gamma) = 0.75-1.3\,{\rm GeV}$.
Can you write Python code for it?

# Same prefactor in picobarn from  Franceschini et al. Eq. (2)
(45*54)/750/(13000)**2 * GeV2fb / 1000


I am working on the file "H750.ipynb".
The first task is:
Models
(For check) Bhupal Dev et al. [1512.08507] 1512.06028
Can you write Python code for it?

bdrep = [smgroup.Dirac(3,1,S(4)/3), smgroup.Dirac(3,1,-S(2)/3), smgroup.Dirac(1,1,-2)]
Rtogg(bdrep)


I am working on the file "H750.ipynb".
The first task is:
SM $h(125)\to\gamma\gamma$ width in GeV (just W and top contributions)
Can you write Python code for it?


I am working on the file "H750.ipynb".
The first task is:
So it is again photon-photon channel that is most restrictive.
Can you write Python code for it?

def funm(VV, mchi, s):
    return sig8CKP(VV, tau=10, sig=10, sigpri=10, mchi=mchi, mphi=s) - sig8[VV]

def mboundCKP(VV, mchi, init=390):
    """Boundary value of mphi to violate 8 TeV VV xs constraint"""
    return root(lambda s: funm(VV, mchi, s), init).x[0]


I am working on the file "H750.ipynb".
The first task is:
So, for $N_E=3$, and $\cos\theta_0\sim 1$ we have $\lambda/(4\pi)$ = 1.1 and we are at the border of perturbativity?
Constraints from 8 TeV VV bounds. First, cross sections for pp->H->VV at 8 TeV if pp->H->gg at 13 TeV is in (3 fb, 9 fb)
Can you write Python code for it?

#rgg = 1.9  # gain for photon fusion xs going from 8 to 13 TeV in Franceschini et al.
# rgg = 3.9   # value from Fichet et al.1512.05751
rgg = 3   # average value
# For 3 pb
[(xs_low*RVV)/rgg for RVV in [RWWbpr, RZZbpr, RZgbpr, 1]]

# And for 9 fb
[(xs_high*RVV)/rgg for RVV in [RWWbpr, RZZbpr, RZgbpr, 1]]

# Bounds on pp->H->VV xs from LHC 8 TeV
sig8 = {'WW' : 40, 'ZZ' : 12, 'Zg' : 11, 'gg' : 1.5}  # in fb, from Franceschini


I am working on the file "H750.ipynb".
The first task is:
These numbers agree with my older notebook used for initial plots. Another check: CKP model widths with $\sigma'=0$ (i.e. septuplet plus universal quintuplet contributions) can also be calculated with generic function GAMHVV.
Can you write Python code for it?

[GAMHckp(VV, sigpri=0) for VV in ['gg', 'Zg', 'ZZ', 'WW']]

[GAMHVV(VV, BSMscalars=[smgroup.RealScalar(1,7,0), smgroup.ComplexScalar(1,5,-2)], gHSS=v) for VV in ['gg', 'Zg', 'ZZ', 'WW']]


I am working on the file "H750.ipynb".
The first task is:
They have e.g. for the Model 3:
RGG = 460, RZg = 1.1,  RZZ = 2.8,   RWW  = 15.
So, their RZg and RWW look factor 2 too large.
(For check) Benbrik et al. 1512.06028
Can you write Python code for it?

# VLTQ model of Benbrik and al.
Rtogg([smgroup.Dirac(3,3,S(4)/3), smgroup.Dirac(3,3,-S(2)/3)])


I am working on the file "H750.ipynb".
The first task is:
This is about right. (2HDMC gives 8.3e-6 GeV).
Now we define $H\to VV$ decay widths expressions for generic BSM and for ČKP model.
Can you write Python code for it?

def GAMHVV(VV='gg',
           BSMfermions=[], BSMscalars=[], gHFF=v, mF=400, gHSS=v, mS=400):
    """Decay width of scalar H to pair of gauge bosons (generic model)"""
    B = float((alpha**2 * mH**3)/(1024 * pi**3).evalf())
    VVf = VVfact(*smgroup.SMDynkin(BSMfermions))[VV]
    VVs = VVfact(*smgroup.SMDynkin(BSMscalars))[VV]
    amp = - (2*gHFF/mF)*VVf*A12N(tauN(mF))
    amp += - (gHSS/mS**2)*VVs*A0N(tauN(mS))
    return B * amp**2

def GAMHckp(VV='gg', tau=1, sig=1, sigpri=1, mchi=400, mphi=400):
    """Decay width of scalar H to pair of gauge bosons (CKP model)"""
    B = float((alpha**2 * mH**3)/(1024 * pi**3).evalf())
    VVtau = VVfactW(D=7, Y=0, real=True)[VV]
    VVsig = VVfactW(D=5, Y=-2, real=False)[VV]
    VVsigpri = VVfactW(D=5, Y=-2, real=False, wght=True)[VV]
    amp = tau*v*VVtau/mchi**2 * A0N(tauN(mchi))
    amp += (sig*v*VVsig/mphi**2 + sigpri*v*VVsigpri/mphi**2) * A0N(tauN(mphi))
    return B * amp**2

GAMHckp('gg'), GAMHckp('WW')


I am working on the file "H750.ipynb".
The first task is:
This is in good agreement with their Table 1:
RGG = 220, RZg = 0.61, RZZ = 0.091
Let's also check decay width formula (again in ratios only)
Can you write Python code for it?

# RGG
GAMHVV('GG', BSMfermions=bdrep, gHFF=246, mF=400)/GAMHVV(BSMfermions=bdrep, gHFF=246, mF=400)

# RZg
GAMHVV('Zg', BSMfermions=bdrep, gHFF=246, mF=400)/GAMHVV(BSMfermions=bdrep, gHFF=246, mF=400)

# RZZ
GAMHVV('ZZ', BSMfermions=bdrep, gHFF=246, mF=400)/GAMHVV(BSMfermions=bdrep, gHFF=246, mF=400)

# RWW
GAMHVV('WW', BSMfermions=bdrep, gHFF=246, mF=400)/GAMHVV(BSMfermions=bdrep, gHFF=246, mF=400)


I am working on the file "H750.ipynb".
The first task is:
[Fig 3] $\Gamma_{H(750)}$ in three-loop ČKP model
Can you write Python code for it?

xmin, xmax = 375, 400
ymin, ymax = 20, 60
xs = np.linspace(xmin,xmax)
plt.figure(figsize=(4,4))
TOT, BR = GAMTOTckpD(8, xs)
plt.plot(xs , TOT, label=r"$\tau=\sigma=\sigma' = 8$")
TOT, BR = GAMTOTckpD(4, xs)
plt.plot(xs , TOT, 'r--', label=r"$\tau=\sigma=\sigma' = 4$")
plt.ylabel(r'$\Gamma_H\;{\rm [GeV]}$', fontsize=16)
plt.xlabel(r'$m_{\chi}=m_{\phi}\;{\rm [GeV]}$', fontsize=16)
#plt.fill_between(xs, xs_low*np.ones(xs.shape), xs_high*np.ones(xs.shape), facecolor='lightgreen', alpha=0.5)
#plt.text(388, 8, r'${\rm ATLAS+CMS}\; \gamma\gamma\; {\rm range}$')
plt.legend(loc=1).draw_frame(0)
fig = plt.ylim(ymin, ymax)
plt.tight_layout()
if SAVEPDFS:
    plt.savefig('/home/kkumer/diphGAMm.pdf')

xmin, xmax = 0.2, 12
xs = np.linspace(xmin,xmax)
plt.figure(figsize=(3.7,4))
TOT, BR = GAMTOTckpD(xs, 375)
plt.plot(xs , TOT, label=r'$m_{\chi} = m_{\phi} = 375 \;{\rm GeV}$')
TOT, BR = GAMTOTckpD(xs, 400)
plt.plot(xs , TOT, 'r--', label=r'$m_{\chi} = m_{\phi} = 400 \;{\rm GeV}$')
#plt.ylabel(r'$\sigma(pp\to H\to\gamma\gamma)\;{\rm [fb]}$', fontsize=16)
plt.xlabel(r"$\tau=\sigma=\sigma'$", fontsize=16)
#plt.fill_between(xs, xs_low*np.ones(xs.shape), xs_high*np.ones(xs.shape), facecolor='lightgreen', alpha=0.5)
#plt.text(2.5, 8, r'${\rm ATLAS+CMS}\; \gamma\gamma\; {\rm range}$')
plt.legend(loc=2).draw_frame(0)
#plt.xlim(xmin, xmax)
fig = plt.ylim(ymin, ymax)
plt.tight_layout()
if SAVEPDFS:
    plt.savefig('/home/kkumer/diphGAMlam.pdf')


I am working on the file "H750.ipynb".
The first task is:
one-loop BPR model
Can you write Python code for it?

## So in pure photon fusion production and pure VV decay scenario, we have for total H(750) width range in GeV
GAMbpr_low, GAMbpr_high = [(siggg/10800)*45/BrBPRgg**2 for siggg in (xs_low, xs_high)]
GAMbpr_low, GAMbpr_high

# Translating this into H->gamma gamma width in GeV:
GAMbpr_HGG_low, GAMbpr_HGG_high = sqrt((xs_low/10800)*GAMbpr_low*45), sqrt((xs_high/10800)*GAMbpr_high*45)
GAMbpr_HGG_low, GAMbpr_HGG_high

def lam(msig, GAM):
    """Coupling to get given width H->gamma gamma for given loop fermion mass."""
    ss = ( sqrt(256 * pi**3 * GAM / (alpha**2 * mH**3)) ).evalf()
    return float(msig * ss / A12N(tauN(msig)))

lamN = np.frompyfunc(lam, 2, 1)

# Checking that above inverted formula lambda(sigma) is consistent
# with "master" formula sigma(lambda):
lamN(400, GAMHVV('gg', BSMfermions=bpr, gHFF=42, mF=400))


I am working on the file "H750.ipynb".
The first task is:
three-loop ČKP model
With ggF as dominant production mechanism we have
$$ \sigma_{VV}^{8\,{\rm TeV}} = 157\,{\rm fb} \; Br(H\to VV)$$
Can you write Python code for it?

def sig8CKP(VV, tau, sig, sigpri, mchi, mphi):
    """xs in fb for pp-->H-->VV at 8 TeV in CKP model with degenerate couplings an masses"""
    TOT = GAMTOTckp(tau, sig, sigpri, mchi, mphi)
    BrVV = GAMHckp(VV, tau, sig, sigpri, mchi, mphi)/TOT
    # print('GAMH = {:.1f} GeV, Br(H->{}) = {}'.format(TOT, VV, BrVV))
    return ggF8 * BrVV

def fun(VV, tau, s):
    return sig8CKP(VV, tau=tau, sig=s, sigpri=s, mchi=375, mphi=375) - sig8[VV]

def sigboundCKP(VV, tau, init=6):
    """Boundary value of sig=sig' to violate 8 TeV VV xs constraint"""
    return root(lambda s: fun(VV, tau, s), init).x[0]

[sigboundCKP(VV, 8) for VV in ['WW', 'Zg', 'ZZ', 'gg']]


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
2.3 Structurer les données
Définir le chemin d'accès aux données puis les fonctions utiles.
Can you write Python code for it?

# Attention: le chemin ci-dessous doit être adapté au contexte
DATADIR_UCI = './UCI HAR Dataset'
# Liste des noms des fichiers afin d'automatiser la lecture.
SIGNALS = [ "body_acc_x", "body_acc_y", "body_acc_z", "body_gyro_x", "body_gyro_y", "body_gyro_z", "total_acc_x", "total_acc_y", "total_acc_z"]

# Fonctions permettant de lire la séquence des fichiers avant de restructurer les données
# dans le fortmat recherché.
def my_read_csv(filename):
    return pd.read_csv(filename, delim_whitespace=True, header=None)

def load_signal(data_dir, subset, signal):
    filename = data_dir+'/'+subset+'/Inertial Signals/'+signal+'_'+subset+'.txt'
    x = my_read_csv(filename).values
    return x

def load_signals(data_dir, subset, flatten = False):
    signals_data = []
    for signal in SIGNALS:
        signals_data.append(load_signal(data_dir, subset, signal))
    if flatten :
        X = np.hstack(signals_data)
    else:
        X = np.transpose(signals_data, (1, 2, 0))
    return X

def load_y(data_dir, subset, dummies = False):
    filename = data_dir+'/'+subset+'/y_'+subset+'.txt'
    y = my_read_csv(filename)[0]
    if dummies:
        Y = pd.get_dummies(y).values
    else:
        Y = y.values
    return Y


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
2.4 Visualisations
Cette phase est essentielle à la bonne compréhension des données, de leur structure et donc des problèmes qui vont être soulevés par la suite. La visualisation est très élémentaire d'un point de vue méthodologique mais nécessite des compétences plus élaborées en Python et donc des fonctions préalables.
Fonctions utiles
Can you write Python code for it?

# Liste des couleurs
CMAP = plt.get_cmap("Accent")
# Liste des types de signaux
SIGNALS = ["body_acc x", "body_acc y", "body_acc z",
                "body_gyro x", "body_gyro y", "body_gyro z",
               "total_acc x", "total_acc y", "total_acc z"]
# Dictionnaire en clair des activités expérimentées (contexte supervisé)
ACTIVITY_DIC = {1 : "WALKING",
2 : "WALKING UPSTAIRS",
3 : "WALKING DOWNSTAIRS",
4 : "SITTING",
5 : "STANDING",
6 : "LAYING"}
labels = ACTIVITY_DIC.values()

# Fonction pour le tracé d'un signal
def plot_one_axe(X, fig, ax, sample_to_plot, cmap):
    for act,Xgb in X.groupby("Activity"):
        Xgb_first_values = Xgb.values[:sample_to_plot,:-1]
        x = Xgb_first_values[0]
        ax.plot(x, linewidth=1, color=cmap(act-1), label = label_dic[act])
        for x in Xgb_first_values[1:]:
            ax.plot(x, linewidth=1, color=cmap(act-1))
def plot_one_axe_shuffle(X, fig, ax, sample_to_plot, cmap):
    plot_data = []
    for act,Xgb in X.groupby("Activity"):
        Xgb_first_values = Xgb.values[:sample_to_plot,:-1]
        x = Xgb_first_values[0]
        ax.plot(x, linewidth=1, color=cmap(act-1), label = label_dic[act])
        for x in Xgb_first_values[1:]:
            plot_data.append([x,cmap(act-1)])
    random.shuffle(plot_data)
    for x,color in plot_data:
        ax.plot(x, linewidth=1, color=color)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
3.3 Analyse Factorielle Discriminante (AFD)
Principe
L'ACP ne prend pas en compte la présence de la variable qualitative à modéliser contrairement à l'analyse factorielle discriminante (AFD) adaptés à ce contexte "supervisé" puisque l'activité est connue sur un échantillon d'apprentissage. L'AFD est une ACP des barycentres des classes munissant l'espace des individus d'une métrique spécifique dite de Mahalanobis. Métrique définie par l'inverse de la matrice de covariance intraclase. L'objectif est alors de visualiser les capacités des variables à discriminer les classes.
La librairie scikit-learn ne propose pas de fonction spécifique d'analyse factorielle discriminante mais les coordonnées des individus dans la base des vecteurs discriminants sont obtenues comme résultats de l'analyse discriminante linéaire décisionnnelle. Cette dernière sera utilisée avec une finalité prédictive dans un deuxième temps (autre calepin).
Les résultats de la fonction LinearDiscriminantAnalysis de scikit-learn sont identiques à ceux de la fonction lda de R. Elle est donc utilisée strictement de la même façon.
Can you write Python code for it?

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
method = LinearDiscriminantAnalysis()
lda=method.fit(Xtrain,ytrain)
X_r2=lda.transform(Xtrain)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
4.3 K plus proches voisins
Cette méthode peut être vue comme un cas particulier d'analyse discriminante avec une estimation locale des fonctions de densité conditionnelle.
Q. Combien de voisins sont utilisés par défaut ? Optimiser ce paramètre.
Can you write Python code for it?

from sklearn.neighbors import KNeighborsClassifier
ts = time.time()
method = KNeighborsClassifier(n_jobs=-1)
method.fit(Xtrain,ytrain)
score = method.score(Xtest, ytest)
ypred = method.predict(Xtest)
te = time.time()
t_total = te-ts

print("Score : %f, time running : %d secondes" %(score, te-ts))
pd.DataFrame(confusion_matrix(ytest, ypred), index = labels, columns=labels)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
4.4 SVM linéaire
Le nombre max d'itérations a été très sensiblement augmenté (1000 par défaut) sans pour autant améliorer la performance. Le cas multi-classes est traité en considérant une classe contre les autres donc 6 modèles.
Q. Utiliser le paramètre par défaut puis faire varier le paramètre de pénalisation.
Can you write Python code for it?

from sklearn.svm import LinearSVC
ts = time.time()
method = LinearSVC(max_iter=20000)
method.fit(Xtrain,ytrain)
score = method.score(Xtest, ytest)
ypred = method.predict(Xtest)
te = time.time()
t_total = te-ts

print("Score : %f, time running : %d secondes" %(score, te-ts))
pd.DataFrame(confusion_matrix(ytest, ypred), index = labels, columns=labels)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
4.5 SVM avec noyau gaussien
Apprentissage avec les valeurs par défaut puis optimisation des paramètres.
Q. Quels sont les paramètres à optimiser ?
Can you write Python code for it?

from sklearn.svm import SVC
ts = time.time()
method = SVC(gamma='auto')
method.fit(Xtrain,ytrain)
score = method.score(Xtest, ytest)
ypred = method.predict(Xtest)
te = time.time()

print("Score : %f, time running : %d secondes" %(score, te-ts))
pd.DataFrame(confusion_matrix(ytest, ypred), index = labels, columns=labels)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
ACP sur un type de signal
Can you write Python code for it?

from sklearn.decomposition import PCA
pca = PCA()
# Choix du signal
isignal = 4
signal = SIGNALS[isignal]
print("ACP Sur signal : " +signal)
X_c = pca.fit_transform(X_train[:,:,isignal])


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Attention: les diagrammes boîtes sont très perturbés par les distributions des composantes avec une très forte concentration autour de 0 et énormément de valeurs atypiques. D'où l'utilisation du paramètre whis=100 pour rallonger les moustaches.
Q. Que sont les graphes ci-dessus ? Quelles interprétations ou absence d'interprétation en tirer ?
Représentation du premier plan factoriel :
Can you write Python code for it?

fig = plt.figure(figsize=(10,10), )
ax = fig.add_subplot(1,1,1)
plot_pca(X_c, Y_train_label,fig ,ax ,1 ,2)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Calcul de la matrice des composantes principales. C'est aussi un changement (transformation) de base; de la base canonique dans la base des vecteurs propres.
Can you write Python code for it?

pca = PCA()
X_c = pca.fit_transform(Xtrain)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Définition du réseau
Une couche cachée, une couche de reformattage puis une couche de sortie à 6 classes. Le nombre de neurones (50) sur la couche cachée a été optimisé par ailleurs. Le nombre d'epochs  et la taille des batchs devraient être optimisés surtout dans le cas d'utilisation d'une carte GPU.
Remarquer le nombre de paramètres à estimer.
Can you write Python code for it?

epochs=20
batch_size=32
n_hidden = 50

timesteps = len(X_train[0])
input_dim = len(X_train[0][0])
n_classes = 6

model_base_mlp =km.Sequential()
model_base_mlp.add(kl.Dense(n_hidden, input_shape=(timesteps, input_dim),  activation = "relu"))
model_base_mlp.add(kl.Reshape((timesteps*n_hidden,) , input_shape= (timesteps, n_hidden)  ))
model_base_mlp.add(kl.Dense(n_classes, activation='softmax'))

model_base_mlp.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
model_base_mlp.summary()

t_start = time.time()
model_base_mlp.fit(X_train,  Y_train_dummies, batch_size=batch_size, validation_data=(X_test, Y_test_dummies), epochs=epochs)
t_end = time.time()
t_learning = t_end-t_start

score = model_base_mlp.evaluate(X_test, Y_test_dummies)[1]
print("\nScore With Simple MLP on Multidimensional Inertial Signals = %.2f, Learning time = %.2f secondes" %(score*100, t_learning) )
metadata_mlp = {"time_learning" : t_learning, "score" : score}
base_mlp_prediction = model_base_mlp.predict(X_test)

my_confusion_matrix(Y_test_dummies, base_mlp_prediction)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Identification des variables participant le plus au premier axe. Ce n'est pas plus clair! Seule la réprésentation des individus apporte finalement des éléments de compréhension.
Can you write Python code for it?


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Lecture des données
Can you write Python code for it?

#Multidimensional Data
X_train, X_test = load_signals(DATADIR_UCI, 'train'), load_signals(DATADIR_UCI, 'test')
# Flattened Data
X_train_flatten, X_test_flatten = load_signals(DATADIR_UCI, 'train', flatten=True), load_signals(DATADIR_UCI, 'test', flatten=True)

# Label Y
Y_train_label, Y_test_label = load_y(DATADIR_UCI, 'train', dummies = False), load_y(DATADIR_UCI, 'test', dummies = False)
#Dummies Y (For Keras)
Y_train_dummies, Y_test_dummies = load_y(DATADIR_UCI, 'train', dummies = True), load_y(DATADIR_UCI, 'test', dummies = True)

N_train = X_train.shape[0]
N_test = X_test.shape[0]


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Les autres plans factoriels ne sont guère plus informatifs.
Sur tous les signaux
Tous les signaux sont concaténés à plat en un seul signal.
Can you write Python code for it?

pca = PCA()
print("ACP Sur tous les signaux")
X_c = pca.fit_transform(X_train_flatten)

fig = plt.figure(figsize=(15,10))
ax = fig.add_subplot(1,2,1)
ax.bar(range(10), pca.explained_variance_ratio_[:10]*100, align='center',
        color='grey', ecolor='black')
ax.set_xticks(range(10))
ax.set_ylabel("Variance")
ax.set_title("", fontsize=35)
ax.set_title(u"Pourcentage de variance expliqué \n des premières composantes", fontsize=20)

ax = fig.add_subplot(1,2,2)
box=ax.boxplot(X_c[:,0:10],whis=100)
ax.set_title(u"Distribution des premières composantes", fontsize=20)

fig.suptitle(u"Résultat ACP", fontsize=25)

fig = plt.figure(figsize=(10,10), )
ax = fig.add_subplot(1,1,1)
plot_pca(X_c, Y_train_label,fig ,ax ,1 ,3)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Prévision de l'activité de l'échantillon test
Une fois le modèle estimé, l'erreur de prévision est évaluée, sans biais optimiste, sur un autre échantillon, dit échantillon test, qui n'a pas participé à l'apprentissage du modèle.
Can you write Python code for it?

from sklearn.metrics import confusion_matrix, accuracy_score
print("Score : %f, time running : %d secondes" %(score, te-ts))
pd.DataFrame(confusion_matrix(ytest, ypred), index = labels, columns=labels)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Q. Commenter la séparation des deux types de situation par le premier axe.
Q. Que dire sur la forme des nuages ?
Q. Que dire sur la plus ou moins bonne séparation des classes ?
Représentation des variables en ACP
Lecture des libellés des variables et constitution d'une liste. Souci de la grande dimension (561), les représentations ne sont guère exploitables.
Can you write Python code for it?

with open('features.txt', 'r') as content_file:
    featuresNames = content_file.read()
columnsNames = list(map(lambda x : x.split(" ")[1],featuresNames.split("\n")[:-1]))


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Q. Comparer les deux approches par SVM (linéaire et radiale): temps de calcul et performance.
4.6 Random forest
Q. Quel serait le paramètre à optimiser ?
Can you write Python code for it?

from sklearn.ensemble import RandomForestClassifier
ts = time.time()
method = RandomForestClassifier(n_estimators=200,n_jobs=-1)
method.fit(Xtrain,ytrain)
score = method.score(Xtest, ytest)
ypred = method.predict(Xtest)
te = time.time()
t_total = te-ts

print("Score : %f, time running : %d secondes" %(score, te-ts))
pd.DataFrame(confusion_matrix(ytest, ypred), index = labels, columns=labels)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Q. L'amélioration est-elle bien significative au regard du temps de calcul ?
Q. Déterminer les variables sélectionnées par la méthode LASSO.
4.2 Analyse discriminante linéaire
Q. Que dire de l'optimisation de cette méthode ? Celle-ci est proposée dans une librairie de R mais pas disponible en Python.
Q. L'analyse discriminante quadratique pose quelques soucis. Pourquoi ?
Can you write Python code for it?

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
ts = time.time()
method = LinearDiscriminantAnalysis()
method.fit(Xtrain,ytrain)
score = method.score(Xtest, ytest)
ypred = method.predict(Xtest)
te = time.time()
score

print("Score : %f, time running : %d secondes" %(score, te-ts))
pd.DataFrame(confusion_matrix(ytest, ypred), index = labels, columns=labels)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Q. Que dire de l'efficacité d'une approche non supervisée pour catégoriser les activités ?
Episode 2
4 Prévision de l'activité à partir des variables "métier"
Plusieurs méthodes sont successivement testées dans ce calepin : SVM, analyse discriminate décisionnelle, $k$ plus proches voisins, forêts aléatoires, réseaux de neurones... Nous commençons par la régression logistique  pour la prévision du comportement.
4.1 Régression logistique
Principe
Une méthode statistique ancienne mais finalement efficace sur ces données. La régression logistique est adaptée à la prévision d'une variable binaire. Dans le cas multiclasse, la fonction logistique de la librairie Scikit-learn estime par défaut un modèle par classe: une classe contre les autres.
La probabilité d'appartenance d'un individu à une classe est modélisée à l'aide d'une combinaison linéaire des variables explicatives. Pour transformer une combinaison linéaire à valeur dans $R$ en une probabilité à valeurs dans l'intervalle $[0, 1]$, une fonction de forme sigmoïdale est appliquée.  Ceci donne: $$P(y_i=1)=\frac{e^{Xb}}{1+e^{Xb}}$$ ou, c'est équivalent, une décomposition linéaire du logit ou log odd ratio de  $P(y_i=1)$:  $$\log\frac{P(y_i=1)}{1-P(y_i=1)}=Xb.$$
Estimation du modèle sans optimisation
Le modèle est estimé sans chercher à raffiner les valeurs de certains paramètres (pénalisation). Ce sera fait dans un deuxième temps. Le paramètre de choix du solver est précisé car le choix par défaut (lbfgs) semble converger moins vite. Une comparaison systématique des différentes options (liblinear, lbfgs, saga, sag, newton-cg) serait bienvenue en association avec le choix de modèle lorsque le nombre de classes est plus grand que 2: fonction perte multinomiale ou un modèle binomial par classe.
Can you write Python code for it?

from sklearn.linear_model import LogisticRegression
ts = time.time()
method = LogisticRegression(solver='liblinear',multi_class='auto')
method.fit(Xtrain,ytrain)
score = method.score(Xtest, ytest)
ypred = method.predict(Xtest)
te = time.time()


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Q. Quelle procédure est exécutée ci-après et dans quel but ?
Attention: l'exécution est un peu longue et peut être omise en preière lecture.
Can you write Python code for it?

ts = time.time()
param=[{"C":[4,5,6],"gamma":[.01,.02,.03]}]
svm= GridSearchCV(SVC(),param,cv=10,n_jobs=-1)
svmOpt=svm.fit(Xtrain, ytrain)
te = time.time()
te-ts

# paramètre optimal
print("Meilleur score = %f, Meilleur paramètre = %s" % (svmOpt.best_score_,svmOpt.best_params_))


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Q. Quelles sont les classes qui restent difficiles à discriminer ?
Q. Commenter la qualité des résultats obtenus. Sont-ils cohérents avec l'approche exploratoire ?
Optimisation du modèle par pénalisation Lasso
Attention l'exécution est un peu longue... cette optimisation peut être omise en première lecture.
Can you write Python code for it?

# Optimisation du paramètre de pénalisation
# grille de valeurs
from sklearn.model_selection import GridSearchCV
ts = time.time()
param=[{"C":[0.5,1,5,10,12,15,30]}]
logit = GridSearchCV(LogisticRegression(penalty="l1",solver='liblinear',
                                        multi_class='auto'), param,cv=10,n_jobs=-1)
logitOpt=logit.fit(Xtrain, ytrain)
# paramètre optimal
logitOpt.best_params_["C"]
te = time.time()
print("Temps : %d secondes" %(te-ts))

print("Meilleur score = %f, Meilleur paramètre = %s" % (logitOpt.best_score_,logitOpt.best_params_))

yChap = logitOpt.predict(Xtest)
# matrice de confusion
logitOpt.score(Xtest, ytest)

pd.DataFrame(confusion_matrix(ytest, yChap), index = labels, columns=labels)


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Remarque : Apprécier la difficulté à distinguer les activités au sein d'un même signal.
3.3 Par signal
Le seul signal "acélération en" x est tracé en distinguant les activités.
Can you write Python code for it?

sample_to_plot = 10
isignal = 1
index_per_act_dict = dict([(act, np.where(Y_train_label==act)[0][:sample_to_plot]) for act in range(1,7)])

fig = plt.figure(figsize=(15,8), num=SIGNALS[isignal])
for act , index in index_per_act_dict.items():
    ax = fig.add_subplot(3,2,act)
    for x in X_train[index]:
        ax.plot(range(128), x[:,0],color=CMAP(act-1), linewidth=1)
    ax.set_title(ACTIVITY_DIC[act])
plt.tight_layout()


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Tracés de tous les signaux
Tous les signaux sont tracés par type en superposant les activités.
Can you write Python code for it?

sample_to_plot = 50
index_per_act = [list(zip(np.repeat(act, sample_to_plot), np.where(Y_train_label==act)[0][:sample_to_plot])) for act in range(1,7)]
index_to_plot = list(itertools.chain.from_iterable(index_per_act))
random.shuffle(index_to_plot)

fig = plt.figure(figsize=(15,10))
for isignal in range(9):
    ax = fig.add_subplot(3,3,isignal+1)
    for act , i in index_to_plot:
        ax.plot(range(128), X_train[i,:,isignal],color=CMAP(act-1), linewidth=1)
        ax.set_title(SIGNALS[isignal])
plt.tight_layout()


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Un graphique plus explicite décrit les distribution de ces composantes par des diagrames boîtes; seules les premières sont affichées.
Can you write Python code for it?


I am working on the file "HAR/ML-4-IoT-Har.ipynb".
The first task is:
Vérification des dimensions afin de s'assurer de la bonne lecture des fichiers.
Can you write Python code for it?

print("Dimension")
print("Données Multidimensionelles, : " + str(X_train.shape))
print("Données Unimensionelles, : " + str(X_train_flatten.shape))
print("Vecteur réponse (scikit-learn) : " + str(Y_train_label.shape))
print("Matrice réponse(Keras) : " + str(Y_train_dummies.shape))


I am working on the file "HARVESTING_DATA_PYT_DS_SAISOFT.ipynb".
The first task is:
This is not quite how we want to see the data.  We need to flip it, or swap axes.  Transpose will do.  Then lets change the column names.  Finally, we'll sort.
Can you write Python code for it?

chems = chems.T
chems.columns=["Protons", "Symbol", "Name", "Mass", "Category", "Changed", "Initials"]

chems.sort_values("Protons")
chems.head()

chems = chems.sort_index()  # lets get this in index order

chems.head()


I am working on the file "HDDM/Hddm_test_parallel.ipynb".
The first task is:
Check convergence (visual inspection)
plot multiple chain and visually inspect how the convergence goes.
Can you write Python code for it?

def plot_grp_trace(models, param):
    import seaborn as sns
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt

    df_grp_traces = []
    for i in range(4):
        df_m = models[i]
        df_grp_trace = df_m.get_group_traces()
        df_grp_trace['chain'] = i
        print('chain', i, df_grp_trace.shape)
        df_grp_traces.append(df_grp_trace)
    df_grp_traces = pd.concat(df_grp_traces)

    # select the columns included that parameter
    filter_col = [col for col in df_grp_traces if col.startswith(param)]
    filter_col.append('chain') # add the chain column to the data

    #df_tmp = df_grp_traces[df_grp_traces.columns.intersection(filter_col)]
    df_tmp = df_grp_traces.loc[:,filter_col]
    df_tmp["sample_id"] = df_tmp.index

    df_melt = pd.melt(df_tmp,
                      id_vars=['sample_id', 'chain'],
                      value_vars=filter_col[:-1],
                      var_name='cond',
                      value_name='value')

    sns.set_palette("Set2")
    for indx, df_tmp in df_melt.groupby('cond'):

        fig, axs = plt.subplots(ncols=2,figsize=(20, 9))
        print(indx)

        sns.lineplot(x='sample_id', y='value', hue="chain", data=df_tmp, ax=axs[0], palette='Set2')

        sns.distplot(df_tmp.loc[df_tmp['chain'] == 0, 'value'], hist = False, kde = True,
                         kde_kws = {'shade': False, 'linewidth': 3},ax=axs[1], label = '0')
        sns.distplot(df_tmp.loc[df_tmp['chain'] == 1, 'value'], hist = False, kde = True,
                         kde_kws = {'shade': False, 'linewidth': 3},ax=axs[1], label = '1')
        sns.distplot(df_tmp.loc[df_tmp['chain'] == 2, 'value'], hist = False, kde = True,
                         kde_kws = {'shade': False, 'linewidth': 3},ax=axs[1], label = '2')
        sns.distplot(df_tmp.loc[df_tmp['chain'] == 3, 'value'], hist = False, kde = True,
                         kde_kws = {'shade': False, 'linewidth': 3},ax=axs[1], label = '3')

        #axs[0].set_ylim([0.5, 3])
     ...


I am working on the file "HDDM/Hddm_test_parallel.ipynb".
The first task is:
Test the docker image: Single chain
Can you write Python code for it?

start_time = time.time()  # the start time of the processing

m = run_model(0)

print("\nRunning 1 chain used: %f seconds." % (time.time() - start_time))

# m = hddm.load("df_example_chain_vaz_test_0")
print("M_df1a_v_corr DIC: %f" % m.dic)

m.plot_posterior_predictive()


I am working on the file "HDDM/Within_models_from_tutorial.ipynb".
The first task is:
Comparing generative and recovered model parameters
First, let's print the model stats
Can you write Python code for it?


I am working on the file "HDDM/Within_models_from_tutorial.ipynb".
The first task is:
Let's first replicate the tutorial on HDDM website
http://ski.clps.brown.edu/hddm_docs/tutorial_regression_stimcoding.html
Can you write Python code for it?

import sys
# the following will save all the stdout to the txt file, which is not necessary
# sys.stdout = open('ModelRecoveryOutput.txt', 'w')


I am working on the file "HDDM/Within_models_from_tutorial.ipynb".
The first task is:
Next, set up parameters of the DDM for three levels and the first stimulus.
Can you write Python code for it?


I am working on the file "HDDM/Within_models_from_tutorial.ipynb".
The first task is:
Now, let's generate data for both stimuli
Can you write Python code for it?

random.seed(123)
np.random.seed(123)
data_a, params_a = hddm.generate.gen_rand_data({'level1': level1a,
                                                'level2': level2a,
                                                'level3': level3a},
                                              size=trials_per_level,
                                              subjs=n_subjects)

data_b, params_b = hddm.generate.gen_rand_data({'level1': level1b,
                                                'level2': level2b,
                                                'level3': level3b},
                                              size=trials_per_level,
                                              subjs=n_subjects)

data_a['stimulus'] = pd.Series(np.ones((len(data_a))), index=data_a.index)
data_b['stimulus'] = pd.Series(np.ones((len(data_b)))*2, index=data_a.index)
mydata = data_a.append(data_b,ignore_index=True)

mydata.head(10)


I am working on the file "HDDM/Within_models_from_tutorial.ipynb".
The first task is:
Now, we set up the regression models for z and v and also include the link functions. The relevant string here used by patsy is 1 + C(condition). This will generate a design matrix with an intercept (that's what the 1 is for) and two dummy variables for remaining levels. (The column in which the levels are coded has the default name condition):
Can you write Python code for it?

z_reg = {'model': 'z ~ 1 + C(condition)', 'link_func':z_link_func}


I am working on the file "HDDM/Within_models_from_tutorial.ipynb".
The first task is:
Setting up the HDDM regression model
The parameter z is bound between 0 and 1, but the standard linear regression does not generate values between 0 and 1. Therefore, we use a link-function, here the inverse logit $1/(1+exp(-x))$, which transform values between plus and minus infinity into values ranging from (just above) 0 to (nearly) 1. [this link function is related to logistic regression].
Next, we need to ensure that bias is $z$ for one stimulus and $1-z$ for the other stimulus. To achive this, we can simply multiply the regression output for one stimulus with $-1$. This is implemented here by dot-multiplying the regression output "x" (which is an array) with equally sized array "stim", which is 1 for all stimulus A trials and -1 for stimulus B trials. We use the patsy command dmatrix to generate such an array from the stimulus column of our stimulated data.
Note
* dot-multiplying is vague here, it should be Hadamard Product, see here.
Can you write Python code for it?

tmpdata = mydata[mydata['subj_idx'] <= 0]
tmpdata

stim = (dmatrix('0 + C(s, [[1], [-1]])',
                {'s':tmpdata.stimulus.loc[tmpdata.index]},
                return_type="dataframe")
        )

#tmp = np.multiply(tmpdata[['stimulus']], stim[:])
tmp = np.multiply(tmpdata.stimulus.loc[tmpdata.index].to_frame(),stim)
#1/(1+np.exp(-((np.asarray(mydata.stimulus.iloc[mydata.index])) * stim)))
type(stim)
#stim.index
#type(mydata.stimulus.iloc[mydata.index])

tmp

tmp2 = tmpdata.stimulus.loc[tmpdata.index]
tmp2.to_frame().shape

# x is a pandas Series, it should be converted to dataframe to do the multiplication.

def z_link_func(x, data=mydata):
    stim = (dmatrix('0 + C(s, [[1], [-1]])',
                    {'s':data.stimulus.loc[x.index]},
                    return_type="dataframe")
#    stim = (np.asarray(dmatrix('0 + C(s, [[1], [-1]])',
                               #{'s':data.stimulus.ix[x.index]})) # original .ix is deprecated.
           )
    #print(x.shape)
    return 1/(1+np.exp(-np.multiply(x.to_frame(), stim)))
    #return 1 /( 1+np.exp(-(x * stim)))


I am working on the file "HDDM/Within_models_from_tutorial.ipynb".
The first task is:
The last step before running the model is to construct the complete hddm regression model by adding data etc.
Can you write Python code for it?

m_reg = hddm.HDDMRegressor(mydata, reg_descr, include='z')