|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | + |
| 7 | +def ar1_model(theta, N, rndSeed=0): |
| 8 | + """Autoregressive model. |
| 9 | +
|
| 10 | + A simple AR(1) model. This is a Python wrapper for an underlying |
| 11 | + Java implementation. In order to run this code you need to first |
| 12 | + compile the Java code via "javac ARModel.java". |
| 13 | +
|
| 14 | + Args: |
| 15 | + theta: the two parameters of the process |
| 16 | + N: the length of the generated time series |
| 17 | + rndSeed: the random seed of the simulation |
| 18 | +
|
| 19 | + Returns: |
| 20 | + the generated time series |
| 21 | + """ |
| 22 | + |
| 23 | + # AR(1) constant term |
| 24 | + const = theta[0] |
| 25 | + # AR(1) multiplicative term |
| 26 | + mul_par = theta[1] |
| 27 | + |
| 28 | + # the path of the Java executable |
| 29 | + file_path = os.path.realpath(os.path.dirname(__file__)) |
| 30 | + |
| 31 | + command = "java -classpath " + file_path + " ARModel {} {} {} {}".format( |
| 32 | + const, mul_par, N, rndSeed |
| 33 | + ) |
| 34 | + |
| 35 | + res = subprocess.run( |
| 36 | + command.split(), |
| 37 | + shell=False, |
| 38 | + stdout=subprocess.PIPE, |
| 39 | + stderr=subprocess.STDOUT, |
| 40 | + text=True, |
| 41 | + ) |
| 42 | + |
| 43 | + stdout = res.stdout |
| 44 | + |
| 45 | + # remove first lines and last line |
| 46 | + lines = stdout.split("\n") |
| 47 | + |
| 48 | + # parse the result of the simulation |
| 49 | + time_series = [] |
| 50 | + for line in lines[:-1]: |
| 51 | + |
| 52 | + splitted_line = line.split() |
| 53 | + time_series.append(float(splitted_line[-1])) |
| 54 | + |
| 55 | + time_series = np.array([time_series]).T |
| 56 | + |
| 57 | + return time_series |
| 58 | + |
| 59 | + |
| 60 | +def ar1_model_not_random(theta, N, rndSeed=0): |
| 61 | + """Autoregressive model. |
| 62 | +
|
| 63 | + A simple AR(1) model. This is a Python wrapper for an underlying |
| 64 | + Java implementation. In order to run this code you need to first |
| 65 | + compile the Java code via "javac ARModel.java". |
| 66 | +
|
| 67 | + Args: |
| 68 | + theta: the two parameters of the process |
| 69 | + N: the length of the generated time series |
| 70 | + rndSeed: the random seed of the simulation |
| 71 | +
|
| 72 | + Returns: |
| 73 | + the generated time series |
| 74 | + """ |
| 75 | + |
| 76 | + # AR(1) constant term |
| 77 | + const = theta[0] |
| 78 | + # AR(1) multiplicative term |
| 79 | + mul_par = theta[1] |
| 80 | + |
| 81 | + # the path of the Java executable |
| 82 | + file_path = os.path.realpath(os.path.dirname(__file__)) |
| 83 | + |
| 84 | + # fixed seed to zero in this |
| 85 | + seed = 0 |
| 86 | + command = "java -classpath " + file_path + " ARModel {} {} {} {}".format( |
| 87 | + const, mul_par, N, rndSeed |
| 88 | + ) |
| 89 | + |
| 90 | + res = subprocess.run( |
| 91 | + command.split(), |
| 92 | + shell=False, |
| 93 | + stdout=subprocess.PIPE, |
| 94 | + stderr=subprocess.STDOUT, |
| 95 | + text=True, |
| 96 | + ) |
| 97 | + |
| 98 | + stdout = res.stdout |
| 99 | + |
| 100 | + # remove first lines and last line |
| 101 | + lines = stdout.split("\n") |
| 102 | + |
| 103 | + # parse the result of the simulation |
| 104 | + time_series = [] |
| 105 | + for line in lines[:-1]: |
| 106 | + splitted_line = line.split() |
| 107 | + time_series.append(float(splitted_line[-1])) |
| 108 | + |
| 109 | + time_series = np.array([time_series]).T |
| 110 | + |
| 111 | + return time_series |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + results = ar1_model([0., 1.], 10, 3) |
| 117 | + |
| 118 | + print(results) |
| 119 | + print(results.shape) |
0 commit comments