Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: add basic calibration of AR model written in Java #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)