2024年10月8日 星期二

描述常態分佈的特徵: 偏度與峰度




對於常態分佈(可以Histogram 畫下來) ,除了mu (mean), sigma (variance), 還可以用 Skwness 及kurtosis 來描述, 此亦可以作為特徵。

Skwness: 描述圖形是對稱分佈嗎? 還是左偏分佈? 或右偏分佈分佈? 指大部份的數值落在平均數的左邊或右邊







正偏態分配(positive skewness):數值較集中在低分部分,即平均數左邊的數值較多,表示考低分的人較多;負偏態分配(negative skewness):其數值較集中在平均數的右邊,表示考高分的人較多


偏度 k3k_3 的計算公式:

Skewness=E[(Xμ)3]σ3\text{Skewness} = \frac{\mathbb{E}[(X - \mu)^3]}{\sigma^3}
  • 如果 k3>  0,分佈為正偏(右偏),表示數據右側異常值很多大於均值, 3次方後正更多。
  • 如果 k3 < 0,分佈為負偏(左偏),表示數據左側異常值很多小於均值, 3次方後負更多。

峰度(Kurtosis)決定數值分布的同質性與異質性,越接近高狹峰表示越同質,越趨向低闊峰表示越異質。



峰度 (Kurtosis) 的計算公式是:

Kurtosis

=1ni=1n(Xiμσ)4\text{Kurtosis} = \frac{1}{n} \sum_{i=1}^{n} \left(\frac{X_i - \mu}{\sigma}\right)^4


 標準常態分佈的峰度為 3.  為使標準常態分佈的峰度為 0,故會將原始峰度減去 3,從而更方便地比較其他分佈的峰度。也因此為峰度會有負值的原因。

數據分佈的尾部厚度愈厚,表示有較多異常值(離峰較遠者),故按公式計算其峰值會較大。
 







2024年10月5日 星期六

多變量常態分佈 Normal Distribution

Multinormal Distribution (多態性常態分佈)


當K=1 時, 即是單維的常態分佈 f(x)


若 u=0, covariane matrix= I  
當K=2 時, 為雙變數的常態分佈 PDF f(x,y)










2024年9月25日 星期三

觀察資料的趨勢



EDA 中觀察資料的趨勢

針對Time Series Data 通常會觀察原始資料的趨勢(Trend), 季節性/週期性(Seasonality)的狀態或資料中的雜訊.

但這些數據是怎麼取得的呢? 
  1. 趨勢線: 用原始資料的移動平均值(MA)作為長期走勢的觀察. (移動窗口可以自己設定) 
  2. 季節性(Seaonality): 原始資料減去趨勢線
  3. 雜訊(Residual) :   原始資料減去趨勢線再減去週期性線, 剩下的值認定是噪訊(隨機波動)或短期的變化。

可以自己用Python 完成或者利用 statsmodels 工具來完成。 statsmodels 本身提供許多不同統計模型和統計資料探索的函數。


另外, 也可以計算ACF ( Autocorrelation)  , 自相關係數為原始資料 Xt 和 X的lagged  Xt+k計算相關係數,ACF 大小可以幫助我們了解數據中週期性的狀態。當lagged 越大 (即K愈大),ACF 通常會逐漸減少,特別是對於沒有長期相關性的數據而言。


$$\rho_k = \frac{\sum_{t=k+1}^{n} (X_t - \bar{X})(X_{t-k} - \bar{X})}{\sum_{t=1}^{n} (X_t - \bar{X})^2}$$

Note:$ \rho_0=1 $ 

繪製出ACF圖, 若每隔 $i$ 之後都能看到這個峰值, 則可能存週期 $i$。序列資料可能有存在一個以上的週期,如每週會有一個週期, 而每季也有一個週期。 






ACF圖( X軸為不同的lag k, Y軸為ACF值)

底下ACF圖說明序列資料沒有存在週期性.


--------------

具有週期性的資料
 seasonal_pattern = 10 * np.sin(2 * np.pi * time / 12
 trend = 0.1 * time 
 noise = np.random.normal(0, 1, size=100
 data = seasonal_pattern + trend + noise






ACF圖( X軸為不同的lag k, Y軸為ACF值)

底下ACF圖說明序列資料存在一個週期性, 在Lag n*K 處都有差不多的峰值, 表示其週期性為 k


2024年9月21日 星期六

what is Botnet ?

 

A botnet is a network of compromised computers or devices, often referred to as "bots" or "zombies," that are controlled remotely by a malicious actor (known as a "botmaster"). These devices are typically infected with malware, allowing the botmaster to execute various commands on them without the device owner’s knowledge.

Here are some common uses and dangers of botnets:

  1. Distributed Denial of Service (DDoS) Attacks: Botnets are often used to flood a target server or website with traffic, overwhelming it and causing it to crash or become unavailable to users.

  2. Spam Distribution: They can be used to send out massive amounts of spam emails or phishing messages, which can lead to further infections or fraud.

  3. Credential Stuffing: Botnets may attempt to use stolen usernames and passwords on different sites, automating the process to try many combinations quickly.



2024年9月18日 星期三

最大概似估計 (MLE) 與 GLL

 




看到 y1 情況下 , 機率分佈其參數會是 $ \theta_1 $ 的可能性高於$ \theta_2 $.

可能性的意義亦等同於  $ P( y_1 \mid \theta_1 ) > P( y_1 \mid \theta_2 ) $ , 

但如何找到最好的$ \theta $ 使其可能性達到最大,即機率 $ P( y_1 \mid \theta )$ 能達到最大, 此即為 最大概似估計 (Maximum Likelihood Estimation) 的目標。

若此機率模型為高斯分佈, 即什麼樣的參數 $(\mu, \sigma^2)$ , 能使 $ P(y_1 \mid \mu, \sigma^2)$ 最大.

若你的機器學習模型是預測一個高斯分佈的參數  $(\mu, \sigma^2)$

$$X \sim \mathcal{N}(\mu, \sigma^2)$$

$$f(x) = \frac{1}{\sigma \sqrt{2\pi}} e^{ - \frac{(x - \mu)^2}{2 \sigma^2} }$$

則你可以將對應答案 $y_1$ 丟入得出其機率,即 $$ f ( y_1 \mid \mu, \sigma^2) $$

若預測的參數夠好的話,則$f(y_1\mid \mu, \sigma^2)$ 應該要很大,若取Log 改以$$X \sim \text{Log-N}(\mu, \sigma^2)$$ 表示,值也會是大的。

$$log(f(y_1\mid \mu, \sigma^2)) = -\frac{1}{2} \log(2\pi) -\frac{1}{2}  \log(\sigma^2) - \frac{(y_1 - \mu)^2}{2 \sigma^2}$$










 若預測多組 $(\mu_1, \sigma_1^2), (\mu_2, \sigma_2^2),...,(\mu_n, \sigma_n^2)$

效能可以直接加總 $$f(y_1\mid \mu_1, \sigma_1^2) +f(y_2\mid \mu_2, \sigma_2^2)+...+f(y_n\mid \mu_n, \sigma_n^n)$$  , 或取log 值相加也可以。

 $log(f(y_1\mid \mu_1, \sigma_1^2)) +log(f(y_2\mid \mu_2, \sigma_2^2))+...+log(f(y_n\mid \mu_n, \sigma_n^n))$   ---> 這就是 GLL (Gaussian Log Likelihood)

a^2))$$






如何同時衡量模型的正確性與長期穩定性?

 

如何同時衡量模型的正確性與長期穩定性?

Home Credit - Credit Risk Model Stability

Create a model measured against feature stability over time

https://www.kaggle.com/competitions/home-credit-credit-risk-model-stability/overview


gini = 2 * AUC - 1   (AUC:0~1)

stability metric = mean(gini) + 88.0 * min(0, a) - 0.5 * std(residuals)

針對預測能力呈現下滑趨勢給予懲罰 :即 a < 0 的情況

穏定性評估: 計算 residuals 的標準差, 愈小表示模型愈穏定. 最好是 0



[References ]


Understanding ROC and AUC 

2024年8月3日 星期六

iPAS 機器學習工程師參考樣題解說




更多補充說明 :

1.) 神經網路加入Embedding 層的用意? 
將輸入的 sparse vector 轉換成高維度的 dense vector,如此的 dense vector可能可以表現資料更多元的特徵。

2.) 模型效能評估為何使用RMSLE (Root Mean Square Logarithmic Error)  而不是MSE  ? 

因為當資料數據 y 本身差異很大時, 決定MSE 的大小其實都是由數值很大的 y 所主導, 對於數值很小的 y 就顯得微不足道。  故若想要觀察每一筆資料預測的誤差不要被某一筆數量級較大的y所主導,可以使用RMSLE.


因為RMSLE 先取 log  再計算MSE.。 由於取log 會使數值 scale 變小, 因此即使存在某些 y 很大, 對MSE 的影響也會相對變小。

另外,也可以用MAPE, 尤其當每筆資料數據 y 都是很大的情況,用MSE 計算都將超大,故用平均絕對百分比誤差反而是一個比較能看出預測和標準答案的誤差率。

 



Read More: