A) Assume that during a particular hour the following 30 bids (demand = load) were observed in the NordPool (first entry is Load in 1000 MWh (= Q), and second entry is €/MWh (= P)).

datainitial={{24,30.7},{23.5,36},{22.6,30},{22.5,30},{22,30},{21.5,31},{21.5,40.2},
{21.5,29.8},{23,29.6},{23.7,30.5},{24,30},{24.2,34},{25.2,30.2},{25.5,30.5},{23.5,41.2},
{22,32.5},{23.5,30.7},{24.5,31},{22.8,34},{22.7,36.8},{23.5,30.2},{25,29.5},{22.5,35.5},
{23,30.5},{22.5,45},{22,57},{21.7,75},{25.1,30.1},{23.2,43.2},{22,36.5}};

Estimate the demand elasticity for system price (equilibrium) of 22.045€/MWh, assuming the demand function is given by: (i) Q= A(P^b );(ii) Q=A+bP

(B) Suppose that the consumption of electricity per hour, during a normal day (starting at 00.00), by all villa owners (type 1) and the factories (type 2) is given in the following table. Estimate the peak load for the 95% and 99% probability level. Type 1:{1,1,1,1,2,2,2,3,2.5,2,1.5,1,1,1,1,2,2,4,5,6,5,4,2,1}
Type 2: {5,5,5,5,5,5,8,24,28,33,33,33,33,32,32,32,32,26,20,15,10,6,6,4}

(A) To estimate the demand elasticity for the given system price of 22.045€/MWh, we need to use the provided demand function and the observed bids.

(i) Demand function: Q = A(P^b)

Step 1: Calculate the logarithm of both sides of the demand function to linearize it:
log(Q) = log(A) + b * log(P)

Step 2: Prepare the data for regression analysis. Create a new array, `data`, with log-transformed demand (log(Q)) as the dependent variable and log-transformed price (log(P)) as the independent variable. We need to convert the original data from {Q, P} to {log(Q), log(P)}.

data = Transpose[{Log[datainitial[[All, 1]]], Log[datainitial[[All, 2]]]}]

Step 3: Perform a linear regression analysis to estimate the demand elasticity. We need to fit a linear model to the transformed data using the `LinearModelFit` function. This will provide us with estimates for the intercept (log(A)) and the slope (b).

model = LinearModelFit[data, x, x];

Step 4: Extract the estimated values for the intercept (log(A)) and slope (b) from the model.

intercept = model["BestFitParameters"][[1]]
slope = model["BestFitParameters"][[2]]

Step 5: Calculate the demand elasticity at the given system price. The demand elasticity is defined as the derivative of the demand function with respect to price (dQ/dP) multiplied by the ratio of price to quantity (P/Q).

demand_price_elasticity = slope * (22.045/Exp[intercept])

(ii) Demand function: Q = A + bP

Step 1: Prepare the data for regression analysis. Create a new array, `data_2`, with demand (Q) as the dependent variable and price (P) as the independent variable. We can use the original data for this case.

data_2 = datainitial

Step 2: Perform a linear regression analysis to estimate the demand elasticity. Fit a linear model to the data using the `LinearModelFit` function, which will provide us with estimates for the intercept (A) and slope (b).

model_2 = LinearModelFit[data_2, x, x];

Step 3: Extract the estimated values for the intercept (A) and slope (b) from the model.

intercept_2 = model_2["BestFitParameters"][[1]]
slope_2 = model_2["BestFitParameters"][[2]]

Step 4: Calculate the demand elasticity at the given system price using the formula: dQ/dP * (P/Q).

demand_price_elasticity_2 = slope_2 * (22.045/(intercept_2 + slope_2 * 22.045))

(B) To estimate the peak load for the 95% and 99% probability levels, we need to analyze the consumption data for type 1 (villa owners) and type 2 (factories).

Step 1: Sort the consumption data for both types in ascending order.

type1data = {1,1,1,1,2,2,2,3,2.5,2,1.5,1,1,1,1,2,2,4,5,6,5,4,2,1}
type2data = {5,5,5,5,5,5,8,24,28,33,33,33,33,32,32,32,32,26,20,15,10,6,6,4}

sorted_type1data = Sort[type1data]
sorted_type2data = Sort[type2data]

Step 2: Calculate the index corresponding to the desired probability level. For the 95% probability level, we take the index corresponding to the 5th percentile and for the 99% probability level, we take the index corresponding to the 1st percentile.

index_95 = Ceiling[Length[sorted_type1data] * 0.05]
index_99 = Ceiling[Length[sorted_type1data] * 0.01]

Step 3: Calculate the peak load for each probability level by selecting the data point at the calculated indices.

peak_load_95 = sorted_type1data[[index_95]]
peak_load_99 = sorted_type1data[[index_99]]

Repeat Steps 2 and 3 for type 2 data to estimate the peak load for the factories.