# **Combinatorial Promoter Example**

written by Andrey Shur, 5/4/2020

### CombinatorialPromoter Overview

The CombinatorialPromoter is supposed to represent the general case where a promoter is transcribable when bound by a specific combination of regulators.

In general, consider a set of $M$ regulators $R_1...R_M$ which bind to the promoter $P$ with cooperativities $n_1...n_M$ ie $n_i$ copies of $R_i$ bind to the promoter at once. The CombinatorialPromoter enumerates all possible bound Promoter-Regulator complexes which are returned as a list of ComplexSpecies using the Combinatorial_Cooperative_Binding Mechanism. These complexes will be default take the following form with the regulators in alphabetical order:

\begin{align}
\textrm{Species}=\{&P, P:n_1\textrm{x}R_1, &&..., P:n_M\textrm{x}R_M, \\
&P:n_1\textrm{x}R_1:n_2\textrm{x}R_2, &&..., P:n_{M-1}\textrm{x}R_{M-1}:n_M\textrm{x}R_M \\
&&. \\
&&. \\
&&. \\
&P:n_1\textrm{x}R_1:n_2\textrm{x}R_2: &&...:n_{M-1}\textrm{x}R_{M-1}:n_M\textrm{x}R_M \}
\end{align}

The Combinatorial_Cooperative_Binding Mechanism in Combinatorial Promoter returns all reactions where a Complex consisting of a promoter and a set of regulators bind to one additional regulator.
\begin{align}
\textrm{Reactions} =
\{&P + n_1 R_1 \leftrightarrow P:n_1\textrm{x}R_1, ...\\
 &..., P + n_M R_M \leftrightarrow P:n_M\textrm{x}R_M, ... \\
  &..., P:n_1\textrm{x}R_1 + n_2 R_2 \leftrightarrow P:n_1\textrm{x}R_1:n_2\textrm{x}R_2,... \\
  &..., P:n_{M-1}\textrm{x}R_{M-1} + n_M R_M \leftrightarrow P:n_{M-1}\textrm{x}R_{M-1}:n_M\textrm{x}R_M, ... \\
  &. \\
  &. \\
  &. \\
  &..., P:n_1\textrm{x}R_1:n_2\textrm{x}R_2:...:n_{M-1}\textrm{x}R_{M-1} + n_M R_M
  \leftrightarrow
  P:n_1\textrm{x}R_1:n_2\textrm{x}R_2:...:n_{M-1}\textrm{x}R_{M-1}:n_M\textrm{x}R_M
\end{align}

Arbitrary combinations of these regulators can be defined as transcribable using:

    CombinatorialPromoter(name, [Regulators], tx_capable_list = [[Transcribable Combination fo regulators 1], [Combination 2], ...])

Additionally, there is the option for leak reactions in which case the free promoter is transcribed at some leak rate.

    CombinatorialPromoter(name, [Regulators], leak = True/False )

### Below are imports required for these examples and plotting.

```python
#Using Bioscrape: Basic Imports
#from bioscrape.types import Model
#from bioscrape.simulator import py_simulate_model
#For arrays and plotting
import numpy as np
import pylab as plt
try:
    make_heatmap = True
    import seaborn as sns
    import pandas as pd
except ModuleNotFoundError:
    make_heatmap = False
#this is for the network representation
try:
    import bokeh.plotting
    import bokeh.io
    bokeh.io.output_notebook()
    plotCRN = True
    from biocrnpyler import plotting
except (ModuleNotFoundError,ImportError) as e:
    plotCRN = False
#biocrnpyler
from biocrnpyler import *
```

## Example 1: Default Behavior to Produce AND logic with leak
In the following example, we consider a promoter phrpL which can be bound by the proteins hrpR and hrpS. When both proteins are present, transcription is activated, but when one or the other (or none) is present, transcription is not activated.

```python
#these are the additional parameters that you need:
parameters = {
    ('phrpL_hrpR', 'cooperativity'): 1.0, #default is 2, but these proteins bind at cooperativity = 1
    ('phrpL_hrpS', 'cooperativity'): 1.0,
    ('phrpL_hrpR', 'ku'):50, #these are the regulator unbinding rates
    ('phrpL_hrpS', 'ku'):50,
    ('phrpL_leak', 'ktx'): 0.01,  #this is the leak rate
    ('phrpL_leak', 'ku'): 100, #this is the leak polymerase unbinding rate

    ('translation_mm', 'B0030', 'ku'): 10.0, #Unbinding
    ('translation_mm', 'B0030', 'ktl'): 1.5, #Translation Rate

    #('transcription_mm',  'ktx'): .01,    #These are the parameters for transcription leak
    #('transcription_mm', 'ku'): 100,
    "kb":100, "ku":10, "ktx":.05, "ktl":.2, "kdeg":2 #some default parameters
}
#above you can see the desired part_id is very simple. The general syntax is <promoter.name>_<regulator.name>.
#using this syntax you can define the strength of binding of each regulator to the promoter.
#cooperativity between different regulators is not supported.

#you can also define the transcription rate and binding and unbinding (kb or ku) of polymerase from a transcribable
#complex. A transcribable complex is a promoter with regulators bound that is capable of transcription.

#By default, all combinations of regulators bound will be capable of transcription. They will use the "ktx" parameter
#promoter with nothing bound will use the phrpL_leak parameter

#now we define the proteins. Their names should match what you put in the promoter, below
hrpR = Protein("hrpR")
hrpS = Protein("hrpS")

#Define the promoter

#this is an "AND gate" promoter. That means that it only leads to transcript if both regulators are bound
#binding of one or the other regulator should not lead to any transcription
#this is the network:
# hrpR + mydna <--> mydna:hrpR
# hrpS + mydna <--> mydna:hrpS
# hrpR + mydna:hrpS <--> mydna:hrpR:hrpS
# hrpS + mydna:hrpR <--> mydna:hrpR:hrpS

phrpL = CombinatorialPromoter("phrpL",["hrpR","hrpS"], leak=True)
#this defines a promoter which binds hrpR and hrpS, and it leaks.
#By default, leak only happens when there is nothing bound to the DNA.
#this also means that in this promoter, individually bound species (with only one regulator) will not lead to
#any transcription! This means they will not leak also.

#this is our DNA
dna = DNAassembly("mydna",promoter=phrpL,rbs="B0030",protein="GFP")

#TxTl Extract is a Mixture with more complex internal models
extract_1_TXTL = TxTlExtract(name = "e coli extract 1", components = [dna,hrpR,hrpS], \
                                     parameters=parameters, parameter_file = "Default Param File Example.tsv")
CRN_extract_1 = extract_1_TXTL.compile_crn()

print("The species and reactions in the CRN:\n", CRN_extract_1.pretty_print())

#below is CRN plotting code. set plotCRN to False to disable
if(plotCRN):
    DG, DGspec, DGrxn = generate_networkx_graph(CRN_extract_1)
    plot = Plot(plot_width=500, plot_height=500, x_range=Range1d(-500, 500), y_range=Range1d(-500, 500))
    graphPlot(DG,DGspec,DGrxn,plot)
    bokeh.io.show(plot)

#this is just a normal time trace of the model, with both proteins present
try:
    timepoints = np.linspace(0, 200, 1000)
    x0 = {"dna_mydna":5.0, "protein_hrpS":10, "protein_hrpR":7, "protein_RNAP":10., "protein_Ribo":50.,}
    Re1 = CRN_extract_1.simulate_with_bioscrape(timepoints, initial_condition_dict = x0)
    plt.plot(timepoints,Re1["protein_GFP"], label = "protein_GFP")
    plt.plot(timepoints,Re1["protein_hrpR"], label = "protein_hrpR")
    plt.plot(timepoints,Re1["protein_hrpS"], label = "protein_hrpS")
    plt.plot(timepoints,Re1["rna_mydna"], label = "rna_mydna")
    plt.gca().set_yscale("log")
    plt.title("Time trace of AND gate promoter phrpL")
    plt.xlabel("time")
    plt.ylabel("protein")
    plt.legend()
    print(parameters)
except ModuleNotFoundError:
    pass
```

# Vary hrpR and hrpS
To really test the combinatorial promoter we will vary the concentrations of hrpR and hrpS.

```python
if(make_heatmap):
    #aggregator data frame
    GFP_max = pd.DataFrame(columns = ['hrpS_conc', 'hrpR_conc', 'GFP_max'])
    #Different initial values of R and S
    conc_hrpR = np.linspace(0, 30, 9)
    conc_hrpS = np.linspace(0, 30, 9)

    try:
        for conc_R in conc_hrpR:
            x0["protein_hrpR"] = conc_R
            for conc_S in conc_hrpS:
                x0["protein_hrpS"] = conc_S #Change my initial condition dictionary
                Re1 = CRN_extract_1.simulate_with_bioscrape(timepoints, initial_condition_dict = x0)
                #now we are simulating over and over again, but only taking the final protein_GFP value
                GFP_max = GFP_max.append({'hrpS_conc':conc_S,
                                'hrpR_conc':conc_R,
                                'GFP_max': Re1["protein_GFP"].values[-1]}, ignore_index=True)
        #now, you make a 2d plot with all the data
        data = pd.pivot_table(data = GFP_max, index = 'hrpS_conc',
                    columns = 'hrpR_conc',
                    values = 'GFP_max')

        ax = sns.heatmap(data,
                        cmap = 'viridis',
                        linewidths=.2)
        ax.set_ylim(9,-0.5) #not sure why this is needed

        ax.set(xlabel='hrpS', ylabel='hrpR')
        ax.set_title("hrpR, hrpS Combinatorial Promoter Heatmap")
    except ModuleNotFoundError:
        pass
```

### Improving the AND Logic Model using the tx_capable_list keyword and more detailed parameters

After careful inspection of the above heatmap, you might notice two important features:

1) The promoter is on at all times except when no regulators are present

2) the promoter is on very strongly for most possible concentrations

We will adjust this model by changing the parameters and adding additional transcribable states

1) increase the unbinding rate

2) define single regulator bound promoters as being "leak"

To do this, we will use the tx_capable_list keyword and modify the list of parameters

    phrpL = CombinatorialPromoter(... tx_capable_list = [["hrpR","hrpS"]] ...)

Notice that there are 3 transcribable states, each regulator bound individually and both regulators bound together. Each of these combination of regulators requires their own parameters which are seen below in the newparam dictionary.

```python
newparam = {
    ('phrpL_hrpR', 'cooperativity'): 1.0,
    ('phrpL_hrpS', 'cooperativity'): 1.0,
    ('phrpL_hrpR','ku'):5, #unbinding rate of each regulator
    ('phrpL_hrpS','ku'):5,
    ('phrpL_hrpR_hrpS_RNAP', 'ktx'): 0.17025, #these are the rates for the correctly bound promoter
    ('phrpL_hrpR_hrpS_RNAP', 'ku'): 11.01321586,
    ('phrpL_leak','ktx'):0.01, #this is the leak transcription rate
    ('phrpL_leak', 'ku'): 100.,#this unbinding rate is now needed because we changed the tx_capable_list. Transcription from singly bound promoter uses the default "ktx"

}

#WARNING!!!
#out of order regulators will not work! For example, phrpL_hrpR_hrpS_RNAP is correct, but phrpL_hrpS_hrpR_RNAP is not.

parameters.update(newparam)
#you can also define the ktx for the leak reaction. I don't think this is necessary
#    ('phrpL_hrpR_RNAP', 'ktx'): 0.17,
#    ('phrpL_hrpS_RNAP', 'ktx'): 0.17,

phrpL = CombinatorialPromoter("phrpL",["hrpR","hrpS"],
                              tx_capable_list = [[Species("hrpR",material_type="protein"),"hrpS"]], #species can also be used
                              leak=True)
#now we are saying that there are more species that are capable of transcribing. Specifically, that we
#can still transcribe when both are bound, but also when one or the other is bound.

#this is our DNA
dna = DNAassembly("mydna",promoter=phrpL,rbs="B0030",protein="GFP")

extract_1_TXTL = TxTlExtract(name = "e coli extract 1", components = [dna,hrpR,hrpS], \
                                     parameters=parameters, parameter_file = "Perfect Param File Example.tsv")
CRN_extract_1 = extract_1_TXTL.compile_crn()
try:
    if(make_heatmap):
        #aggregator data frame
        GFP_max = pd.DataFrame(columns = ['hrpS_conc', 'hrpR_conc', 'GFP_max'])
        #Different initial values of R and S
        conc_hrpR = np.linspace(0, 30, 9)
        conc_hrpS = np.linspace(0, 30, 9)

        for conc_R in conc_hrpR:
            x0["protein_hrpR"] = conc_R
            for conc_S in conc_hrpS:
                x0["protein_hrpS"] = conc_S #Change my initial condition dictionary
                Re1 = CRN_extract_1.simulate_with_bioscrape(timepoints, initial_condition_dict = x0)
                #now we are simulating over and over again, but only taking the final protein_GFP value
                GFP_max = GFP_max.append({'hrpS_conc':conc_S,
                                'hrpR_conc':conc_R,
                                'GFP_max': Re1["protein_GFP"].values[-1]}, ignore_index=True)

        #now, you make a 2d plot with all the data
        data = pd.pivot_table(data = GFP_max, index = 'hrpS_conc',
                    columns = 'hrpR_conc',
                    values = 'GFP_max')

        ax = sns.heatmap(data,
                        cmap = 'viridis',
                        linewidths=.2)
        ax.set_ylim(9,-0.5) #not sure why this is needed

        ax.set(xlabel='hrpS', ylabel='hrpR')
        ax.set_title("hrpR, hrpS Combinatorial Promoter Heatmap")
except ModuleNotFoundError:
    pass
```

## Example 2: Using the tx_capable_list keyword to implement XOR logic
In the following example, we consider a promoter phrpL which can be bound by the proteins hrpR and hrpS. When either one  proteins or the other is present, but not both, transcription is activated. To do this, we pass in the following combinations to transcribable list:

    phrpL = CombinatorialPromoter(... tx_capable_list = [["hrpR"],["hrpS"]] ...)

This ensures that only when hrpR or hrpS is individually bound to the promoter can it be transcribed.

```python
newparam = {
    ('phrpL_hrpR', 'cooperativity'): 1.0,
    ('phrpL_hrpS', 'cooperativity'): 1.0,
    ('phrpL_hrpR','ku'):3, #we have to massively decrease the unbinding rate to make this xor gate work
    ('phrpL_hrpS','ku'):3,
    #('phrpL_hrpR_hrpS_RNAP', 'ku'): 100, #this time, this one is leak
    ('phrpL_hrpR_RNAP', 'ktx'): 0.17, #these are now transcribing at high rates!
    ('phrpL_hrpS_RNAP', 'ktx'): 0.17,
    ('phrpL_hrpS_RNAP', 'ku'): 11.,
    ('phrpL_hrpR_RNAP', 'ku'): 11,
    ('phrpL','ku'):100,

}

parameters.update(newparam)
phrpL = CombinatorialPromoter("phrpL",["hrpR","hrpS"],
                              tx_capable_list = [["hrpR"],["hrpS"]],
                              leak=True)
#this is our DNA
dna = DNAassembly("mydna",promoter=phrpL,rbs="B0030",protein="GFP")

extract_1_TXTL = TxTlExtract(name = "e coli extract 1", components = [dna,hrpR,hrpS], \
                                     parameters=parameters, parameter_file = "Perfect Param File Example.tsv")
CRN_extract_1 = extract_1_TXTL.compile_crn()

try:
    if(make_heatmap):
        #aggregator data frame
        GFP_max = pd.DataFrame(columns = ['hrpS_conc', 'hrpR_conc', 'GFP_max'])
        #Different initial values of R and S
        conc_hrpR = np.linspace(0,30, 9)
        conc_hrpS = np.linspace(0, 30, 9)
        for conc_R in conc_hrpR:
            x0["protein_hrpR"] = conc_R
            for conc_S in conc_hrpS:
                x0["protein_hrpS"] = conc_S #Change my initial condition dictionary
                Re1 = CRN_extract_1.simulate_with_bioscrape(timepoints, initial_condition_dict = x0)
                #now we are simulating over and over again, but only taking the final protein_GFP value
                GFP_max = GFP_max.append({'hrpS_conc':conc_S,
                                'hrpR_conc':conc_R,
                                'GFP_max': Re1["protein_GFP"].values[-1]}, ignore_index=True)

        #now, you make a 2d plot with all the data
        data = pd.pivot_table(data = GFP_max, index = 'hrpS_conc',
                    columns = 'hrpR_conc',
                    values = 'GFP_max')

        ax = sns.heatmap(data,
                        cmap = 'viridis',
                        linewidths=.2)
        ax.set_ylim(9,-0.5) #not sure why this is needed

        ax.set(xlabel='hrpS', ylabel='hrpR')
        ax.set_title("hrpR, hrpS Combinatorial Promoter Heatmap")
except ModuleNotFoundError:
    pass
```

```python

```


2019年度
線形代数　I　講座資料

資料番号　No.1

### [1]ベクトルと演算

####  ベクトルの大きさと方向について

平面ベクトルの時、始点Aから終点Bの移動線分を$\vec{AB}$と表す、１文字で表現するときには太文字$\boldsymbol{ a }$で表す。

１）ベクトルには大きさは、ベクトルの線分の大きさで、$ |\vec{AB}|, |\boldsymbol{ a }|$で表す。

２）ベクトルには方向がある

３）２つのベクトルが等しいとは、大きさと方向の両方ともに等しいときである。

$$ \vec{AB} = \vec{CD} $$

位置ベクトル
座標の原点O からの座標Pまでのベクトル$　\boldsymbol{a}=\vec{OP}$を点Pの位置ベクトルという

```python
from sympy import *
init_printing()
# init_session()
```

```python
import numpy as np
from matplotlib import pyplot
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
```

```python
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111)
a1 = mpatches.Arrow(x=0,y=0,dx=3,dy=5,width=1,color='red' )
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])
ax.add_patch(a1)
ax.grid(linestyle='-')
plt.show()
```

```python
# インタラクティブな作図　ベクトルの大きさと向きと成分の関係

from ipywidgets import interact
import numpy as np
from mpmath import *
import matplotlib.pyplot as plt

def scatter(data_x=5,data_y=5 ):
    fig = plt.figure(figsize=(4,4))
    ax = fig.add_subplot(111)
    a1 = mpatches.Arrow(x=0,y=0,dx=data_x,dy=data_y,width=1,color='red' )
    ax.set_xlim([0, 10])
    ax.set_ylim([0, 10])
    ax.add_patch(a1)
    plt.show()
    print( '大きさ ',sqrt(data_x**2+data_y**2))
    print( '角度[°]',degrees( atan2(data_y, data_x))  )

interact(scatter, data_x=(0, 10, 0.01),data_y=(0,10,0.01));

```

Failed to display Jupyter Widget of type interactive.

  If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean
  that the widgets JavaScript is still loading. If this message persists, it
  likely means that the widgets JavaScript library is either not installed or
  not enabled. See the Jupyter
  Widgets Documentation for setup instructions.

  If you're reading this message in another frontend (for example, a static
  rendering on GitHub or NBViewer),
  it may mean that your frontend doesn't currently support widgets.

```python
def arrow():
    fig = pyplot.figure()
    ax = fig.add_subplot(111)

    point = {
        'start': [10, 10],
        'end': [90, 90]
    }

    ax.plot(*point['start'], 'o', color="red")
    ax.plot(*point['end'], 'o', color="blue")

    ax.annotate('', xy=point['end'], xytext=point['start'],
                arrowprops=dict(shrink=0, width=1, headwidth=8,
                                headlength=10, connectionstyle='arc3',
                                facecolor='gray', edgecolor='gray')
               )

    ax.set_xlim([0, 100])
    ax.set_ylim([0, 100])
    ax.grid(linestyle='-')
    pyplot.show()
arrow()
```

ベクトルの成分表示につてについて
$  \boldsymbol{a}= (a_1, a_2 )$と表す

縦に書く時は　$$ \boldsymbol{a}= \left(  \begin{array}{c}   a_1 \\    a_2 \end{array}\right)$$

```python
a, b,a1,a2 = symbols('a b,a1,a2')
a = Matrix([a1, a2])
b = Matrix([2,  -1])
```

```python
a
```

$$\left[\begin{matrix}a_{1}\\a_{2}\end{matrix}\right]$$

```python
b
```

$$\left[\begin{matrix}2\\-1\end{matrix}\right]$$

ベクトルの大きさは　$ ||ベクトル量|| $、または　$ |ベクトル量| $ で示す
例　$ ||a|| $, $ |b| $

```python
a.norm()
```

```python
from sympy import *
sqrt(a[0]**2+a[1]**2)
```

```python
b.norm()
```

```python
sqrt(b[0]**2+b[1]**2)
```

ベクトルの図を書いてみる　成分（５，７）で原点からベクトルを引いた図を以下に示す。

```python
import numpy as np
import matplotlib.pyplot as plt

plt.figure()

# 矢印（ベクトル）の始点
X = 0
Y = 0
# 矢印（ベクトル）の成分
U = 5
V = 7

# 矢印（ベクトル）
plt.quiver(X,Y,U,V,angles='xy',scale_units='xy',scale=1)

# グラフ表示
plt.xlim([-10,10])
plt.ylim([-10,10])
plt.grid()
plt.draw()
plt.show()
```

### ベクトルの和
2次元ベクトルのでの和の計算について

$  \boldsymbol{a}=(a_1 , a_2),  \boldsymbol{b}=(b_1 , b_2)  $とすると和（＋）の演算は

$  \boldsymbol{a}+ \boldsymbol{b} = (a_1+b_1 , a_2+b_2 )$となる
###### 交換法則
$  \boldsymbol{a}+ \boldsymbol{b} =  \boldsymbol{b}+ \boldsymbol{a} $
###### 結合法則
$  (\boldsymbol{a}+ \boldsymbol{b})+ \boldsymbol{c} = \boldsymbol{a}+ (\boldsymbol{b}+ \boldsymbol{c}) $

#### ゼロベクトル
すべての成分が０であるベクトル

例として２次元の時は　$ \boldsymbol{0} = (0,0) $

#### 逆ベクトル

ベクトルの大きさが等しく、方向が逆なベクトルを逆ベクトルという

$\boldsymbol{a}$の逆ベクトルを$ -\boldsymbol{a}$と表す、$ -\boldsymbol{a}=(-a_1 , -a_2)$

２つのベクトルの差は、和と逆ベクトルにより   $ \boldsymbol{a}=(a_1 , a_2),  \boldsymbol{b}=(b_1 , b_2) $

$\boldsymbol{a}- \boldsymbol{b} = \boldsymbol{a}+ (- \boldsymbol{b})$と定める
$\boldsymbol{a}- \boldsymbol{b} = (a_1,a_2)+(-b_1,-b_2)=(a_1-b_1,a_2-b_2)$

### ベクトルのスカラー倍（ここでは、スカラーを実数で説明する）

$k$,$l$をスカラーとする

$k\boldsymbol{a}=k(a_1,a_2)=(ka_1,ka_2) $

(1) 1 $\boldsymbol{a}=\boldsymbol{a} $,   (-1) $\boldsymbol{a}=-\boldsymbol{a} $

(2) $k ( \boldsymbol{a}+ \boldsymbol{b})= k \boldsymbol{a}+k \boldsymbol{b}$

(3) $(k+l)  \boldsymbol{a}= k \boldsymbol{a}+l \boldsymbol{a}$

(4) $ k(l\boldsymbol{a}) = (kl)\boldsymbol{a}$

(5) $|k \boldsymbol{a} | = |k|\cdot| \boldsymbol{a}|$

###  単位ベクトル

大きさが１であるベクトルのことをいう

あるベクトル$\boldsymbol{a}$を大きさ１にしたベクトル $\boldsymbol{n}$を作るには以下のようにする

$$ \boldsymbol{n} = \frac{ \boldsymbol{a} }{|\boldsymbol{a}| } $$

### 基本ベクトル

大きさが１であるベクトルで、各軸の正方向を向いたベクトルをいう

3次元のベクトルでは基本ベクトル（　x軸方向　$\boldsymbol{i}$,　　y軸方向　$\boldsymbol{j}$,　　z軸方向　$ \boldsymbol{k}$　）

その成分表現では　$\boldsymbol{i}=(1,0,0)$,　　$\boldsymbol{j}=(0,1,0)$,　　$\boldsymbol{k}=(0,0,1)$

注意）平行なベクトルとは、同じ方向を向いているものと、その逆方向を向いているものの２つがあること

問1.1　(1)$\vec{OA}$

```python
Oa ,a,O = symbols('Oa a O')
a = Matrix([3, 1])
O = Matrix([0,  0])
Oa = a-O
Oa
```

$$\left[\begin{matrix}3\\1\end{matrix}\right]$$

```python
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111)
a1 = mpatches.Arrow(x=0,y=0,dx=3,dy=1,width=0.5,color='red' )
ax.set_xlim([0, 4])
ax.set_ylim([0, 4])
ax.add_patch(a1)
ax.grid()
plt.show()
```

問１．１　(3)$\vec{AB}$

```python
ab ,a,b = symbols('ab a b')
a = Matrix([3, 1])  # a=(3,1) を定義した
b = Matrix([1, 2])  # b=(1,2) を定義した
ab = b-a            # ベクトルabを 計算している
ab                  # ベクトルabの値を表示
```

$$\left[\begin{matrix}-2\\1\end{matrix}\right]$$

```python
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111)
a1 = mpatches.Arrow(x=3,y=1,dx=-2,dy=1,width=0.5,color='red' )
ax.set_xlim([0, 4])
ax.set_ylim([0, 4])
ax.add_patch(a1)
ax.grid(linestyle='-')
plt.show()
```

問１．５　(5) $\boldsymbol{a}$と平行な大きさ１のベクトル

注意）大きさを１にするために、$ \boldsymbol{n} = \frac{ \boldsymbol{a} }{|\boldsymbol{a}|} $をすること、平行とは、方向が逆のものも答えなので、±を答えにつけること

```python
a,b= symbols('a,b')
a = Matrix([1, 2])
b = Matrix([-2,3])
a.norm()
a/(a.norm())
```

$$\left[\begin{matrix}\frac{\sqrt{5}}{5}\\\frac{2 \sqrt{5}}{5}\end{matrix}\right]$$

```python
-a/(a.norm())
```

$$\left[\begin{matrix}- \frac{\sqrt{5}}{5}\\- \frac{2 \sqrt{5}}{5}\end{matrix}\right]$$

問1.6 (4) $ |3\boldsymbol{a}- 2\boldsymbol{b}| $

```python
a,b= symbols('a,b')
a = Matrix([1, 2,-1])
b = Matrix([-2,3,4])
3*a-2*b
```

$$\left[\begin{matrix}7\\0\\-11\end{matrix}\right]$$

```python
(3*a-2*b).norm()
```

####  右手系

fig-1-2.png

####  右ネジ

fig-1-1.png

```python

```


*Sebastian Raschka*
last modified: 03/31/2014

I am really looking forward to your comments and suggestions to improve and extend this tutorial! Just send me a quick note
via Twitter: [@rasbt](https://twitter.com/rasbt)
or Email: [bluewoodtree@gmail.com](mailto:bluewoodtree@gmail.com)

### Problem Category
- Statistical Pattern Recognition
- Supervised Learning
- Parametric Learning
- Bayes Decision Theory
- Univariate data
- 2-class problem
- different variances
- different priors
- Gaussian model (2 parameters)
- With conditional Risk (loss functions)

# Sections

• Given information

• Deriving the decision boundary

• Plotting the posterior probabilities and decision boundary

• Classifying some random example data

• Calculating the empirical error rate

## Given information:

[back to top]

####model: continuous univariate normal (Gaussian) model for the class-conditional densities

$ p(x | \omega_j) \sim N(\mu|\sigma^2) $
$ p(x | \omega_j) \sim \frac{1}{\sqrt{2\pi\sigma^2}} \exp{ \bigg[-\frac{1}{2}\bigg( \frac{x-\mu}{\sigma}\bigg)^2 \bigg] } $

####Prior probabilities:

$ P(\omega_1) = \frac{2}{3}, \quad P(\omega_2) = \frac{1}{3} $

####Loss functions:

where

$ \lambda(\alpha_i|\omega_j) = \lambda_{ij}$,

the loss occured if $action_i$ is taken if the actual true class is $ \omega_j$ (assuming that $action_i$ classifies sample as $\omega_i$)

\begin{equation}
\lambda = \begin{pmatrix}
\lambda_{11} \quad \lambda_{12} \\
\lambda_{21} \quad \lambda_{22}
\end{pmatrix}
= \begin{pmatrix}
0 \quad 1 \\
2 \quad 0 \\
\end{pmatrix}\\
\end{equation}

#### Variances of the sample distributions

$ \sigma_1^2 = 0.25, \quad \sigma_2^2 = 0.04 $

#### Means of the sample distributions

$ \mu_1 = 2, \quad \mu_2 = 1.5 $

## Deriving the decision boundary
[back to top]

### Bayes' Rule:

$ P(\omega_j|x) = \frac{p(x|\omega_j) * P(\omega_j)}{p(x)}$

### Risk Functions:

$ R(\alpha_1|x) = \lambda_{11}P(\omega_1|x) + \lambda_{12}P(\omega_2|x) $

$ R(\alpha_2|x) = \lambda_{21}P(\omega_1|x) + \lambda_{22}P(\omega_2|x) $

###Decision Rule:

Decide $ \omega_1 $ if $ R(\alpha_2|x) > R(\alpha_1|x) $ else decide $ \omega_2 $.

$ \Rightarrow \lambda_{21}P(\omega_1|x) + \lambda_{22}P(\omega_2|x) > \lambda_{11}P(\omega_1|x) + \lambda_{12}P(\omega_2|x) $

$ \Rightarrow (\lambda_{21} - \lambda_{11}) \; P(\omega_1|x) > (\lambda_{12} - \lambda_{22}) \; P(\omega_2|x) $

$ \Rightarrow \frac{P(\omega_1|x)}{P(\omega_2|x)} > \frac{(\lambda_{12} - \lambda_{22})}{(\lambda_{21} - \lambda_{11})} $

$ \Rightarrow \frac{p(x|\omega_1) \; P{(\omega_1)}}{p(x|\omega_2) \; P{(\omega_2)}} > \frac{(\lambda_{12} - \lambda_{22})}{(\lambda_{21} - \lambda_{11})} $

$ \Rightarrow \frac{p(x|\omega_1)}{p(x|\omega_2)} > \frac{(\lambda_{12} - \lambda_{22}) \; P{(\omega_2)}}{(\lambda_{21} - \lambda_{11}) \; P{(\omega_1)}} $

$ \Rightarrow \frac{p(x|\omega_1)}{P(x|\omega_2)} > \frac{(1 - 0) \; (1/3)}{(2 - 0) \; (2/3)} $

$ \Rightarrow \frac{p(x|\omega_1)}{P(x|\omega_2)} > \frac{1}{4} $

$ \Rightarrow  \Bigg( \frac{1}{\sqrt{2\pi\sigma_1^2}} \exp{ \bigg[-\frac{1}{2}\bigg( \frac{x-\mu_1}{\sigma_1}\bigg)^2 \bigg] } \Bigg) \Bigg/  \Bigg( \frac{1}{\sqrt{2\pi\sigma_2^2}}  \exp{ \bigg[-\frac{1}{2}\bigg( \frac{x-\mu_2}{\sigma_2}\bigg)^2 \bigg] } \Bigg)> \frac{1}{4} \quad \bigg| \quad \sigma_1^2 = 0.25, \quad \sigma_2^2 = 0.04, \quad \mu_1 = 2, \quad \mu_2 = 1.5 $

$ \Rightarrow  \Bigg( \frac{1}{\sqrt{2\pi0.25}} \exp{ \bigg[-\frac{1}{2}\bigg( \frac{x-2}{0.5}\bigg)^2 \bigg] } \Bigg) \Bigg/  \Bigg( \frac{1}{\sqrt{2\pi0.04}}  \exp{ \bigg[-\frac{1}{2}\bigg( \frac{x-1.5}{0.2}\bigg)^2 \bigg] } \Bigg)> \frac{1}{4}  $

$ \Rightarrow  \Bigg( 2 \; \cdot \;\frac{1}{\sqrt{2\pi}} \exp{ \bigg[-2 \; \cdot \; (x-2)^2 \bigg] } \Bigg) \Bigg/  \Bigg( 5 \; \cdot \; \frac{1}{\sqrt{2\pi}}  \exp{ \bigg[-12.5 \; \cdot \; (x-1.5)^2 \bigg] } \Bigg)> \frac{1}{4}  $

$ \Rightarrow \frac{2}{5} \Bigg( \exp{ \bigg[-2 \; \cdot \; (x-2)^2 \bigg] } \Bigg) \Bigg/ \Bigg( \exp{ \bigg[-12.5 \; \cdot \; (x-1.5)^2 \bigg] } \Bigg) \Bigg) > \frac{1}{4} \quad \bigg| \quad ln $

$ \Rightarrow \bigg(-2(x-2)^2 \bigg) - \bigg(-12.5 (x-1.5)^2 \bigg) > ln(\frac{5}{8}) $

$ \Rightarrow -2(x-2)^2 + 12.5 (x-1.5)^2  - 0.47 > 0 $

$ \Rightarrow -2x^2 + 8x -8 + 12.5x^2 - 37.5x + 27.655 > 0 $

$ \Rightarrow 10.5 x^2-29.5 x+19.655 > 0$

$ \Rightarrow x = 1.08625 \quad and \quad x = 1.72328 $

## Plotting the class posterior probabilities and decision boundary

[back to top]

```
%pylab inline

import numpy as np
from matplotlib import pyplot as plt

def pdf(x, mu, sigma_sqr):
    """
    Calculates the normal distribution's probability density
    function (PDF).

    """
    term1 = 1.0 / ( math.sqrt(2*np.pi * sigma_sqr))
    term2 = np.exp( (-1/2.0) * ( ((x-mu)**2 / sigma_sqr) ))
    return term1 * term2

# generating some sample data
x = np.arange(0, 50, 0.05)

def posterior(likelihood, prior):
    """
    Calculates the posterior probability (after Bayes Rule) without
    the scale factor p(x) (=evidence).

    """
    return likelihood * prior

# probability density functions
posterior1 = posterior(pdf(x, mu=2, sigma_sqr=0.25), prior=2/3.0)
posterior2 = posterior(pdf(x, mu=1.5, sigma_sqr=0.04), prior=1/3.0)

# Class conditional densities (likelihoods)
plt.plot(x, posterior1)
plt.plot(x, posterior2)
plt.title('Posterior Probabilities w. Decision Boundary')
plt.ylabel('P(w)')
plt.xlabel('random variable x')
plt.legend(['P(w_1|x)', 'p(w_2|x)'], loc='upper right')
plt.ylim([0,1])
plt.xlim([0,4])
plt.axvline(1.08625, color='r', alpha=0.8, linestyle=':', linewidth=2)
plt.axvline(1.72328, color='r', alpha=0.8, linestyle=':', linewidth=2)
plt.annotate('R1', xy=(0.5, 0.8), xytext=(0.5, 0.8))
plt.annotate('R2', xy=(1.3, 0.8), xytext=(1.3, 0.8))
plt.annotate('R1', xy=(2.3, 0.8), xytext=(2.3, 0.8))
plt.show()
```

## Classifying some random example data

[back to top]

```
# Parameters
mu_1 = 2
mu_2 = 1.5
sigma_1_sqr = 0.25
sigma_2_sqr = 0.04

# Generating 10 random samples drawn from a Normal Distribution for class 1 & 2
x1_samples = sigma_1_sqr**0.5 * np.random.randn(10) + mu_1
x2_samples = sigma_1_sqr**0.5 * np.random.randn(10) + mu_2
y = [0 for i in range(10)]

# Plotting sample data with a decision boundary

plt.scatter(x1_samples, y, marker='o', color='green', s=40, alpha=0.5)
plt.scatter(x2_samples, y, marker='^', color='blue', s=40, alpha=0.5)
plt.title('Classifying random example data from 2 classes')
plt.ylabel('P(x)')
plt.xlabel('random variable x')
plt.legend(['w_1', 'w_2'], loc='upper right')
plt.ylim([-1,1])
plt.xlim([0,4])
plt.axvline(1.08625, color='r', alpha=0.8, linestyle=':', linewidth=2)
plt.axvline(1.72328, color='r', alpha=0.8, linestyle=':', linewidth=2)
plt.annotate('R1', xy=(0.5, 0.8), xytext=(0.5, 0.8))
plt.annotate('R2', xy=(1.3, 0.8), xytext=(1.3, 0.8))
plt.annotate('R1', xy=(2.3, 0.8), xytext=(2.3, 0.8))
plt.show()
```

## Calculating the empirical error rate

[back to top]

```
w1_as_w2, w2_as_w1 = 0, 0
for x1,x2 in zip(x1_samples, x2_samples):
    if x1 > 1.08625 and x1 < 1.72328:
        w1_as_w2 += 1
    if x2 <= 1.08625 and x2 >= 1.72328:
        w2_as_w1 += 1

emp_err =  (w1_as_w2 + w2_as_w1) / float(len(x1_samples) + len(x2_samples))

print('Empirical Error: {}%'.format(emp_err * 100))
```

    Empirical Error: 20.0%


###### Material desenvolvido para o minicurso: Introdução à solução numérica de EDP's, ministrado no ERMAC/2018 de 5 a 6 de abril de 2018 na Universidade Federal de Lavras, Lavras/MG, Brasil. Autor: [Jonas Laerte Ansoni](http://jonasansoni.blogspot.com.br/).

#  Minicurso: Introdução à solução numérica de EDP's

## 1. Motivação

### 1.1. Problemas de interesse

A matemática tem um papel importante na relação entre o homem e natureza, pois através dessa ciência o homem consegue descrever o comportamento de alguns sistemas ou fenômenos da vida real em termos matemáticos.

Ex.: economia, engenharias em geral, ciências biológicas, etc...

A maioria dessas formulações matemáticas para esses fenômenos conduzem a taxas de variação de duas ou mais variáveis independentes, como por exemplo: tempo, comprimento, temperatura, velocidade, etc...

Assim, a maioria dessas formulações conduzem à equações diferenciais parciais (EDP's).

#####  Exemplos de problemas de interesse.

#### _Pergunta:_  Como podemos obter a solução de problemas modelados por essas equações??

#### Resposta: Fundamentalmente existem três abordagens que podem ser utilizadas independentemente ou conjuntamente: Experimental, analítica e computacional.

* **Abordagem experimental:** Um modelo físico é construído de forma a possibilitar os estudos e análises de medição dos parâmetros determinantes do problema. Apresentam a capacidade de produzir as mais realísticas respostas, no entanto, seu custo é elevado, problemas de escala, custo operacional e muitas vezes é difícil a medição dos parâmetros desejados.

* **Abordagem analítica:** Utiliza técnicas matemáticas disponíveis para apresentar uma solução para o problema, mas na maioria dos casos isso não é possível devido a limitações nas técnicas disponíveis. As dificudades surgem devido por exemplo: complexidade da região, coeficientes da equação diferencial podem variar ponto a ponto e até mesmo depender da própria solução (problemas não-lineares).

* **Abordagem numérica:** Tem atuado como uma importante ferramenta na ausência de soluções analíticas. Não há restrição à linearidade, podem ser aplicados a geometrias e processos complicados e apresentam evolução temporal. No entanto, simplificações são realizadas, de modo a proporcionar a elaboração de um modelo computacional consistente a ser resolvido através de métodos numéricos. Atenção à erros de truncamento, prescrição das condições de fronteira apropriadas e aos custos computacionais.

   __Observação:__  O uso de técnicas numéricas não implica que as análises experimentais e teóricas estejam sendo postas de lado. É muito comum as três técnicas se complementarem durante o desenvolvimento de um projeto. Como exemplo para o caso de escoamento de fluidos é muito comum o uso das abordagens experimentais e numéricas com o intuíto de aperfeiçoar os modelos teóricos de algum fenômeno.

A idéia central dos métodos computacionais para a resolução de EDP's é a discretização do contínuo que torna finito o problema, viabilizando assim, a sua solução através de computadores. A aplicabilidade de cada algoritmo depende da situação tratada, sendo importante que o usuário conheça tanto os aspectos físicos do problema como os aspectos numéricos da metodologia adotada.

### 1.2. Métodos numéricos para solução de EDP's

Podemos dizer que a tarefa de um método numérico é resolver uma ou mais equações diferenciais, onde as derivadas existentes na equação são substituidas por expressões algébricas que envolvem a função incógnita.

Entre os vários métodos numéricos que evoluíram ao longo dos anos, as técnicas mais utilizadas são:

**Diferenças finitas**

**Elementos finitos**

**Volumes finitos**

**Nota:** Neste minicurso será abordado de maneira __introdutória__ o método das diferenças finitas em problemas de dinâmica dos fluidos.

"A **Dinâmica dos Fluidos Computacional _(Computational Fluid Dynamics - CFD)_** é a área da computação científica que estuda métodos computacionais para simulação de fenômenos que envolvem fluidos em movimento com ou sem trocas de calor" (Fortuna, 2000).

Muitos desses problemas de dinâmica dos fluidos envolvem quantidades que se conservam e que levam a certos tipos de EDP's denominadas **leis de conservação**. (Equações de Navier-Stokes - Fortuna página 245)

  Outros médotos numéricos para solução de EDP's:
  * CVFEM (_Control Volume Finite Element Method_)
  * BEM (Boundary Element Method)
  * Spectral Method...

#### Equações de Navier-Stokes

As equações de Navier-Stokes modelam os escoamento de fluidos compressíveis e incompressíveis, turbulentos e laminares. Representam a expressão matemática de princípios físicos muito familiares:

1. [Conservação de massa](https://en.wikipedia.org/wiki/Conservation_of_mass).
2. Conservação de momento (Segunda lei de Newton).
3. Conservação de energia (Primeira lei da Termodinâmica).

As equações de Navier-Stokes podem ser escritas de muitos modos dependendo das propriedades do fluido e do escoamento.

Exemplo: Em coordenadas cartesianas bidimensionais, escoamento laminar, incompressíveis e isotérmicos

\begin{equation}
\frac{\partial u}{\partial x}+\frac{\partial v}{\partial y}=0 \\
\frac{\partial u}{\partial t}+\frac{\partial u^2}{\partial x}+\frac{\partial (uv)}{\partial y}=-\frac{1}{\rho}\frac{\partial p}{\partial x}+\nu\left(\frac{\partial^2 u}{\partial x^2}+\frac{\partial^2 u}{\partial y^2}\right) \\
\frac{\partial v}{\partial t}+\frac{\partial v^2}{\partial y}+\frac{\partial (uv)}{\partial x}=-\frac{1}{\rho}\frac{\partial p}{\partial y}+\nu\left(\frac{\partial^2 v}{\partial x^2}+\frac{\partial^2 v}{\partial y^2}\right)
\end{equation}

onde $u$ e $v$ são as velocidades nas direções $x$ e $y$, respectivamente. Densidade do fluido $\rho$, viscosidade cinemática $\nu$, pressão $p$.

Primeira equação conhecida como __equação da continuidade__. Segunda e terceira equações são as __equações de momento__ para as direções $x$ e $y$.

### 1.3. Etapas para a obtenção da solução numérica de um problema de fluidos

### 1.4. Equações diferenciais parciais

Conforme já discutido, a simulação de um escoamento via técnicas de CFD envolve obter as soluções numéricas para as equações diferencias paraciais que modelam o problema físico. Para podermos aplicar técnicas computacionais a esses problemas é importante primeiro identificar as características gerais de suas equações.

As EDP's que descrevem os fenômenos de interesse da dinâmica dos fuidos computacinal podem ser classificadas em três categorias básicas:

1. Elípticas
2. Parabólicas
3. Hiperbólicas

##  Vamos inicar os exemplos!!

```python
from IPython.core.display import HTML
css_file = '../styles/custom.css'
HTML(open(css_file, "r").read())
```