kimpro82 / MyInvestmentModules

Someday these will be an integrated trading system, but not now. So I am sad.
1 stars 1 forks source link

R : Simulation to Compare Arithmetic Mean and Geometrical Mean #30

Closed kimpro82 closed 1 year ago

kimpro82 commented 1 year ago

Here is some R code that demonstrates how to compare the arithmetic mean and geometric mean using Monte Carlo simulation:

# Set the number of simulations
n_simulations <- 10000

# Set the sample size
sample_size <- 10

# Set the range of values for the random sample
sample_range <- c(1, 10)

# Initialize vectors to store the results of the simulations
arithmetic_mean <- numeric(n_simulations)
geometric_mean <- numeric(n_simulations)

# Run the simulations
for (i in 1:n_simulations) {
  # Generate a random sample
  sample <- runif(sample_size, min = sample_range[1], max = sample_range[2])

  # Calculate the arithmetic mean of the sample
  arithmetic_mean[i] <- mean(sample)

  # Calculate the geometric mean of the sample
  geometric_mean[i] <- exp(mean(log(sample)))
}

# Calculate the mean and standard deviation of the arithmetic means
arithmetic_mean_mean <- mean(arithmetic_mean)
arithmetic_mean_sd <- sd(arithmetic_mean)

# Calculate the mean and standard deviation of the geometric means
geometric_mean_mean <- mean(geometric_mean)
geometric_mean_sd <- sd(geometric_mean)

# Print the results
print(paste("Arithmetic mean:", arithmetic_mean_mean, "±", arithmetic_mean_sd))
print(paste("Geometric mean:", geometric_mean_mean, "±", geometric_mean_sd))

This code will generate n_simulations random samples of size sample_size from a uniform distribution with a range of sample_range, and calculate the arithmetic and geometric means of each sample. It will then calculate the mean and standard deviation of the arithmetic means and the mean and standard deviation of the geometric means, and print the results.

I hope this helps! Let me know if you have any questions.

kimpro82 commented 1 year ago

Q. show cases that arithmetic mean is under 0 but geometric mean is over 0 by R code

☞ (ChatGPT) To show cases where the arithmetic mean is less than 0 but the geometric mean is greater than 0 using R code, you can use the following steps:

  1. Generate a sample of random numbers using the rnorm function in R.
  2. Calculate the arithmetic mean and geometric mean of the sample using the mean and geoMean functions, respectively.
  3. Compare the arithmetic mean and geometric mean to 0 and print the results.

Here is an example of how this can be done:

# Generate a sample of random numbers
sample <- rnorm(10, mean = -1, sd = 1)

# Calculate the arithmetic mean and geometric mean
arithmetic_mean <- mean(sample)
geometric_mean <- geoMean(sample)

# Compare the means to 0 and print the results
if (arithmetic_mean < 0 && geometric_mean > 0) {
  print("Arithmetic mean is less than 0 and geometric mean is greater than 0.")
} else {
  print("Arithmetic mean is not less than 0 or geometric mean is not greater than 0.")
}

This code will generate a sample of 10 random numbers with a mean of -1 and a standard deviation of 1, calculate the arithmetic mean and geometric mean of the sample, and print a message indicating whether the arithmetic mean is less than 0 and the geometric mean is greater than 0.

Note that the geometric mean will only be defined if all the values in the sample are greater than 0, since the geometric mean is the nth root of the product of n values. If any of the values in the sample are negative, the geometric mean will not be defined.

kimpro82 commented 1 year ago