owned this note
owned this note
Published
Linked with GitHub
# Fintech2023
## [Week4, 2023/9/27]
## [Week3, 2023/9/20]
The solution to exercise 4.5 of Cauchy inequality.
Lemma: $\sqrt{a^2+b^2}+\sqrt{c^2+d^2}\geq\sqrt{(a+c)^2+(b+d)^2}$
proof. Consider $3$ points $O=(0, 0), A=(-|a|, -|b|), B=(|c|, |d|)$ on a plane.
$\sqrt{a^2+b^2}+\sqrt{c^2+d^2}=\overline{OA}+\overline{OB}\geq\overline{AB}=\sqrt{(|a|+|c|)^2+(|b|+|d|)^2}\geq\sqrt{(a+c)^2+(b+d)^2}$.
$\sqrt{x^2+1}+\sqrt{y^2+1}+\sqrt{z^2+1}\\
\overset{\text{Lemma}}\geq\sqrt{(x+y)^2+(1+1)^2}+\sqrt{z^2+1}\\
\overset{\text{Lemma}}\geq\sqrt{(x+y+z)^2+(1+1+1)^2}\\
\overset{\text{Lemma}}\geq\sqrt{2\sqrt{9(x+y+z)^2}}=\sqrt{6(x+y+z)}$
> **Provided by b10902085 許博翔**
Another solution using Theroem 4.3 and AM-GM: ([Proof of Theorem 4.3](#[Week03,-2022/9/21]))
By Theorem 4.3, we have
$\sqrt{x^2+1}+\sqrt{y^2+1}+\sqrt{z^2+1} \geq \sqrt{(x+y+z)^2+(1+1+1)^2} = \sqrt{(x+y+z)^2+3^2}$
Then, by AM-GM,
$\sqrt{(x+y+z)^2+3^2} \geq \sqrt{ 2 \sqrt{(x+y+z)^2 \cdot 3^2} } = \sqrt{ 6(x+y+z) }$
Thus,
$\sqrt{x^2+1}+\sqrt{y^2+1}+\sqrt{z^2+1} \geq \sqrt{ 6(x+y+z) }$
> **Provided by r12922076 鄭仲語**
[toc]
# Fintech2022
## [Week13, 2022/11/23]
## [Week12, 2022/11/16]
:::spoiler **Quiz 8 詳解 想問這兩題**
1. **Over Z7, the quadratic (degree-2) polynomial p(x) has p(1) = 3, p(2) = 5, p(3) = 4. What is p(4)?**
**Ans. 0**
![](https://i.imgur.com/j3Iz7a7.jpg)
> **Provided by b08901216 Chun-Yi Kuan**
2. **Over Z7, the quadratic (degree-2) polynomial p(x) has p(1) = 3, p(2) = 5, p(3) = 4. What is the constant term of p(x)?**
**Ans. 5**
![](https://i.imgur.com/j3Iz7a7.jpg)
> *[](https:/[](https://)/)*Provided by b08901216 Chun-Yi Kuan**
![](https://i.imgur.com/BBjfvul.jpg)
![](https://i.imgur.com/ni2j8Wd.jpg)
![](https://i.imgur.com/ZEqrjdZ.jpg)
![](https://i.imgur.com/1xWqL1P.jpg) *Provided by B09303015 HSIN-CHIAO TSAI
:::
## [Week11, 2022/11/09]
## [Week10, 2022/11/02]
## [Week09, 2022/11/02]
:::spoiler **小考題**
1. 可能的計算題
```python=
import numpy as np
def add_p(a, b, p):
return (a + b) % p
def sub_p(a, b, p):
t = a - b
if t >= 0:
return t % p
else:
k = np.ceil(abs(t) / p)
t = t + k*p
return t
def mul_p(a, b, p):
return (a * b) % p
def inv_p(a, p):
for i in range(p):
if (a*i) % p == 1:
return i
def div_p(a, b, p):
a = a % p
b = inv_p(b % p, p)
return (a * b) % p
def addition(p1, p2, p):
s = div_p(sub_p(p2[1], p1[1], p), sub_p(p2[0], p1[0], p), p)
x3 = sub_p(sub_p(mul_p(s, s, p), p1[0], p), p2[0], p)
y3 = sub_p(mul_p(s, sub_p(p1[0], x3, p), p), p1[1], p)
return (x3, y3)
def doubling(p1, a, p):
s = div_p(add_p(mul_p(mul_p(p1[0], p1[0], p), 3, p), a, p), mul_p(p1[1], 2, p), p)
x3 = sub_p(sub_p(mul_p(s, s, p), p1[0], p), p1[0], p)
y3 = sub_p(mul_p(s, sub_p(p1[0], x3, p), p), p1[1], p)
return (x3, y3)
p1 = (5, 1)
p2 = (6, 3)
a = 2
p = 17
print(inv_p(a, p)) #9
print(doubling(p1, a, p)) #(6, 3.0)
print(addition(p1, p2, p)) #(10.0, 6.0)
```
> **Provided by b06204039 林有安**
:::
:::spoiler **Quiz 6 詳解**
:::info
* Which point is $B + C$ ?
Note that $C$ is the intersection point of the tangent line of $B$. It indicate that $A=2B$, or $C=-2B$. So $B+C=B+(-2B)=-B$. Recall that the inverse of a point is the reflection point over $x$-axis. So $B+C=-B=D$
* $(8, 1) + (10, 4) =$ ?
According to the figure, $(8, 1) = 6G$ and $(10, 4)=15G$, so $(8, 1) + (10, 4) = 6G + 15G = 21G = (13, 3)$
* Which point is $C - 2D$ ?
We know that $C=-2B$ and $D=-B$, so $C-2D = -2B - (-2B) = E$
* What is the inverse of $(12, 8)$?
According to the figure, $(12, 8) = 17G$ and the order = $31$ because the last emelent is $30G$. So $-17G = E - 17G = 31G - 17G = 14G = (12, 15)$
* Which point is $2B$ ?
According to the definition, $C=-2B$, $2B=-C=A$
* $(2 / 5 + 2022) \mod 7 =$ ?
First, $5^{-1} \equiv x \mod 7$ means that $5x \equiv 1 \mod 7$. It is obvious that $x=3$ as $5\times3=15 \equiv 1 \mod 7$. So
\begin{align}
2 / 5 + 2022
&\equiv 2 \times 3 + 2022
\\
&\equiv 2028
\\
&\equiv 5 \mod 7
\end{align}
* Given a point $P$ on an elliptic curve $E$. To compute $21P$ on $E$ with “double and add”, $D$ doublings and $A$ additions are required. $(D, A) =$?
$21 = (10101)_2 = 2^4 + 2^2 + 1 = (2^2)^2 + 2^2 + 1$
For $2^2$ we need $2$ doubling, and we only $1$ doubling to get $(2^2)^2$ from $2^2$, and $2$ additions. So $(D, A) = (4, 2)$
* Which is a generator of the cyclic group $G = (Z^*_{17}, × \mod 17)$?
The generator is $g=14$ because $g$ can generate all element of $G$, while other choice can only generate some elements.
\begin{align}
g^1 &= 14 \mod 17
\\
g^2 &\equiv (-3)^2 \equiv 9 \mod 17
\\
g^3 &\equiv (-3)^3 \equiv 9 \times (-3) \equiv -27 \equiv 7 \mod 17
\\
g^4 &\equiv (-3)^4 \equiv 7 \times (-3) \equiv -21 \equiv 13 \mod 17
\\
g^5 &\equiv (-3)^5 \equiv 13 \times (-3) \equiv -39 \equiv 12 \mod 17
\\
g^6 &\equiv (-3)^6 \equiv 12 \times (-3) \equiv -36 \equiv 15 \mod 17
\\
g^7 &\equiv (-3)^7 \equiv 15 \times (-3) \equiv -45 \equiv 6 \mod 17
\\
g^8 &\equiv (-3)^8 \equiv 6 \times (-3) \equiv -18 \equiv 16 \mod 17
\\
g^9 &\equiv (-3)^9 \equiv 16 \times (-3) \equiv -48 \equiv 3 \mod 17
\\
g^{10} &\equiv (-3)^{10} \equiv 3 \times (-3) \equiv -9 \equiv 8 \mod 17
\\
g^{11} &\equiv (-3)^{11} \equiv 8 \times (-3) \equiv -24 \equiv 10 \mod 17
\\
g^{12} &\equiv (-3)^{12} \equiv 10 \times (-3) \equiv -30 \equiv 4 \mod 17
\\
g^{13} &\equiv (-3)^{13} \equiv 4 \times (-3) \equiv -12 \equiv 5 \mod 17
\\
g^{14} &\equiv (-3)^{14} \equiv 5 \times (-3) \equiv -15 \equiv 2 \mod 17
\\
g^{15} &\equiv (-3)^{15} \equiv 2 \times (-3) \equiv -6 \equiv 11 \mod 17
\\
g^{16} &\equiv (-3)^{16} \equiv 11 \times (-3) \equiv -33 \equiv 1 \mod 17
\end{align}
:::
> **Provided by 黃振哲 R10946005**
## [Week08, 2022/10/26]
## [Week07, 2022/10/19]
:::spoiler **Quiz 4 詳解**
1. **按單期二元定價模型,若期初股價為1元,一季內上漲與下跌幅度皆為10%,履約價亦為1元,無風險利率為0,一季後到期的歐式賣權的理論價格應為? (四捨五入至小數點後第二位 Ex. 答案若為 0.123,則填 0.12;若為 0.1,就填 0.1 即可,不必在後面補0 )**
**Ans. 0.5**
Follow FE_Intro_NTU_CS_1018 slides page38, 39的符號:
$xu=1*(1+0.1)=1.1$ since 上漲幅度10%
$xd=1*(1-0.1)=0.9$ since 下跌幅度10%
履約價 $K=1$
無風險利率 $r=0$
將上述參數強力代入page 39最底下的公式,可以得到:
$P = \frac{exp(0*\Delta t) - 0.9}{1.1-0.9}*exp(-0*\Delta t)*(1.1-1)^+\frac{1.1 - exp(0*\Delta t)}{1.1-0.9}*exp(-0*\Delta t)*(0.9-1)^+
=max(\frac{0.1}{0.2}*0.1, 0)+max(\frac{0.1}{0.2}*(-0.1), 0)=0.05+0=0.05$
:::
> **Provided by b08901216 Chun-Yi Kuan**
## [Week06, 2022/10/12]
:::spoiler **Quiz 3 詳解 求解plz**
1. **在提供投資服務時,智能投顧最像是:(想問bc)**
a. 依靠他們的成本效益來追求主動型策略
**b. 提供相當保守的建議作為容易導入的指引**
c. 在擔任全自動財富管理經理人時不受監管
2. **假設投資組合A和B有相同的平均報酬,相同的報酬標準差,但投資組合A相較於投資組合B有較低的貝塔值(beta)。以夏普比例(Sharpe ratio)而言,投資組合A的表現:**
**Ans. 相同於投資組合B的表現**
夏普率 = (報酬率 – 無風險利率)/標準差,與beta值無關
投資組合A與B具有相同報酬及相同標準差,故具有相同的sharp ratio。
:::
> **Provided by R10521604 羅凱芸**
## [Week05, 2022/10/5]
:::spoiler **Quiz 2 詳解**
1. **DP 多選題,選正確的**
a. Once DP is sloved, you can find second best, third best, .. solution based on the known best solution
b. Once DP is solved, all the related subproblem are also solved
c. backtracking 什麼的
d. All DP problem can be visualized as a path-finding problem
2. **DP 計算 c[] 跟 s[]: p=[2,1,3,4,5,2], c1=10, rho=0**
```python
from typing import List
def stockTrading(p: List[float], c1: float, rho: float):
c = [0 for _ in range(len(p))]
s = [0 for _ in range(len(p))]
c[0] = c1
s[0] = c1 * (1 - rho) / p[0]
for i in range(1, len(p)):
c[i] = max(c[i-1], s[i-1] * p[i] * (1 - rho))
s[i] = max(s[i-1], c[i-1] * (1 - rho) / p[i])
print(c) # [10, 10, 30, 40, 50, 50]
print(s) # [5, 10, 10, 10, 10, 25]
stockTrading(
p=[2,1,3,4,5,2],
c1=10.,
rho=0.
)
```
> **Provided by r10921050 王韋翰**
3. **六面公平骰子 E(X) 和 Var(X)**
E(X) = 7/2, Var(X) = 35/12
下方的 Pre-class reading 有詳解
4. **rho=? efficient frontier is a straight line**
rho= -1 or 1
Efficient frontier 只有算上半部而已...
6. **求 w1, when rho=1,0,-1**
1, 0.8, 0.6667
:::
:::spoiler **求Pre-class reading 題目答案QQ**
**1. Example of DP for Trading**
Given p = [2 3 4 2 3 2], c1=10, Compute ci and si, i=1~6
```python
# 有可能是錯的
import numpy as np
def dp_for_trade(index, p_list, c_1, rho):
p_j = p_list[index]
if index != 0:
c_i, s_i, pre_pos = dp_for_trade(index - 1, p_list, c_1, rho)
else:
c_i, s_i, pre_pos = c_1, c_1/p_j*(1-rho), 0
c_j = max(c_i, s_i*p_j*(1-rho))
s_j = max(s_i, c_i/p_j*(1-rho))
pos = 1 if c_j != c_i else 0
pos_diff = pos - pre_pos
if pos_diff > 0:
action = "Buy"
elif pos_diff < 0:
action = "Sell"
elif index == 0:
action = "None"
else:
action = "Hold"
print(index + 1, "c:", c_j, ", s:", s_j, ", p:", p_j, ", Action at " + str(index) + ":", action)
return c_j, s_j, pos
p_list = np.array([2, 3, 4, 2, 3, 2])
c_1 = 10
rho = 0
dp_for_trade(len(p_list) - 1, p_list, c_1, rho)
```
```
1 c: 10 , s: 5.0 , p: 2 , Action at 0: None
2 c: 15.0 , s: 5.0 , p: 3 , Action at 1: Buy
3 c: 20.0 , s: 5.0 , p: 4 , Action at 2: Hold
4 c: 20.0 , s: 10.0 , p: 2 , Action at 3: Sell
5 c: 30.0 , s: 10.0 , p: 3 , Action at 4: Buy
6 c: 30.0 , s: 15.0 , p: 2 , Action at 5: Sell
(30.0, 15.0, 0)
```
> **Provided by b06204039 林有安**
**2. Random Variable**
![](https://i.imgur.com/MRJFsP8.png)
1. Given a fair dice with its face value as a discrete random variable $X$, what are $E(X)$ and $V(X)$?
六面出現機率相等,皆為 $\frac{1}{6}$,因此
$$E(X) = \sum_{i=1}^6 i \cdot \frac{1}{6} = \frac{21}{6} = 3.5$$
又 $$E(X^2) = \sum_{i=1}^6 i^2 \cdot \frac{1}{6} = \frac{91}{6}$$
因此 $V(X) = E(X^2) - E(X)^2 = \frac{91}{6} - (\frac{21}{6})^2 = \frac{105}{36} = \frac{35}{12}$
> **Provided by r10942144 林家頡**
2. Given two fair dices with their face values as two discrete random variables $X$ and $Y$, what are $E(Z)$ and $V(Z)$ if $Z = 3X + 4Y$?
from 1. we know
$$E(X) = E(Y) = \frac{21}{6}, V(X) = V(Y) = \frac{105}{36}$$
So,
$$E(Z) = E(3X + 4Y) = 3E(X) + 4E(Y) = \frac{147}{6}$$
$$V(Z) = 3^2V(X) + 4^2V(Y) = \frac{875}{12}$$
> **Provided by r10942144 林家頡**
3. Define two random variables as follows:
- X: face values [$1,2,3,4,5,6$] of a dice
- Y: face values [$0fortails,1forheads$] of an unfair coin, with $P(tails)=1/3$ and $P(heads)=2/3$.
What are $E(Z)$ and $V(Z)$ if $Z=X+2Y?$
from 1, $E(X)=\frac{7}{2}, V(X)=\frac{35}{12}$
and $E(Y)=0\cdot\frac{1}{3}+1\cdot\frac{2}{3}=\frac{2}{3}, V(Y)=E(Y^2)-E(Y)^2=\frac{6}{9}-\frac{4}{9}=\frac{2}{9}$
$$E(Z)=E(X+2Y)=E(X)+2E(Y)=\frac{7}{2}+2\cdot\frac{2}{3}=\frac{29}{6}$$
$$V(Z)=V(X+2Y)=V(X)+4V(Y)=\frac{35}{12}+4\cdot\frac{2}{9}=\frac{137}{36}$$
> **Provided by r11944035 魏湧致**
4. $Probs = [4/6, 2/18, 4/18], Values = [2, 0, 1]$
$E(Z) = 14/9, E(Z^2) = 26/9, Var(Z^2) = 0.469$
(有可能是錯的)
> **Provided by b06204039 林有安**
補充計算過程:
由 3. 已知:
$$E(Y) = \frac{2}{3}, E(Y^2) = \frac{6}{9}$$
$X >= 3$ 的機率為 $\frac{2}{3}$,故
$$\begin{aligned}
&E(Z) = \frac{2}{3} \cdot 2 + \frac{1}{3} E(Y) = \frac{14}{9} \newline
&E(Z^2) = \frac{2}{3} \cdot 2^2 + \frac{1}{3} E(Y^2) = \frac{26}{9} \newline
&Var(Z^2) = E(Z^2) - (E(Z))^2 = \frac{38}{81}
\end{aligned}$$
> **Provided by r10942144 林家頡**
5. $E(X) = 0, Var(X) = 4, E(Y) = 2, Var(Y) = 1/3$
$E(Z) = 8, Var(Z) = 124/3$
> **Provided by b06204039 林有安**
補充計算過程:
由 [uniform random variable 的期望值與變異數公式](https://zh.wikipedia.org/zh-tw/%E9%80%A3%E7%BA%8C%E5%9E%8B%E5%9D%87%E5%8B%BB%E5%88%86%E5%B8%83) 可知:
$$\begin{aligned}
&E(Y) = \frac{1+3}{2} =2 \newline
&Var(Y) = \frac{(3-1)^2}{12} = \frac{1}{3}
\end{aligned}$$
又
$$Z = 3X + 4Y$$
故
$$\begin{aligned}
&E(Z) = 3E(X) + 4E(Y) = 8 \newline
&Var(Z) = 3^2Var(X) + 4^2Var(Y) = \frac{124}{3}
\end{aligned}$$
> **Provided by r10942144 林家頡**
6. $E(Z) = E(Z|X \le \mu_x)P(X \le \mu_x) + E(Z|X > \mu_x)P(X > \mu_x) = \frac{1}{2}E(2Y) + \frac{1}{2}E(Y)=3$
$E(Z^2) = E(Z^2|X \le \mu_x)P(X \le \mu_x) + E(Z^2|X > \mu_x)P(X > \mu_x) = \frac{1}{2}E(4Y^2) + \frac{1}{2}E(Y^2) = \frac{5}{2}E(Y^2) = \frac{65}{6}$
$Var(Z^2) =E(Z^2) - E(Z)^2 = 11/6$
(有可能是錯的)
> **Provided by b06204039 林有安**
**3. Portfolio Optimize**
![](https://i.imgur.com/IhULz0N.png)
![](https://i.imgur.com/6GzCnt9.png)
1. In PO for 2 assets, when will the efficient frontier reduce to a straight line?
When $\rho_{xy} = 1$
> **Provided by r10942144 林家頡**
2. In PO for 2 assets, when will the efficient frontier reduce to a parabola?
when $-1 < \rho_{xy} < 1$
> **Provided by r10942144 林家頡**
3. In PO for 2 assets, when will the overall risk go to zero? What are the weights when this happens?
when $\rho_{xy} = -1$.
$$w_1=\frac{\sigma_2}{\sigma_1 + \sigma_2}, w_2 = \frac{\sigma_1}{\sigma_1 + \sigma_2}$$
> **Provided by r10942144 林家頡**
4. In PO for 2 assets, can you derive the general formula for minimum-variance portfolio? i.e. What is the minimum variance? What is the corresponding return and weights?
Given 2 risky assets:
$$\begin{cases} Asset\ 1:\ (\mu_1, \sigma_1) \newline Asset\ 2:\ (\mu_2, \sigma_2)\end{cases}$$
And the corresponding weight $w_1, w_2$ which satisfied:
$$w_1 + w_2 = 1 \Rightarrow w_2 = 1 -w_1$$
let $w_1 = w$, We have:
$$\begin{cases} \mu = w \mu_1 + (1 - w) \mu_2 \newline \sigma^2 = w^2 \sigma_1^2 + (1 - w)^2\sigma_2^2 + 2w(1-w)\sigma_{12}\end{cases}$$
taking derivative over $w$:
$$\begin{aligned} &\frac{\partial{\sigma^2}}{\partial{w}} = 2w \sigma_1^2 - 2(1 - w)\sigma_2^2 + 2(1-2w)\sigma_{12}= 0 \newline &\Rightarrow w = \frac{\sigma_2^2 - \sigma_{12}}{\sigma_1^2 + \sigma_2^2 - 2 \sigma_{12}} \newline \end{aligned}$$
$\begin{cases} \mu = w \mu_1 + (1 - w) \mu_2 = \frac{\mu_1 \sigma_2^2 + \mu_2 \sigma_1^2 - \sigma_{12}(\mu_1 + \mu_2)}{\sigma_1^2 + \sigma_2^2 - 2 \sigma_{12}} \newline \sigma^2 = w^2 \sigma_1^2 + (1 - w)^2\sigma_2^2 + 2w(1-w)\sigma_{12} =\frac{\sigma_1^2 \sigma_2^2 - \sigma_{12}^2}{\sigma_1^2 + \sigma_2^2 - 2\sigma_{12}} \end{cases}$
So, $\sigma^2 = \frac{\sigma_1^2 \sigma_2^2 - \sigma_{12}^2}{\sigma_1^2 + \sigma_2^2 - 2\sigma_{12}}$ is our desired minimum variance, and the corresponding return $\mu = \frac{\mu_1 \sigma_2^2 + \mu_2 \sigma_1^2 - \sigma_{12}(\mu_1 + \mu_2)}{\sigma_1^2 + \sigma_2^2 - 2 \sigma_{12}}$ and weights $w_1 = w = \frac{\sigma_2^2 - \sigma_{12}}{\sigma_1^2 + \sigma_2^2 - 2 \sigma_{12}}, w_2 = 1 - w$.
> **Provided by r10942144 林家頡**
5. Given 2 risky assets:
$$\begin{cases} Asset\ 1:\ (\mu_1 = 0.2, \sigma_1 = 0.1) \newline Asset\ 2:\ (\mu_2 = 0.3, \sigma_2 = 0.2)\end{cases}$$
Under the following conditions: $\rho_{12} = 1, 0,-1$, respectively what are the corresponding minimum variances when we achieve minimum-variance portfolio?
Ans: We know that:
$$\rho_{12} = \frac{\sigma_{12}}{\sigma_1 \sigma_2} \Rightarrow \sigma_{12} = \rho_{12} \sigma_1 \sigma_2$$
So,
$$\sigma_{12} = 0.02, 0, -0.02$$, respectively.
and the corresponding minimum variance can be computed by $$\sigma^2 = \frac{\sigma_1^2 \sigma_2^2 - \sigma_{12}^2}{\sigma_1^2 + \sigma_2^2 - 2\sigma_{12}}$$
which is equal to
$$\frac{(0.1)^2 (0.2)^2 - (0.02)^2}{(0.1)^2 + (0.2)^2 - 2(0.02)}, \frac{(0.1)^2 (0.2)^2}{(0.1)^2 + (0.2)^2}, \frac{(0.1)^2 (0.2)^2 - (-0.02)^2}{(0.1)^2 + (0.2)^2 - 2(-0.02)}$$, respectively
> **Provided by r10942144 林家頡**
6. Given 2 risky assets:
$$\begin{cases} Asset\ 1:\ (\mu_1 = 0.2, \sigma_1 = 0.1) \newline Asset\ 2:\ (\mu_2 = 0.3, \sigma_2 = 0.2)\end{cases}$$
And the correlation coefficient of these two assets is $\sigma_{12} = 0$. We want to perform portfolio optimization with investment weigthing of $w_1$ and $w_2$ for assets 1 and 2, respectively.
What are the overall $\mu$ (return) and $\sigma$ (volatility) when $w_1 = 0.4$ and $w_2 = 0.6$ ? What are the overall $\mu$, , overall $\sigma$, and $w_1$ for achieving the minimum-variance portfolio?
Ans: We know that:
$$\begin{cases} \mu = w_1 \mu_1 + w_2 \mu_2 \newline \sigma^2 = w_1^2 \sigma_1^2 + w_2^2\sigma_2^2 + 2w_1w_2\sigma_{12}\end{cases}$$
So, when $w_1 = 0.4$ and $w_2 = 0.6$
$$\begin{cases} \mu = 0.4 \cdot 0.2 + 0.6 \cdot 0.3 = 0.26 \newline \sigma^2 = (0.4)^2 (0.1)^2 +(0.6)^2(0.2)^2 = 0.016\end{cases}$$
The overall $\mu$, , overall $\sigma$, and $w_1$ for achieving the minimum-variance portfolio can be calculated using the general formula derived in 4. and sub in the corresponding mean & variance.
> **Provided by r10942144 林家頡**
:::
## [Week03, 2022/9/21]
* [Cauchy-Schwarz Inequality Theorem 4.3](https://artofproblemsolving.com/community/c2473)
Let $a_1,a_2,...,a_n$ and $b_1,b_2,...,b_n$ be real numbers.Then we have:
$\sqrt{a_1^2+b_1^2}+\sqrt{a_2^2+b_2^2}+...+\sqrt{a_n^2+b_n^2} \geq \sqrt{(a_1+a_2+...+a_n)^2+(b_1+b_2+...+b_n)^2}$
求證明
:::spoiler Proof by William 黃振哲 R10946005
:::info
\begin{align}
&\left( \sum^n_{i=1} \sqrt{a^2_i + b^2_i} \right)^2 - \sqrt{\left( \sum^n_{i=1} a_i \right)^2 + \left( \sum^n_{i=1} b_i \right)^2}^2
\\
&= \left[ \sum^n_{i=1} (a^2_i + b^2_i) + \sum_{i \neq j} \sqrt{a^2_i+b^2_i}\sqrt{a^2_j+b^2_j} \right] - \left[\sum^n_{i=1} a^2_i + \sum^n_{i=1} b^2_i + \sum_{i \neq j} (a_ia_j + b_ib_j)\right]
\\
&= \sum_{i \neq j} \sqrt{a^2_i+b^2_i}\sqrt{a^2_j+b^2_j} - \sum_{i \neq j} (a_ia_j + b_ib_j)
\\
&= \sum_{i \neq j} \sqrt{a^2_i+b^2_i}\sqrt{a^2_j+b^2_j} - (a_ia_j + b_ib_j)
\\
&\ge \sum_{i \neq j} \sqrt{(a_ia_j+b_ib_j)^2} - (a_ia_j + b_ib_j)
\\
&= \sum_{i \neq j} |a_ia_j+b_ib_j| - (a_ia_j + b_ib_j)
\\
&\ge 0
\end{align}
:::
:::spoiler **Quiz 1 詳解**
1. **Max volume of an open box: What is the maximum volume for a rectangular box (square base, no top) made from 12 square meters of cardboard?<br>Ans: 4**
假設長寬高分別為 $x,x,y$,可得限制式:$x^2 + 4xy = 12$
令lagrange multiplier 為 $\lambda$,可得 $f(x,y) = x^2y - \lambda(x^2 + 4xy -12)$
分別對 $f(x,y)$ 做$x, y$ 的偏微分,再加上原本的限制式可求得$x^2y$ 之最大值為 $4$。
不想自己解方程式的可用此:[lagrange online calculator](https://www.emathhelp.net/calculators/calculus-3/lagrange-multipliers-calculator/?f=xxy&g=xx+%2B+4xy+%3D+12)
欸這是可以免費用的嗎!
可以吧,點進去就是本題的結果了
我以為是 square base? square base 是什麼意思?底是正方形,喔喔懂了,以這題來說square base對解沒有影響~懂感謝~
> **Provided by r10942144 林家頡**
:::spoiler Proof by Roger
:::info
Proof by Roger:
$12=x^2+4xy = x^2+2xy+2xy \geq 3 \sqrt[3]{4x^4y^2} \Rightarrow x^2y \leq 4$
:::
[無腦解法](https://www.quora.com/A-rectangular-box-with-a-square-base-and-no-top-is-to-be-constructed-out-of-4m-2-of-material-What-is-the-maximum-volume-of-the-box)
---
2. **IRR comparison: 有兩個投資案如下:
十年後的總報酬是 100%
六年後的總報酬是 55%
若以年化報酬率來看,請回答下列問題:
a. 何者比較好?A 或 B?(請填 A, B 其中一個字母)<br>Ans: B
b. 好了幾個百分點?(請四捨五入至小數點以下一位)<br>Ans: 0.4**
```python
'''
Provided by b08701244 蔡鉎驊
'''
def IRR(ttlReturn, year):
return (1+ttlReturn)**(1/year) - 1
IRR_a = IRR(1, 10)
IRR_b = IRR(0.55, 6)
print(IRR_a) # 0.07177346253629313
print(IRR_b) # 0.07577624405028471
print(f"好了 {IRR_b*100 - IRR_a*100} 個百分點") # 0.40027815139915823
```
> **Provided by b08701244 蔡鉎驊**
---
3. **Sharpe ratio computation: 有一支股票過去 5 天的股價表現為 [5 6 7 6 8]。若無風險的年報酬率為 0.01,請根據這些資料來計算(以年為主的)Sharpe ratio。<br>Ans: 10.975**
```python
'''
Provided by r10921050 王韋翰, 有打錯幫註解糾正一下
'''
import math
from statistics import stdev
data = [5,6,7,6,8]
miu_0 = 0.01
daily_return = [ (data[i] - data[i-1]) / data[i-1] for i in range(1, len(data)) ]
avg = sum(daily_return) / len(daily_return)
std = stdev(daily_return)
SR = (avg - miu_0/252) / std * math.sqrt(252)
print(f"Shapr Ratio = {SR}") # Ans: 10.975
```
> **Provided by r10921050 王韋翰**
---
4. **MA computation: 某一檔股票在某 8 個營業日的股價為 [2 4 6 4 6 0 6 8],請計算其對應的 4MA。(為避免出現 nan,前三天請分別使用 1MA、2MA、3MA 來進行計算。) (答案請前後加入中括弧,每個數字使用半形空白分開,例如 [2 3 5 4 3 2 1 3]。)<br>Ans: [2 3 4 4 5 4 4 5]**
```python
import pandas as pd
def SMA(price, N):
price_series = pd.Series(price)
return price_series.rolling(N, min_periods = 1).mean()
```
> **Provided by b06204039 林有安**
---
5. **RSI computation: 有一支股票過去 9 天的股價表現為 [3 1 4 3 4 5 3 6 13]。請根據此表現來計算 RSI。 (答案介於0和100之間,請四捨五入至整數,且無須加百分比符號,例如 68。)<br>Ans: 75**
```python
import pandas as pd
def RSI(price):
price_series = pd.Series(price)
diff = price_series.diff(1)[1:]
SMAU = diff[(diff > 0)].sum() / len(diff)
SMAD = -diff[(diff < 0)].sum() / len(diff)
return SMAU, SMAD, SMAU / (SMAU + SMAD)
```
> **Provided by b06204039 林有安**
:::
## 退選集氣區
同學您好:
一、 111學年度第1學期停修申請及上傳繳交,於111年9月20至11月25日17:00開放。myNTU→課程學習→「停修課程網路申請系統」。
二、申請方式:
1.請至系統申請後,下載申請書送交任課教師與就讀系所主管,得簽章或以郵件表示同意。
2.截止前需將已核章之停修申請書,以下方式擇一繳交始辦妥停修,逾期恕無法受理:
(1)將申請書紙本送至所屬教務處。
(2)同學請將申請書、任課教師及就讀系所主管同意文件,掃描合併成一個PDF檔並於上述期間上傳至「停修課程網路申請系統」。
3.密集課程上課時間與一般課程有別,停修申請期限請依開課單位規定。
# Fintech2021
## [Week06, 2021/10/27]
### About Homework 1
繳交網址:https://www.kaggle.com/t/dd7267ee0a4b4b20a63011cef9ce9387
說明影片:https://youtu.be/gOFQJy9KMDc
### Efficient frontier of portfolio optimization
![](https://i.imgur.com/WUnryuu.png)
![](https://i.imgur.com/Vha2dFR.png)
![](https://i.imgur.com/1YDeCGX.png)
* Note:
1. X軸應該是標準差(sigma),而不是變異數(sigma^2)
2. efficient frontier 是否應該是 hyperbola 而非 parabola ? (***待補***)
3. $\sigma^2_1 +\sigma^2_2 - 2\sigma_{12}=0$ 應該是 parabola?
### Robo Advisor
課程講義:http://mirlab.org/jang/courses/finTech/slide/2021/1027roboadvisor_NTU.pdf
(***待補***)
## 11/24 - Jimmy (by R10944053)
### ECDSA Signing - 簽章
* 情境:A要傳送signed message $m$ 給B
* 參數:
* CURVE - 橢圓曲線方程式
* G - 橢圓曲線的base point
* n - G的整數order,$n \ast G=O$ (單位圓)
* 前置:
* A由 $[1, n-1]$ 挑選私鑰 $d_A$
* 公鑰 $Q_A = d_A * G$
* 步驟:
1. 計算hash value $e = HASH(m)$,這邊舉例使用SHA-1
2. $z$ 為 $e$ 最左邊 $L_n$ 位元,$L_n$ 是 $n$ 的位元長
3. 由 $[1, n-1]$ 挑出 **empheral key** $k$
4. 計算曲線上的點 $(x_1, y_1) = k * G$
5. $r = X_1$ mod $n$, if $r = 0$, go back to step 3
6. $s = k^-1(z + rd_A)$ mod $n$, if $s = 0$, go back to step 3
7. $(r, s)$ 為所求
* 註記:
* step 4所花時間最多,$\because k$ 很大
* bit coin使用的是SHA-2 (256 bits),SHA-1 (160 bits)
* 小寫字母代表int、大寫字母代表橢圓曲線上的點
* 結論:
* $n$, $r$, $s$ 皆為256 bits,$\therefore$ 簽章結果為橢圓曲線位元的**兩倍**
* attacker不知道$k, d_A$
### ECDSA Verification - 驗章
* 簽章通常在運算能力較低的小晶片上run,而驗章通常在處理速度較快的server上run,較不需要protect
* focus在驗證過程 $u_1 * G + u_2 * Q_A$ 如何計算得更快
### Ephemeral Key
* 特性
* entropy - 亂度要夠亂
* secrecy - 不可公開
* uniqueness - 只能使用一次
* 說明
* 三個特性其一不滿足,私鑰就會被破解
* entropy不滿足,將可能導致uniqueness不滿足
* uniqueness
* 如果此特性不滿足,$r$ 可能會一樣,attacker可以解step 6的聯立方程式將私鑰算出
### RNG
* SONY的PlayStation 3遊戲用常數作為$k$,導致盜版也能和正版一樣具有signature
* Java class中的Secure Random亂度不夠,產生相同 $k$ (collision),導致bitcoin wallet私鑰被破解
* Solution - RFC 6979,$k$ 的產生過程,輸入加入私鑰以及訊息
### Simple Power Analysis
* #### Side-Channel Attack
* 原理:當所作運算以及運算值不同時,電量消耗也會有所不同
* 作法:透過位元1 (1 add + 1 multi) 和 位元0 (1 add),可以直接在示波器上讀出有幾個D and A
* #### Extraction from Mobile Devices
* 作法:利用線圈貼在手機後面,搜集電磁波變化,利用類似side-channel原理將絲要算出
* #### Acoustic SCA
* 作法:麥克風對準筆電的散熱口,搜集散熱的聲音。分析聲紋,判斷目前是位元0或是1
* 註記:
* 破解的是RSA-4096,4096版本所需要的運算量較大,所以比較好抓取聲音
* bitcoin沒有使用RSA,但RSA市占率其實最大,大都為銀行使用
* 銀行大多使用RSA-1024, RSA-2048
* #### Solution
* 原理:讓位元0和1所用到的加法、乘法運算數目相同 -> 遇到位元0也做加法
* 作法:加入一個變數*trash*,用來接空轉加法運算過後的值
* 結果:
* 可以抵擋simple power analysis
* 無法抵擋fault injection,因為如果雷射打到的是trash,trash出錯結果仍不會出錯,那就可以知道該位元是0
### Fault Injection
* #### 原理
* 此攻擊透過一些方法,使運算發生錯誤,例如用雷射去打某個位元
* #### Montgomery Ladder
* 和simple power solution類似,但不產生trash值
* $R_1, R_2$ 一定要交互儲存
* 最後回傳 $R_1$