**Submitted by**:

[Ray], [Zhu], [LinkedIn](https://www.linkedin.com/in/jiasheng-ray-zhu-845241177/),
Majoring in [Political Economy with Economics Concentration]
Class of [2022]
Duke Kunshan University

---

*Disclaimer: Submissions to Problem Set 1 for COMPSCI/ECON 206 Computational Microeconomics, 2022 Spring Term (Seven Week - Second) instructed by [Prof. Luyao Zhang](http://scholars.duke.edu/person/luyao.zhang) at Duke Kunshan University.*

**Resources:**

*   Colab Instructions: https://colab.research.google.com/
*   Markdown Guide: https://www.markdownguide.org
*   Latex Math wiki: https://en.wikibooks.org/wiki/LaTeX/Mathematics

*   OER: https://ie.pubpub.org/pub/gpd9nks0/release/2

---

# Part I: Game Theory Concepts (6 points)

**Note:**

*if you can't find definitions in the second computer science textbook, you can also refer to the algorithmic game theory textbook for substitutions.*

## 1. Normal Form Game

### 1.1. What defines a normal form game? Find answers in “A Course in Game Theory by Prof. Ariel Rubinstein and M. Osborne. ([Download](https://arielrubinstein.tau.ac.il/books.html))

Refer to Textbook: [Osborne, Martin J. and Ariel Rubinstein](https://arielrubinstein.tau.ac.il/books.html). 1994. A Course in Game Theory. (Chapter 1, Page 9)

In this part we study a model of strategic interaction known as a strategic game, or, in the terminology of von Neumann and Morgenstern (1944), a “game in normal form”. This model specifies for each player a set of possible actions and a preference ordering over the set of possible action profiles.

### 1.2. What defines a normal form game? Find answers in “Multiagent Systems: Algorithmic, Game-Theoretic, and Logical Foundations” by Prof. Yoav Shoham and Kevin Leyton-Brown. ([Download](http://www.masfoundations.org/download.html))

Refer to Textbook: [Noam and Nisan.](https://www.cs.cmu.edu/~sandholm/cs15-892F13/algorithmic-game-theory.pdf) 2007. Algorithmic Game Theory (Page 182)

A multiplayer game consists of $n$ players, each with a finite set of pure strategies or actions available to them, along with a specification of the payoffs to each player. Throughout the chapter, we use $a_{i}$ to denote the action chosen by player $i$. For simplicity we will assume a binary action space, so $a_{i} \in\{0,1\}$. (The generalization of the results examined here to the multiaction setting is straightforward.) The payoffs to player $i$ are given by a table or matrix $M_{i}$, indexed by the joint action $\vec{a} \in\{0,1\}^{n}$. The value $M_{i}(\vec{a})$, which we assume without loss of generality to lie in the interval $[0,1]$, is the payoff to player $i$ resulting from the joint action $\vec{a}$. Multiplayer games described in this way are referred to as normal form games.

## 2. Static Game with Complete Information and Nash Equilibrium

### 2.1. What defines Nash Equilibrium? Find answers in “A Course in Game Theory by Prof. Ariel Rubinstein and M. Osborne. ([Download](https://arielrubinstein.tau.ac.il/books.html))

Refer to Textbook: [Osborne, Martin J. and Ariel Rubinstein](https://arielrubinstein.tau.ac.il/books.html). 1994. A Course in Game Theory. (Chapter 2, Page 14, DEFINITION 14.1)

**Definition of Nash equilibrium**

A Nash Equilibrium of a strategic game $\langle{N}, {A_{i}},(\succeq_{i})\rangle$ is a profile $a^{*}\in{A}$ of actions with the property that for every player $i\in {N}$, we have:
$$(a^{*}_{-i},a^{*}_{i}) \succeq_{i}(a^{*}_{-i},a_{i}), \forall \in {A_{i}}.$$
And, a strategic game $\langle{N}, {A_{i}},(\succeq_{i})\rangle$  consist of:
*   a finite set ${N}$ as the set of players
*   for each player $i\in{N}$, a nonempty set $A_{i}$ as the set of actions available to player $i$
*   for each player $i\in{N}$, a preference relation $\succeq_{i}$ on ${A}=\times_{j\in {N}}{A}_{j}$

### 2.2. What defines Nash Equilibrium? Find answers in “Multiagent Systems: Algorithmic, Game-Theoretic, and Logical Foundations” by Prof. Yoav Shoham and Kevin Leyton-Brown. ([Download](http://www.masfoundations.org/download.html))

Refer to Textbook: [Shoham, Yoav, and Kevin Leyton-Brown](http://www.masfoundations.org/download.html). 2008. Multiagent Systems: Algorithmic, Game-Theoretic, and Logical Foundations. Cambridge: Cambridge University Press. (Chapter 3, Page 62, Definition 3.3.4)

**Definition of Nash equilibrium**

Nash Equilibrium A strategy profile $s^{*}=(s_{1}^{*},...,s_{n}^{*})\in S$ is a \textbf{Nash Equilibrium} of a normal for game $({N}, {A}, \mu)$ if, $\forall$ agents $i$, $s_{i}^{*}$ is a best response to $s_{-i}^{*}$:

$$\mu_{i}(s_{i}^{*},s_{-i}^{*}) \geq \mu_{i}(s_{i},s_{-i}^{*}), \forall -i.$$
And a normal game $({N}, {A}, \mu)$ consist of:
*   ${N}$, a finite set of $n$ players, indexed by $i$
*   ${A} ={A_{1}}\times ...{A_{n}}$, where ${A_{i}}$ is a finite set of actions available to player $i$. Each vector $a=(a_{1},...,a_{n})\in A$ is called an action profile; the set of mixed strategy for player $i$ is $S_{i}=\prod(A_{i})$, where for any set $X$, $\prod(X)$ denotes the set of all probability distributions over $X$
*   $\mu = (\mu_{1},...,\mu_{n})$ where $\mu_{i}: {A} \mapsto \mathbb{R} $

## 3.Bayesian Game and Perfect Bayesian Nash Equilibrium

### 3.1. How to define Bayesian Game and Bayesian Nash Equilibrium? Find answers in “A Course in Game Theory by Prof. Ariel Rubinstein and M. Osborne. ([Download](https://arielrubinstein.tau.ac.il/books.html))

Refer to Textbook: [Osborne, Martin J. and Ariel Rubinstein](https://arielrubinstein.tau.ac.il/books.html). 1994. A Course in Game Theory. (Chapter 2, Page 25-26, DEFINITION 14.1)

DEFINITION 25.1 A Bayesian game consists of
- a finite set $N$ (the set of players)
- a finite set $\Omega$ (the set of states)
and for each player $i \in N$
- a set $A_{i}$ (the set of actions available to player $i$ )
- a finite set $T_{i}$ (the set of signals that may be observed by player $i$ ) and a function $\tau_{i}: \Omega \rightarrow T_{i}$ (the signal function of player $i$ )
- a probability measure $p_{i}$ on $\Omega$ (the prior belief of player $i$ ) for which $p_{i}\left(\tau_{i}^{-1}\left(t_{i}\right)\right)>0$ for all $t_{i} \in T_{i}$
- a preference relation $\succsim_{i}$ on the set of probability measures over $A \times \Omega$ (the preference relation of player $i$ ), where $A=\times_{j \in N} A_{j}$.

Definition $26.1$ A Nash equilibrium of a Bayesian game $\langle N$, $\left.\Omega,\left(A_{i}\right),\left(T_{i}\right),\left(\tau_{i}\right),\left(p_{i}\right),\left(\succsim_{i}\right)\right\rangle$ is a Nash equilibrium of the strategic game defined as follows.
- The set of players is the set of all pairs $\left(i, t_{i}\right)$ for $i \in N$ and $t_{i} \in T_{i}$.
- The set of actions of each player $\left(i, t_{i}\right)$ is $A_{i}$.
- The preference ordering $\succsim_{\left(i, t_{i}\right)}^{*}$ of each player $\left(i, t_{i}\right)$ is defined by $a^{*} \succsim_{\left(i, t_{i}\right)}^{*} b^{*}$ if and only if $L_{i}\left(a^{*}, t_{i}\right) \succsim_{i} L_{i}\left(b^{*}, t_{i}\right)$,
where $L_{i}\left(a^{*}, t_{i}\right)$ is the lottery over $A \times \Omega$ that assigns probability $p_{i}(\omega) / p_{i}\left(\tau_{i}^{-1}\left(t_{i}\right)\right)$ to $\left(\left(a^{*}\left(j, \tau_{j}(\omega)\right)\right)_{j \in N}, \omega\right)$ if $\omega \in \tau_{i}^{-1}\left(t_{i}\right)$, zero otherwise.

### 3.2. How to define Bayesian Game and Beyesian Nash Equilibrium? Find answers in “Multiagent Systems: Algorithmic, Game-Theoretic, and Logical Foundations” by Prof. Yoav Shoham and Kevin Leyton-Brown. ([Download](http://www.masfoundations.org/download.html))

Refer to Textbook: [Shoham, Yoav, and Kevin Leyton-Brown](http://www.masfoundations.org/download.html). 2008. Multiagent Systems: Algorithmic, Game-Theoretic, and Logical Foundations. Cambridge: Cambridge University Press. (Chapter 6, Page 167-170, Definition 6.3.2 & 6.3.7)

Definition 6.3.2 (Bayesian game: types) A Bayesian game is a tuple $(N, A, \Theta, p, u)$ where:
- $N$ is a set of agents;
- $A=A_{1} \times \cdots \times A_{n}$, where $A_{i}$ is the set of actions available to player $i$;
- $\Theta=\Theta_{1} \times \ldots \times \Theta_{n}$, where $\Theta_{i}$ is the type space of player $i$;
- $p: \Theta \mapsto[0,1]$ is a common prior over types; and
- $u=\left(u_{1}, \ldots, u_{n}\right)$, where $u_{i}: A \times \Theta \mapsto \mathbb{R}$ is the utility function for player $i$.
The assumption is that all of the above is common knowledge among the players, and that each agent knows his own type. This definition can seem mysterious, because the notion of type can be rather opaque. In general, the type of agent encapsulates all the information possessed by the agent that is not common knowledge. This is often quite simple (e.g., the agent's knowledge of his private payoff function), but can also include his beliefs about other agents' payoffs, about their beliefs about his own payoff, and any other higher-order beliefs.

We can get further insight into the notion of a type by relating it to the formulation at the beginning of this section. Consider again the Bayesian game in Figure 6.7. For each of the agents we have two types, corresponding to his two information sets. Denote player 1's actions as $\mathrm{U}$ and D, player 2's actions as $\mathrm{L}$ and R. Call the types of the first agent $\theta_{1,1}$ and $\theta_{1,2}$, and those of the second agent $\theta_{2,1}$ and $\theta_{2,2}$. The joint distribution on these types is as follows: $p\left(\theta_{1,1}, \theta_{2,1}\right)=.3$, $p\left(\theta_{1,1}, \theta_{2,2}\right)=.1, p\left(\theta_{1,2}, \theta_{2,1}\right)=.2, p\left(\theta_{1,2}, \theta_{2,2}\right)=.4$. The conditional probabilities for the first player are $p\left(\theta_{2,1} \mid \theta_{1,1}\right)=3 / 4, p\left(\theta_{2,2} \mid \theta_{1,1}\right)=1 / 4$, $p\left(\theta_{2,1} \mid \theta_{1,2}\right)=1 / 3$, and $p\left(\theta_{2,2} \mid \theta_{1,2}\right)=2 / 3$.

Definition 6.3.7 (Bayes-Nash equilibrium) A Bayes-Nash equilibrium is a mixedstrategy profile s that satisfies $\forall i \quad s_{i} \in B R_{i}\left(s_{-i}\right)$.

This is exactly the definition we gave for the Nash equilibrium in Definition 3.3.4: each agent plays a best response to the strategies of the other players. The difference from Nash equilibrium, of course, is that the definition of Bayes-Nash equilibrium is built on top of the Bayesian game definitions of best response and expected utility. Observe that we would not be able to define equilibrium in this way if an agent's strategies were not defined for every possible type. In order for a given agent $i$ to play a best response to the other agents $-i, i$ must know what strategy each agent would play for each of his possible types. Without this information, it would be impossible to evaluate the term $E U_{i}\left(s_{i}^{\prime}, s_{-i}\right)$ in Equation (6.7).

## 4. Extensive Form Game

### 4.1. How to define extensive form game? Find answers in “A Course in Game Theory by Prof. Ariel Rubinstein and M. Osborne. ([Download](https://arielrubinstein.tau.ac.il/books.html))

Refer to Textbook: [Osborne, Martin J. and Ariel Rubinstein](https://arielrubinstein.tau.ac.il/books.html). 1994. A Course in Game Theory. (Chapter 6, Page 89, DEFINITION 6.1.1)

An extensive game is a detailed description of the sequential structure of the decision problems encountered by the players in a strategic situation.

### 4.2. How to define extensive form game? Find answers in “Multiagent Systems: Algorithmic, Game-Theoretic, and Logical Foundations” by Prof. Yoav Shoham and Kevin Leyton-Brown. ([Download](http://www.masfoundations.org/download.html))

Refer to Textbook: [Noam and Nisan.](https://www.cs.cmu.edu/~sandholm/cs15-892F13/algorithmic-game-theory.pdf) 2007. Algorithmic Game Theory. (Page 53)

Extensive games are game trees, with information sets that model imperfect information of the players.

## 5. Subgame Perfect Nash Equilibrium

### 5.1 How to define Subgame Perfect Nash Equilibrium (SPNE)? Find answers in “A Course in Game Theory by Prof. Ariel Rubinstein and M. Osborne. ([Download](https://arielrubinstein.tau.ac.il/books.html))

Refer to Textbook: [Osborne, Martin J. and Ariel Rubinstein](https://arielrubinstein.tau.ac.il/books.html). 1994. A Course in Game Theory. (Chapter 6, Page 97, DEFINITION 97.2)

A subgame perfect equilibrium of an extensive game with perfect information $\Gamma=\left\langle N, H, P,\left(\succsim_{i}\right)\right\rangle$ is a strategy profile $s^{*}$ such that for every player $i \in N$ and every nonterminal history $h \in H \backslash Z$ for which $P(h)=i$ we have
$$
\left.O_{h}\left(\left.s_{-i}^{*}\right|_{h},\left.s_{i}^{*}\right|_{h}\right) \succsim_{i}\right|_{h} O_{h}\left(\left.s_{-i}^{*}\right|_{h}, s_{i}\right)
$$
for every strategy $s_{i}$ of player $i$ in the subgame $\Gamma(h)$.
Equivalently, we can define a subgame perfect equilibrium to be a strategy profile $s^{*}$ in $\Gamma$ for which for any history $h$ the strategy profile $\left.s^{*}\right|_{h}$ is a Nash equilibrium of the subgame $\Gamma(h)$.

The notion of subgame perfect equilibrium eliminates Nash equilibria in which the players' threats are not credible. For example, in the game in Figure $96.2$ the only subgame perfect equilibrium is $(A, R)$ and in the game in Figure $91.1$ the only subgame perfect equilibria are $((2,0), y y y)$ and $((1,1)$, nyy $)$.

### 5.2 How to define Subgame Perfect Nash Equilibrium (SPNE)? Find answers in “Multiagent Systems: Algorithmic, Game-Theoretic, and Logical Foundations” by Prof. Yoav Shoham and Kevin Leyton-Brown. ([Download](http://www.masfoundations.org/download.html))

Refer to Textbook: [Shoham, Yoav, and Kevin Leyton-Brown](http://www.masfoundations.org/download.html). 2008. Multiagent Systems: Algorithmic, Game-Theoretic, and Logical Foundations. Cambridge: Cambridge University Press. (Chapter 5, Page 123, Definition 5.1.5)

Definition 5.1.5 (Subgame-perfect equilibrium) The subgame-perfect equilibria $(S P E)$ of a game $G$ are all strategy profiles s such that for any subgame $G^{\prime}$ of $G$, the restriction of s to $G^{\prime}$ is a Nash equilibrium of $G^{\prime}$.

## 6. Perfect Bayesian Equilibrium

### 6.1 How to define Perfect Bayesian Equilibrium (PBE)? Find answers in “A Course in Game Theory by Prof. Ariel Rubinstein and M. Osborne. ([Download](https://arielrubinstein.tau.ac.il/books.html))

Refer to Textbook: [Osborne, Martin J. and Ariel Rubinstein](https://arielrubinstein.tau.ac.il/books.html). 1994. A Course in Game Theory. (Chapter 12, Page 232-233, DEFINITION 232.1)

Let $\left\langle\Gamma,\left(\Theta_{i}\right),\left(p_{i}\right),\left(u_{i}\right)\right\rangle$ be a Bayesian extensive game with observable actions, where $\Gamma=\langle N, H, P\rangle$. A pair $\left(\left(\sigma_{i}\right),\left(\mu_{i}\right)\right)=$ $\left(\left(\sigma_{i}\left(\theta_{i}\right)\right)_{i \in N, \theta_{i} \in \Theta_{i}},\left(\mu_{i}(h)\right)_{i \in N, h \in H \backslash Z}\right)$, where $\sigma_{i}\left(\theta_{i}\right)$ is a behavioral strategy of player $i$ in $\Gamma$ and $\mu_{i}(h)$ is a probability measure on $\Theta_{i}$, is a perfect Bayesian equilibrium of the game if the following conditions are satisfied.

- Sequential rationality: For every nonterminal history $h \in H \backslash Z$, every player $i \in P(h)$, and every $\theta_{i} \in \Theta_{i}$ the probability measure $O\left(\sigma_{-i}, \sigma_{i}\left(\theta_{i}\right), \mu_{-i} \mid h\right)$ is at least good for type $\theta_{i}$ as $O\left(\sigma_{-i}, s_{i}, \mu_{-i} \mid h\right)$ for any strategy $s_{i}$ of player $i$ in $\Gamma$.
- Correct initial beliefs: $\mu_{i}(\varnothing)=p_{i}$ for each $i \in N$.
- Action-determined beliefs: If $i \notin P(h)$ and $a \in A(h)$ then $\mu_{i}(h, a)=$ $\mu_{i}(h)$; if $i \in P(h), a \in A(h), a^{\prime} \in A(h)$, and $a_{i}=a_{i}^{\prime}$ then $\mu_{i}(h, a)=$ $\mu_{i}\left(h, a^{\prime}\right)$.
- Bayesian updating: If $i \in P(h)$ and $a_{i}$ is in the support of $\sigma_{i}\left(\theta_{i}\right)(h)$ for some $\theta_{i}$ in the support of $\mu_{i}(h)$ then for any $\theta_{i}^{\prime} \in \Theta_{i}$ we have
$$
\mu_{i}(h, a)\left(\theta_{i}^{\prime}\right)=\frac{\sigma_{i}\left(\theta_{i}^{\prime}\right)(h)\left(a_{i}\right) \cdot \mu_{i}(h)\left(\theta_{i}^{\prime}\right)}{\sum_{\theta_{i} \in \Theta_{i}} \sigma_{i}\left(\theta_{i}\right)(h)\left(a_{i}\right) \cdot \mu_{i}(h)\left(\theta_{i}\right)} .
$$

### 6.2 How to define Perfect Bayesian Equilibrium (PBE)? Find answers in “Multiagent Systems: Algorithmic, Game-Theoretic, and Logical Foundations” by Prof. Yoav Shoham and Kevin Leyton-Brown. ([Download](http://www.masfoundations.org/download.html))

Refer to Textbook: [Shoham, Yoav, and Kevin Leyton-Brown](http://www.masfoundations.org/download.html). 2008. Multiagent Systems: Algorithmic, Game-Theoretic, and Logical Foundations. Cambridge: Cambridge University Press. (Chapter 5, Page 144, Definition 5.2.10)

Definition 5.2.10 (Sequential equilibrium) A strategy profile $s$ is a sequential equilibrium of an extensive-form game $G$ if there exist probability distributions $\mu(h)$ for each information set $h$ in $G$, such that the following two conditions hold:
1. $(s, \mu)=\lim _{m \rightarrow \infty}\left(s^{m}, \mu^{m}\right)$ for some sequence $\left(s^{1}, \mu^{1}\right),\left(s^{2}, \mu^{2}\right), \ldots$, where $s^{m}$ is fully mixed, and $\mu^{m}$ is consistent with $s^{m}$ (in fact, since $s^{m}$ is fully mixed, $\mu^{m}$ is uniquely determined by $\left.s^{m}\right)$; and
2. For any information set $h$ belonging to agent $i$, and any alternative strategy $s_{i}^{\prime}$ of $i$, we have that
$$
u_{i}(s \mid h, \mu(h)) \geq u_{i}\left(\left(s^{\prime}, s_{-i}\right) \mid h, \mu(h)\right)
$$
Analogous to subgame-perfect equilibria in games of perfect information, sequential equilibria are guaranteed to always exist.

# Part II: Computational Issues (1 points)

### Brieftly read Algorithmic Game Theory e-book Chapter 1 and 2. ([URL](https://drive.google.com/file/d/1qbWzkgKbej9KTRcOOK7a57LEU83lRpfY/view?usp=sharing)) Answer the following questions:

### RQ1: What is the computational issues of the Nash Equilibrium?

1. No Nash equilibria: Even though mixed-strategy Nash equilibrium exists for a game with a finite number of players and a finite number of actions, a game with an infinite number of players, or a game with a finite number of players with an infinite strategy set may not have Nash equilibria[(Noam Nisan 2007, 13)](https://www.cs.cmu.edu/~sandholm/cs15-892F13/algorithmic-game-theory.pdf). This reminds me of the intermediate microeconomic class. When we tried to find the optimal solutions for a given utility function and choice set, even though in most cases they have mathematical optimal solutions, we should be careful about some special cases, such as the boundary solutions. While we are appreciating theoretical models, we should also be cautious about how it interacts with and informs about the real world.

2. Multiple Nash equilibria: Even though in many cases Nash equilibria could be computed, it is not strange that for some games there exist mutiple Nash Equilibria and we cannot explicitly tell which equilibria to be selected. In the case of multiple Nash equilibria, the solutions are less useful and directional in predicting players' decision-making and behaviors. As a result, it is even harder for players to know which equilibrium they are supposed to coordinate on [(Noam Nisan 2007, 12)](https://www.cs.cmu.edu/~sandholm/cs15-892F13/algorithmic-game-theory.pdf). This also reminds me of the models in intermediate microeconomic class. When either the choice set or the utility function is not convex, there might exist mutiple solutions and we have to look at them case by case.

### RQ2: What is the complexity of finding Nash Equilibrium?

In the case of a two-player game, applying the Lemke–Howson algorithm, we are able to compute a Nash Equilibrium in exponential time at worst. NP completeness, the “standard” way of establishing intractability of individual problems, is not appropriate because Nash (1951) proved that Nash equilibria always exist. The problem of finding a Nash equilibrium is PPAD-complete even for two-player games in standard form[(Noam Nisan 2007, 16)](https://www.cs.cmu.edu/~sandholm/cs15-892F13/algorithmic-game-theory.pdf). The correlated equilibrium is a computationally benign generalization of the intractable Nash equilibrium. We can find in polynomial time a correlated equilibrium for any game by linear programming techniques [(Noam Nisan 2007, 47)](https://www.cs.cmu.edu/~sandholm/cs15-892F13/algorithmic-game-theory.pdf).

*   [NP completeness](https://www.britannica.com/science/NP-complete-problem)
> A problem is called NP (nondeterministic polynomial) if its solution can be guessed and verified in polynomial time; nondeterministic means that no particular rule is followed to make the guess. If a problem is NP and all other NP problems are polynomial-time reducible to it, the problem is NP-complete.

*   [PPAD](https://en.wikipedia.org/wiki/PPAD_(complexity))
>In computer science, PPAD ("Polynomial Parity Arguments on Directed graphs") is a complexity class introduced by Christos Papadimitriou in 1994.

# Part III: Case Studies (8 points)

## 1.NashPy

### 1.1. What types of games and solution concepts can NashPy deal with?

Nashpy is a python library for the computation of equilibria of 2 player strategic games.

[(Knight 2021)](https://github.com/drvinceknight/Nashpy)

### 1.2. Apply NashPy to solve a solution concept on a specific game (your own choices) different from the case studies in the OER

Rock Paper Scissors is a hand game originating from China, usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand.

A simultaneous, zero-sum game, it has three possible outcomes: a draw, a win or a loss. A player who decides to play rock will beat another player who has chosen scissors ("rock crushes scissors" or "breaks scissors" or sometimes "blunts scissors"), but will lose to one who has played paper ("paper covers rock"); a play of paper will lose to a play of scissors ("scissors cuts paper"). If both players choose the same shape, the game is tied and is usually immediately replayed to break the tie. The type of game originated in China and spread with increased contact with East Asia, while developing different variants in signs over time.

[(“Rock Paper Scissors” 2020)](https://en.wikipedia.org/wiki/Rock_paper_scissors)

```python
# install the tools you will use later
!pip install --upgrade setuptools
!pip install --upgrade pip
!pip install quantecon
```

    Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (62.1.0)
    WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
    Requirement already satisfied: pip in /usr/local/lib/python3.7/dist-packages (22.0.4)
    WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
    Requirement already satisfied: quantecon in /usr/local/lib/python3.7/dist-packages (0.5.3)
    Requirement already satisfied: scipy>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from quantecon) (1.4.1)
    Requirement already satisfied: numba in /usr/local/lib/python3.7/dist-packages (from quantecon) (0.51.2)
    Requirement already satisfied: sympy in /usr/local/lib/python3.7/dist-packages (from quantecon) (1.7.1)
    Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from quantecon) (1.21.5)
    Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from quantecon) (2.23.0)
    Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from numba->quantecon) (62.1.0)
    Requirement already satisfied: llvmlite<0.35,>=0.34.0.dev0 in /usr/local/lib/python3.7/dist-packages (from numba->quantecon) (0.34.0)
    Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->quantecon) (2021.10.8)
    Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->quantecon) (3.0.4)
    Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->quantecon) (1.24.3)
    Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->quantecon) (2.10)
    Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.7/dist-packages (from sympy->quantecon) (1.2.1)
    WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

```python
!which python
```

    /usr/local/bin/python

```python
!pip3 install nashpy --use-deprecated=backtrack-on-build-failures
```

    DEPRECATION: Backtracking on build failures can mask issues related to how a package generates metadata or builds a wheel. This flag will be removed in pip 22.2. A possible replacement is avoiding known-bad versions by explicitly telling pip to ignore them (either directly as requirements, or via a constraints file). Discussion can be found at https://github.com/pypa/pip/issues/10655
    Collecting nashpy
      Using cached nashpy-0.0.22.tar.gz (11 kB)
      error: subprocess-exited-with-error

      × python setup.py egg_info did not run successfully.
      │ exit code: 1
      ╰─> See above for output.

      note: This error originates from a subprocess, and is likely not a problem with pip.
      Preparing metadata (setup.py) ... error
    WARNING: Discarding https://files.pythonhosted.org/packages/93/1c/9005c6a0a3a3b183b5b216cc02cef14bb080fd3ee8733a1006fbcd81fffc/nashpy-0.0.22.tar.gz#sha256=9378fd492f01163ac01a7384dd94f7ffa3ce40e31fe2a08844a2e34576b3d8db (from https://pypi.org/simple/nashpy/) due to build failure: metadata generation failed
      Downloading nashpy-0.0.21.tar.gz (11 kB)
      Preparing metadata (setup.py) ... done
    Requirement already satisfied: numpy>=1.12.1 in /usr/local/lib/python3.7/dist-packages (from nashpy) (1.21.5)
    Requirement already satisfied: scipy>=0.19.0 in /usr/local/lib/python3.7/dist-packages (from nashpy) (1.4.1)
    Building wheels for collected packages: nashpy
      Building wheel for nashpy (setup.py) ... done
      Created wheel for nashpy: filename=nashpy-0.0.21-py3-none-any.whl size=15279 sha256=d6a0755aa742f535a94d26247d80a704995b49b6d60c73263a5ffc51c4fa3cb7
      Stored in directory: /root/.cache/pip/wheels/02/08/62/cf4fa931e0a317d180936b266169a57f4bb4eb801465bbe8a1
    Successfully built nashpy
    Installing collected packages: nashpy
    Successfully installed nashpy-0.0.21
    WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

```python
import nashpy as nash
import numpy as np

# Creater the game with the payoff matrix

A = np.array([[0, 0, 1],
              [1, 0, 0],
              [0, 1, 0]]) # A is the row player (in this case, prisoner)

B = np.array([[0, 1, 0],
              [0, 0, 1],
              [1, 0, 0]]) # B is the column player
```

```python
# Form the game
game2 = nash.Game(A,B)
game2
```

    Bi matrix game with payoff matrices:

    Row player:
    [[0 0 1]
     [1 0 0]
     [0 1 0]]

    Column player:
    [[0 1 0]
     [0 0 1]
     [1 0 0]]

```python
# Find the Nash Equilibrium with Support Enumeration
equilibria = game2.support_enumeration()
for eq in equilibria:
    print(eq)
```

    (array([0.33333333, 0.33333333, 0.33333333]), array([0.33333333, 0.33333333, 0.33333333]))

The results are consistent with the facts and are explainable. This game has a mixed Nash equilibrium. In this equilibium, both players play the mixed strategy that puts equal probabilities on all three actions. In short words, they play rock, paper, or scissors with the equal frequency (probability).

## 2. QuantEcon

### 2.1. What types of games and soution concepts can QuantEcon deal with?

QuantEcon is the open source code for economic modeling. It can deal with normal form game with more than 2 players and solve for the Nash Equilibrium.
There are several algorithms implemented to compute Nash equilibria:

*   Brute force
> Find all pure-action Nash equilibria of an N-player game (if any).
*   Sequential best response
> Find one pure-action Nash equilibrium of an N-player game (if any).
*   Support enumeration
> Find all mixed-action Nash equilibria of a two-player nondegenerate game.
*   Vertex enumeration
> Find all mixed-action Nash equilibria of a two-player nondegenerate game.
*   Lemke-Howson
> Find one mixed-action Nash equilibrium of a two-player game.
*   McLennan-Tourky
> Find one mixed-action Nash equilibrium of an N-player game.

QuantEcon is also developing Julia code that implements an algorithm for computing equilibrium values (e.g. subgame perfect equilibria) and associated equilibrium strategy profiles for repeated games with N players and finite actions.

[(Sargent and Stachurski 2021)](https://quantecon.org/notebooks/)

### 2.2. Apply QuantEcon to solve a solution concept on a specific game (your own choices) different from the case studies in the OER

Rock Paper Scissors is a hand game originating from China, usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand.

A simultaneous, zero-sum game, it has three possible outcomes: a draw, a win or a loss. A player who decides to play rock will beat another player who has chosen scissors ("rock crushes scissors" or "breaks scissors" or sometimes "blunts scissors"), but will lose to one who has played paper ("paper covers rock"); a play of paper will lose to a play of scissors ("scissors cuts paper"). If both players choose the same shape, the game is tied and is usually immediately replayed to break the tie. The type of game originated in China and spread with increased contact with East Asia, while developing different variants in signs over time.

[(“Rock Paper Scissors” 2020)](https://en.wikipedia.org/wiki/Rock_paper_scissors)

```python
# import the QuantEcon package
import quantecon.game_theory as gt
import numpy as np

# The first way to form the game: by the payoff matrix
prisoner_dilemma_matrix = np.array([[[0, 0],  [0, 1],  [1, 0]],
                                    [[1, 0],  [0, 0],  [0, 1]],
                                    [[0, 1],  [1, 0],  [0, 0]]])

g_PD = gt.NormalFormGame(prisoner_dilemma_matrix)
print(g_PD)
```

```python
# Finding the Nash equilibrium: pure_nash_brute

NE = gt.pure_nash_brute(g_PD)
print(NE)
```

```python
# Finding the Nash equilibrium: support_enumeration

NE = gt.support_enumeration(g_PD)
print(NE)
```

```python
NE = gt.vertex_enumeration(g_PD)
print(NE)
```

The results are consistent with the facts and are explainable. The game of Rock-Paper-Scissors doesn't have pure Nash equilibrium. Imagine you are playing rock all the time, once the other player figures out your strategy and he/she can win by always playing paper. However, this game has a unique mixed Nash equilibrium. In this equilibium, both players play the mixed strategy that puts equal probabilities on all three actions. In short words, they play rock, paper, or scissors with the equal frequency (probability).

## 3. Game Theory Explorer

### 3.1. What types of games and soution concepts can Game Theory Explorer deal with?

Game theory explorer is a web interface to gambit useful for teaching. An extensive or strategic-form game can be created and nicely displayed with a graphical user interface in a web browser. State-of-the-art algorithms then compute one or all Nash equilibria of the game.

[(Savani and von Stengel 2014)](https://gte.csc.liv.ac.uk/index/)

### 3.2. Apply Game Theory Explorer to solve a solution concept on a specific game (your own choices) different from the case studies in the OER

Rock Paper Scissors is a hand game originating from China, usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand.

A simultaneous, zero-sum game, it has three possible outcomes: a draw, a win or a loss. A player who decides to play rock will beat another player who has chosen scissors ("rock crushes scissors" or "breaks scissors" or sometimes "blunts scissors"), but will lose to one who has played paper ("paper covers rock"); a play of paper will lose to a play of scissors ("scissors cuts paper"). If both players choose the same shape, the game is tied and is usually immediately replayed to break the tie. The type of game originated in China and spread with increased contact with East Asia, while developing different variants in signs over time.

[(“Rock Paper Scissors” 2020)](https://en.wikipedia.org/wiki/Rock_paper_scissors)

(1) General Matrix

(2) Strategic Form

（3）Extensive Form

The results are consistent with the facts and are explainable. The game of Rock-Paper-Scissors doesn't have pure Nash equilibrium. Imagine you are playing rock all the time, once the other player figures out your strategy and he/she can win by always playing paper. However, this game has a unique mixed Nash equilibrium. In this equilibium, both players play the mixed strategy that puts equal probabilities on all three actions. In short words, they play rock, paper, or scissors with the equal frequency (probability).

## 4. Gambit

### 4.1. What types of games and solution concepts can Gambit deal with?

Gambit is a library of game theory software and tools for the construction and analysis of finite extensive and strategic noncooperative games. Gambit supports building game trees, finding all equilibria of a two-player game, computing equilibria in games with three or more players, and doing statistical game theory. It is more powerful and includes more various games than QuantEcon.

[(McKelvey, McLennan, and Turocy 2016)]( http://www.gambit-project.org)

### 4.2. Apply Gambit to solve a solution concept on a specific game (your own choices) different from the case studies in the OER

**Poker Game - Description**

The game begins with each player being dealt two cards which are hidden from the other player.
A round of betting takes place, where there are four actions available to the players: check, bet, call, raise. A player can check or bet if no amount has yet been made in the current round of betting and a player can call (match the amount bet by the opponent) or raise (bet an additional amount on top of opponent’s bet) if the opponent bets. After the initial round of betting (pre-flop), the first three community cards (visible to both players) come out (flop). Another round of betting proceeds before the fourth card comes out and likewise before the fifth and final card. After all cards are out, there is one last round of betting before the players’ hands are compared (showdown). The complexity of poker arises from inferring probabilities through the many rounds of betting and making decisions that consider events in the future.

[(Li 2018)](https://math.mit.edu/~apost/courses/18.204_2018/Jingyu_Li_paper.pdf)

Gambit can show various games in several forms, including the graphical form. The poker game's rule is not that complex, but with the help of Gambit, it is easier and clearer to analyze the decision tree and unfold the strategies. Besides, Gambit users can change the color to adjust the plot accordingly.

# 5. Mesa [optional]

## 5.1. What types of games and solution concepts can Mesa deal with?

It allows users to quickly create agent-based models using built-in core components (such as spatial grids and agent schedulers) or customized implementations; visualize them using a browser-based interface; and analyze their results using Python’s data analysis tools.
Agent-based models are computer simulations involving multiple entities (the agents) acting and interacting with one another based on their programmed behavior. Agents can be used to represent living cells, animals, individual humans, even entire organizations or abstract entities. Sometimes, we may have an understanding of how the individual components of a system behave, and want to see what system-level behaviors and effects emerge from their interaction. Other times, we may have a good idea of how the system overall behaves, and want to figure out what individual behaviors explain it. Or we may want to see how to get agents to cooperate or compete most effectively. Or we may just want to build a cool toy with colorful little dots moving around.

[(Thomson 2020)](https://mesa.readthedocs.io/en/latest/index.html)

# References

Noam Nisan. 2007. Algorithmic Game Theory. Cambridge ; New York: Cambridge University Press.

Osborne, Martin J, and Ariel Rubinstein. 1994. A Course in Game Theory. Cambridge, Mass.: Mit Press.

Yoav Shoham, and Kevin Leyton-Brown. 2009. Multiagent Systems : Algorithmic, Game-Theoretic, and Logical Foundations. Cambridge ; New York: Cambridge University Press.

Kazil, Jackie, Masad, David, Crooks, and Andrew. 2013. “Mesa: Agent-Based Modeling in Python 3+.” GitHub. 2013. https://github.com/projectmesa/mesa.Knight.

Vincent. 2021. “Nashpy: A Python Library for the Computation of Equilibria of 2 Player Strategic Games, Version 0.0.28.” GitHub. May 4, 2021. https://github.com/drvinceknight/Nashpy.McKelvey.

Richard, Andrew McLennan, and Theodore Turocy. n.d. “Gambit: Software Tools for Game Theory, Version 16.0.1.” Www.gambit-Project.org. http://www.gambit-project.org/.Sargent.

Thomas, and John Stachurski. 2021. “Quantitative Economics (Python), Version 0.5.1.” QuantEcon. 2021. https://quantecon.org/quantecon-py/.Savani.

Rahul, and Bernhard von Stengel. 2014. “Game Theory Explorer: Software for the Applied Game Theorist.” Computational Management Science 12 (1): 5–33. https://doi.org/10.1007/s10287-014-0206-x.

Li, Jingyu. 2018. “Exploitability and Game Theory Optimal Play in Poker.” Boletín de Matemáticas 0 (0): 1. https://math.mit.edu/~apost/courses/18.204_2018/

Jingyu_Li_paper.pdf.“Rock Paper Scissors.” 2020. Wikipedia. August 21, 2020. https://en.wikipedia.org/wiki/Rock_paper_scissors.

```python

```


```python
# Erasmus+ ICCT project (2018-1-SI01-KA203-047081)

# Toggle cell visibility

from IPython.display import HTML
tag = HTML('''
Toggle cell visibility <a href="javascript:code_toggle()">here</a>.''')
display(tag)

# Hide the code completely

# from IPython.display import HTML
# tag = HTML('''<style>
# div.input {
#     display:none;
# }
# </style>''')
# display(tag)

```

Toggle cell visibility here.

## Costruire un osservatore per il sistema massa-molla-smorzatore

Questo esempio mostra come sviluppare un osservatore per il sistema massa-molla-smorzatore. Le equazioni scritte in forma di stato (vedi l'esempio [Analisi modale del sistema Massa-Molla-Smorzatore](SS-08-Analisi_modale_Massa-Molla-Smorzatore.ipynb) per maggiori dettagli) sono:

\begin{cases}
\begin{bmatrix}
\dot{x_1} \\
\dot{x_2}
\end{bmatrix}=\underbrace{\begin{bmatrix}
0 && 1 \\
-\frac{k}{m} && -\frac{c}{m}
\end{bmatrix}}_{A}\begin{bmatrix}
x_1 \\
x_2
\end{bmatrix}+\underbrace{\begin{bmatrix}
0 \\
\frac{1}{m}
\end{bmatrix}}_{B}u \\
y = \underbrace{\begin{bmatrix}1&0\end{bmatrix}}_{C}\begin{bmatrix}
x_1 \\
x_2
\end{bmatrix}
\end{cases}
con $m=1\,$kg, $k=2\,$N/m e $c=1\,$Ns/m. Gli autovalori corrispondenti sono $\lambda_{1,2} = -\frac{c}{2m} \pm \frac{\sqrt{c^2 - 4km}}{2m} = -\frac{1}{2} \pm i\frac{\sqrt{7}}{2}$.

La matrice di osservabilità ha rango pieno ed è uguale a:
$$
\begin{bmatrix}C\\CA\end{bmatrix} = \begin{bmatrix}1&0\\0&1\end{bmatrix},
$$
quindi il sistema è osservabile ed è possibile sviluppare un osservatore dello stato. Per avere una convergenza della stima in un tempo ragionevole, è conveniente impostare la dinamica dell'errore in modo che sia più rapida, o almeno 10 volte, rispetto alla dinamica del sistema. Gli autovalori scelti sono $\lambda_{\text{err} 1,2}=-10\sqrt{\left(\frac{1}{2}\right)^2+\left(\frac{\sqrt{7}}{2}\right)^2}=-10\sqrt{2}$.

La struttura dell'osservatore è:

$$
\dot{\hat{\textbf{x}}}=A\hat{\textbf{x}}+B\textbf{u}+L\textbf{y},
$$

con la matrice $L$ definita come $L = \begin{bmatrix}l_1&l_2\end{bmatrix}^T$. I valori necessari per avere la giusta convergenza (di $\dot{\textbf{e}}=(A-LC)\textbf{e}$) sono quindi:

\begin{cases}
l_1 = -c/m + 20\sqrt{2} = -1+20\sqrt{2}\\
l_2 = \frac{c^2}{m^2} - 20\sqrt{2}\frac{c}{m} - \frac{k}{m} + 200 = 197-20\sqrt{2}
\end{cases}

ottenuti imponendo $\text{det}(\lambda I_{2\text{x}2}-A+LC) = \left(\lambda+10\sqrt{2}\right)^2$.

### Come utilizzare questo notebook?
L'osservatore sviluppato è simulato di seguito e l'interfaccia interattiva consente di modificare tutti i valori e visualizzare i relativi cambiamenti nel comportamento.

```python
%matplotlib inline
import control as control
import numpy
import sympy as sym
from IPython.display import display, Markdown
import ipywidgets as widgets
import matplotlib.pyplot as plt

#print a matrix latex-like
def bmatrix(a):
     """Returns a LaTeX bmatrix - by Damir Arbula (ICCT project)

     :a: numpy array
     :returns: LaTeX bmatrix as a string
     """
     if len(a.shape) > 2:
         raise ValueError('bmatrix can at most display two dimensions')
     lines = str(a).replace('[', '').replace(']', '').splitlines()
     rv = [r'\begin{bmatrix}']
     rv += ['  ' + ' & '.join(l.split()) + r'\\' for l in lines]
     rv +=  [r'\end{bmatrix}']
     return '\n'.join(rv)

# Display formatted matrix:
def vmatrix(a):
    if len(a.shape) > 2:
         raise ValueError('bmatrix can at most display two dimensions')
    lines = str(a).replace('[', '').replace(']', '').splitlines()
    rv = [r'\begin{vmatrix}']
    rv += ['  ' + ' & '.join(l.split()) + r'\\' for l in lines]
    rv +=  [r'\end{vmatrix}']
    return '\n'.join(rv)

#matrixWidget is a matrix looking widget built with a VBox of HBox(es) that returns a numPy array as value !
class matrixWidget(widgets.VBox):
    def updateM(self,change):
        for irow in range(0,self.n):
            for icol in range(0,self.m):
                self.M_[irow,icol] = self.children[irow].children[icol].value
                #print(self.M_[irow,icol])
        self.value = self.M_

    def dummychangecallback(self,change):
        pass

    def __init__(self,n,m):
        self.n = n
        self.m = m
        self.M_ = numpy.matrix(numpy.zeros((self.n,self.m)))
        self.value = self.M_
        widgets.VBox.__init__(self,
                             children = [
                                 widgets.HBox(children =
                                              [widgets.FloatText(value=0.0, layout=widgets.Layout(width='90px')) for i in range(m)]
                                             )
                                 for j in range(n)
                             ])

        #fill in widgets and tell interact to call updateM each time a children changes value
        for irow in range(0,self.n):
            for icol in range(0,self.m):
                self.children[irow].children[icol].value = self.M_[irow,icol]
                self.children[irow].children[icol].observe(self.updateM, names='value')
        #value = Unicode('example@example.com', help="The email value.").tag(sync=True)
        self.observe(self.updateM, names='value', type= 'All')

    def setM(self, newM):
        #disable callbacks, change values, and reenable
        self.unobserve(self.updateM, names='value', type= 'All')
        for irow in range(0,self.n):
            for icol in range(0,self.m):
                self.children[irow].children[icol].unobserve(self.updateM, names='value')
        self.M_ = newM
        self.value = self.M_
        for irow in range(0,self.n):
            for icol in range(0,self.m):
                self.children[irow].children[icol].value = self.M_[irow,icol]
        for irow in range(0,self.n):
            for icol in range(0,self.m):
                self.children[irow].children[icol].observe(self.updateM, names='value')
        self.observe(self.updateM, names='value', type= 'All')

                #self.children[irow].children[icol].observe(self.updateM, names='value')

#overlaod class for state space systems that DO NOT remove "useless" states (what "professor" of automatic control would do this?)
class sss(control.StateSpace):
    def __init__(self,*args):
        #call base class init constructor
        control.StateSpace.__init__(self,*args)
    #disable function below in base class
    def _remove_useless_states(self):
        pass
```

```python
# Preparatory cell

A = numpy.matrix('0 1; -2 -1')
B = numpy.matrix('0; 1')
C = numpy.matrix('1 0')
X0 = numpy.matrix('2; 2')
L = numpy.matrix([[-1+20*numpy.sqrt(2)],[197-20*numpy.sqrt(2)]])
sol1 = numpy.linalg.eig(A)

Aw = matrixWidget(2,2)
Aw.setM(A)
Bw = matrixWidget(2,1)
Bw.setM(B)
Cw = matrixWidget(1,2)
Cw.setM(C)
X0w = matrixWidget(2,1)
X0w.setM(X0)
Lw = matrixWidget(2,1)
Lw.setM(L)

eig1o = matrixWidget(1,1)
eig2o = matrixWidget(2,1)
eig1o.setM(numpy.matrix([-10*numpy.sqrt(2)]))
eig2o.setM(numpy.matrix([[-10*numpy.sqrt(2)],[0]]))
```

```python
# Interactive widgets

m = widgets.FloatSlider(
    value=1,
    min=0.1,
    max=10.0,
    step=0.1,
    description='m [kg]:',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True,
    readout_format='.1f',
)
k = widgets.FloatSlider(
    value=2,
    min=0,
    max=10.0,
    step=0.1,
    description='k [N/m]:',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True,
    readout_format='.1f',
)
c = widgets.FloatSlider(
    value=1,
    min=0,
    max=10.0,
    step=0.1,
    description='c [Ns/m]:',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True,
    readout_format='.1f',
)
# Define the values of the input
u = widgets.FloatSlider(
    value=1,
    min=0,
    max=20.0,
    step=0.1,
    description='input u:',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True,
    readout_format='.1f',
)
period = widgets.FloatSlider(
    value=0.5,
    min=0.0,
    max=1,
    step=0.05,
    description='Periodo: ',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True,
    readout_format='.2f',
)

#create dummy widget
DW = widgets.FloatText(layout=widgets.Layout(width='0px', height='0px'))

#create button widget
START = widgets.Button(
    description='Test',
    disabled=False,
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Test',
    icon='check'
)

def on_start_button_clicked(b):
    #This is a workaround to have intreactive_output call the callback:
    #   force the value of the dummy widget to change
    if DW.value> 0 :
        DW.value = -1
    else:
        DW.value = 1
    pass
START.on_click(on_start_button_clicked)

# Define type of method
selm = widgets.Dropdown(
    options= [('Imposta L','Set L'), ('Imposta gli autovalori','Set the eigenvalues')],
    value= 'Set L',
    description='',
    disabled=False
)

# Define the number of complex eigenvalues for the observer
selo = widgets.Dropdown(
    options= [('0 autovalori complessi','0 complex eigenvalues'), ('2 autovalori complessi','2 complex eigenvalues')],
    value= '0 complex eigenvalues',
    description='Autovalori:',
    disabled=False
)

#define type of ipout
selu = widgets.Dropdown(
    options=[('impulso','impulse'), ('gradino','step'), ('sinusoide','sinusoid'), ('onda quadra','square wave')],
    value='impulse',
    description='Input:',
    disabled=False
)
```

```python
# Support functions

def eigen_choice(selo):
    if selo == '0 complex eigenvalues':
        eig1o.children[0].children[0].disabled = False
        eig2o.children[1].children[0].disabled = True
        eigo = 0
    if selo == '2 complex eigenvalues':
        eig1o.children[0].children[0].disabled = True
        eig2o.children[1].children[0].disabled = False
        eigo = 2
    return eigo

def method_choice(selm):
    if selm == 'Set L':
        method = 1
        selo.disabled = True
    if selm == 'Set the eigenvalues':
        method = 2
        selo.disabled = False
    return method
```

```python
def main_callback(m, k, c, X0w, L, eig1o, eig2o, u, period, selm, selo, selu, DW):
    A = numpy.matrix([[0,1],[-k/m,-c/m]])
    eigo = eigen_choice(selo)
    method = method_choice(selm)

    if method == 1:
        sol = numpy.linalg.eig(A-L*C)
    if method == 2:
        if eigo == 0:
            L = control.acker(A.T, C.T, [eig1o[0,0], eig2o[0,0]]).T
            Lw.setM(L)
        if eigo == 2:
            L = control.acker(A.T, C.T, [numpy.complex(eig2o[0,0],eig2o[1,0]),
                                         numpy.complex(eig2o[0,0],-eig2o[1,0])]).T
            Lw.setM(L)
        sol = numpy.linalg.eig(A-L*C)
    print('Gli autovalori del sistema sono:',round(sol1[0][0],4),'e',round(sol1[0][1],4))
    print('Gli autovalori dell\'osservatore sono:',round(sol[0][0],4),'e',round(sol[0][1],4))

    sys = sss(A,B,C,0)
    syso = sss(A-L*C, numpy.concatenate((B,L),axis=1), numpy.eye(2), numpy.zeros(4).reshape((2,2)))
    T = numpy.linspace(0, 6, 1000)

    if selu == 'impulse': #selu
        U = [0 for t in range(0,len(T))]
        U[0] = u
        T, yout, xout = control.forced_response(sys,T,U,X0w)
        T, youto, xouto = control.forced_response(syso,T,numpy.matrix([U,yout]),[[0],[0]])
    if selu == 'step':
        U = [u for t in range(0,len(T))]
        T, yout, xout = control.forced_response(sys,T,U,X0w)
        T, youto, xouto = control.forced_response(syso,T,numpy.matrix([U,yout]),[[0],[0]])
    if selu == 'sinusoid':
        U = u*numpy.sin(2*numpy.pi/period*T)
        T, yout, xout = control.forced_response(sys,T,U,X0w)
        T, youto, xouto = control.forced_response(syso,T,numpy.matrix([U,yout]),[[0],[0]])
    if selu == 'square wave':
        U = u*numpy.sign(numpy.sin(2*numpy.pi/period*T))
        T, yout, xout = control.forced_response(sys,T,U,X0w)
        T, youto, xouto = control.forced_response(syso,T,numpy.matrix([U,yout]),[[0],[0]])

    fig = plt.figure(num='Simulation', figsize=(16,10))

    fig.add_subplot(211)
    plt.ylabel('Posizione vs Posizione stimata (uscita del sistema)')
    plt.plot(T,xout[0])
    plt.plot(T,xouto[0])
    plt.xlabel('t [s]')
    plt.legend(['Reale','Stimata'])
    plt.axvline(x=0,color='black',linewidth=0.8)
    plt.axhline(y=0,color='black',linewidth=0.8)
    plt.grid()

    fig.add_subplot(212)
    plt.ylabel('Velocità vs Velocità stimata')
    plt.plot(T,xout[1])
    plt.plot(T,xouto[1])
    plt.xlabel('t [s]')
    plt.legend(['Reale','Stimata'])
    plt.axvline(x=0,color='black',linewidth=0.8)
    plt.axhline(y=0,color='black',linewidth=0.8)
    plt.grid()

alltogether = widgets.VBox([widgets.HBox([m,
                                          k,
                                          c]),
                            widgets.HBox([selm,
                                          selo,
                                          selu]),
                            widgets.Label(' ',border=3),
                            widgets.HBox([widgets.Label('L:',border=3), Lw,
                                          widgets.Label(' ',border=3),
                                          widgets.Label(' ',border=3),
                                          widgets.Label('Autovalori:',border=3),
                                          eig1o,
                                          eig2o,
                                          widgets.Label(' ',border=3),
                                          widgets.Label(' ',border=3),
                                          widgets.Label('X0:',border=3), X0w]),
                            widgets.Label(' ',border=3),
                            widgets.HBox([u,
                                          period,
                                          START])])
out = widgets.interactive_output(main_callback, {'m':m, 'k':k, 'c':c, 'X0w':X0w, 'L':Lw, 'eig1o':eig1o, 'eig2o':eig2o,
                                                 'u':u, 'period':period, 'selm':selm, 'selo':selo, 'selu':selu, 'DW':DW})
out.layout.height = '640px'
display(out, alltogether)
```

    Output(layout=Layout(height='640px'))

    VBox(children=(HBox(children=(FloatSlider(value=1.0, continuous_update=False, description='m [kg]:', max=10.0,…