<jupyter_start><jupyter_text>ADG with two real variables and Covariance inequality**author:<NAME>** alessiobenavoli.com We will learn how to build a PyRational **ADG (Almost Desirable Gambles)** belief model on the outcome of an experiment whose space of possibility is $\mathbb{R}^2$.<jupyter_code>%load_ext autoreload
%autoreload 2
from __future__ import absolute_import 
from PyRational.models.ADG import  ADG
from sympy import symbols,  Interval, Piecewise, Eq, exp
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>*PyRational* uses *Sympy* for symbolic mathematics. We need to define in *Sympy*  a `symbol` associated to the real variable and its relative domain  (we use Sympy `Interval` for the latter).<jupyter_code>x1=symbols('x1', real=True)
x2=symbols('x2', real=True)
domain_x=[Interval(-10,10),Interval(-10,10)]
model = ADG([x1,x2],domain_x)
model<jupyter_output><empty_output><jupyter_text>We assume that our agent, Alice, finds the following gambles desirable.<jupyter_code>G=[]
G.append( x1) 
G.append(-x1) 
G.append(x1**2-1)
G.append(1-x1**2)    
G.append( x2) 
G.append(-x2) 
G.append(x2**2-1)
G.append(1-x2**2)<jupyter_output><empty_output><jupyter_text>We add all these gambles to `model` as follows:<jupyter_code>model.add_gambleList(G)
model
model.Gambles<jupyter_output><empty_output><jupyter_text>Note that $G$ is a list that includes all Alice's desirable gambles. We now `build` the belief modeland check if it avods sure loss:<jupyter_code>optimoptions={'method_LISP': 'Cutting_plane', #'Cutting_plane', 'discretise'
                       'SolverLP':'linprog', #'linprog', 'cplex'
                       'LP_acc_constraints':1e-8,
                       'SolverNLP':'differential_evolution',
                       'NLP_alpha_cut':-0.00001,
                       'num_support_points': 150,
                       'verbose':False}
model.buildModel(options=optimoptions)
model.check_avs(options=optimoptions)
model<jupyter_output>/home/benavoli/anaconda3/lib/python3.6/site-packages/scipy/optimize/_linprog_ip.py:1262: OptimizeWarning: Solving system with option 'sym_pos':True failed. It is normal for this to happen occasionally, especially as the solution is approached. However, if you see this frequently, consider setting option 'sym_pos' to False.
  OptimizeWarning)
/home/benavoli/anaconda3/lib/python3.6/site-packages/scipy/optimize/_linprog_ip.py:1274: OptimizeWarning: Solving system with option 'sym_pos':False failed. This may happen occasionally, especially as the solution is approached. However, if you see this frequently, your problem may be numerically challenging. If you cannot improve the formulation, consider setting 'lstsq' to True. Consider also setting `presolve` to True, if it is not already.
  OptimizeWarning)<jupyter_text>So Alice is **rational** or, equivalently, her set of desirable gambles is coherent.  InferenceAssume Alice is interested in computing her maximum buying/minimum selling price for the gamble$$f=x_1 x_2$$ We can do that as follows:<jupyter_code>f_range=(None,None)
f=x1*x2  
lp=model.lower_prevision(f,f_range,options=optimoptions)
up=model.upper_prevision(f,f_range,options=optimoptions)
print(lp,str("<= E[x_1 x_2] <="), up)<jupyter_output>-0.9955920104136098 <= E[x_1 x_2] <= 0.9983673733966469<jupyter_text>We have obtained the covariance inequality for standardised variables:$$|E(X_1X_2)|^2 \leq E(X_1^2) E(X_2^2).$$which implies$$-1=-\sqrt{E(X_1^2) E(X_2^2)} \leq E(X_1X_2) \leq \sqrt{E(X_1^2) E(X_2^2)}=1.$$  Structual judgments (independence) Under a judgment of 'independence', if Alice finds the gambles in $G_x$ on $x_1$ desirable and the gamblesin $G_y$ on $x_2$ desirable, she should also find the gambles in $G_x \otimes G_y$ desirable<jupyter_code>Gx=[]
Gx.append( Piecewise((1,True)) ) # constant 1
Gx.append( x1) 
Gx.append(-x1) 
Gx.append(x1**2-1)
Gx.append(1-x1**2)    

Gy=[]
Gy.append( Piecewise((1,True)) )
Gy.append( x2) 
Gy.append(-x2) 
Gy.append(x2**2-1)
Gy.append(1-x2**2) 

Gprod=[a*b for a in Gx for b in Gy]
Gprod
model1 = ADG([x1,x2],domain_x)
model1.add_gambleList(Gprod)
model1.buildModel(options=optimoptions)
model1.check_avs(options=optimoptions)
model1
f_range=(None,None)
f=x1*x2  
lp=model1.lower_prevision(f,f_range,options=optimoptions)
up=model1.upper_prevision(f,f_range,options=optimoptions)
print(lp,str("<= E[x_1 x_2] <="), up)<jupyter_output>/home/benavoli/anaconda3/lib/python3.6/site-packages/scipy/optimize/_linprog_ip.py:1262: OptimizeWarning: Solving system with option 'sym_pos':True failed. It is normal for this to happen occasionally, especially as the solution is approached. However, if you see this frequently, consider setting option 'sym_pos' to False.
  OptimizeWarning)
/home/benavoli/anaconda3/lib/python3.6/site-packages/scipy/optimize/_linprog_ip.py:1274: OptimizeWarning: Solving system with option 'sym_pos':False failed. This may happen occasionally, especially as the solution is approached. However, if you see this frequently, your problem may be numerically challenging. If you cannot improve the formulation, consider setting 'lstsq' to True. Consider also setting `presolve` to True, if it is not already.
  OptimizeWarning)
{'fraction_non_alphanumeric': 0.11577341216860576, 'fraction_numerical': 0.03166126263589548, 'mean_word_length': 5.555, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '257765', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1810, 'n_words': 482, 'path': 'notebooks/Two_Real_Variables_and_Covariance_Inequality.ipynb', 'repo_name': 'PyRational/PyRational', 'size': '5253'}