2024年9月25日 星期三

觀察資料的趨勢



EDA 中觀察資料的趨勢

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

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

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


另外, 也可以計算ACF ( Autocorrelation)  , 自相關係數為原始資料 Xt 和 X的lagged data Xt-k計算類似相關係數的概念, ,ACF 值 介於-1~1,其大小可以幫助我們了解數據中的趨勢和季節性模式。當lagged 越大,ACF 通常會逐漸減少,特別是對於沒有長期相關性的數據而言。

$ \rho(x_{t},x_{t-1}),  \rho(x_{t},x_{t-2}),..., \rho(x_{t},x_{t-k})$

$$\rho(x_{t},x_{t-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}$$

ACF值愈大的, 如 $ \rho(x_{t},x_{t-i}) $ 很大, 表示$x$和 lagged $i$ 後, 有存在週期性 






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








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