Skip to content

Commit

Permalink
examples: add basic calibration of AR model written in Java
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
AldoGl committed Sep 21, 2022
1 parent 8221916 commit f8b8e30
Show file tree
Hide file tree
Showing 3 changed files with 627 additions and 0 deletions.
478 changes: 478 additions & 0 deletions examples/AR1_process_in_Java.ipynb

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions examples/models/ar_model_java/ARModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.Random;

public class ARModel {

public static void main(String[] args) {

double constant = Double.parseDouble(args[0]);
double autoregressiveParameter = Double.parseDouble(args[1]);

int nPeriods = Integer.parseInt(args[2]);
long seed = Long.parseLong(args[3]);

double[] timeSeries = new double[nPeriods];
timeSeries[0] = 0;

Random rnd = new Random();
rnd.setSeed(seed);

for (int i = 1; i < nPeriods; i++) {
timeSeries[i] = constant + autoregressiveParameter * timeSeries[i-1] + rnd.nextGaussian();
}

for (int i = 0; i < nPeriods; i++) {
System.out.println(timeSeries[i]);
}
}
}
122 changes: 122 additions & 0 deletions examples/models/ar_model_java/ar1_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import os
import subprocess

import numpy as np


def ar1_model(theta, N, rndSeed=0):
"""Autoregressive model.
A simple AR(1) model. This is a Python wrapper for an underlying
Java implementation. In order to run this code you need to first
compile the Java code via "javac ARModel.java".
Args:
theta: the two parameters of the process
N: the length of the generated time series
rndSeed: the random seed of the simulation
Returns:
the generated time series
"""

# AR(1) constant term
const = theta[0]
# AR(1) multiplicative term
mul_par = theta[1]

# the path of the Java executable
file_path = os.path.realpath(os.path.dirname(__file__))

command = (
"java -classpath "
+ file_path
+ " ARModel {} {} {} {}".format(const, mul_par, N, rndSeed)
)

res = subprocess.run(
command.split(),
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)

stdout = res.stdout

# remove first lines and last line
lines = stdout.split("\n")

# parse the result of the simulation
time_series = []
for line in lines[:-1]:

splitted_line = line.split()
time_series.append(float(splitted_line[-1]))

time_series = np.array([time_series]).T

return time_series


def ar1_model_not_random(theta, N, rndSeed=0):
"""Autoregressive model.
A simple AR(1) model. This is a Python wrapper for an underlying
Java implementation. In order to run this code you need to first
compile the Java code via "javac ARModel.java".
Args:
theta: the two parameters of the process
N: the length of the generated time series
rndSeed: the random seed of the simulation
Returns:
the generated time series
"""

# AR(1) constant term
const = theta[0]
# AR(1) multiplicative term
mul_par = theta[1]

# the path of the Java executable
file_path = os.path.realpath(os.path.dirname(__file__))

# fixed seed to zero in this
seed = 0
command = (
"java -classpath "
+ file_path
+ " ARModel {} {} {} {}".format(const, mul_par, N, rndSeed)
)

res = subprocess.run(
command.split(),
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)

stdout = res.stdout

# remove first lines and last line
lines = stdout.split("\n")

# parse the result of the simulation
time_series = []
for line in lines[:-1]:
splitted_line = line.split()
time_series.append(float(splitted_line[-1]))

time_series = np.array([time_series]).T

return time_series


if __name__ == "__main__":
results = ar1_model([0.0, 1.0], 10, 3)

print(results)
print(results.shape)

0 comments on commit f8b8e30

Please sign in to comment.