Ejemplo n.º 1
0
 def getMeanProfit(self):
     all_values = []
     for x in self.firm:
         all_values.extend(x.get_profits())
     return round(get_mean(all_values), ndigits=2)
Ejemplo n.º 2
0
    def analyzeGamma(self):
        # desired scope of the weight to be analyzed
        weightStart = 0.0
        weightEnd = 1
        weightSteps = 0.01

        # fixed alpha and delta values as a framework to analyze gamma
        alphastart = 0.05
        alphaend = 0.15
        alphastep = 0.05

        deltastart = 0.8
        deltaend = 0.95
        deltastep = 0.05

        # total number of steps per parameter
        numberOfWeightSteps: int = int(
            (weightEnd - weightStart + weightSteps) / weightSteps)
        numberOfAlpha: int = int(
            (alphaend - alphastart + alphastep) / alphastep)
        numberOfDelta: int = int(
            (deltaend - deltastart + deltastep) / deltastep)

        stepcounter: int = 1
        # indices for column and row
        c = 0
        r = 0

        # arrays to store results
        degree: List[List[float]] = [[0] * (numberOfAlpha * numberOfDelta)
                                     ] * numberOfWeightSteps
        percentage = [[None] *
                      (numberOfAlpha * numberOfDelta)] * numberOfWeightSteps

        print("Analyzing gamma at " + str(self.marketTiming) +
              " interaction in a " + str(self.competition) +
              " competition with " + str(self.marketSize) + " firms")

        i = alphastart
        while round(i, 3) <= alphaend:
            j = deltastart
            while round(j, 2) <= deltaend:
                k = weightStart
                while round(k, 3) <= weightEnd:

                    self.resetEnvironment()
                    self.alpha = round(i, 4)
                    self.delta = round(j, 4)
                    self.gamma = round(k, 4)
                    print(
                        stepcounter, "/",
                        (numberOfWeightSteps * numberOfAlpha * numberOfDelta),
                        f"with (alpha = {self.alpha}  delta = {self.delta} at gamma = {self.gamma} "
                    )

                    self.simulate()

                    degree[c][r] = get_mean(self.degrees)
                    percentage[c][r] = len(
                        list(
                            filter(lambda x: x < self.marketSize, self.pricesSD
                                   ))) * 100.0 / self.numberOfSimulationRuns

                    c += 1
                    stepcounter += 1

                    k += weightSteps
                c = 0
                r += 1
                i += deltastep
            i += alphastep

        content = ""

        content += "alpha"
        for j in range(numberOfAlpha):
            for k in range(numberOfDelta):
                val = alphastart + j * alphastep
                content += f",{val:.4f}"

        content += "\ndelta"
        for j in range(numberOfAlpha):
            for k in range(numberOfDelta):
                val = alphastart + k * alphastep
                content += f",{val:.4f}"

        content += "\n \ngamma"
        for i in range(numberOfWeightSteps):
            content += "\n"
            val = weightStart + i * weightSteps
            content += f",{val:.4f}"

            for j in range(numberOfAlpha * numberOfDelta):
                val = degree[i][j]
                content += f",{val:.4f}"

        content += "\n \n"

        for i in range(numberOfWeightSteps):
            content += "\n"
            val = weightStart + i * weightSteps
            content += f",{val:.4f}"

            for j in range(numberOfAlpha * numberOfDelta):
                val = percentage[i][j]
                content += f",{val:.4f}"
        ts = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
        fn = f"{ts}_weightAnalysis_{self.alpha * 100}_{self.delta * 100}.csv"
        with open(fn, "w", encoding="utf8") as outfile:
            outfile.write(content)
Ejemplo n.º 3
0
 def getMeanQuantity(self):
     all_values = []
     for x in self.firm:
         all_values.extend(x.get_quantities())
     return round(get_mean(all_values), ndigits=2)
Ejemplo n.º 4
0
    def export(self):
        end_time = datetime.now()
        duration = end_time - self.startingTime
        content = ""

        content += "SETTING"
        content += "\ntiming,"
        content += self.marketTiming

        if self.marketTiming == "discrete":
            content += f",,update interval,{self.updateInterval}"

        content += "\nmarket setting,"
        content += self.competition
        content += "\nmarket size,"
        content += str(self.marketSize)

        # simulation data and conditions
        content += "\n \nSIMULATION"
        content += "\nstart,"
        content += self.startingTime.strftime("%H:%M:%S")
        content += "\nduration,"
        content += str(duration)
        content += "\nend,"
        content += end_time.strftime("%H:%M:%S")
        content += "\nmax number of periods,"
        content += str(self.maxNumberOfPeriods)
        content += "\nnumber of runs,"
        content += str(self.numberOfSimulationRuns)
        content += "\nnumber of collusive runs,"
        foo = len(list(filter(lambda x: x < self.marketSize, self.pricesSD)))
        bar = foo / self.numberOfSimulationRuns * 100.0
        content += str(foo)
        content += ","
        content += str(bar)
        content += "%"

        # parameters of the Q-Learning algorithms
        content += "\n \nQ-LEARNING"
        content += "\nactionset,[0; "
        content += str(self.sizeOfActionSet - 1)
        content += "]"
        content += "\nalpha,"
        content += str(self.alpha)
        content += "\nbeta,"
        content += str(self.beta)

        if (self.marketSize > 2):
            content += "\ngamma,"
            content += str(self.gamma)
        content += "\ndelta,"
        content += str(self.delta)
        content += "\nØ epsilon at the end,"
        val = (math.exp(-self.beta * get_mean(self.periods)))
        content += f"{val:.8f}"

        content += "\n \nMEAN"
        content += "\nALL RUNS,mean,σ"
        content += "\nprice,"
        content += str(get_mean(self.prices))
        content += ","
        content += str(get_sd(self.prices))
        content += "\nσ (of all period prices),"
        content += str(get_mean(self.pricesSD))
        content += "\nquantity,"
        content += str(get_mean(self.quantities))
        content += ","
        content += str(get_sd(self.quantities))
        content += "\nprofit,"
        content += str(get_mean(self.profits))
        content += ","
        content += str(get_sd(self.profits))
        content += "\nperiods (in mio),"
        content += str(round(get_mean(self.periods) / 1000000, 3))
        content += ","
        content += str(round(get_sd(self.periods) / 1000000, 3))
        content += "\ndegree,"
        content += str(get_mean(self.degrees))
        content += ","
        content += str(get_sd(self.degrees))
        content += "\n \nDATA"
        content += "\ni,"
        content += "price,"
        content += "σ (of all prices),"
        content += "quantity,"
        content += "profit,"
        content += "periods (in mio),"
        content += "degree"
        content += "\n"

        for i in range(self.numberOfSimulationRuns):
            content += str(i + 1)
            content += ","
            content += str(self.prices[i])
            content += ","
            content += str(self.pricesSD[i])
            content += ","
            content += str(self.quantities[i])
            content += ","
            content += str(self.profits[i])
            content += ","
            content += str(round(self.periods[i] / 1000000, 3))
            content += ","
            content += str(self.degrees[i])
            content += "\n"

        ts = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
        filename = f"{ts}.csv"

        with open(filename, "w", encoding="utf8") as outfile:
            outfile.write(content)

        print("File " + filename + ".csv exported")