Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Example Model

Austin Drenski edited this page Feb 6, 2017 · 34 revisions

This example looks at the effect of a 1.0% price shock on one supplier in a sector where four suppliers initially have equal market shares.

Parameter Value
Elasticity of substitution 4.00
Elasticity of supply 5.00
Elasticity of demand -1.00
Initial price 1.00
Current price 1.00
Market share 0.25
Shock (only Supplier3) 0.01
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using AD.IO;
using AD.PartialEquilibriumApi.Optimization;

namespace AD.PartialEquilibriumApi.Example
{
    public static class Example
    {
        public static void Example1()
        {
            XmlFilePath structureFile = CreateTempXmlFile();
            DelimitedFilePath dataFile = CreateTempCsvFile();

            // Read in the model and the data.
            XElement model = XElement.Load(structureFile)
                                     .DefineAttributeData(dataFile);

            // Set the current prices.
            model.SetCurrentPrices(model.DescendantsAndSelf()
                                        .Select(x => x.InitialPrice())
                                        .ToArray());

            // Apply the price shocks.
            model.ShockAllPrices();

            // Calculate the price indices.
            model.CalculatePriceIndex();

            // Calculate the market equilibrium starting on the root.
            model.CalculateRootMarketEquilibrium();

            // Create boolean vector indicating which nodes (in document-order) are variable.
            bool[] variables =
                new bool[]
                {
                    false,
                    false,
                    false,
                    true,
                    false
                };

            // Create the objective function.
            Func<double[], double> objectiveFunction =
                x =>
                {
                    // Update current prices to the argument vector.
                    // Result: x[i] if variables[i] is true
                    model.SetCurrentPrices(x, variables);
                    // Shock the current prices:
                    // Result: currentPrice * (1 + shock)
                    model.ShockAllPrices();
                    // Calculate a price index for the sector:
                    // Result: [Σ marketShare[i] * (price[i] ^ (1 - elasticityOfSubstitution[i])] ^ [1 / (1 - elasticityOfSubstitution)]
                    model.CalculatePriceIndex();
                    // Caclulate the market equilibrium. Zero means equilibrium.
                    // [shockedPrice ^ elasticityOfSupply] - [(priceIndex ^ (elasticityOfSubstitution + elasticityOfDemand)) / (initialPrice ^ elasticityOfSubstitution)]
                    model.CalculateRootMarketEquilibrium();
                    // Return the sector's equilibrium value to the caller.
                    return model.MarketEquilibrium();
                };

            // Set up the simplex solver.
            Simplex simplex =
                new Simplex(
                    numberOfSolutions: 5,
                    dimensions: 5,
                    lowerBound: 0,
                    upperBound: 100,
                    iterations: 1000,
                    objectiveFunction: x => objectiveFunction(x));

            // Find the minimum solution.
            Solution solution = simplex.Minimize();

            // Update the XML tree one more time with the optimal result.
            double[] result = solution.Vector;
            model.SetCurrentPrices(result, variables);
            model.ShockAllPrices();
            model.CalculatePriceIndex();
            model.CalculateRootMarketEquilibrium();

            // Print the results.
            Console.WriteLine("-------------------------");
            foreach (XElement item in model.DescendantsAndSelf().Reverse())
            {
                Console.WriteLine();
                Console.WriteLine($"Name: {item.Name}");
                foreach (XAttribute attribute in item.Attributes())
                {
                    Console.WriteLine(attribute);
                }
            }
            Console.WriteLine();
            Console.WriteLine(model);
            Console.WriteLine("-------------------------");
            Console.ReadLine();
        }

        private static XmlFilePath CreateTempXmlFile()
        {
            string xml = Path.ChangeExtension(Path.GetTempFileName(), ".xml");
            using (StreamWriter writer = new StreamWriter(xml))
            {
                writer.WriteLine(
                    @"<Retail>
                        <Supplier1 />
                        <Supplier2 />
                        <Supplier3 />
                        <Supplier4 />
                      </Retail>");
            }
            return new XmlFilePath(xml);
        }

        private static DelimitedFilePath CreateTempCsvFile()
        {
            string csv = Path.ChangeExtension(Path.GetTempFileName(), ".csv");
            using (StreamWriter writer = new StreamWriter(csv))
            {
                writer.WriteLine("ElasticityOfSubstitution,ElasticityOfSupply,ElasticityOfDemand,InitialPrice,CurrentPrice,MarketShare,Shock");
                writer.WriteLine("4,5,-1,1.0,1.0,1.00,0.00");
                writer.WriteLine("4,5,-1,1.0,1.0,0.25,0.05");
                writer.WriteLine("4,5,-1,1.0,1.0,0.25,0.00");
                writer.WriteLine("4,5,-1,1.0,1.0,0.25,0.00");
                writer.WriteLine("4,5,-1,1.0,1.0,0.25,0.00");
            }
            return new DelimitedFilePath(csv, ',');
        }
    }
}
-------------------------
> i =   0: [  3e00, 9e01, 1e02, 7e01, 3e01 ] = 7e04
> i =  10: [ 5e-01, 8e01, 9e01, 8e01, 4e01 ] = 3e01
...
> i =  80: [ 7e-01, 8e01, 9e01, 7e01, 4e01 ] = 1e01
...
> i = 210: [ 7e-01, 8e01, 9e01, 7e01, 4e01 ] = 1e01
> i = 220: [  1e00, 8e01, 9e01, 7e01, 5e01 ] = 2e-01
> i = 230: [  1e00, 8e01, 9e01, 7e01, 4e01 ] = 1e-01
...
> i = 990: [  1e00, 8e01, 9e01, 7e01, 4e01 ] = 8e-02
-------------------------

Name: Retail
ElasticityOfSubstitution="4"
ElasticityOfSupply="5"
ElasticityOfDemand="-1"
InitialPrice="1"
CurrentPrice="1"
MarketShare="1"
Shock="0"
ShockedPrice="1"
PriceIndex="1.0007311534043639"
MarketEquilibrium="0.0757220266173961"

Name: Supplier1
ElasticityOfSubstitution="4"
ElasticityOfSupply="5"
ElasticityOfDemand="-1"
InitialPrice="1"
CurrentPrice="1"
MarketShare="0.25"
Shock="0.05"
ShockedPrice="1.05"
PriceIndex="1.0007311534043639"
MarketEquilibrium="0.27408649814014252"

Name: Supplier2
ElasticityOfSubstitution="4"
ElasticityOfSupply="5"
ElasticityOfDemand="-1"
InitialPrice="1"
CurrentPrice="1"
MarketShare="0.25"
Shock="0"
ShockedPrice="1"
PriceIndex="1.0007311534043639"
MarketEquilibrium="-0.0021950643598578345"

Name: Supplier3
ElasticityOfSubstitution="4"
ElasticityOfSupply="5"
ElasticityOfDemand="-1"
InitialPrice="1"
CurrentPrice="1.0029375160196059"
MarketShare="0.25"
Shock="0"
ShockedPrice="1.0029375160196059"
PriceIndex="1.0007311534043639"
MarketEquilibrium="0.024268941871905203"

Name: Supplier4
ElasticityOfSubstitution="4"
ElasticityOfSupply="5"
ElasticityOfDemand="-1"
InitialPrice="1"
CurrentPrice="1"
MarketShare="0.25"
Shock="0"
ShockedPrice="1"
PriceIndex="1.0007311534043639"
MarketEquilibrium="-0.0021950643598578345"

<Retail ElasticityOfSubstitution="4" ElasticityOfSupply="5" ElasticityOfDemand="-1" InitialPrice="1" CurrentPrice="1" MarketShare="1" Shock="0" ShockedPrice="1" PriceIndex="1.0007311534043639" MarketEquilibrium="0.0757220266173961">
  <Supplier1 ElasticityOfSubstitution="4" ElasticityOfSupply="5" ElasticityOfDemand="-1" InitialPrice="1" CurrentPrice="1" MarketShare="0.25" Shock="0.05" ShockedPrice="1.05" PriceIndex="1.0007311534043639" MarketEquilibrium="0.27408649814014252" />
  <Supplier2 ElasticityOfSubstitution="4" ElasticityOfSupply="5" ElasticityOfDemand="-1" InitialPrice="1" CurrentPrice="1" MarketShare="0.25" Shock="0" ShockedPrice="1" PriceIndex="1.0007311534043639" MarketEquilibrium="-0.0021950643598578345" />
  <Supplier3 ElasticityOfSubstitution="4" ElasticityOfSupply="5" ElasticityOfDemand="-1" InitialPrice="1" CurrentPrice="1.0029375160196059" MarketShare="0.25" Shock="0" ShockedPrice="1.0029375160196059" PriceIndex="1.0007311534043639" MarketEquilibrium="0.024268941871905203" />
  <Supplier4 ElasticityOfSubstitution="4" ElasticityOfSupply="5" ElasticityOfDemand="-1" InitialPrice="1" CurrentPrice="1" MarketShare="0.25" Shock="0" ShockedPrice="1" PriceIndex="1.0007311534043639" MarketEquilibrium="-0.0021950643598578345" />
</Retail>
-------------------------

Clone this wiki locally