Skip to content

Commit

Permalink
v1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
chengpeter88 committed Jan 17, 2024
1 parent 113995c commit 876ea12
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 12 deletions.
2 changes: 1 addition & 1 deletion arma.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions arma.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
README.md
setup.py
arma/__init__.py
arma/arma.py
Expand Down
Binary file modified arma/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file modified arma/__pycache__/arma.cpython-311.pyc
Binary file not shown.
96 changes: 91 additions & 5 deletions arma/arma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

96 changes: 91 additions & 5 deletions build/lib/arma/arma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Binary file added dist/arma-1.0.3-py3-none-any.whl
Binary file not shown.
Binary file added dist/arma-1.0.3.tar.gz
Binary file not shown.
22 changes: 22 additions & 0 deletions file_name=arma_model.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 876ea12

Please sign in to comment.