(* Here we analyze stability and calculate the variance for a wide range of control networks associated  with mouse airway epithelium *)

R[1]={{-1},{2},{0}}; R[2]={{-1},{0},{2}}; R[3]={{1},{0},{0}}; R[4]={{0},{-1},{2}}; R[5]={{0},{1},{0}}; R[6]={{1},{-1},{0}}; R[7]={{0},{-1},{0}}; R[8]={{0},{0},{-1}}; R[9]={{0},{1},{0}}; R[10]={{0},{0},{1}}; R[11]={{0},{0},{1}};

nproc=11;x0=.; y0=.; z0=.; (* Make sure that all the functions are kept general (not specified) *) Quiet[Do[Q0[i]=.;Qx[i]=.; Qy[i]=.; Qz[i]=.,{i,1,nproc}]];Quiet[Do[QK0[i]=.;QKx[i]=.; QKy[i]=.; QKz[i]=.,{i,1,nproc}]]

(* Siplify the system by setting some of the controls to zero *)

(* z does not control anything except Q8 *)  Do[ Qz[i]=0,{i,1,nproc}];Qz[8]=.;(* y-processes are not controlled by y *) Do[Qy[i]=0,{i,4,8}];Qy[11]=0;(* x-processes are not controlled by x *) Do[Qx[i]=0,{i,7,8}];  Do[Qx[i]=0,{i,1,3}]; 
(* asymmetric divisions of x are controlled by y only *) Do[Qx[i]=0; Qz[i]=0,{i,9,10}];

(* Preparing matrixes for the analysis *) Do[QQz[i]=QK0[i]+QKx[i]*(x-x0)+QKy[i]*(y-y0)+QKz[i]*(z-z0),{i,1,nproc}];QQQ=Table[QQz[i],{i,1,nproc}]; RR=Table[R[i],{i,1,nproc}]

(* Obtain the constraint on the equilibrium values of the rates, equation (14) of the main text *) Zerosol=Simplify[Solve[Simplify[QQQ.RR/.{x->x0,y->y0,z->z0}]==0,{QK0[6], QK0[7], QK0[8]}]]

(* Make sure the steady state is balanced *)Ksub=Table[QK0[i]->Q0[i],{i,1,nproc}];
 Q0[6]=QK0[6]/.Zerosol[[1]]/.Ksub; Q0[7]=QK0[7]/.Zerosol[[1]]/.Ksub; Q0[8]=QK0[8]/.Zerosol[[1]]/.Ksub; Do[QQ[i]=Q0[i]+Qx[i]*(x-x0)+Qy[i]*(y-y0)+Qz[i]*(z-z0),{i,1,nproc}]

(* This lists the functions for all the processes 1--11 *) TableForm[Table[QQ[i],{i,1,nproc}]]

(* Study stability *)

(* Define the Jacobian, formula (15) of the main text *) var[1]=x; var[2]=y; var[3]=z;Do[Do[aa[m,j]=Sum[(D[QQ[k],var[j]]/.{x->x0,y->y0,z->z0})*R[k][[m]][[1]],{k,1,nproc}],{m,1,3}],{j,1,3}];Q1=Table[aa[m,j],{m,1,3},{j,1,3}]

(* Skip the next two lines if the general analysis is performed; otherwise set to zero some of the rates. The two examples below correspnd to purely symmetruc and purely asymmetric divisions *)

(* Only asymm divisions *) Do[Q0[i]=0; Qx[i]=0; Qy[i]=0; Qz[i]=0,{i,1,5}]

(* Only symmetric*) Do[Q0[i]=0;Qx[i]=0; Qy[i]=0;Qz[i]=0,{i,9,11}]

(* Eigenvalues *) EV=Simplify[Eigenvalues[Q1]]

(* Characteristic polynomial *) CP=CharacteristicPolynomial[Q1,lam]

(* Study variance *)

(* This calculates all the 2nd moments by using equation (24) of the main text. The output is the variances y11, y22, and y33 *) var[1]=x; var[2]=y; var[3]=z;SOL=Transpose[{Flatten[Table[sol[i,j],{i,1,3},{j,1,3}]]}]; BigMatr=(KroneckerProduct[Q1,IdentityMatrix[3]]+KroneckerProduct[IdentityMatrix[3],Q1]);RHS=Transpose[{Flatten[Table[-Sum[((QQ[k]/.{x->x0,y->y0,z->z0})*R[k][[i]][[1]]*R[k][[j]][[1]]),{k,1,nproc}],{i,1,3},{j,1,3}]]}];Otv=Solve[BigMatr.SOL==RHS,Flatten[SOL]]; VARI=Simplify[Table[sol[i,i],{i,1,3}]/.Otv][[1]]