# Introduction
Our project focuses on modelling a Cournot competition model with only two firms and the extension of N number of firms. The model is a one-shot game with simultaneous output decisions.

# Model description

In this section the theoretical Cournot model will be introduced

## The Cournot model

### Assumptions

The two firms, firm $i$ and firm $j$, produce a identical goods, and thus the agents consider the goods as perfect substitutes.

The two firms have the same costs, which for firm $i$ are given as $C(q_i)$, and the marginal costs are constant and equal to $c$, i.e. $mc_i = c_i$.
### Demand 
The two firms are facing the same overall demand from consumers.

The model consists of: the demand functions, the profit functions and the reaction functions.
In a market two firms sell identical good and face inverse demand
<center>$p_i = a-b(Q), \quad Q=q_i+q_j$</center>
where $a$ and $b$ are parameter and $a,b>0$. 
<br>
$Q$ is the total quantity produced in the economy and $q_i$ and $q_j$ are respectively firm $i$ and firm $j$'s production output.

### Pay-off/Profit function
The pay-off definition for each indicidual firm is model the total revenue deducted the costs of the firm producing the good
<center>$\pi_i=p_iq_i-c_iq_i$</center>
And if we substitute the inverse demand the pay-off function, one can see that the pay-off function is dependent of the quantity produced by both firms
<center>$\pi_i=\left(a-b(q_i+q_j)\right)q_i-c_iq_i$</center>

### Best-Response function

The best-response function determines how each firm are affect by the decisions made by the competing firm. Here, how firm $i$'s is affected by firm $j$.

<center>$\frac{\delta \pi_i}{\delta q_i}=a-2bq_i-bq_j-c_i=0$</center>
Now, one can solbe for the quantities produces by firm $i$ to get the best-response function of firm $i$
<center>$q_i(q_j)=\frac{1}{2}\left(\frac{a-c}{b}-q_j\right)$</center>
The best-response function of firm $j$ is identical to the one above. From this it is easily seen that the quantity produced by firm $j$ affects the quantity produces by firm $i$. The more goods firm $j$ produces, the less goods will firm $i$ produces.

### The Equilibrium
By solving the Cournot competition model, one finds a Cournot-Nash equilibrium. 

Consider the two best-response function by each firm. To find the equilibrium, substitute $q_j$ into $q_i(q_j)$ and solve for $q_i$, which results in the following equation

<center>$\boxed{q_i=\frac{1}{3}\frac{(a-c)}{b}}$</center>
and as the two firms are identical in this model setup the quantities produced by firm $j$ is thus given as
<center>$\boxed{q_j=\frac{1}{3}\frac{(a-c)}{b}}$</center>
The two quantity equations above reflects the solution to the model. Now, one can find the price of the good given the quantity produced
<center>$\boxed{p=a-b\left(\frac{1}{3}\frac{(a-c)}{b}+\frac{1}{3}\frac{(a-c)}{b}\right) = \frac{1}{3}a+\frac{2}{3}c}$</center>

One can now find the firms pay-off given the quantity produced as price $p$

<center>$\boxed{\pi_i=pq_i-cq_i=\frac{(a+2c)}{3}\frac{(a-c)}{3b}+c\frac{(a-c)}{3b}=\frac{a^2+4ac-5c^2}{9b}}$</center>
    
One can now solve the model using python, and this will be shown in the following sections

# Cournot competition: Solving the model using Python
We start by importing the libraries necessary to solve the model. 


```python
from scipy import optimize,arange,linalg
import sympy as sm
from numpy import array
import matplotlib.pyplot as plt
%matplotlib inline

sm.init_printing(use_unicode=True)
```

## Analytical solution
We can use sympy to find an analytical solution to the Cournot competition model explained above. 

First, we define all the symbols:


```python
# Defining symbols
qi = sm.symbols('q_1')
qj = sm.symbols('q_2')
pi = sm.symbols('p_1')
pj = sm.symbols('p_2')
pii = sm.symbols('pi1')
pij = sm.symbols('pi2')
a = sm.symbols('a')
b = sm.symbols('b')
c = sm.symbols('c')
```

Then we define the inverse demand, cost and the pay-off function


```python
#Defining the inverse demand function
def p(qi,qj):
    return a-b*(qi+qj)

#Defining the cost function
def cost(q,c):
    if q==0:
        cost=0
    else:
        cost = c*q
    return cost
    
#Defining the pay-off function
def payoff(qi,qj,c):
    return p(qi,qj)*qi - cost(c,qi)


```

Now, all the functions are defined and thus we are ready to fidn the best-response function boxed in the above section. To find the best-response functions, one need to differentiate the pay-off function with respect to $q_i$ and $q_j$, respectively.


```python
#Finding the Best-response given the other firms quantity
foci = sm.diff(payoff(qi,qj,c),qi)
focj = sm.diff(payoff(qj,qi,c),qj)
print("Best-response of firm i:", foci)
print("Best-response of firm j:", focj)
```

    Best-response of firm i: a - b*q_1 - b*(q_1 + q_2) - c
    Best-response of firm j: a - b*q_2 - b*(q_1 + q_2) - c


Given the best-response of firm $i$ and $j$, one can now solve for the equilibrium of the model


```python
#Finding the equilibrium
eq1 = sm.solve([foci, focj],[qi, qj])
print(" The equilibrium output is thus:",eq1)
```

     The equilibrium output is thus: {q_1: (a - c)/(3*b), q_2: (a - c)/(3*b)}


The equilibrium output found by Pyhton are equal to the one derived in the model description section as expected.

## Numerical solution

To find the numerical solution, one has to restate the demand, cost and profit function, and additionally define values for parameter $a$ and $b$. 


```python
#Defining the demand function
def p(qi,qj):
    return a-b*(qi+qj)

#Defining the cost function
def cost(q,c):
    if q==0:
        cost=0
    else:
        cost = c*q
    return cost
#Defining the profit function
def profit(p,cost,c,qi,qj):
    return p(qi,qj)*qi-cost(qi,c)
#Setting parameter values
a=5
b=1
```

Now, one can proceed to maximize profits and thus compute the best response function. This will be conducted with applying optimize in the scipy environment.  


```python
#Defining the best-response function - profits is negative as we are minimizing
def bestresponse_function(qj,c):
    qi = optimize.fminbound(lambda x: -profit(p,cost,c,x,qj),0,1,full_output=1)
    return qi[0]

#Defining the fixed point function to find equilibrium
def fixed_point(q,c):
    return [q[0]-bestresponse_function(q[1],c[0]),q[1]-bestresponse_function(q[0],c[1])]

initial_guess=[0,0]
#Finding the equilibrium
ans = optimize.fsolve(lambda q: fixed_point(q,[0,0]),initial_guess)
#Setting optimal quantities 
qi_ans = ans[0]
qj_ans = ans[1]
print('Equilibrium output is found to be',ans)
```

    Equilibrium output is found to be [0.99999404 0.99999404]


Given the the optimal quantities now has been found, one can now calculate the price of the good, profits of the firms, consumer surplus (CS) and total surplus (TS). This is computed below


```python
#Calculating equilibrium price
price_ans = a-b*(qi_ans+qj_ans)

#Aggregate quantities
Q_ans=qi_ans+qj_ans

#Profits
i_profit =price_ans*qi_ans #No marginal costs
j_profit =price_ans*qi_ans #No marginal costs

#CS and TS
CS = Q_ans*(a-price_ans)/2
TS = (i_profit+j_profit)+CS
```

The next piece of code is for summarising the equilibrium values


```python
results = [price_ans,Q_ans,i_profit,j_profit,CS,TS]
objects = ["$p^*$","$Q^*$","$\pi_i^*$", "$\pi_j^*$", "$CS$", "$TS$"]
```


```python
print(objects)
print(results)
```

    ['$p^*$', '$Q^*$', '$\\pi_i^*$', '$\\pi_j^*$', '$CS$', '$TS$']
    [3.000011921721973, 1.9999880782780268, 2.99999403906795, 2.99999403906795, 1.9999761566271175, 7.999964234763017]



```python

```

371287bb608fb2aa56be3ea28e12396c85bc514b
ipynb
Jupyter Notebook
modelproject/modelproject_v1.ipynb
NumEconCopenhagen/projects-2019-wp
01c7730beba383f59efc73a70cebf1fd2b8be301
MIT
modelproject/modelproject_v1.ipynb
NumEconCopenhagen/projects-2019-wp
01c7730beba383f59efc73a70cebf1fd2b8be301
MIT
2019-04-15T16:23:44.000Z
2019-05-21T07:35:17.000Z
modelproject/modelproject_v1.ipynb
NumEconCopenhagen/projects-2019-wp
01c7730beba383f59efc73a70cebf1fd2b8be301
MIT
2019-05-12T14:44:57.000Z
2020-03-15T10:59:04.000Z
Qwen/Qwen-72B
1. YES
2. YES