# Load the required library
library(tseriesChaos)

# Read the CSV file
x <- read.csv("C:/Users/whoami/Machine Learning/Training/eu-west-1.csv")

# Convert date_hour column to POSIXct format
x$date_hour <- as.POSIXct(x$date_hour, format = "%Y-%m-%d %H:%M:%S")

# Calculate the neighbourhood diameter
eps <- sd(x$price) / 10

# Create a new dataframe with only the price column
price_data <- x$price

# Take the first 2000 elements of the price data
price_data_subset <- price_data[1:2000]

# Define parameters
m_max <- 10       # Maximum embedding dimension to explore
d <- 18            # Tentative time delay
tw <- 100          # Theiler window
rt <- 10           # Escape factor

# Calculate false nearest neighbors for the price subset
fn <- false.nearest(price_data_subset, m_max, d, tw, rt, eps)

# Print the result
print(fn)

# Plot the result
plot(fn)

library(scatterplot3d)


# Calculate the AMI

# Calculate average mutual information for time delay selection
lm <- 120  # Largest lag
ami_result <- mutual(price_data_subset, lag.max = lm)

# Print the AMI result
print(ami_result)

# Find the first minimum of the AMI
estimated_d <- which.min(ami_result)

# Print the estimated time delay
cat("Estimated Time Delay (d):", estimated_d, "\n")

# Choose embedding dimension (m) and time delay (d)
m <- 3  # Embedding dimension
d <- estimated_d  # Choose the estimated time delay

# Embed the 'observed' series
xyz <- embedd(price_data_subset, m, d)

# Create a scatterplot in 3D
windows()
scatterplot3d(xyz, type = "l")


# Load the required libraries
library(tseriesChaos)
library(gplots)
library(scatterplot3d)

# Set parameters for the Lorenz system
a <- 10
b <- 28
c <- -8/3
x0 <- 1
y0 <- 1
z0 <- 1
t_init <- 0
t_fin <- 1000
step.int <- 0.01

# Simulate the Lorenz system
lorenz.syst <- function(t, x, parms) {
  dx <- a * (x[2] - x[1])
  dy <- x[1] * (c - x[3]) - x[2]
  dz <- x[1] * x[2] - b * x[3]
  return(list(c(dx, dy, dz)))
}

times <- seq(t_init, t_fin, by = step.int)
xyz <- sim.cont(lorenz.syst, start = t_init, end = t_fin, dt = step.int, 
                start.x = c(x0, y0, z0), parms = c(a, b, c))  # Adjust these values as needed

# Create a scatterplot in 3D
scatterplot3d(xyz[, 1], xyz[, 2], xyz[, 3], type = "l", xlim = c(-30, 30),
              cex.lab = 1.4, cex.axis = 1.2)



# Assuming you have processed price_data and have a two-dimensional matrix price_matrix
# Calculate the two-dimensional histogram for price_data
h2d <- hist2d(price_matrix, show = FALSE, same.scale = FALSE, nbins = 100)
h2d$counts <- h2d$counts / max(h2d$counts) # normalization

# Plot the invariant density
par(mai = c(1.02, 1., 0.82, 0.42) + 0.1, cex.axis = 1.2, cex.lab = 1.6)
filled.contour(h2d$x, h2d$y, h2d$counts, col = gray.colors(10, start = 0, end = 1), nlevels = 10,
               xlab = "Price", ylab = "Density", main = "", xlim = c(min(price_data), max(price_data)),
               ylim = c(0, 1), las = 0, key.axes = axis(4, las = 1))

+ }
> times <- seq(t_init, t_fin, by = step.int)
> xyz <- sim.cont(lorenz.syst, start = t_init, end = t_fin, dt = step.int, 
+                 start.x = c(x0, y0, z0), parms = c(a, b, c))  # Adjust these values as needed
DLSODA-  At T (=R1), too much accuracy requested  
      for precision of machine..  See TOLSF (=R2) 
In above message, R1 = 126.892, R2 = nan
 
Warning messages:
1: In lsoda(start.x, times, func = syst, parms = parms) :
  Excessive precision requested.  scale up `rtol' and `atol' e.g by the factor 10
2: In lsoda(start.x, times, func = syst, parms = parms) :
  Returning early. Results are accurate, as far as they go
> # Create a scatterplot in 3D
> scatterplot3d(xyz[, 1], xyz[, 2], xyz[, 3], type = "l", xlim = c(-30, 30),
+               cex.lab = 1.4, cex.axis = 1.2)
Error in `[.default`(xyz, , 1) : incorrect number of dimensions