### UNCERTAINTY ESTIMATION FOR AN ESTIMATED PARAMETER
### RANGE AVAILABLE
### THIS CODE CAN BE USED FOR THE SYMMETRICAL OR NON-SYMMETRICAL RANGE...
### ... WITH RESPECT TO THE ESTIMATED PARAMETER

rm(list=ls())

set.seed(2)
n               <-  102
Parameter           <-  1.73        ### Parameter: Gw
x               <-  c(0.65, 2.82)   ### Lower limit and upper limit of possible values for the parameter

### Step 1: Determine the magnitude of the range

magnitude_x         <-  max(x)- min(x)  ### Range 

### Step 2: Verify if the range is symmetrical around the parameter

values.inf.Parameter    <-  (Parameter - min(x))/magnitude_x
values.sup.Parameter    <-  (max(x)- Parameter)/magnitude_x
symmetry            <-  c(values.inf.Parameter , values.sup.Parameter)
print(symmetry * 100)                   ### Percentage of values located on both sides of the parameter
## [1] 49.76959 50.23041
### Step 3: Simulate a sample of size n from a Uniform distribution U(x_min, x_max)

sample_1            <-  runif(n * values.inf.Parameter, min = min(x), max = Parameter)
sample_2            <-  runif(n * values.sup.Parameter, min = Parameter, max = max(x))
sample          <-   c(sample_1 , sample_2)     ### Simulation of the values using the uniform distribution

### Step 4: Estimate the sample mean and the sample standard deviation

Mean_sample <-  mean(sample)
sd_sample       <-  sd(sample)

### Step 5: Determine a Confidence interval (CI)

lim_inf_CI      <-  Mean_sample - 1.96*sd_sample/sqrt(n)  ### Lower limit of the confidence interval
lim_sup_CI      <-  Mean_sample + 1.96*sd_sample/sqrt(n)  ### Upper limit of the confidence interval
CI          <-  c(lim_inf_CI,lim_sup_CI)          ### confidence interval
magnitude_CI        <-  lim_sup_CI - lim_inf_CI           ### Magnitude of the confidence interval

### Step 6: Estimate the uncertainty (U)

uncertainty_1   <-  ((max(x) -min(x))/(2* Parameter))*100           ### Uncertainty based on range
uncertainty_2   <-  (((1.96 * sd_sample)/(sqrt(n) *Parameter)))*100     ### Uncertainty based on confidence interval

print(Mean_sample)
## [1] 1.725527
print(sd_sample)
## [1] 0.6128082
print(CI)
## [1] 1.606600 1.844454
print(magnitude_CI)
## [1] 0.2378541
print(magnitude_x)
## [1] 2.17
print(uncertainty_1)
## [1] 62.71676
print(uncertainty_2)
## [1] 6.874395