diff --git a/arma.egg-info/PKG-INFO b/arma.egg-info/PKG-INFO index fbcee54..7624487 100644 --- a/arma.egg-info/PKG-INFO +++ b/arma.egg-info/PKG-INFO @@ -1,5 +1,5 @@ Metadata-Version: 2.1 Name: arma -Version: 1.0.0 +Version: 1.0.3 Summary: ARMA, AR amd MA model process Author: Peter_Cheng diff --git a/arma.egg-info/SOURCES.txt b/arma.egg-info/SOURCES.txt index 0b343da..440b8a1 100644 --- a/arma.egg-info/SOURCES.txt +++ b/arma.egg-info/SOURCES.txt @@ -1,3 +1,4 @@ +README.md setup.py arma/__init__.py arma/arma.py diff --git a/arma/__pycache__/__init__.cpython-311.pyc b/arma/__pycache__/__init__.cpython-311.pyc index 7c4a13e..d6475b0 100644 Binary files a/arma/__pycache__/__init__.cpython-311.pyc and b/arma/__pycache__/__init__.cpython-311.pyc differ diff --git a/arma/__pycache__/arma.cpython-311.pyc b/arma/__pycache__/arma.cpython-311.pyc index e31b560..3aca34a 100644 Binary files a/arma/__pycache__/arma.cpython-311.pyc and b/arma/__pycache__/arma.cpython-311.pyc differ diff --git a/arma/arma.py b/arma/arma.py index 403ffa4..3320827 100644 --- a/arma/arma.py +++ b/arma/arma.py @@ -4,6 +4,19 @@ class ARMA: def __init__(self, p:int, q:int, phi:list, theta:list, c:float, mu:float, sigma:float): + ''' + p: AR 的階次 + q: MA 的階次 + phi: AR 的係數 + theta: MA 的係數 + c: 常數項 + -------------------- + 常態分佈的進行抽樣生成隨機誤差 N(mu, sigma) + mu: 隨機誤差的平均值 + sigma: 隨機誤差的標準差 + ex: ARMA(1, 1, [0.5], [0.5], 0, 0, 1) + # ARMA(1, 1) process 係數設定 phi=0.5, theta=0.5, c=0, mu=0, sigma=1 + ''' if not isinstance(p, int) or not isinstance(q, int): raise ValueError("p and q must be integers") if len(phi) != p: @@ -32,8 +45,12 @@ def plot(self): def save(self, file_name): return self._save(file_name) + def covariance(self,max_lag): + return self._covariance(max_lag) + + + - ##priveate function def _simulate(self, num_samples): X = np.zeros(num_samples) @@ -76,31 +93,100 @@ def _save(self, file_name): raise ValueError("file_name must end with .xlsx or .txt or .csv") return f"File saved as {file_name}" + def _covariance(arma, max_lag): + covariances = np.zeros(max_lag+1) + for k in range(max_lag+1): + if k > arma.q: + covariance = 0 + else: + theta_padded = np.append(arma.theta, np.zeros(k)) # Pad theta with zeros for lag + covariance = arma.sigma**2 * np.sum(theta_padded[k:arma.q+k] * arma.theta[:arma.q]) + + # Add the contribution from the AR part + if k <= arma.p: + phi_padded = np.append(arma.phi, np.zeros(k)) # Pad phi with zeros for lag + covariance += np.sum(phi_padded[k:arma.p+k] * arma.phi[:arma.p]) + covariances[k] = covariance + print(f'共變異數(lag {k}):{covariances[k]:.4f}') + + + return covariances + +####AR class AR(ARMA): + """ + AR(p,phi, c, mu,sigma) process + p : AR 的階次 + phi : AR 的係數 + c : 常數項 + -------------------- + 常態分佈的進行抽樣生成隨機誤差 N(mu, sigma) + mu : 隨機誤差的平均值 + sigma : 隨機誤差的標準差 + ex: AR(1, [0.5], 0, 0, 1) + # AR(1) process 係數設定 phi=0.5, c=0, mu=0, sigma=1 + """ def __init__(self, p, phi, c, mu, sigma): super().__init__(p, 0, phi, [], c, mu, sigma) # 調用父類的構造方法,MA部分設為空 def simulate(self, num_samples=1): return self._simulate(num_samples) def statistics(self): return self._statistics() + + # def plot(self): + # return self._plot() + def plot(self): - return self._plot() + plt.style.use('ggplot') + plt.figure(figsize=(10, 4)) + plt.plot(self.memory, marker='o') + plt.title(f"AR({self.p}) Process") + plt.xlabel("Period") + plt.ylabel("Value") + plt.show() + def save(self, file_name): return self._save(file_name) + def covariance(self,max_lag): + return self._covariance(max_lag) + - - +###MA class MA(ARMA): + """ + MA(q,theta, c, mu,sigma) process + q : MA 的階次 + theta : MA 的係數 + c : 常數項 + -------------------- + 常態分佈的進行抽樣生成隨機誤差 N(mu, sigma) + mu : 隨機誤差的平均值 + sigma : 隨機誤差的標準差 + ex: MA(1, [0.5], 0, 0, 1) + # MA(1) process 係數設定 theta=0.5, c=0, mu=0, sigma=1 + """ def __init__(self, q, theta, mu, c,sigma): super().__init__(0, q, [], theta, c, mu, sigma) def simulate(self, num_samples=1): return self._simulate(num_samples) def statistics(self): return self._statistics() + + # def plot(self): + # return self._plot() + def plot(self): - return self._plot() + plt.style.use('ggplot') + plt.figure(figsize=(10, 4)) + plt.plot(self.memory, marker='o') + plt.title(f"MA({self.q}) Process") + plt.xlabel("Period") + plt.ylabel("Value") + plt.show() def save(self, file_name): return self._save(file_name) + def covariance(self,max_lag): + return self._covariance(max_lag) \ No newline at end of file diff --git a/build/lib/arma/arma.py b/build/lib/arma/arma.py index 403ffa4..3320827 100644 --- a/build/lib/arma/arma.py +++ b/build/lib/arma/arma.py @@ -4,6 +4,19 @@ class ARMA: def __init__(self, p:int, q:int, phi:list, theta:list, c:float, mu:float, sigma:float): + ''' + p: AR 的階次 + q: MA 的階次 + phi: AR 的係數 + theta: MA 的係數 + c: 常數項 + -------------------- + 常態分佈的進行抽樣生成隨機誤差 N(mu, sigma) + mu: 隨機誤差的平均值 + sigma: 隨機誤差的標準差 + ex: ARMA(1, 1, [0.5], [0.5], 0, 0, 1) + # ARMA(1, 1) process 係數設定 phi=0.5, theta=0.5, c=0, mu=0, sigma=1 + ''' if not isinstance(p, int) or not isinstance(q, int): raise ValueError("p and q must be integers") if len(phi) != p: @@ -32,8 +45,12 @@ def plot(self): def save(self, file_name): return self._save(file_name) + def covariance(self,max_lag): + return self._covariance(max_lag) + + + - ##priveate function def _simulate(self, num_samples): X = np.zeros(num_samples) @@ -76,31 +93,100 @@ def _save(self, file_name): raise ValueError("file_name must end with .xlsx or .txt or .csv") return f"File saved as {file_name}" + def _covariance(arma, max_lag): + covariances = np.zeros(max_lag+1) + for k in range(max_lag+1): + if k > arma.q: + covariance = 0 + else: + theta_padded = np.append(arma.theta, np.zeros(k)) # Pad theta with zeros for lag + covariance = arma.sigma**2 * np.sum(theta_padded[k:arma.q+k] * arma.theta[:arma.q]) + + # Add the contribution from the AR part + if k <= arma.p: + phi_padded = np.append(arma.phi, np.zeros(k)) # Pad phi with zeros for lag + covariance += np.sum(phi_padded[k:arma.p+k] * arma.phi[:arma.p]) + covariances[k] = covariance + print(f'共變異數(lag {k}):{covariances[k]:.4f}') + + + return covariances + +####AR class AR(ARMA): + """ + AR(p,phi, c, mu,sigma) process + p : AR 的階次 + phi : AR 的係數 + c : 常數項 + -------------------- + 常態分佈的進行抽樣生成隨機誤差 N(mu, sigma) + mu : 隨機誤差的平均值 + sigma : 隨機誤差的標準差 + ex: AR(1, [0.5], 0, 0, 1) + # AR(1) process 係數設定 phi=0.5, c=0, mu=0, sigma=1 + """ def __init__(self, p, phi, c, mu, sigma): super().__init__(p, 0, phi, [], c, mu, sigma) # 調用父類的構造方法,MA部分設為空 def simulate(self, num_samples=1): return self._simulate(num_samples) def statistics(self): return self._statistics() + + # def plot(self): + # return self._plot() + def plot(self): - return self._plot() + plt.style.use('ggplot') + plt.figure(figsize=(10, 4)) + plt.plot(self.memory, marker='o') + plt.title(f"AR({self.p}) Process") + plt.xlabel("Period") + plt.ylabel("Value") + plt.show() + def save(self, file_name): return self._save(file_name) + def covariance(self,max_lag): + return self._covariance(max_lag) + - - +###MA class MA(ARMA): + """ + MA(q,theta, c, mu,sigma) process + q : MA 的階次 + theta : MA 的係數 + c : 常數項 + -------------------- + 常態分佈的進行抽樣生成隨機誤差 N(mu, sigma) + mu : 隨機誤差的平均值 + sigma : 隨機誤差的標準差 + ex: MA(1, [0.5], 0, 0, 1) + # MA(1) process 係數設定 theta=0.5, c=0, mu=0, sigma=1 + """ def __init__(self, q, theta, mu, c,sigma): super().__init__(0, q, [], theta, c, mu, sigma) def simulate(self, num_samples=1): return self._simulate(num_samples) def statistics(self): return self._statistics() + + # def plot(self): + # return self._plot() + def plot(self): - return self._plot() + plt.style.use('ggplot') + plt.figure(figsize=(10, 4)) + plt.plot(self.memory, marker='o') + plt.title(f"MA({self.q}) Process") + plt.xlabel("Period") + plt.ylabel("Value") + plt.show() def save(self, file_name): return self._save(file_name) + def covariance(self,max_lag): + return self._covariance(max_lag) \ No newline at end of file diff --git a/dist/arma-1.0.3-py3-none-any.whl b/dist/arma-1.0.3-py3-none-any.whl new file mode 100644 index 0000000..9f2a598 Binary files /dev/null and b/dist/arma-1.0.3-py3-none-any.whl differ diff --git a/dist/arma-1.0.3.tar.gz b/dist/arma-1.0.3.tar.gz new file mode 100644 index 0000000..11494bf Binary files /dev/null and b/dist/arma-1.0.3.tar.gz differ diff --git a/file_name=arma_model.csv b/file_name=arma_model.csv new file mode 100644 index 0000000..cc19612 --- /dev/null +++ b/file_name=arma_model.csv @@ -0,0 +1,22 @@ +0 +3.1899210099327746 +3.789687398455347 +3.562788655308019 +2.7198282727217 +2.2734373599966227 +3.314763502530126 +3.3618291920854193 +2.411443170352059 +2.0569450953029156 +0.6436660274221947 +3.3751642110778004 +2.621653486511918 +1.8234711457067176 +2.6057025080104284 +3.0562586409286823 +2.346018988966077 +2.198230516152887 +1.9029125879564355 +1.3133840074780667 +0.09505671985988684 +0.0 diff --git a/setup.py b/setup.py index baabbfb..809366c 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='arma', - version='1.0.2', + version='1.0.3', description=' ARMA, AR amd MA model process', author='Peter_Cheng', packages=find_packages(),