I am working on the file "4.Docs/assign1.ipynb".
The first task is:
1. Comparando composicionalidad y analogía.
Composicionalidad y analogía son dos mecanismos diferentes que se pueden usar con representaciones distribuidas. La idea es usar independientemente composicionalidad y analogía para resolver el mismo problema. El problema a resolver es encontrar el presidente de un país dado.
Primero usaremos composicionalidad. La función siguiente debe recibir el nombre de un país y retornar una lista de palabras que posiblemente corresponden a presidentes.
Por ejemplo, si la función se invoca con 'ecuador' como argumento:
```python

presidents_comp('ecuador')
[u'jamil_mahuad',
 u'presidencia',
 u'jose_maria_velasco_ibarra',
 u'republica',
 u'rafael_correa',
 u'gustavo_noboa',
 u'lucio_gutierrez',
 u'abdala_bucaram',
 u'vicepresidente',
 u'gabriel_garcia_moreno']
 ```
Can you write Python code for it?

def presidents_comp(country):
    ### Su código debe ir aquí
    return []

for country in ['colombia', 'venezuela', 'ecuador', 'brasil', 'argentina', 'chile']:
    print country
    for president in presidents_comp(country):
        print ' ', president


I am working on the file "4.Docs/assign1.ipynb".
The first task is:
Busque más ejemplos en los que funcione y otros en los que no funcione. Explique.
4. Una de estas cosas no es como las otras...
Gensim provee la función doesnt_match, la cual permite encontrar, dentro de una lista de palabras, una palabra que está fuera de lugar. Por ejemplo:
Can you write Python code for it?

model.doesnt_match("azul rojo abajo verde".split())


I am working on the file "4.Docs/assign1.ipynb".
The first task is:
El siguiente paso es usar analogías para encontrar el presidente de un país dado.
Can you write Python code for it?

def presidents_analogy(country):
    ### Su código debe ir aquí
    return []

for country in ['colombia', 'venezuela', 'ecuador', 'brasil', 'argentina', 'chile']:
    print country
    for president in presidents_analogy(country):
        print ' ', president


I am working on the file "4.Docs/assign2.ipynb".
The first task is:
Explique claramente como es su estrategia. Pruebe la función con diferentes ejemplos. Discuta los resultados.
2. Jugando al ahorcado
Diseñe una función que sea capaz de encontrar los caracteres faltantes en una palabra. La función deber funcionar de la siguiente manera:
```

hangman('al_br')
'palabra'
hangman('erp__nt')
'serpiente'
```
Can you write Python code for it?

def hangman(word):
    ### Su código debe ir aquí
    return ''


I am working on the file "4.Docs/quickIntro2NN.ipynb".
The first task is:

Answer: It is impossible with only one neuron!

We need to use more than one neuron....
Multilayer Neural Network

Learning an XOR NN
Can you write Python code for it?

Dtrain = SupervisedDataSet(2,1) # define a dataset in pybrain
Dtrain.addSample([0,0],[0])
Dtrain.addSample([0,1],[1])
Dtrain.addSample([1,0],[1])
Dtrain.addSample([1,1],[0])

from pybrain.supervised.trainers import BackpropTrainer

net = buildNetwork(2, 2, 1, hiddenclass=pybrain.SigmoidLayer, outclass=pybrain.SigmoidLayer)
T = BackpropTrainer(net, learningrate=0.1, momentum=0.9)
T.trainOnDataset(Dtrain, 1000)
print_pred2(D, net)


I am working on the file "4.Docs/quickIntro2NN.ipynb".
The first task is:
AND Neural Network
<img width=500 src="2in-neuron.jpg" align="middle">
Can you write Python code for it?

net.params[:] = [0, 0,  0]
print_pred2(D, net)


I am working on the file "4.Docs/quickIntro2NN.ipynb".
The first task is:
Backpropagation
Can you write Python code for it?

T = BackpropTrainer(net, learningrate=0.01, momentum=0.99)
scores = []
for i in xrange(1000):
    T.trainOnDataset(Dlrrh, 1)
    prediction = net.activateOnDataset(Dlrrh)
    scores.append(validator.MSE(prediction, Dlrrh.getField('target')))
plt.ylabel('Mean Square Error')
plt.xlabel('Iteration')
plt.plot(scores)


I am working on the file "4.Docs/quickIntro2NN.ipynb".
The first task is:
Plotting the NN Output
Can you write Python code for it?

net.params[:] = [-30, 20, 20]
plot_nn_prediction(net)


I am working on the file "4.Docs/quickIntro2NN.ipynb".
The first task is:
Prediction
Can you write Python code for it?

def lrrh_input(vals):
    return pd.DataFrame(vals,index=['big ears', 'big teeth', 'handsome', 'wrinkled'], columns=['input'])

def lrrh_output(vals):
    return pd.DataFrame(vals,index=['scream', 'hug', 'offer food', 'kiss cheek'], columns=['output'])

in_vals = [1, 1, 0, 0]
lrrh_input(in_vals)

lrrh_output(net.activate(in_vals))


I am working on the file "4.Docs/quickIntro2NN.ipynb".
The first task is:
The Little Red Riding Hood Neural Network

LRRH Network Architecture

Training
Can you write Python code for it?

from pybrain.tools.validation import Validator

validator =  Validator()
Dlrrh = SupervisedDataSet(4,4)
Dlrrh.addSample([1,1,0,0],[1,0,0,0])
Dlrrh.addSample([0,1,1,0],[0,0,1,1])
Dlrrh.addSample([0,0,0,1],[0,1,1,0])
df = pd.DataFrame(Dlrrh['input'],columns=['Big Ears', 'Big Teeth', 'Handsome', 'Wrinkled'])
print df.join(pd.DataFrame(Dlrrh['target'],columns=['Scream', 'Hug', 'Food', 'Kiss']))
net = buildNetwork(4, 3, 4, hiddenclass=pybrain.SigmoidLayer, outclass=pybrain.SigmoidLayer)


I am working on the file "4.Docs/quickIntro2NN.ipynb".
The first task is:
XOR NN Output Plot
Can you write Python code for it?


I am working on the file "4.Docs/word2vec.ipynb".
The first task is:
Calculating comparatives from adjectives
Can you write Python code for it?

def plot_data(orig_data, labels):
    pca = PCA(n_components=2)
    data = pca.fit_transform(orig_data)
    plt.plot(data[:,0], data[:,1], '.')
    for i in range(len(data)):
        plt.annotate(labels[i], xy = data[i])
    for i in range(len(data)/2):
        plt.annotate("",
                xy=data[i],
                xytext=data[i+len(data)/2],
                arrowprops=dict(arrowstyle="->",
                                connectionstyle="arc3")
                )

def analogy(worda, wordb, wordc):
    result = model.most_similar(negative=[worda],
                                positive=[wordb, wordc])
    return result[0][0]
adjectives = ['big', 'small', 'large', 'wide', 'strong']
comparatives = [analogy('good', 'better', adjective) for adjective in adjectives]
pp(zip(adjectives,comparatives))


I am working on the file "4.Docs/word2vec.ipynb".
The first task is:
Compositionality
Can you write Python code for it?


I am working on the file "4.Docs/word2vec.ipynb".
The first task is:
Continuous representation of words
Can you write Python code for it?


I am working on the file "4.Docs/word2vec.ipynb".
The first task is:
Playing with word2vec
Fabio A. González, Universidad Nacional de Colombia
Google News dataset

Internal Google data set with one billion words
692k vocabulary. Words with frequency below 5 were discarded.
300-dimensions representation was obtained by training a skip-gram model.
Model available at https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM/edit?usp=sharing
Can you write Python code for it?

## Loading the model with `gensim`

# import wrod2vec model from gensim
from gensim.models.word2vec import Word2Vec

# load Google News pre-trained network
model = Word2Vec.load_word2vec_format('GNvectors.bin', binary=True)


I am working on the file "4.Docs/word2vec.ipynb".
The first task is:
Semantically related words have similar representations
Can you write Python code for it?

plt.plot(model['car'][:50], label = 'car')
plt.plot(model['vehicle'][:50], label = 'vehicle')
plt.legend()


I am working on the file "4.Docs/word2vec.ipynb".
The first task is:
Vector representation similarity = semantic similarity
Can you write Python code for it?


I am working on the file "4.Docs/word2vec.ipynb".
The first task is:
Word vector space encodes linguistic regularities
<img width= 600 src="linguistic regularities.jpg">
Solving analogies

man is to woman as king is to ??
Relationships are encoded by word vector differences:
$$ f(\textrm{"woman"}) - f(\textrm{"man"}) = f(\textrm{"??"}) - f(\textrm{"king"})$$
We can add the relationship encoding vector to a the vector of king: $$ f(\textrm{"king"}) + (f(\textrm{"woman"}) - f(\textrm{"man"})) = f(\textrm{"??"}) $$

$$ f(\textrm{"king"}) + (f(\textrm{"woman"}) - f(\textrm{"man"})) = f(\textrm{"??"}) $$
Can you write Python code for it?

result = model.most_similar(negative=['man'],
                            positive=['woman', 'king'])
pp(result)


I am working on the file "4.Logistic_Regression.ipynb".
The first task is:
Cost Function
$$Cost (P(X\beta), y)) =
\begin{cases}
-log(P(X\beta)),  & \text{if $y=1$ } \
-log(1 -P(X\beta)), & \text{if $y =0$}
\end{cases}$$
Can you write Python code for it?

z = np.linspace(0.001, 0.999, 1000)

c1 = -np.log(z)
c2 = -np.log(1-z)

plt.plot(z,c1)
plt.plot(z,c2)
#plt.hlines(0.5, -10,10)
#plt.vlines(0, 0,1)
plt.xlabel('z')
plt.ylabel('Cost')


I am working on the file "4.Logistic_Regression.ipynb".
The first task is:
Lets say we want to classify the vehicles by 'Hatchback' and 'Sedan'
Can you write Python code for it?

class_mapping = {'Hatchback': 0, 'Sedan': 1}

pop['types'] = pop['type'].map(class_mapping)

plt.scatter(pop.price, pop.types, c=pop.types, s = 150, alpha = 0.8 )
plt.xlabel('price')
plt.ylabel('types')


I am working on the file "4.Logistic_Regression.ipynb".
The first task is:
Logistic Function
So we need a function which is bounded between 0 < f(X) < 1, for our classification to work. We will use a logit function for this purpose
$$ P(z) = \frac {1}{1 + e^{-z}} $$
Can you write Python code for it?

z = np.linspace(-10, 10, 100)

p = 1/(1+np.exp(-z))

plt.plot(z,p)
plt.hlines(0.5, -20,20)
plt.vlines(0, 0,1)
plt.xlabel('z')
plt.ylabel('P(z)')


I am working on the file "4.Logistic_Regression.ipynb".
The first task is:
Why Linear Function does not work
Now we can use a linear function and try to choose a cutoff value to show where the two class separate - lets say cutoff = 0.5
Can you write Python code for it?

def ols (df, xlabel, ylabel):
    n = df.shape[0]
    x0 = np.ones(n)
    x1 = df[xlabel]
    X = np.c_[x0, x1]
    X = np.asmatrix(X)
    y = np.transpose(np.asmatrix(df[ylabel]))
    X_T = np.transpose(X)
    X_pseudo = np.linalg.inv(X_T * X) * X_T
    beta = X_pseudo * y
    return beta

def plot_ols(df, xlabel, ylabel):
    beta = ols(df, 'price', 'types')
    beta_0 = beta.item(0)
    beta_1 = beta.item(1)
    plt.scatter(df[xlabel], df[ylabel], c=df[ylabel], s = 150, alpha = 0.8 )
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)

    y = beta_0 + beta_1 * df[xlabel]
    plt.plot(df[xlabel], y, '-')

    cutoff = (0.5 - beta_0)/beta_1
    plt.vlines(cutoff, -0.4, 1.4)

ols(pop, 'price', 'types')

plot_ols(pop, 'price', 'types')


I am working on the file "4.visualization/Plotting Basics.ipynb".
The first task is:
Only for advanced students: Classification
In the following we will try how good our features are to automatically classify the different activities.
Can you write Python code for it?

#don't look here ... I split the data in test and training set
#bad code :)

l_1 = len(feat1)
l_2 = len(feat2)
l_3 = len(feat3)
l_4 = len(feat4)

#X = feat1.append(feat2).append(feat3).append(feat4)
#Y = [1.0] * l_1 + [2.0]* l_2 + [3] * l_3 + [4] * l_4

X = feat1[0:l_1/2].append(feat2[0:l_2/2]).append(feat3[0:l_3/2]).append(feat4[0:l_4/2])
Y = [1]*(l_1/2) + [2]*(l_2/2)+[3]*(l_3/2) + [4]*(l_4/2)

T = feat1[l_1/2:l_1].append(feat2[l_2/2:l_2]).append(feat3[l_3/2:l_3]).append(feat4[l_4/2:l_4])
t_gg = [1]*len(feat1[l_1/2:l_1]) + [2]*len(feat2[l_2/2:l_2]) + [3]*len(feat3[l_3/2:l_3]) + [4]*len(feat1[l_4/2:l_4])

from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier

neigh = KNeighborsClassifier(n_neighbors=3)
dt = DecisionTreeClassifier()

dt.fit(X,Y)
res = dt.predict(T)
plot(res+0.2, 'r.')
plot(t_gg, 'b.')

print dt.score(T, t_gg)

from sklearn.utils import shuffle
from sklearn.cross_validation import StratifiedKFold, cross_val_score

Xn, yn = shuffle(T, t_gg)
skf = StratifiedKFold(yn, 10)
print cross_val_score(dt, Xn, yn, cv=skf)


I am working on the file "4.visualization/Plotting Basics.ipynb".
The first task is:
try it with the gyroscope data ... what's the difference?
Barcharts
Can you write Python code for it?

y = [3, 10, 7, 5, 3, 4.5, 6, 8.1]
N = len(y)
x = range(N)
width = 1/1.5
plt.bar(x, y, width, color="blue")

plt.barh(x, y, width, color="blue")


I am working on the file "4/0-Time-Series-Analysis.ipynb".
The first task is:
A simple moving average is the most basic way to predict the trend of a series, taking advantage of the generally continuous nature of trends. For example, if I told you to predict the number of wins of a basketball team this season, without giving you any information about the team apart from its past record, you would take the average of the team's wins over the last few seasons as a reasonable predictor.
The simple moving average operates on this exact principle. Choosing an $n$ element window to average over, the prediction at each point is obtained by taking the average of the last $n$ values. Notice that the moving average is undefined for first 12 values because we're looking at a 12 value window.
Can you write Python code for it?

moving_avg = pd.Series(ts_log).rolling(window=12).mean()

plt.plot(ts_log)
plt.plot(moving_avg, color='red')


I am working on the file "4/0-Time-Series-Analysis.ipynb".
The first task is:
Finally, let's plot the time series to get an intial visualization of how the series grows.
Can you write Python code for it?


I am working on the file "4/0-Time-Series-Analysis.ipynb".
The first task is:
Now that we have a model for the stationary series that we can use to predict future values in the stationary series, and we want to get back to the original series. Note that we won't have a value for the first element because we are working with a one step lag. The following procedure takes care of that.
Can you write Python code for it?

predictions_ARIMA_diff = pd.Series(results_ARIMA.fittedvalues, copy=True)
predictions_ARIMA_diff_cumsum = predictions_ARIMA_diff.cumsum()
predictions_ARIMA_log = pd.Series(ts_log.ix[0], index=ts_log.index)
predictions_ARIMA_log = predictions_ARIMA_log.add(predictions_ARIMA_diff_cumsum,fill_value=0)


I am working on the file "4/0-Time-Series-Analysis.ipynb".
The first task is:
Now we can subtract the trend from the original data (eliminating the null values in the case of the simple moving average) to create a new series that is hopefully more stationary. The blue graph represents the smoothing difference, while the red graph represents the simple moving average difference
Can you write Python code for it?

ts_exp_moving_avg_diff = ts_log - expwighted_avg
ts_log_moving_avg_diff = ts_log - moving_avg
ts_log_moving_avg_diff.dropna(inplace=True)
plt.plot(ts_exp_moving_avg_diff)
plt.plot(ts_log_moving_avg_diff, color='red')


I am working on the file "4/0-Time-Series-Analysis.ipynb".
The first task is:
Pandas has great support for datetime objects and general time series analysis operations. We'll be working with an example of predicting the number of airline passengers (in thousands) by month adapted from this tutorial.
First, download this dataset and load it into a Pandas Dataframe by specifying the 'Month' column as the datetime index.
Can you write Python code for it?

dateparse = lambda dates: pd.datetime.strptime(dates, '%Y-%m')
data = pd.read_csv('AirPassengers.csv', parse_dates=['Month'], index_col='Month',date_parser=dateparse)
print data.head()


I am working on the file "4/0-Time-Series-Analysis.ipynb".
The first task is:
Stationarity
Most of the important results for time series forecasting (including the ARIMA model, which we focus on today) assume that the series is stationary - that is, its statistical properties like mean and variance are constant. However, the graph above certainly isn't stationary, given the obvious growth. Thus, we want to manipulate the time series to make it stationary. This process of reducing a time series to a stationary series is a hallmark of time series analysis and forecasting, as most real world time series' aren't initially stationary.
To solve this nonstationarity issue, we can break a time series up into its trend and seasonality. These are the two factors that make a series nonstationary, so the main idea is to remove these factors, operate on the resulting stationary series, then add these factors back in.
First, we will take the log of the series to reduce the positive trend. This gives a seemingly linear trend, making it easier to estimate.
Can you write Python code for it?


I am working on the file "4/0-Time-Series-Analysis.ipynb".
The first task is:
We can also use the Pandas datetime index support to retrieve entire years
Can you write Python code for it?


I am working on the file "4/0-Time-Series-Analysis.ipynb".
The first task is:
You might be unhappy with having to choose a window size. How do we know what window size we want if we don't know much about the data? One solution is to average over all past data, discounting earlier values because they have less predictive power than more recent values. This method is known as smoothing.
Can you write Python code for it?

expwighted_avg = pd.Series(ts_log).ewm(halflife=12).mean()

plt.plot(ts_log)
plt.plot(expwighted_avg, color='red')


I am working on the file "4/4.ipynb".
The first task is:
2. Dominance duration
Can you write Python code for it?

def dominance_durations(x1, x2, t):
    durations = []
    start = None
    for X1, X2, T in zip(x1, x2, t):
        if start is None and X1 > X2:
            start = T
        elif start is not None and X1 < X2:
            durations.append(T - start)
            start = None
    if start is not None:  # population 1 active at the end of the simulation
        durations.append(t[-1] - start)
    return durations

dominance_durations(u1, u2, t)


I am working on the file "4/4.ipynb".
The first task is:
4. Moreno-Bote simulation
Can you write Python code for it?

def sigmoid(x, theta = 0.1, k = 0.05):
    return 1.0/(1+np.exp(-(x-theta)/k))

def f_moreno_bote(y, t, alpha = 0.75, beta= 0.5, gamma= 0.1, phi=0.5, tau= 0.01, tau_a= 2,
          tau_b= 2, tau_s= 0.1, eta= 0.5, g_a= 0.05, g_b= 0.05, f= sigmoid, sigma= 0.03):

    r_a, alpha_a,  r_b, alpha_b, n_a_d, n_b_d = y

    r_pool = max(0, phi*(r_a + r_b) + g_a + g_b)
    r_a_inh = (r_pool + eta * r_a)**2
    r_b_inh = (r_pool + eta * r_b)**2

    dydt = np.asarray([
         (-r_a + f(alpha * r_a - beta * r_a_inh + g_a - alpha_a + n_a_d))/tau,
         (-alpha_a + gamma * r_a)/tau_a,
         (-r_b + f(alpha * r_b - beta * r_b_inh + g_b - alpha_b + n_b_d))/tau,
         (-alpha_b + gamma * r_b)/tau_b,
         -n_a_d/tau_s,
         -n_b_d/tau_s])

    return dydt

def g_moreno_bote(y, t, tau_s = 0.1, sigma = 0.03):
    _, _, _, _, n_a_s, n_b_s = y
    dydt = np.asarray([
            0.0,
            0.0,
            0.0,
            0.0,
            sigma * np.sqrt(2.0/tau_s) * np.random.normal(),
            sigma * np.sqrt(2.0/tau_s) * np.random.normal()
        ])
    return dydt

y0 = [1, 0, 0.01, 0.04, 0, 0]
t=np.linspace(0,20,2000)
y7 = euler_maruyama(f_moreno_bote, g_moreno_bote, y0, t_max = 20, dt=0.01)

plt.subplot(211)
plt.plot(t, y7[:,0], label='Rate a')
plt.plot(t,y7[:,2], label='Rate b')
plt.grid()
plt.xlim(0, 20)
plt.title("Evolution of Rates in Moreno-Bote Model")
plt.legend()

plt.subplot(211)
plt.plot(t, y7[:,1], label='Current a')
plt.plot(t,y7[:,3], label='Current b')
plt.grid()
plt.xlim(0, 20)
plt.title("Evolution of Hyperpolarizing in Moreno-Bote Model")
plt.legend()

plt.subplot(211)
plt.plot(t, y7[:,4], label='Noise a')
plt.plot(t,y7[:,5], label='NOise b')
plt.grid()
plt.xlim(0, 20)
plt.title("Evolution of Noise in Moreno-Bote Model")
plt.legend()


I am working on the file "4/4.ipynb".
The first task is:
5. Dominance Durations
Can you write Python code for it?

y0 = [1, 0, 0.01, 0.04, 0, 0]
t=np.linspace(0,500,50000)
y8 = euler_maruyama(f_moreno_bote, g_moreno_bote, y0, t_max = 500, dt=0.01)

t=np.linspace(0,500,50000)
moreno_dominace=dominance_durations(y8[:,0], y8[:,2], t)
plt.hist(moreno_dominace)
plt.title("Histogram of Dominance Durations")
plt.xlabel("Dominance Duration [ms]")
plt.ylabel("Frequency")

np.mean(moreno_dominace)


I am working on the file "4/4.ipynb".
The first task is:
6. Effect of Gamma
Can you write Python code for it?

# I could not manage to paramaterise the function appologies for the hack

def f_moreno_bote(y, t, alpha = 0.75, beta= 0.5, gamma= 0, phi=0.5, tau= 0.01, tau_a= 2,
          tau_b= 2, tau_s= 0.1, eta= 0.5, g_a= 0.05, g_b= 0.05, f= sigmoid, sigma= 0.03):

    r_a, alpha_a,  r_b, alpha_b, n_a_d, n_b_d = y

    r_pool = max(0, phi*(r_a + r_b) + g_a + g_b)
    r_a_inh = (r_pool + eta * r_a)**2
    r_b_inh = (r_pool + eta * r_b)**2

    dydt = np.asarray([
         (-r_a + f(alpha * r_a - beta * r_a_inh + g_a - alpha_a + n_a_d))/tau,
         (-alpha_a + gamma * r_a)/tau_a,
         (-r_b + f(alpha * r_b - beta * r_b_inh + g_b - alpha_b + n_b_d))/tau,
         (-alpha_b + gamma * r_b)/tau_b,
         -n_a_d/tau_s,
         -n_b_d/tau_s])

    return dydt

y9 = euler_maruyama(f_moreno_bote, g_moreno_bote, y0, t_max = 500, dt=0.01)
moreno_dominace=dominance_durations(y9[:,0], y9[:,2], t)
plt.hist(moreno_dominace)
plt.title("Histogram of Dominance Durations")
plt.xlabel("Dominance Duration [ms]")
plt.ylabel("Frequency")

np.mean(moreno_dominace)


I am working on the file "4/4.ipynb".
The first task is:
Exercise 1
1. Integration
Can you write Python code for it?

from scipy.integrate import odeint

def step(x):
    return int(x >= 0)

x = np.linspace(-10, 10, 1000)
plt.plot(x, np.vectorize(step)(x))

def laing_chow(y, t, I1, I2, alpha=0.2, beta=0.4, phi=0.4, tau=20, g1=1, g2=1):
    u1, u2, a1, a2 = y
    du1 = -u1 + step(alpha * u1 * g1 - beta * u2 * g2 - a1 + I1)
    du2 = -u2 + step(alpha * u2 * g2 - beta * u1 * g1 - a2 + I2)
    da1 = 1 / tau * (-a1 + phi * step(alpha * u1 * g1 - beta * u2 * g2 - a1 + I1))
    da2 = 1 / tau * (-a2 + phi * step(alpha * u2 * g2 - beta * u1 * g1 - a2 + I2))
    return [du1, du2, da1, da2]

x0 = [1, 0, 0.1, 0.25]
t = np.linspace(0, 500, 10000)
I1 = 0.43
I2 = 0.5
u1, u2, a1, a2 = odeint(laing_chow, x0, t, args=(I1, I2)).T

def plot_results(u1, u2, a1, a2):
    plt.subplot(211)
    plt.plot(t, u1, label='u1')
    plt.plot(t, a1, label='a1')
    plt.grid()
    plt.xlim(0, 500)
    plt.legend()

    plt.subplot(212)
    plt.plot(t, u2, label='u2')
    plt.plot(t, a2, label='a2')
    plt.grid()
    plt.xlim(0, 500)
    plt.legend()
    plt.xlabel('t')

plot_results(u1, u2, a1, a2)


I am working on the file "4/4.ipynb".
The first task is:
For intermediate inputs (here: 0.55) the populations are inactive for shorter periods of time. Analytically, $T_1$ = 10.22. This deviates strongly from the simulated dominance durations (see above).
Exercise 2
1. Problems with scipy.integrate.odeint
The Moereno-Bote model uses stochastic differential equations, therefore is not compatable with scipy.integrate.odeint
2. Euler-Maruyama
Can you write Python code for it?

def laing_chow(y, t, I1, I2, alpha=0.2, beta=0.4, phi=0.4, tau=20, g1=1, g2=1):
    u1, u2, a1, a2 = y
    du1 = -u1 + step(alpha * u1 * g1 - beta * u2 * g2 - a1 + I1)
    du2 = -u2 + step(alpha * u2 * g2 - beta * u1 * g1 - a2 + I2)
    da1 = 1 / tau * (-a1 + phi * step(alpha * u1 * g1 - beta * u2 * g2 - a1 + I1))
    da2 = 1 / tau * (-a2 + phi * step(alpha * u2 * g2 - beta * u1 * g1 - a2 + I2))
    return np.asarray([du1, du2, da1, da2])

def euler_maruyama(ffun, gfun, x_0, t_max, dt, *args,  **params):
    print(params)
    """
    Multidimensional Euler-Maruyama DE solver.
    """
    x_0 = np.asarray(x_0)
    time = np.arange(0, t_max, dt)
    dx = np.zeros((x_0.size, time.size)).T
    dx[0,:] = x_0
    for t in range(time.size-1):
        W = np.random.normal()
        dx[t+1,:] = dx[t, :] + ffun(dx[t, :], t*dt, *args, **params) * dt + gfun(dx[t, :], t*dt, *args, **params) * np.sqrt(dt) * W
    return dx

I1, I2 = 0.43, 0.5
g_fun = lambda *args: 0
y0 = [1.0, 0, 0.1, 0.25]
y1 = euler_maruyama(laing_chow, g_fun, y0, 500, 0.1, I1, I2)
t=np.linspace(0, 500, 5000)
plot_results(y4[:,0],y4[:,1],y4[:,2],y4[:,3])


I am working on the file "4/4.ipynb".
The first task is:
Inserting the parameter values from above into eq. 9, one gets an analytical dominance duration of $T_1$ = 26.17. This is roughly in agreement with the simulated dominance durations (see above), but with a sligh deviation of ~2.
3. Oscillation stop
With the parameter values from above, $- \alpha + \beta + \phi$ = 0.6.
Can you write Python code for it?

u1, u2, a1, a2 = odeint(laing_chow, x0, t, args=(0.8, 0.8)).T
plot_results(u1, u2, a1, a2)


I am working on the file "4/4.ipynb".
The first task is:
Laing-Chow calculated using Euler-Maruyama
3. Stochastic DEs
Can you write Python code for it?

f_fun= lambda *args: 0
g_fun= lambda *args:(1/tau)
y0=0
tau=0.1
y2=euler_maruyama(f_fun, g_fun, y0, t_max=5, dt=0.01)
t=np.linspace(0,5,500)
plt.plot(t,y2)
plt.title("Tau=0.1")

f_fun= lambda *args: 0
g_fun= lambda *args:(1/tau)
y0=0
tau=1
y3=euler_maruyama(f_fun, g_fun, y0, t_max=5, dt=0.01)
t=np.linspace(0,5,500)
plt.plot(t,y3)
plt.title("Tau=1")

f_fun= lambda x, *args: (x/tau)
g_fun= lambda *args:(1/tau)
y0=0
tau=1
y4=euler_maruyama(f_fun, g_fun, y0, t_max=5, dt=0.01)
t=np.linspace(0,5,500)
plt.plot(t,y5)
plt.title("Tau=1")


I am working on the file "4/4.ipynb".
The first task is:
The scale is very different between the three simulations, tau=0.1 produces a much greater range of values.
Can you write Python code for it?

# This probably requires some more explanation.
#Including reference to Wiener processes and Ornstein-Uhlenbeck processes


I am working on the file "4/4.ipynb".
The first task is:
The simulation confirms that for high inputs (here: 0.8), both populations are active and oscillation stops.
Can you write Python code for it?

u1, u2, a1, a2 = odeint(laing_chow, x0, t, args=(0.55, 0.55)).T
plot_results(u1, u2, a1, a2)

dominance_durations(u1, u2, t)


I am working on the file "4/CaseStudy4.ipynb".
The first task is:
A brief exploration
Can you write Python code for it?

df[
    ['shares','n_tokens_title', 'n_tokens_content', 'n_unique_tokens']
].describe()

# Avg number of words in title and popular article content
popular_articles = df.ix[data_frame['shares'] >= 1400]
popular_articles[
    ['shares','n_tokens_title', 'n_tokens_content','n_unique_tokens']
].describe()

# Mean shares for each article type
type_articles = df.pivot_table('shares', index=[
        'is_lifestyle', 'is_entertainment', 'is_business', 'is_social_media', 'is_tech', 'is_world'
    ], aggfunc=[np.mean])
print type_articles

type_articles.plot(kind='bar', color=red)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.title('Mean shares by article type')
plt.xlabel('Article type')
plt.ylabel('Shares')

#### On avg, which day has more shares

# On avg, which day has more shares

day_articles = df.pivot_table('shares', index=[
        'is_monday', 'is_tuesday', 'is_wednesday', 'is_thursday', 'is_friday', 'is_saturday', 'is_sunday'
    ], aggfunc=[np.mean])
print day_articles

day_articles.plot(kind='bar', color=green)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.title('Mean shares by day')
plt.xlabel('Day')
plt.ylabel('Shares')

# Mean shares for tech and not tech channels during and not during weekends
df.pivot_table('shares', index=['is_weekend'], columns=['is_tech'], aggfunc=[np.mean], margins=True)

# Mean tech shares during work week (Monday to Friday)
tech_articles = df.ix[data_frame['is_tech'] == 1]
tech_articles = tech_articles.pivot_table('shares', index=[
        'is_monday', 'is_tuesday', 'is_wednesday', 'is_thursday', 'is_friday'
    ], aggfunc=[np.mean])
print tech_articles

tech_articles.plot(kind='bar', color=blue)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.title('Mean Tech Article Shares by Week Day')
plt.xlabel('Week Day')
plt.ylabel('Shares')

# Explore relationship with some features and number of shares
df.plot(kind='scatter', x='n_tokens_title', y='shares')

df.plot(kind='scatter', x='n_tokens_content',...


I am working on the file "4/CaseStudy4.ipynb".
The first task is:
Added 'buzz_factor' column: "Buzz-Factor" (using shares/day)
Can you write Python code for it?

buzz_factor = df['shares'] / df['timedelta']


I am working on the file "4/CaseStudy4.ipynb".
The first task is:
Added 'popularity' column: split # of shares into 4 "popularity" bins:

0-25%-- "Unpopular"
25%-50%-- "Mildly Popular"
50%-75%--"Popular"
75%-100%-- "Very Popular"
Can you write Python code for it?

popularity = pd.qcut(df['shares'], 4,  labels=[
        "Unpopular",
        "Midly Popular",
        "Popular",
        "Very Popular"
    ])

# Add these two statistics to the Data Frame

df.is_copy = False # turn off chain index warning
df['buzz_factor'] = buzz_factor.values
df['popularity'] = popularity.values


I am working on the file "4/CaseStudy4.ipynb".
The first task is:
Articles with no Buzz
At the very least, we want our software to predict whether an article will generate no "buzz". In this case, we define "no buzz" as belonging in the bottom 25% in terms of shares per day amongst articles published on a similar platform.
Can you write Python code for it?

# Select features
all_features = df.columns.values
excluded_features = [
    'buzz',
    'buzz_factor',
    'no_buzz',
    'popularity',
    'shares',
    'timedelta',
    'unpopular'
]
features1 = [f for f in all_features if f not in excluded_features]

#Rank features by importance
feature_selection_model = ExtraTreesClassifier().fit(df[features1], df['no_buzz'])
feature_importance=feature_selection_model.feature_importances_
importance_matrix=np.array([features, list(feature_importance)]).T
def sortkey(s):
    return s[1]
sort=zip(features,list(feature_importance))

# Extract top 10 important features
f_b=pd.DataFrame(sorted(sort,key=sortkey,reverse=True),columns=['variables','importance'])[:10]
f_b

features_b=f_b['variables']

#split data into two parts
np.random.seed(0)
x_train, x_test, y_train, y_test = train_test_split(df[features_b], df.no_buzz, test_size=0.4, random_state=None)
x_train.shape

# Decision Tree accuracy and time elapsed caculation
#t0=time()
#print "DecisionTree"
#dt = DecisionTreeClassifier(min_samples_split=25,random_state=1)
#clf_dt=dt.fit(x_train,y_train)
#y_predicted = clf_dt.predict(x_test)
#print(metrics.classification_report(y_test, y_predicted))
#t1=time()
#print "time elapsed: ", t1-t0

#Random Forest

# Train classifier and time elapsed caculation
t2=time()
print "RandomForest"
rf = RandomForestClassifier(n_estimators=100,n_jobs=1)
clf_rf = rf.fit(x_train,y_train)
y_predicted_rf = clf_rf.predict(x_test)

# See how random forest did on its own.
print "Acurracy: ", clf_rf.score(x_test,y_test)
t3=time()
print "time elapsed: ", t3-t2

# KNN

from sklearn.neighbors import KNeighborsClassifier

# Determine K by cross-validation.
x_cv_train, x_cv_test, y_cv_train, y_cv_test = train_test_split(x_train, y_train, test_size=0.3, random_state=None)

# We use K-values ranging from 1-10
k=[5,10,15,20,25,30,35,40,45,50]

# Train a model on the trainng set and use that model to predict on the testing...


I am working on the file "4/CaseStudy4.ipynb".
The first task is:
Background analysis/exploration.
IMPORTANT NOTE: The code cells above this one are the complete "solution" cells for problem 3.  What follows was additional exploration/analysis.  It was not used in our results, but we retained it in this notebook for completeness.
Here we try to use PCA to improve our results.
Can you write Python code for it?

from sklearn.decomposition import PCA
pca = PCA(4)
plot_columns = pca.fit_transform(df[features1])
plt.scatter(x=plot_columns[:,0], y=plot_columns[:,1], c=df['unpopular'], color=[red, blue])
plt.show()

np.random.seed(0)
x_train, x_test, y_train, y_test = train_test_split(plot_columns, df.buzz_factor, test_size=0.4, random_state=None)
x_train.shape

#linear regression
from sklearn.linear_model import LinearRegression
model = LinearRegression(normalize=True)
model.fit(x_train, y_train)
from sklearn.metrics import mean_squared_error
predictions = model.predict(x_test)
mean_squared_error(predictions, y_test)

# Import the random forest model.
from sklearn.ensemble import RandomForestRegressor
model2 = RandomForestRegressor(n_estimators=60, min_samples_leaf=10, random_state=1)
model2.fit(x_train, y_train)
predictions = model2.predict(x_test)
mean_squared_error(predictions, y_test)


I am working on the file "4/CaseStudy4.ipynb".
The first task is:
Final Predictions: Ensemble Learning
Can you write Python code for it?

# Predict no_buzz = True if either RF or KNN predict no_buzz = True
y_predicted = y_predicted_knn + y_predicted_rf

# Print and plot confusion matrix
cm = metrics.confusion_matrix(y_test, y_predicted)
print(cm)
plt.matshow(cm)
plt.title('Confusion matrix')
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()

# Print prediction accuracy
print 'Prediction Accuracy'
print (cm[0,0]+cm[1,1])/float(cm[0,0] + cm[0,1] + cm[1,0] + cm[1,1])


# Predict unpopular = True if either RF or KNN predict unpopular = True
y_predicted = y_predicted_knn + y_predicted_rf

#Print and plot confusion matrix
cm = metrics.confusion_matrix(y_test, y_predicted)
print(cm)
plt.matshow(cm)
plt.title('Confusion matrix')
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()

# Print prediction Accuracy
print 'Prediction Accuracy'
print (cm[0,0]+cm[1,1])/float(cm[0,0] + cm[0,1] + cm[1,0] + cm[1,1])


I am working on the file "4/CaseStudy4.ipynb".
The first task is:
Remove outliers if applicable
Can you write Python code for it?

# keep points that are within +2 to -2 standard deviations in column 'Data'.
df = data_frame[data_frame.shares-data_frame.shares.mean()<=(2*data_frame.shares.std())]
df.shape


I am working on the file "4/CaseStudy4.ipynb".
The first task is:
Similarly, split buzz factor into four percentile bins:
* 0-25% "No Buzz"
* 25%-50% "Some Buzz"
* 50%-75% "Buzz"
* 75%-100% "Lots of Buzz"
Can you write Python code for it?

buzz = pd.qcut(df['buzz_factor'], 4,  labels=["No Buzz","Some Buzz","Buzz","Lots of Buzz"])
df['buzz']=buzz.values


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Build the Decoding Layer
Implement decoding_layer() to create a Decoder RNN layer.

Create RNN cell for decoding using rnn_size and num_layers.
Create the output fuction using lambda to transform it's input, logits, to class logits.
Use the your decoding_layer_train(encoder_state, dec_cell, dec_embed_input, sequence_length, decoding_scope, output_fn, keep_prob) function to get the training logits.
Use your decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id, end_of_sequence_id, maximum_length, vocab_size, decoding_scope, output_fn, keep_prob) function to get the inference logits.

Note: You'll need to use tf.variable_scope to share variables between training and inference.
Can you write Python code for it?

def decoding_layer(dec_embed_input, dec_embeddings, encoder_state, vocab_size, sequence_length, rnn_size,
                   num_layers, target_vocab_to_int, keep_prob):
    """
    Create decoding layer
    :param dec_embed_input: Decoder embedded input
    :param dec_embeddings: Decoder embeddings
    :param encoder_state: The encoded state
    :param vocab_size: Size of vocabulary
    :param sequence_length: Sequence Length
    :param rnn_size: RNN Size
    :param num_layers: Number of layers
    :param target_vocab_to_int: Dictionary to go from the target words to an id
    :param keep_prob: Dropout keep probability
    :return: Tuple of (Training Logits, Inference Logits)
    """
    # TODO: Implement Function
    dec_cell = tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.BasicLSTMCell(rnn_size), output_keep_prob=keep_prob)
    dec_cell = tf.contrib.rnn.MultiRNNCell([dec_cell]*num_layers)
    with tf.variable_scope("decoding") as decoding_scope:
        # Output Layer
        output_fn = lambda x: tf.contrib.layers.fully_connected(x, vocab_size, None, scope=decoding_scope)
    with tf.variable_scope("decoding") as decoding_scope:
        # Training Decoder
        train_logits = decoding_layer_train(encoder_state, dec_cell,
                                            dec_embed_input, sequence_length,
                                            decoding_scope,output_fn, keep_prob)
    with tf.variable_scope("decoding", reuse=True) as decoding_scope:
        # Inference Decoder
        inference_logits = decoding_layer_infer(encoder_state, dec_cell, dec_embeddings,
                                            target_vocab_to_int['<GO>'], target_vocab_to_int['<EOS>'],
                                            sequence_length-1, vocab_size, decoding_scope, output_fn, keep_prob)
    return train_logits, inference_logits

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_decoding_layer(decoding_layer)


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Build the Neural Network
You'll build the components necessary to build a Sequence-to-Sequence model by implementing the following functions below:
- model_inputs
- process_decoding_input
- encoding_layer
- decoding_layer_train
- decoding_layer_infer
- decoding_layer
- seq2seq_model
Input
Implement the model_inputs() function to create TF Placeholders for the Neural Network. It should create the following placeholders:

Input text placeholder named "input" using the TF Placeholder name parameter with rank 2.
Targets placeholder with rank 2.
Learning rate placeholder with rank 0.
Keep probability placeholder named "keep_prob" using the TF Placeholder name parameter with rank 0.

Return the placeholders in the following the tuple (Input, Targets, Learing Rate, Keep Probability)
Can you write Python code for it?

def model_inputs():
    """
    Create TF Placeholders for input, targets, and learning rate.
    :return: Tuple (input, targets, learning rate, keep probability)
    """
    # TODO: Implement Function
    inputs = tf.placeholder(name="input", shape=[None, None], dtype=tf.int32)
    targets = tf.placeholder(name="targets", shape=[None, None], dtype=tf.int32)
    lr = tf.placeholder(name="lr", dtype=tf.float32)
    keep_prob = tf.placeholder(name="keep_prob", dtype=tf.float32)
    return inputs, targets, lr, keep_prob

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_inputs(model_inputs)


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Check Point
This is your first checkpoint. If you ever decide to come back to this notebook or have to restart the notebook, you can start from here. The preprocessed data has been saved to disk.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np
import helper

(source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), _ = helper.load_preprocess()


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Check the Version of TensorFlow and Access to GPU
This will check to make sure you have the correct version of TensorFlow and access to a GPU
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
from distutils.version import LooseVersion
import warnings
import tensorflow as tf

# Check TensorFlow Version
assert LooseVersion(tf.__version__) >= LooseVersion('1.0'), 'Please use TensorFlow version 1.0 or newer'
print('TensorFlow Version: {}'.format(tf.__version__))

# Check for a GPU
if not tf.test.gpu_device_name():
    warnings.warn('No GPU found. Please use a GPU to train your neural network.')
else:
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Decoding - Training
Create training logits using tf.contrib.seq2seq.simple_decoder_fn_train() and tf.contrib.seq2seq.dynamic_rnn_decoder().  Apply the output_fn to the tf.contrib.seq2seq.dynamic_rnn_decoder() outputs.
Can you write Python code for it?

def decoding_layer_train(encoder_state, dec_cell, dec_embed_input, sequence_length, decoding_scope,
                         output_fn, keep_prob):
    """
    Create a decoding layer for training
    :param encoder_state: Encoder State
    :param dec_cell: Decoder RNN Cell
    :param dec_embed_input: Decoder embedded input
    :param sequence_length: Sequence Length
    :param decoding_scope: TenorFlow Variable Scope for decoding
    :param output_fn: Function to apply the output layer
    :param keep_prob: Dropout keep probability
    :return: Train Logits
    """
    # TODO: Implement Function
    train_fn = tf.contrib.seq2seq.simple_decoder_fn_train(encoder_state)
    dec_cell = tf.contrib.rnn.DropoutWrapper(dec_cell, output_keep_prob=keep_prob)
    output, final_state, final_context_state = tf.contrib.seq2seq.dynamic_rnn_decoder(dec_cell, train_fn, dec_embed_input, sequence_length, scope=decoding_scope)
    return output_fn(output)

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_decoding_layer_train(decoding_layer_train)


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Encoding
Implement encoding_layer() to create a Encoder RNN layer using tf.nn.dynamic_rnn().
Can you write Python code for it?

def encoding_layer(rnn_inputs, rnn_size, num_layers, keep_prob):
    """
    Create encoding layer
    :param rnn_inputs: Inputs for the RNN
    :param rnn_size: RNN Size
    :param num_layers: Number of layers
    :param keep_prob: Dropout keep probability
    :return: RNN state
    """
    # TODO: Implement Function
    rnn_cell = tf.contrib.rnn.BasicLSTMCell(rnn_size)
    rnn_cell = tf.contrib.rnn.DropoutWrapper(rnn_cell, output_keep_prob=keep_prob)
    rnn_cell = tf.contrib.rnn.MultiRNNCell([rnn_cell]*num_layers)
    output, state = tf.nn.dynamic_rnn(rnn_cell, rnn_inputs, dtype=tf.float32)
    return state

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_encoding_layer(encoding_layer)


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Explore the Data
Play around with view_sentence_range to view different parts of the data.
Can you write Python code for it?

view_sentence_range = (0, 10)

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np

print('Dataset Stats')
print('Roughly the number of unique words: {}'.format(len({word: None for word in source_text.split()})))

sentences = source_text.split('\n')
word_counts = [len(sentence.split()) for sentence in sentences]
print('Number of sentences: {}'.format(len(sentences)))
print('Average number of words in a sentence: {}'.format(np.average(word_counts)))

print()
print('English sentences {} to {}:'.format(*view_sentence_range))
print('\n'.join(source_text.split('\n')[view_sentence_range[0]:view_sentence_range[1]]))
print()
print('French sentences {} to {}:'.format(*view_sentence_range))
print('\n'.join(target_text.split('\n')[view_sentence_range[0]:view_sentence_range[1]]))


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Neural Network Training
Hyperparameters
Tune the following parameters:

Set epochs to the number of epochs.
Set batch_size to the batch size.
Set rnn_size to the size of the RNNs.
Set num_layers to the number of layers.
Set encoding_embedding_size to the size of the embedding for the encoder.
Set decoding_embedding_size to the size of the embedding for the decoder.
Set learning_rate to the learning rate.
Set keep_probability to the Dropout keep probability
Can you write Python code for it?

# Number of Epochs
epochs = 6
# Batch Size
batch_size = 128
# RNN Size
rnn_size = 512
# Number of Layers
num_layers = 3
# Embedding Size
encoding_embedding_size = 128
decoding_embedding_size = 128
# Learning Rate
learning_rate = 0.001
# Dropout Keep Probability
keep_probability = 0.7


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Preprocess all the data and save it
Running the code cell below will preprocess all the data and save it to file.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
helper.preprocess_and_save_data(source_path, target_path, text_to_ids)


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Process Decoding Input
Implement process_decoding_input using TensorFlow to remove the last word id from each batch in target_data and concat the GO ID to the begining of each batch.
Can you write Python code for it?

def process_decoding_input(target_data, target_vocab_to_int, batch_size):
    """
    Preprocess target data for dencoding
    :param target_data: Target Placehoder
    :param target_vocab_to_int: Dictionary to go from the target words to an id
    :param batch_size: Batch Size
    :return: Preprocessed target data
    """
    # TODO: Implement Function
    decoding_data=tf.strided_slice(target_data, [0, 0], [batch_size, -1], [1, 1])
    #print (decoding_data.get_shape())
    decoding_data=tf.concat([tf.fill([batch_size, 1], target_vocab_to_int['<GO>']), decoding_data], 1)
    #print (decoding_data.get_shape())
    return decoding_data

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_process_decoding_input(process_decoding_input)


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Save Parameters
Save the batch_size and save_path parameters for inference.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
# Save parameters for checkpoint
helper.save_params(save_path)


I am working on the file "4/dlnd_language_translation.ipynb".
The first task is:
Train
Train the neural network on the preprocessed data. If you have a hard time getting a good loss, check the forms to see if anyone is having the same problem.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import time

def get_accuracy(target, logits):
    """
    Calculate accuracy
    """
    max_seq = max(target.shape[1], logits.shape[1])
    if max_seq - target.shape[1]:
        target = np.pad(
            target_batch,
            [(0,0),(0,max_seq - target_batch.shape[1]), (0,0)],
            'constant')
    if max_seq - batch_train_logits.shape[1]:
        logits = np.pad(
            logits,
            [(0,0),(0,max_seq - logits.shape[1]), (0,0)],
            'constant')

    return np.mean(np.equal(target, np.argmax(logits, 2)))

train_source = source_int_text[batch_size:]
train_target = target_int_text[batch_size:]

valid_source = helper.pad_sentence_batch(source_int_text[:batch_size])
valid_target = helper.pad_sentence_batch(target_int_text[:batch_size])

with tf.Session(graph=train_graph) as sess:
    sess.run(tf.global_variables_initializer())
    start_time = time.time()
    for epoch_i in range(epochs):
        for batch_i, (source_batch, target_batch) in enumerate(
                helper.batch_data(train_source, train_target, batch_size)):
            #start_time = time.time()

            _, loss = sess.run(
                [train_op, cost],
                {input_data: source_batch,
                 targets: target_batch,
                 lr: learning_rate,
                 sequence_length: target_batch.shape[1],
                 keep_prob: keep_probability})

            batch_train_logits = sess.run(
                inference_logits,
                {input_data: source_batch, keep_prob: 1.0})
            batch_valid_logits = sess.run(
                inference_logits,
                {input_data: valid_source, keep_prob: 1.0})

            if batch_i%100==0:
                train_acc = get_accuracy(target_batch, batch_train_logits)
                valid_acc = get_accuracy(np.array(valid_target), batch_valid_logits)
                #end_time = time.time()
                end_time = time.time()
   ...


I am working on the file "4/relationships.ipynb".
The first task is:
Note that the Kullback Leibler Divergence IS NOT A DISTANCE! It's not symmetric, amongst other things. We can use the symmetrised DKL, which is just the sum. This still isn't a distance, but it's symmetric.
Can you write Python code for it?

print DKL(P,Q), DKL(Q,P), DKL(P,Q)+DKL(Q,P)
print scipy.stats.entropy(P,Q) ## note that scipy.stats.entropy will also give you the KL divergence!


I am working on the file "4/relationships.ipynb".
The first task is:
if we make a distribution that is markedly different, we should see a higher divergence
Can you write Python code for it?

R = np.random.dirichlet([np.exp(i+1) for i in categories])
sns.barplot(x="categories", y="probability", data=pd.DataFrame({"probability":R, "categories":categories}))

DKL(P,R)+DKL(R,P), DKL(R,Q)+DKL(Q,R)


I am working on the file "4/relationships.ipynb".
The first task is:
timeseries comparison
We can compare pairs of time series (e.g. rates) as long as the sampling times for the two series are (roughly) the same. Below we make two almost identical series A and B, and look at their correlation. Even though we corrupt B with some noise the correlation is still pretty high.
Can you write Python code for it?

time = np.arange(0,20,0.1)
A = [np.sin(x)**2 for x in time]
sns.tsplot(A,interpolate=False)

B = [np.sin(x)**2 + np.random.exponential(0.2)  for x in time]
sns.tsplot(B,interpolate=False)

phi, p = scipy.stats.pearsonr(A,B)
print phi # correlation is between -1 and 1. -1 means that one series goes up when the other goes down.


I am working on the file "41_Progress.ipynb".
The first task is:
A Reusable class for IPython Progress
There currently are too many caveates with out support for Abort. Let us create a reusable class which will automatically generate the UUID and just display the progress.
Can you write Python code for it?

import uuid
from IPython.display import HTML, Javascript, display

class HTMLProgressWatcher:
    def __init__(self, po):
        self.processObject = po
        self.abort = False

        po.AddCommand(sitk.sitkStartEvent, lambda: self.cmdStartEvent() )
        po.AddCommand(sitk.sitkProgressEvent, lambda: self.cmdProgressEvent() )
        po.AddCommand(sitk.sitkEndEvent, lambda: self.cmdEndEvent() )

    def cmdStartEvent(self):
        global sitkIPythonProgress_UUID
        self.abort=False
        self.divid = str(uuid.uuid4())

        try:
            sitkIPythonProgress_UUID[self.divid] = self
        except NameError:
            sitkIPythonProgress_UUID = {self.divid: self}

        html_progress_abort="""

{0}:

""".format(self.processObject.GetName(), self.divid)

        display(HTML(html_progress_abort+javascript_abort))

    def cmdProgressEvent(self):
        p = self.processObject.GetProgress()
        display(Javascript("$('div#%s').width('%i%%')" % (self.divid, int(p*100))))
        if self.abort:
            self.processObject.Abort()

    def cmdEndEvent(self):
        global sitkIPythonProgress_UUID
        del sitkIPythonProgress_UUID[self.divid]
        del self.divid

filter.RemoveAllCommands()
watcher  = HTMLProgressWatcher(filter)

filter.Execute()

threading.Thread.start?


I am working on the file "41_Progress.ipynb".
The first task is:
A caveat with this approach is that the is that the IPython kernel must continue to execute while the filter is running. So we must place the filter in a thread.
Can you write Python code for it?

import threading
threading.Thread( target=lambda:filter.Execute() ).start()


I am working on the file "41_Progress.ipynb".
The first task is:
A reference to the Command object must be maintained, or else it will be removed from the ProcessObject.
Can you write Python code for it?

filter.AddCommand(sitk.sitkStartEvent, MyCommand("stack scope"))
print("Before Execution")
filter.Execute()


I am working on the file "41_Progress.ipynb".
The first task is:
Access to ITK data during command execution
The commands are not too useful unless you can query the filter through the SimpleITK interface. A couple status variables and methods are exposed in the SimpleITK ProcessObject through the polymorphic interface of the same ITK class.
Can you write Python code for it?

filter.RemoveAllCommands()
filter.AddCommand(sitk.sitkProgressEvent, lambda: print("\rProgress: {0:03.1f}%...".format(100*filter.GetProgress()),end=''))
filter.AddCommand(sitk.sitkProgressEvent, lambda: sys.stdout.flush())
filter.AddCommand(sitk.sitkEndEvent, lambda: print("Done"))
filter.Execute()


I am working on the file "41_Progress.ipynb".
The first task is:
Back to watching the progress of out Gabor image source. First lets create the filter as an object
Can you write Python code for it?

size=256
filter = sitk.GaborImageSource()
filter.SetOutputPixelType(sitk.sitkFloat32)
filter.SetSize([size]*3)
filter.SetSigma([size*.2]*3)
filter.SetMean([size*0.5]*3)
filter.SetFrequency(.1)
img = filter.Execute()
myshow3d(img,zslices=[size/2],dpi=40);


I am working on the file "41_Progress.ipynb".
The first task is:
The ProcessObject interface for the Invoker or Subject
SimpleITK doesn't have a large heirachy of inheritance. It has been kept to a minimal, so there is no common Object or LightObject base class as ITK has. As most of the goals for the events have to do with observing processes, the "Subject" interface of the Observer patter or the "Invoker" part of the Command design pattern, has been added to a ProcessObject base class for filters.
The ProcessObject base class has the following methods of handling commands: AddCommand, RemoveAllCommands, and HasCommand.
Adding these functionalities are not available in the procedural interface available for SimpleITK. They are only available through the Object Oriented interface, and break the method chaining interface.
Can you write Python code for it?


I am working on the file "41_Progress.ipynb".
The first task is:
Using a labmda function as the Command
In Python the AddCommand has been extended to accept PyCommand objects and implicitly creates a PyCommand from a callable python argument. This is really useful.
Can you write Python code for it?

filter.RemoveAllCommands() # this line is here so we can easily re-execute this code block
filter.AddCommand(sitk.sitkStartEvent, lambda: print("Starting...",end=''))
filter.AddCommand(sitk.sitkStartEvent, lambda: sys.stdout.flush())
filter.AddCommand(sitk.sitkEndEvent, lambda: print("Done"))
filter.Execute()


I am working on the file "41_Progress.ipynb".
The first task is:
Utilizing IPython Notebooks and Commands
Utilization of commands and event frequently occours with advanced integration into graphical user iterfaces. Let us now export this advanced integration into IPython Notebooks.
IPython notebooks support displaying output as HTML, and execution of javascript on demand. Together this can produce animation.
Can you write Python code for it?

import uuid
from IPython.display import HTML, Javascript, display

divid = str(uuid.uuid4())

html_progress="""

FilterName:

""".format(divid)

def command_js_progress(processObject):
    p = processObject.GetProgress()
    display(Javascript("$('div#%s').width('%i%%')" % (divid, int(p*100))))

filter.RemoveAllCommands()
filter.AddCommand(sitk.sitkStartEvent, lambda:  display(HTML(html_progress)))
filter.AddCommand(sitk.sitkProgressEvent, lambda: command_js_progress(filter))

filter.Execute()


I am working on the file "41_Progress.ipynb".
The first task is:
While the lambda command are convenient, the lack for having an object to hold data can stil be problematic. For example in the above code the uuid, is used to uniquely identify the HTML element. So if the filter is executed multiple times then the JavaScript update will be confused on what to update.
Can you write Python code for it?

#### The following shows a failure that you will want to avoid.
threading.Thread( target=lambda:filter.Execute() ).start()


I am working on the file "4USCityEmotion/.ipynb_checkpoints/create_database-checkpoint.ipynb".
The first task is:
CREATE TABLE photo
(id BIGINT PRIMARY KEY,
url TEXT NOT NULL,
city CHARACTER VARYING(100) NOT NULL,
face_number INTEGER DEFAULT(NULL),
start_detect BOOLEAN NOT NULL DEFAULT(FALSE),
start_info BOOLEAN NOT NULL DEFAULT(FALSE)
);
CREATE INDEX photo_id_index ON photo(id);
CREATE INDEX photo_start_info_index ON photo(start_info);
CREATE INDEX photo_start_detect_index ON photo(start_detect);
CREATE INDEX photo_face_number_index ON photo(face_number);
CREATE TABLE photo_info
(id BIGINT PRIMARY KEY,
owner CHARACTER VARYING (30),
owner_location CHARACTER VARYING (30),
lat FLOAT,
lon FLOAT,
photo_take_date DATE,
photo_upload BIGINT,
accuracy INTEGER,
geotag TEXT,
neighbourhood TEXT;
locality TEXT;
county TEXT;
region TEXT;
country TEXT;
);
CREATE INDEX photo_id_index ON photo(id);
CREATE INDEX photo_lat_index ON photo(lat);
CREATE INDEX photo_lon_index ON photo(lon);
CREATE INDEX photo_date_index ON photo(photo_take_date);
CREATE INDEX photo_f_hasface_index ON photo(f_hasface);
CREATE INDEX photo_start_detect_index ON photo(start_detect);
CREATE INDEX photo_start_info_index ON photo(start_info);
CREATE INDEX photo_start_recog_index ON photo(start_recog);
CREATE INDEX photo_facenum_index ON photo(facenum);
CREATE INDEX photo_lat_index ON photo(lat);
CREATE INDEX photo_lon_index ON photo(lon);
CREATE INDEX photo_date_index ON photo(photo_take_date);
CREATE INDEX photo_f_hasface_index ON photo(f_hasface);
CREATE INDEX photo_start_detect_index ON photo(start_detect);
Four layers framework:
    1. API generation and location selection: return API and lat, lon, city.
    2. collect photo url and id to photo table: input API and lat, lon, city, return photo id and url.
    3. get photo information to photo_info table: input photo id, return photoinfo.
    4. detect photo face info to face table. input photo id, return emotion info.
Can you write Python code for it?

import datetime
DATE=datetime.date(2012,1,1)
while(True):
    DATE2=DATE+datetime.timedelta(days=10)
    datemin ="{0}-{1}-{2}".format(DATE.year,DATE.month,DATE.day)
    datemax ="{0}-{1}-{2}".format(DATE2.year,DATE2.month,DATE2.day)
    DATE=DATE+datetime.timedelta(days=10)
    print(datemin,datemax)
    #get_photo_from_location(db_connection, db_cursor, site, latitude, longitude, datemin, datemax)
    if DATE.year==2018 and DATE.month==11:
        break


I am working on the file "4USCityEmotion/create_database.ipynb".
The first task is:
CREATE TABLE photo
(id BIGINT PRIMARY KEY,
url TEXT NOT NULL,
city CHARACTER VARYING(100) NOT NULL,
face_number INTEGER DEFAULT(NULL),
start_detect BOOLEAN NOT NULL DEFAULT(FALSE),
start_info BOOLEAN NOT NULL DEFAULT(FALSE)
);
CREATE INDEX photo_id_index ON photo(id);
CREATE INDEX photo_start_info_index ON photo(start_info);
CREATE INDEX photo_start_detect_index ON photo(start_detect);
CREATE INDEX photo_face_number_index ON photo(face_number);
CREATE TABLE photo_info
(id BIGINT PRIMARY KEY,
owner CHARACTER VARYING (30),
owner_location CHARACTER VARYING (30),
lat FLOAT,
lon FLOAT,
photo_take_date DATE,
photo_upload BIGINT,
accuracy INTEGER,
geotag TEXT,
neighbourhood TEXT;
locality TEXT;
county TEXT;
region TEXT;
country TEXT;
);
CREATE INDEX photo_id_index ON photo(id);
CREATE INDEX photo_lat_index ON photo(lat);
CREATE INDEX photo_lon_index ON photo(lon);
CREATE INDEX photo_date_index ON photo(photo_take_date);
CREATE INDEX photo_f_hasface_index ON photo(f_hasface);
CREATE INDEX photo_start_detect_index ON photo(start_detect);
CREATE INDEX photo_start_info_index ON photo(start_info);
CREATE INDEX photo_start_recog_index ON photo(start_recog);
CREATE INDEX photo_facenum_index ON photo(facenum);
CREATE INDEX photo_lat_index ON photo(lat);
CREATE INDEX photo_lon_index ON photo(lon);
CREATE INDEX photo_date_index ON photo(photo_take_date);
CREATE INDEX photo_f_hasface_index ON photo(f_hasface);
CREATE INDEX photo_start_detect_index ON photo(start_detect);
Four layers framework:
    1. API generation and location selection: return API and lat, lon, city.
    2. collect photo url and id to photo table: input API and lat, lon, city, return photo id and url.
    3. get photo information to photo_info table: input photo id, return photoinfo.
    4. detect photo face info to face table. input photo id, return emotion info.
Can you write Python code for it?


I am working on the file "4USCityEmotion/create_database.ipynb".
The first task is:
Location
CREATE TABLE location
(id SERIAL PRIMARY KEY,
city_name CHARACTER VARYING(100) NOT NULL,
lat FLOAT NOT NULL,
lon FLOAT NOT NULL,
start_query BOOLEAN NOT NULL DEFAULT(FALSE)
);
CREATE INDEX id_index ON location(id);
CREATE INDEX city_name_index ON location(city_name);
CREATE INDEX start_query_index ON location(start_query);
Can you write Python code for it?

# Import all sampling points
file = open('/media/raid/PlaceEmotion/pts200_us.csv', 'r')
st = file.read()
file.close()
sql = '''
INSERT INTO location(city_name, lat, lon)
VALUES
{0}
'''.format(st)
#print(sql)
database.execute(sql)


I am working on the file "4_2-Potentialtheorie_Elementarstroemungen.ipynb".
The first task is:
2) Über die Stromfunktion. Bei der Gelegenheit plotten wir auch gleich die Potentiallinien:
Can you write Python code for it?

psi_quelle = source_psi(X, Y, 0, 0, Q)
psi_senke = source_psi(X, Y, x_s, 0, -Q)
psi_gesamt = psi_quelle + psi_senke

phi_quelle = source_phi(X, Y, 0, 0, Q)
phi_senke = source_phi(X, Y, x_s, 0, -Q)
phi_gesamt = phi_quelle + phi_senke

plt.figure(figsize=(10, 5))
plt.xlabel('x')
plt.ylabel('y')
plt.contour(X, Y, psi_gesamt, 20,
            colors='#407eb4', linewidths=1, linestyles='solid')
plt.contour(X, Y, phi_gesamt, [-0.7, -0.5, -0.2, 0.2, 0.5, 0.7],
            colors='green', linewidths=1, linestyles='solid')

plt.scatter([0,x_s], [0,0], color='red', s=40, marker='o', linewidth=1);


I am working on the file "4_2-Potentialtheorie_Elementarstroemungen.ipynb".
The first task is:
Die $x,y$-Ebene, in der wir die Strömung visualisieren wollen, soll sich von (-5,-5) bis (15,5) erstrecken. Die Quelle positionieren wir bei (0,0) und die Senke bei (10,0) und geben ihnen jeweils einen Volumenstrom pro Tiefeneinheit von $2~\text{m}^2/\text{s}$ bzw. $-2~\text{m}^2/\text{s}$.
Als Variablen benötigen wir die $x$- und $y$-Koordinaten, an denen die Geschwindigkeitskomponenten und die Potential- und Stromfunktionen ausgewertet werden sollen:
Can you write Python code for it?

nx = 400                              # Anzahl der Punkte in x-Richtung
ny = 200                              # Anzahl der Punkte in y-Richtung
x = np.linspace(-5, 15, nx)           # 1D-Array mit x-Koordinaten
y = np.linspace(-5,  5, ny)           # 1D-Array mit y-Koordinaten

X, Y = np.meshgrid(x, y)              # erzeugt das Gitter mit nx * ny Punkten


I am working on the file "4_2-Potentialtheorie_Elementarstroemungen.ipynb".
The first task is:
Die Visualisierung der Stromlinien kann auf unterschiedliche Art und Weise erfolgen.
1) Über die in der Matplotlib bereits vorhandene Funktion streamplot:
Can you write Python code for it?

Q = 3
x_s = 0.5

u_quelle, v_quelle = source_v(X, Y, 0, 0, Q)   # Quellenströmung bei (0,0)
u_senke, v_senke = source_v(X, Y, x_s, 0, -Q)  # Senkenströmung bei (10,0)

u_gesamt = u_quelle + u_senke                  # lineare Überlagerung
v_gesamt = v_quelle + v_senke

plt.figure(figsize=(10, 5))
plt.xlabel('x')
plt.ylabel('y')
plt.xlim(-5,15)
plt.ylim(-5,5)
plt.streamplot(X, Y, u_gesamt, v_gesamt,
                  density=2, linewidth=1, arrowsize=2, arrowstyle='->')
plt.scatter(0, 0, color='red', s=50, marker='o', linewidth=0)
plt.scatter(x_s, 0, color='red', s=50, marker='o', linewidth=0);


I am working on the file "4_2-Potentialtheorie_Elementarstroemungen.ipynb".
The first task is:
Wie erwartet, ist der Druck in den Staupunkten maximal und die Geschwindigkeit Null. Aufgrund der symmetrischen Umströmung, ist die Druckverteilung auf Ober- und Unterseite identisch, so dass keine resultierende Auftriebskraft auf den Körper wirkt.
Da die Druckverteilung auch in $x$-Richtung symmetrisch ist und Reibungskräfte nicht berücksichtigt werden, wird auch die Kraft in $x$-Richtung gleich Null.
Wie wir später noch sehen werden, ist die Potentialtheorie zwar gut geeignet, um Auftriebskräfte zu berechnen, die Reibungskräfte (Druckverluste) lassen sich damit allein jedoch nicht bestimmen. In der Praxis wird die Potentialtheorie deshalb häufig mit einem Grenzschichtrechenverfahren gekoppelt.
Im nächsten Kapitel folgen noch weitere wichtige Elementarströmungen, wie die Dipolströmung oder Potentialwirbel.
Hier geht's weiter oder hier zurück zur Übersicht.

Copyright (c) 2018, Florian Theobald und Matthias Stripf
Der folgende Python-Code darf ignoriert werden. Er dient nur dazu, die richtige Formatvorlage für die Jupyter-Notebooks zu laden.
Can you write Python code for it?

from IPython.core.display import HTML
def css_styling():
    styles = open('TFDStyle.css', 'r').read()
    return HTML(styles)
css_styling()


I am working on the file "4_3-Potentialtheorie_Elementarstroemungen2.ipynb".
The first task is:
Der Potentialwirbel ist bis auf sein Zentrum drehungsfrei. D.h. die Zirkulation
$$\Gamma = \oint_C {\overrightarrow{v}(\overrightarrow{r}) \text{d} \overrightarrow{r}}$$
entlang einer beliebigen Kurve $C$ in der Umgebung des Wirbels, die das Zentrum nicht mit einschließt gerade Null ist. Nur wenn das Zentrum innerhalb der geschlossenen Kurve liegt, ist die Zirkulation $\Gamma$ ungleich Null und entspricht dem vorgegebenen Wert.
Übersicht Elementarströmungen
Die folgende Tabelle gibt eine Übersicht, über die zuvor hergeleiteten Elementarströmungen. Zusätzlich ist die Staupunktströmung enthalten, auf die wir im Rahmen der Vorlesung nicht näher eingehen. Die Übersicht enthält die Gleichungen der Geschwindigkeitsvektoren sowie der Potential- und Stromfunktionen in kartesischen sowie Polarkoordinaten.

Hier auch zum Runterladen als PDF
Im nächsten Kapitel werden wir die Elementarströmungen so überlagern, dass wir auch den Auftrieb von umströmten Körpern berechnen können.
Hier geht's weiter oder hier zurück zur Übersicht.

Copyright (c) 2018, Florian Theobald und Matthias Stripf
Der folgende Python-Code darf ignoriert werden. Er dient nur dazu, die richtige Formatvorlage für die Jupyter-Notebooks zu laden.
Can you write Python code for it?


I am working on the file "4_3-Potentialtheorie_Elementarstroemungen2.ipynb".
The first task is:
Die oben gezeigten Gleichungen für die Geschwindigkeitskomponenten sowie die Potential- und Stromfunktionen implementieren wir als Funktionen:
Can you write Python code for it?

def dipolx_v(x, y, xs, ys, M):        # der Geschwindigkeitsvek. der Quellenstr.
    s = -M/(2*math.pi) / ((x-xs)**2+(y-ys)**2)**2
    return s*((x-xs)**2-(y-ys)**2), s*2*(x-xs)*(y-ys)

def dipolx_psi(x, y, xs, ys, M):      # die Stromfunktion der Quellenströmung
    return -M/(2*math.pi) * (y-ys) / ((x-xs)**2+(y-ys)**2)

def dipolx_phi(x, y, xs, ys, M):      # die Potentialfunktion der Quellenströmung
    return M/(2*math.pi) * (x-xs) / ((x-xs)**2+(y-ys)**2)


I am working on the file "4_3-Potentialtheorie_Elementarstroemungen2.ipynb".
The first task is:
Potentialwirbel (drehungsfreier Wirbel)
Unter einem Potentialwirbel versteht man eine Strömung, auf der die Fluidelemente auf kreisförmigen Bahnen um einen zentralen Punkt umlaufen und sich dabei nicht um die eigene Achse drehen. Man spricht dann von einem drehungsfreien Wirbel.

Beim Potentialwirbel ist die Radialkomponente der Geschwindigkeit gleich Null:
$$u_r = 0$$
Die Tangentialkomponente erhalten wir durch die Erfüllung der Drehungsfreiheit
$$\text{rot}\overrightarrow{v} =
\begin{pmatrix}
    \frac{\partial w}{\partial y}-\frac{\partial v}{\partial z} \
    \frac{\partial u}{\partial z}-\frac{\partial w}{\partial x} \
    \frac{\partial v}{\partial x}-\frac{\partial u}{\partial y}
\end{pmatrix}{x,y,z}
=
\begin{pmatrix}
    \frac{1}{r}\frac{\partial u_z}{\partial \varphi}-\frac{\partial u\varphi}{\partial z} \
    \frac{\partial u_r}{\partial z}-\frac{\partial u_z}{\partial r} \
    \frac{1}{r}\frac{\partial (ru_\varphi)}{\partial r}-\frac{1}{r}\frac{\partial u_r}{\partial \varphi}
\end{pmatrix}_{r,\varphi,z} = 0,$$
die hier auch in Zylinderkoordinaten gegeben ist, da die Herleitung der tangentialien Geschwindigkeitskomponente des Potentialwirbels in Zylinderkoordinaten einfacher ist. Im Zweidimensionalen sind die $z$-Komponente der Geschwindigkeit und alle Ableitungen nach $z$ Null und es muss nur die folgende Gleichung erfüllt werden:
$$\frac{\partial (ru_\varphi)}{\partial r}-\frac{\partial u_r}{\partial \varphi} = 0$$
Da der Potentialwirbel axialsymmetrisch ist, ändert sich die Geschwindigkeit in Umfangsrichtung nicht, so dass $\frac{\partial u_r}{\partial \varphi} = 0$ ist und sich damit die Forderung der Drehungsfreiheit auf
$$\frac{\partial (ru_\varphi)}{\partial r} = 0$$
reduziert. Die Gleichung ist erfüllt, wenn $ru_\varphi = const$ ist. Wobei es üblich ist, die Konstante durch die Zirkulation $\Gamma$ auszudrücken und bei positiver Zirkulation eine Drehrichtung des Wirbels im Uhrzeigersinn zu definieren. Damit sind die Geschwindigkeitskomponenten des Potentialwirbels bekannt:
$$u_\varphi =...
Can you write Python code for it?

def vortex_v(x, y, x1, y1, Gamma):   # Geschwindigkeitsvek. des Potentialwirbels
    s = Gamma/(2*math.pi*((x-x1)**2+(y-y1)**2))
    return s*(y-y1), -s*(x-x1)

def vortex_psi(x, y, x1, y1, Gamma): # Stromfunktion des Potentialwirbels
    s = -Gamma/(2*math.pi)
    return -s*np.log((x-x1)**2+(y-y1)**2)

def vortex_phi(x, y, x1, y1, Gamma): # Potentialfunktion des Potentialwirbels
    s = -Gamma/(2*math.pi)
    return np.arctan2((y-y1),(x-x1))


I am working on the file "4_3-Potentialtheorie_Elementarstroemungen2.ipynb".
The first task is:
Und plotten alles wieder mit der Matplotlib:
Can you write Python code for it?

Gamma = 2

u_vortex, v_vortex = vortex_v(X, Y, 0, 0, Gamma)
phi_vortex = vortex_phi(X, Y, 0, 0, Gamma)

# Neuen Plot einrichten
plt.figure(figsize=(10, 5))
plt.xlabel('x')
plt.ylabel('y')

# Stromlinien mit Matplotlib-Funktion darstellen
plt.streamplot(X, Y, u_vortex, v_vortex,
                  density=2, linewidth=1, arrowsize=2, arrowstyle='->')

# Potentiallinien in grün einzeichnen
plt.contour(X, Y, phi_vortex,
            [-math.pi*7/8, -math.pi*3/4, -math.pi*5/8, -math.pi/2,
             -math.pi*3/8, -math.pi/4, -math.pi/8,
             0, math.pi/8, math.pi/4, math.pi*3/8, math.pi/2,
             math.pi*5/8, math.pi*3/4, math.pi*7/8],
            colors='green', linewidths=1, linestyles='solid');


I am working on the file "4_3-Potentialtheorie_Elementarstroemungen2.ipynb".
The first task is:
und schließlich grafisch darstellen:
Can you write Python code for it?

# Neuen Plot einrichten
plt.figure(figsize=(10, 5))
plt.xlabel('x')
plt.ylabel('y')

# Stromlinien mit Matplotlib-Funktion darstellen
plt.streamplot(X, Y, u_gesamt, v_gesamt,
                  density=2, linewidth=1, arrowsize=2, arrowstyle='->')

# Staustromlinie in rot eintragen
plt.contour(X, Y, psi_gesamt, levels=[0],
            colors='red', linewidths=1, linestyles='solid');


I am working on the file "4_3_1__Stock_Picking_Predicting_The_Future_Forecasting_ARIMA.ipynb".
The first task is:
ARIMA
In order to select best model we will use the AIC, i.e. we will take the model with lowest AIC.
How it works on 1 security
Can you write Python code for it?

from statsmodels.tsa.arima_model import ARIMA
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score

## let's start with an example
series = df["AAPL"]
series = series.tolist()

pd.Series(series).plot()
plt.show()

p_tr = 0.66

tr_size = int(len(series) * p_tr)
train, test = series[0:tr_size], series[tr_size:len(series)]
print("tr_size:",tr_size,"xval_size:",(len(series)-tr_size))

from pandas import datetime
from pandas import DataFrame
from statsmodels.tsa.arima_model import ARIMA
from matplotlib import pyplot

# fit model
model = ARIMA(train, order=(5,0,0))
model_fit = model.fit(disp=0)
print(model_fit.summary())
# plot residual errors
residuals = DataFrame(model_fit.resid)
residuals.plot()
pyplot.show()
residuals.plot(kind='kde')
pyplot.show()
print(residuals.describe())

model_fit.aic

from numpy.linalg import LinAlgError

def get_best_arima(sr,maxord = [6,6,6]):
    best_aic = 1000000
    best_ord = maxord
    best_model = None
    for p in range(maxord[0]):
        for q in range(maxord[1]):
            for d in range(maxord[2]):
                try:
                    model = ARIMA(sr, order=(p,q,d))
                    model_fit = model.fit(disp=0)
                    if (best_aic > model_fit.aic):
                        best_aic = model_fit.aic
                        best_ord = [p,q,d]
                        best_model = model_fit
                except:
                    pass
    return best_aic, best_ord , best_model

# supress warnings
import warnings
warnings.filterwarnings('ignore')

best_aic, best_ord , best_model = get_best_arima(train)
print("best_aic:",best_aic)
print("best_ord:",best_ord)

def predict_arima(train,steps,best_order,verbose=True,do_plot=True):
    history = [x for x in train]
    predictions = list()
    yhat = train[len(train)-1]
    for t in range(steps):
        model = ARIMA(history, order=(best_order[0],best_order[1],best_order[2]))
        try:
            model_fit = model.fit(disp=0)
            output = model_fit.forecast()
 ...


I am working on the file "4_3_1__Stock_Picking_Predicting_The_Future_Forecasting_ARIMA.ipynb".
The first task is:
Gain/Loss
Can you write Python code for it?

arima_perf = arima_perf.sort_values(by='ARIMA_R2_cumulative_return_xval' , ascending=False)
gain_loss = arima_perf[arima_perf['ACTION'] != ''][['ACTION','ARIMA_R2_cumulative_return_test','ARIMA_pred_cumulative_return_test','cumulative_return_test']]
gain_loss['EX_POST'] = ''
for i,sec in enumerate(gain_loss.index.tolist()):
    if gain_loss.loc[sec,'cumulative_return_test'] * gain_loss.loc[sec,'ARIMA_pred_cumulative_return_test'] < 0:
        gain_loss.loc[sec,'EX_POST'] = 'WRONG'
    elif np.absolute(gain_loss.loc[sec,'cumulative_return_test']) <  np.absolute(gain_loss.loc[sec,'ARIMA_pred_cumulative_return_test']):
        gain_loss.loc[sec,'EX_POST'] = 'ALMOST CORRECT'
    else:
        gain_loss.loc[sec,'EX_POST'] = 'CORRECT'
gain_loss


I am working on the file "4_3_1__Stock_Picking_Predicting_The_Future_Forecasting_ARIMA.ipynb".
The first task is:
Massive Predictions
Let's do a simple experiments with above defined securities, i.e. for each security
* let's fit best ARIMA model on train period (oldest 60%)
* let's predict stock price/cumulative return on cross-validation period (%20 in the middle)
* let's measure MSE/R2 for price/cumulative return on cross-validation period
* let's predict stock price/cumulative return on test period (most recent %20)
* let's measure MSE/R2 for price/cumulative return on test period
If we considered ARIMA models accurate then we should pick the securities with highest predicted cumulative return. Unfortunately, that would be a huge mistake. Why? Let's consider, for example in the cross validation period, predicted cumulative return vs. actual cumulative return. We would find out huge surprises.
Another technique for model selection is using indicator like AIC or BIC and actually in the below procedure we will use AIC to select the best ARIMA on train set data. The problem with those techniques is that they are very far from extimate the "confidence" of ARIMA predictions.
Hence, the cross validation period is used to select the stock/arima pair. How? There are two possibilities: min MSE / max R2 on cross validation. Minimizing MSE would give more relevance to flat or quasi-flat securities, so we will prefer to use max R2. The idea is to BUY the top x stocks with higest R2 cumulative return in cross validation.
The test period is for testing those orders in terms of cumulative returns.
Can you write Python code for it?

arima_perf = DataFrame({'security':  df.columns.tolist(),

                        'cumulative_return_test': 0,
                        'ARIMA_pred_cumulative_return_test': 0,
                        'ARIMA_MSE_cumulative_return_test': 0,
                        'ARIMA_R2_cumulative_return_test': 0,
                        'ARIMA_MSE_price_test': 0 ,
                        'ARIMA_R2_price_test': 0 ,

                        'cumulative_return_xval': 0,
                        'ARIMA_pred_cumulative_return_xval': 0,
                        'ARIMA_MSE_cumulative_return_xval': 0,
                        'ARIMA_R2_cumulative_return_xval': 0,
                        'ARIMA_MSE_price_xval': 0 ,
                        'ARIMA_R2_price_xval': 0 ,

                        'ARIMA_best_order_0': 0,
                        'ARIMA_best_order_1': 0,
                        'ARIMA_best_order_2': 0} )
arima_perf = arima_perf.set_index(['security'])

p_tr = 0.60
p_xval = 0.20
p_test = 1-p_tr-p_xval

for i,sec in enumerate(arima_perf.index.tolist()):
    if (i % 100 == 0) or (i ==10):
        print(i,sec)

    ## data
    series = df[sec]
    series = series.tolist()
    tr_size = int(len(series) * p_tr)
    xval_size = int(len(series) * p_xval)
    train, txval, test = series[0:tr_size], series[tr_size:(tr_size+xval_size)] , series[(tr_size+xval_size):len(series)]

    ## fit model
    best_aic, best_ord , best_model = get_best_arima(train)

    ## predict, assess
    predictions = predict_arima(train,(len(txval)+len(test)),best_ord,verbose=False,do_plot=False)

    ## store
    arima_perf.loc[sec,'ARIMA_best_order_0'] = best_ord[0]
    arima_perf.loc[sec,'ARIMA_best_order_1'] = best_ord[1]
    arima_perf.loc[sec,'ARIMA_best_order_2'] = best_ord[2]

    # xval
    pred_cumulative_returns_xval = cumulative_returns(pd.Series(predictions[0:xval_size]))
    cumulative_returns_xval = cumulative_returns(pd.Series(txval))
 ...


I am working on the file "4_3_1__Stock_Picking_Predicting_The_Future_Forecasting_ARIMA.ipynb".
The first task is:
Notice that we should not buy SNN (Smith & Nephew PLC) because the predicted cumulative return is too small vs. the actual cumulative return. Is this reasonable?
Note #1: taking just predicted cumulative return would lead to losses
Can you write Python code for it?

arima_perf = arima_perf.sort_values(by='ARIMA_pred_cumulative_return_xval' , ascending=False)
loss = arima_perf[['ACTION','ARIMA_R2_cumulative_return_xval','ARIMA_pred_cumulative_return_xval','cumulative_return_xval']][:20]
loss['UNEXPECTED_LOSS'] = loss['cumulative_return_xval'] - loss['ARIMA_pred_cumulative_return_xval']
loss


I am working on the file "4_3_1__Stock_Picking_Predicting_The_Future_Forecasting_ARIMA.ipynb".
The first task is:
Reloading full results
Can you write Python code for it?


I am working on the file "4_3_1__Stock_Picking_Predicting_The_Future_Forecasting_ARIMA.ipynb".
The first task is:
The percentage of mistakes would be:
Can you write Python code for it?


I am working on the file "4_4-Potentialtheorie_Auftrieb.ipynb".
The first task is:
Die Druckverteilung auf der Oberfläche erhalten wir durch Einsetzen des Geschwindigkeitsverlaufs auf der Trennstromlinie in die Bernoulli-Gleichung, wie im vorletzten Kapitel gezeigt.
$$p(\varphi) + \frac{\rho}{2} \cdot \left( u^2(\varphi)+v^2(\varphi) \right) = p_\infty + \frac{\rho}{2}\cdot \overrightarrow{v}_\infty^2 = const.$$
Die Gleichungen für $u(\varphi)$ und $v(\varphi)$ für $r=R$ haben wir oben schon hergeleitet, so dass wir die Druckverteilung einfach ausrechnen und plotten können:
Can you write Python code for it?

p1 = 102300
rho1 = 1.2

def v_zylinder(phi, u1, Gamma, M):
    u = (u1
         - u1 * np.cos(2*phi)
         +Gamma*math.sqrt(u1/(2*math.pi*M))*np.sin(phi))
    v = (-u1*np.sin(2*phi)
         -Gamma*math.sqrt(u1/(2*math.pi*M))*np.cos(phi))
    return u,v

def p_zylinder(phi, u1, Gamma, M, p1, rho1):
    u,v = v_zylinder(phi, u1, Gamma, M)
    v_abs = np.sqrt(u**2+v**2)
    p = p1 + 0.5 * rho1 * (u1**2 - v_abs**2)
    return p

phi = np.linspace(0, 2*math.pi, 101)
p = p_zylinder(phi, u1, Gamma, M, p1, rho1)

# Neuen Plot einrichten
plt.figure(figsize=(10, 5))
plt.ylabel('p-p1')
plt.xlabel('$\phi$')
plt.axhline(0, color='black')

plt.plot(phi, p-p1);


I am working on the file "4_4-Potentialtheorie_Auftrieb.ipynb".
The first task is:
Man sieht deutlich, dass die Strömung jetzt durch die Überlagerung des Potentialwirbels umgelenkt wird. Die Form des Zylinders ist in etwa erkennbar. Aber welchen Radius hat der Zylinder und welche Druckverteilung herrscht auf der Oberfläche des Zylinders?
Um den Radius zu ermitteln, benotigen wir die Koordinaten eines Staupunkts auf dem Zylinder. Da sich der Durchmesser durch den Potentialwirbel nicht ändert, können wir zunächst nur die Überlagerung aus Translations- und Dipolströmung betrachten:
$$u(x,y) = u_1 - \frac{M}{2\pi}\frac{x^2-y^2}{\left(x^2-y^2\right)^2} \stackrel{!}{=} 0$$
$$v(x,y) = 0 - \frac{M}{2\pi}\frac{2xy}{\left(x^2-y^2\right)^2} \stackrel{!}{=} 0$$
oder in Polarkoordinaten (vgl. Formelsammlung Potentialströmungen), weil eleganter:
$$u(r,\varphi) = u_1 - \frac{M}{2\pi}\frac{\cos(2\varphi)}{r^2} \stackrel{!}{=} 0$$
$$v(r,\varphi) = 0 - \frac{M}{2\pi}\frac{\sin(2\varphi)}{r^2} \stackrel{!}{=} 0$$
Die zweite Gleichung ist erfüllt, wenn $\varphi$ Vielfache von $\frac{1}{2}\pi$ annimmt. Setzen wir z.B. $\varphi = \pi$ in die vorletzte Gleichung ein, erhalten wir für den Radius des Zylinders:
$$R = \pm \sqrt{\frac{M}{2\pi\cdot u_1}}$$
Mit dieser Lösung für den Radius suchen wir nun die Winkel $\varphi$, unter denen die Staupunkte zu finden sind. Dazu benötigen wir die Überlagerung aller drei Elementarströmungen:
$$u(r,\varphi) = u_1 - \frac{M}{2\pi}\frac{\cos(2\varphi)}{r^2}+\frac{\Gamma}{2\pi}\frac{\sin(\varphi)}{r} \stackrel{!}{=} 0$$
$$v(r,\varphi) = 0 - \frac{M}{2\pi}\frac{\sin(2\varphi)}{r^2} - \frac{\Gamma}{2\pi}\frac{\cos(\varphi)}{r} \stackrel{!}{=} 0$$
Die Lösung für $r$ eingesetzt ergibt:
$$u(\varphi) = u_1 - u_1 \cos(2\varphi) + \Gamma \sqrt{\frac{u_1}{2\pi M}} \sin\varphi \stackrel{!}{=} 0$$
$$v(\varphi) = -u_1 \sin(2\varphi) - \Gamma \sqrt{\frac{u_1}{2\pi M}} \cos\varphi \stackrel{!}{=} 0$$
Lösen der vorletzten Gleichung nach $\varphi$ und Einsetzen der Lösungen in die letzte Gleichung liefert die Winkel, unter denen beide Geschwindigkeitskomponenten auf dem Radius $R$ Null...
Can you write Python code for it?

# Radius des Zylinders:
R = math.sqrt(M/(2*math.pi*u1))

# Staupunkte
phi1 = -math.asin(math.sqrt(u1/(2*math.pi*M))*Gamma/(2*u1))
phi2 = -phi1+math.pi
sx1 = R*math.cos(phi1)
sy1 = R*math.sin(phi1)
sx2 = R*math.cos(phi2)
sy2 = R*math.sin(phi2)

# Neuen Plot einrichten
plt.figure(figsize=(10, 5))
plt.xlabel('x')
plt.ylabel('y')
plt.xlim(-10,10)
plt.ylim(-5,5)

# Stromlinien mit Matplotlib-Funktion darstellen
plt.streamplot(X, Y, u_gesamt, v_gesamt,
               density=2, linewidth=1, arrowsize=2,
               arrowstyle='->');

# Zylinder einzeichnen
zylinder = plt.Circle((0,0),R,color='r', alpha=0.5)
plt.gcf().gca().add_artist(zylinder)

# Staupunkte einzeichnen
plt.scatter([sx1,sx2], [sy1,sy2], color='green',
            s=50, marker='o', linewidth=0);


I am working on the file "4_4-Potentialtheorie_Auftrieb.ipynb".
The first task is:
Und siehe da, die Druckverteilung ist nicht mehr symmetrisch. D.h. es wirkt jetzt eine Kraft auf den Körper. Da die Druckkraft immer senkrecht auf die Oberfläche des Zylinders wirkt, können wir die $x$- und $y$-Komponente der Kraft wie folgt ausdrücken:
$$F_x = -\int_0^{2\pi} p(\varphi)~R~\cos\varphi ~d\varphi$$
$$F_y = -\int_0^{2\pi} p(\varphi)~R~\sin\varphi ~d\varphi$$
Can you write Python code for it?

from scipy import integrate

def dFx(phi, u1, Gamma, M, p1, rho1, R):
    return -p_zylinder(phi, u1, Gamma, M, p1, rho1) * R * np.cos(phi)

def dFy(phi, u1, Gamma, M, p1, rho1, R):
    return -p_zylinder(phi, u1, Gamma, M, p1, rho1) * R * np.sin(phi)

F_x, err = integrate.quad(dFx, 0, 2*math.pi, args=(u1,Gamma,M,p1,rho1,R))
F_y, err = integrate.quad(dFy, 0, 2*math.pi, args=(u1,Gamma,M,p1,rho1,R))

print ('Widerstandskraft in x-Richtung: {0:7.2f}'.format(F_x))
print ('  Auftriebskraft in y-Richtung: {0:7.2f}'.format(F_y))


I am working on the file "4_5-Potentialtheorie_Quell-Panel-Verfahren.ipynb".
The first task is:
Als nächstes erzeugen wir die 4 Paneele:
Can you write Python code for it?

panels = []    # Liste der Paneele
#panels.append(Panel(ax=-5.0,ay= 0.0, bx= 0.0, by= 2.0))
#panels.append(Panel(ax= 0.0,ay= 2.0, bx= 5.0, by= 0.0))
#panels.append(Panel(ax= 5.0,ay= 0.0, bx= 0.0, by=-2.0))
#panels.append(Panel(ax= 0.0,ay=-2.0, bx=-5.0, by= 0.0))
panels.append(Panel(bx=-5.0,by= 0.0, ax= 0.0, ay= 2.0))
panels.append(Panel(bx= 0.0,by= 2.0, ax= 15, ay= 0.0))
panels.append(Panel(bx= 15,by= 0.0, ax= 0.0, ay=-2.0))
panels.append(Panel(bx= 0.0,by=-2.0, ax=-5.0, ay= 0.0))
N = len(panels)


I am working on the file "4_5-Potentialtheorie_Quell-Panel-Verfahren.ipynb".
The first task is:
Nachdem jetzt allen Paneelen die richtige Stärke $\lambda_i$ zugewiesen wurde, können wir das Geschwindigkeitsfeld berechnen:
Can you write Python code for it?

# Anteil der Translationsströmung:
u = np.full_like(X, u_oo)
v = np.full_like(Y, v_oo)

# Anteil der einzelnen Paneele
for i, panel_i in enumerate(panels):
    u_i,v_i = panel_i.vel(X, Y)
    u += u_i
    v += v_i

# Neuen Plot einrichten
plt.figure(figsize=(10, 5))
plt.xlabel('x')
plt.ylabel('y')
plt.xlim(-10,10)
plt.ylim(-5,5)

# Stromlinien mit Matplotlib-Funktion darstellen
plt.streamplot(X, Y, u, v,
                  density=2, linewidth=1, arrowsize=2, arrowstyle='->');

for i, panel_i in enumerate(panels):
    plt.plot([panel_i.ax, panel_i.bx],[panel_i.ay, panel_i.by],
             color='red', linewidth=3);


I am working on the file "4_5-Potentialtheorie_Quell-Panel-Verfahren.ipynb".
The first task is:
Unser Quell-Paneel macht schon (fast) was es soll. Es hat nur noch einen Schönheitsfehler: aus dem Paneel scheint entgegen der Strömungsrichtung ein Volumenstrom auszutreten. Wir möchten aber, dass das Paneel zur Anströmrichtung hin eine undurchlässige Wand simuliert. Die stromabgewandte Seite wird später auf der Innenseite des umströmten Körpers liegen, so dass die dort austretende Strömung keine Bedeutung hat).
Wir müssen die Stärke $\lambda$ also genau so wählen, dass der Staupunkt gerade auf dem Paneel zum liegen kommt. Da der pro Länge und Einheitstiefe austretende Volumenstrom ohne überlagerte Translationsströmung zu beiden Seiten des Quell-Panels gleich ist, tritt auf der der Strömung zugewandten Seite genau $\lambda/2$ aus. Damit der Staupunkt also auf dem Quell-Paneel liegt, muss dieser auf die Länge und Einheitstiefe bezogene Volumenstrom also gerade durch die Translationsströmung kompensiert werden:
$$\frac{\lambda}{2} = u_\infty$$
Wenn wir oben im Python-Code lamb = 1 setzen, sollten wir also das gewünschte Ergebnis bekommen.
Überlagerung mehrerer Paneele zur Beschreibung eines umströmten Körpers
Zur Abbildung des umströmten Körpers mit Paneelen, müssen wir sicherstellen, dass die Geschwindigkeit am Ort der Paneele genau tangential verläuft. Anders ausgedrückt, muss die Geschwindigkeitskomponente normal zum Paneel $u_n$ gleich Null sein. Der Einfachheit halber fordern wir, dass $u_n$ nur im Mittelpunkt des Paneels Null sein muss.
Da die Normalenkomponente der Geschwindigkeit gerade die Ableitung der Potentialfunktion nach der Normalenrichtung ist, gilt:
$$u_n(x_m,y_m) = \frac{\partial \phi}{\partial n}\left(x_m, y_m\right) \stackrel{!}{=} 0$$
Und jetzt wird's etwas komplizierter. Denn die Potentialfunktion $\phi$ in dieser Gleichung ist nicht einfach die eines einzelnen Paneels, sondern die aller $N$ linear überlagerter Paneele und der überlagerten Translationsströmung:
$$\phi(x,y) = u_\infty x + v_\infty y + \sum_{i=1}^N...
Can you write Python code for it?

def norm_int(panel_i, panel_j):
    # Berechnet das Integral in der Koeffizientenmatrix

    def integrand(s):
        delta_x = panel_j.mx - panel_i.x(s)
        delta_y = panel_j.my - panel_i.y(s)
        return ((delta_x*math.cos(panel_j.beta)
                + delta_y*math.sin(panel_j.beta))
                / (delta_x*delta_x + delta_y*delta_y))

    return integrate.quad(integrand, 0.0, panel_i.laenge)[0]


I am working on the file "4_5-Potentialtheorie_Quell-Panel-Verfahren.ipynb".
The first task is:
Zeit, das Ganze mal grafisch darzustellen. Wir erzeugen zunächst ein Objekt der Klasse Panel mit dem Namen panel1, den Eckpunkten $a=(0,-2)$ und $b=(0,2)$ und einem Volumenstrom pro Länge und Tiefe von $2~\text{m}/{s}$.
Dann berechnen wir die Geschwindigkeitsverteilung an allen Punkten des Rechengitters und Plotten wie gewohnt die Stromlinien.
Can you write Python code for it?

lamb = 1.0
panel1 = Panel(ax=0,ay=-2,bx=0,by=2,lamb=lamb)    # erzeuge neues Panel

nx = 400                              # Anzahl der Punkte in x-Richtung
ny = 200                              # Anzahl der Punkte in y-Richtung
x = np.linspace(-10, 10, nx, dtype=float)  # 1D-Array mit x-Koordinaten
y = np.linspace(-5,  5, ny, dtype=float)   # 1D-Array mit y-Koordinaten

X, Y = np.meshgrid(x, y )      # erzeugt das Gitter mit nx * ny Punkten

u,v = panel1.vel(X, Y)
u += 0.5

# Neuen Plot einrichten
plt.figure(figsize=(10, 5))
plt.xlabel('x')
plt.ylabel('y')
plt.xlim(-10,10)
plt.ylim(-5,5)

# Stromlinien mit Matplotlib-Funktion darstellen
plt.streamplot(X, Y, u, v,
                  density=2, linewidth=1, arrowsize=2, arrowstyle='->')
plt.plot([panel1.ax, panel1.bx],[panel1.ay, panel1.by],
         color='red', linewidth=3);


I am working on the file "4_5-Potentialtheorie_Quell-Panel-Verfahren.ipynb".
The first task is:
Zur Lösung des Gleichungssystems nutzen wir einen in Python (bzw. Numpy) vorhandenen Löser:
Can you write Python code for it?

lambda_i = np.linalg.solve(A_ij, b_i)

for i, panel_i in enumerate(panels):
    panel_i.lamb = lambda_i[i]


I am working on the file "4_5-Potentialtheorie_Quell-Panel-Verfahren.ipynb".
The first task is:
und stellen dann das Gleichungssytem auf:
Can you write Python code for it?

u_oo = 1.0
v_oo = 0.0

# die Koeffizientenmatrix A_ij
A_ij = np.zeros((N,N), dtype=float)

for i, panel_i in enumerate(panels):
    for j, panel_j in enumerate(panels):
        if i == j:
            A_ij[i,j] = 0.5
        else:
            A_ij[i,j] = norm_int(panel_i, panel_j) / (2 * math.pi)

# und die rechte Seite des Gleichungssystems
b_i = np.zeros(N, dtype=float)

for i, panel_i in enumerate(panels):
    b_i[i] = -u_oo * math.cos(panel_i.beta) - v_oo * math.sin(panel_i.beta)


I am working on the file "4_BVP/finite_diff.ipynb".
The first task is:
2. Problemas de Valor de Frontera No Lineales
Si introducimos no linealidades a nuestras ecuaciones diferenciales, resolverlas numéricamente implicará replantear el sistema de ecuaciones para dejarla en función de $w$ y podemos en cambio resolver $F(w) = 0$. Por ejemplo podemos usar el Método de Newton multivariado, y para ello necesitamos la matriz Jacobiana de $F$...
No olvidar la fórmula del jacobiano de una función $F$, que no es más que la matriz de derivadas parciales de $F$
\begin{bmatrix}
\cfrac{\partial F_1}{\partial w_1} & \cdots & \cfrac{\partial F_1}{\partial w_n} \
\vdots & \ddots & \vdots \
\cfrac{\partial F_n}{\partial w_1} & \cdots & \cfrac{\partial F_n}{\partial w_n}
\end{bmatrix}
El método de Newton es $w^{k+1} = w^{k} - \partial F(w^k)^{-1} F(w^k)$. Pero, como siempre, encontrar la inversa del Jacobiano puede ser una operacion costosa y puede llevar a inestabilidades. Por lo que es mejor resolver para $\Delta w$ el sistema análogo $\partial F(w^k) \Delta w = -F(w^k)$
$$Ax=b$$
El siguiente problema puede ser resuelto con diferencias finitas y método de Newton:
\begin{align}
y'' = y - y^2\
y(0) = 1\
y(1) = 4
\end{align}
Reemplazando nuestras fórmulas de diferencias finitas y dejando todo al lado izquierdo obtenemos:
$$w_{i-1} - (2 + h^2)w_i + h^2w_i^2 + w_{i+1} = 0$$
Esto nos asegurará poder expresar el sistema de ecuaciones como una función que depende de las estimaciones $w$. Aparte, las ecuaciones para las condiciones de borde resultan ser:
$$y_a - (2+h^2)w_1 + h^2w_1^2 + w_2 = 0$$
$$w_{n-1} - (2+h^2)w_n + h^2w_n^2 + y_b = 0$$
Para encontrar la solución numérica $w$ debemos calcular tanto $F(w)$ como su jacobiano $\partial F$. $F(w)$ no es más que un vector que contiene las ecuaciones mencionadas anteriormente:
$$F\left[ \begin{array}{c}
w_{1} \
w_{2}\
\vdots \
w_{n-1} \
w_{n}\ \end{array} \right] = \left[ \begin{array}{c}
y_a - (2+h^2)w_1 + h^2w_1^2 + w_2 \
w_{1} - (2 + h^2)w_2 + h^2w_2^2 + w_{3} \
\vdots \
w_{n-2} - (2+h^2)w_{n-1} + h^2w_{n-1}^2 + w_{n} \
w_{n-1} - (2+h^2)w_n + h^2w_n^2...
Can you write Python code for it?

ya = 1
yb = 4
n_iter = 20

# Matriz de diferencias finitas que depende de n
def jacobian(n, w, h):
    m = np.zeros((n,n), dtype=float)
    np.fill_diagonal(m, 2.0*h**2.0 * w -2-h**2)
    dix, diy = np.diag_indices(n)
    dix = dix[:-1]
    diy = diy[:-1] + 1
    m[(dix, diy)] = 1
    dix = dix + 1
    diy = diy - 1
    m[(dix, diy)] = 1
    return m

# Función con el sistema de ecuaciones a la cual le encontraremos el cero
def f(n, w, h):
    y = np.zeros((n))
    y[0] = ya       - (2+h**2)*w[0]   + h**2*w[0]**2   + w[1]
    y[n-1] = w[n-2] - (2+h**2)*w[n-1] + h**2*w[n-1]**2 + yb
    for i in range(1, n-1):
        y[i] = w[i-1] - (2+h**2)*w[i] + h**2*w[i]**2 + w[i+1]
    return y

plt.figure(figsize=(10,7))
# Probar distintos intervalos
for n in [2, 4, 40]:
    h = 1.0 / (n + 1)
    t = np.linspace(0, 1, n+2)
    w = np.zeros((n))
    for i in range(n_iter):
        # wk+1 = wk - delta w
        w = w - np.linalg.solve(jacobian(n, w, h), f(n, w, h))
    plt.plot(t, np.concatenate([[ya],w,[yb]]), 'o-', label="$n = "+str(n)+"$")
plt.legend(loc='best', fontsize=16)
plt.xlabel("$t$", fontsize=20)
plt.ylabel("$y(t)$", fontsize=20)
plt.show()


I am working on the file "4_BVP/finite_diff.ipynb".
The first task is:
Los errores de éste método son dos principalmente:

Truncamiento de las fórmulas de diferencias finitas (es decir, cuando despreciamos los términos de orden superior que provienen de la Serie de Taylor que utilizamos para definir las fórmulas).
El error de solucionar numéricamente el sistema de ecuaciones lineales.

Las fórmulas centradas de diferencia finitas tienen un error proporcional a $h^2$. Nuestro método irá disminuyendo el error a medida que aumentamos la cantidad de subintervalos $n+1$, es decir, a medida que disminuimos $h$, y por lo tanto el error decrece como $\mathcal{O}(n^{-2})$.
Recordemos que para entender gráficamente cómo un error decrece (o crece) es conveniente establecer relaciones utilizando gráficos loglog, donde la pendiente del gráfico indica el exponente para la regla de cambio del error respecto a la variable independiente $n$.
Se puede observar que efectivamente la pendiente de este gráfico es -2, lo que confirma la regla del error que hemos calculado.
Can you write Python code for it?

from scipy.interpolate import BarycentricInterpolator

y_data = y(0.75)
exp = np.linspace(1, 3, 10)
N = np.round(np.power(10, exp))
err = []

for n in N:
    n = int(n)
    h = 1.0/(n + 1.0)
    A = DiffMatrix(n, h)
    b = np.zeros((n))
    b[0] = -1
    b[-1] = -3
    w = np.concatenate(([1], np.linalg.solve(A, b), [3]))
    _t = np.linspace(0, 1, n+2)
    f = BarycentricInterpolator(_t, w)
    err.append(np.abs(f(0.75) - y(0.75)))

logerr = np.log10(err)
plt.figure(figsize=(10,7))
plt.plot(exp, logerr, 'bo')
slope = (logerr[1] - logerr[0]) / (exp[1] - exp[0])
print("pendiente:",slope)
plt.xlabel("$n$",fontsize=20)
plt.ylabel("$|w-y|_{t=0.75}$",fontsize=20)
exp_n = [1,2,3]
plt.xticks(exp_n, ["$10^{"+str(i)+"}$" for i in exp_n], fontsize=16)
exp_error = [-7, -6, -5, -4 , -3]
plt.yticks(exp_error, ["$10^{"+str(i)+"}$" for i in exp_error], fontsize=16)
plt.show()


I am working on the file "4_BVP/shooting_method.ipynb".
The first task is:
Ejemplo 2
Aplique el shooting method al BVP siguiente problema
$$
\begin{align}y'' &= y + \frac{2}{3} e^t\y(0) &= 0 \ y(1) &= \frac{1}{3}e
\end{align}
$$
Debemos identificar el IVP que al ajustarlo generará la solución al BVP. Si aplicamos el cambio de variable sugerido el sistema resulta ser...
Can you write Python code for it?

# Condiciones de borde e intervalo de tiempo
a, b = (0, 1)
yb = 1./3.*np.exp(1)
t = np.linspace(a, b, 101)

def yprime(_y, t):
    y, v = _y
    return [v, y + 2./3.*np.exp(t)]

def F(s):
    y0 = [0, s]
    sol = odeint(yprime, y0, t)
    return sol[:,0][-1] - yb

print(F(0))
print(F(1))
# Solución
sbest = bisect(F, 0, 1)
print(sbest)
print(F(sbest))

# Plot de aproximaciones
y1 = odeint(yprime, [0, 0], t)
y2 = odeint(yprime, [0, 1], t)
plt.figure(figsize=(10,7))
plt.plot(t, y1[:,0],'g--', lw=2.0, label="$s=0$")
plt.plot(t, y2[:,0],'c--', lw=2.0, label="$s=1$")
# Plot de resultado
ybest = odeint(yprime, [0, sbest], t)
plt.plot(1, yb, 'ro')
plt.plot(t, ybest[:,0],'r-', lw=2.0, label="$s=s_{best}$")
# Solución Analítica
plt.plot(t, 1./3.*t*np.exp(t), 'b--', lw=2.0, label="Solución")
plt.legend(loc='best', fontsize=20)
plt.xlabel("$t$", fontsize=20)
plt.ylabel("$y(t)$", fontsize=20)
plt.show()


I am working on the file "4_BVP/shooting_method.ipynb".
The first task is:
Ejemplo 3
Aplique el shooting method al BVP siguiente problema
$$
\begin{align}y'' &= 2e^{-2y}(1-t^2)\y(0) &= 0 \ y(1) &= \ln(2)
\end{align}
$$
Debemos identificar el IVP que al ajustarlo generará la solución al BVP. Si aplicamos el cambio de variable sugerido el sistema resulta ser...
Can you write Python code for it?

# Condiciones de borde e intervalo de tiempo
a, b = (0, 1)
yb = np.log(2.0)
t = np.linspace(a, b, 101)

def yprime(_y, t):
    y, v = _y
    return [v, 2.*np.exp(-2*y)*(1-t**2)]

def F(s):
    y0 = [0, s]
    sol = odeint(yprime, y0, t)
    return sol[:,0][-1] - yb

print(F(-1))
print(F(1))
# Solución
sbest = bisect(F, -1, 1)
print(sbest)
# Plot de aproximaciones
y1 = odeint(yprime, [0, -1], t)
y2 = odeint(yprime, [0, 1], t)
ybest = odeint(yprime, [0, sbest], t)

plt.figure(figsize=(10,7))
plt.plot(t, y1[:,0],'g--', lw=2.0, label="$s=-1$")
plt.plot(t, y2[:,0],'c--', lw=2.0, label="$s=1$")
plt.plot(t, ybest[:,0],'r-', lw=2.0, label="$s=s_{best}$")
# Solución Analítica
plt.plot(t, np.log(t**2.0 + 1), 'b--', lw=2.0, label="Solución")
plt.plot(1, yb, 'ro')
plt.legend(loc='best', fontsize=20)
plt.xlabel("$t$", fontsize=20)
plt.ylabel("$y(t)$", fontsize=20)
plt.show()