# How to get the average x of a density function?

I have this code:

(* Expenditure share on all other goods *)
\[Alpha]=0.86;
(*\[Alpha]=0.86;*)
(* Cost share of housing capital in housing production function *)
\[Beta]=0.6;
(* Scaling on housing production function *)
g=0.0005;
(* Radians available for construction *)
(* benchmark \[Theta] is 3 *)
\[Theta]=3;

p[x_,y_,t_,u_,f_,ta_,fa_]:=(((\[Alpha]^\[Alpha])((1-\[Alpha])^(1-\[Alpha])) (y-(t+ta)*x-(f+fa))/u)^(1/(1-\[Alpha]));
ptax[x_,y_,t_,u_,\[Tau]_,f_,ta_,fa_]:=p[x,y,t,u,f,ta,fa]*(1-\[Tau]);
q[x_,y_,t_,u_,f_,ta_,fa_]:=(((1-\[Alpha]) (y-(t+ta)*x-(f+fa))/(((\[Alpha]^\[Alpha])((1-\[Alpha])^(1-\[Alpha])) (y-(t+ta)*x-(f+fa)))/u)^(1/(1-\[Alpha])));
S[x_,y_,t_,u_,\[Tau]_,f_,ta_,fa_]:=(1/(ptax[x,y,t,u,\[Tau],f,ta,fa](\[Beta])(g)))^(1/(\[Beta]-1));
r[x_,y_,t_,u_,\[Tau]_,f_,ta_,fa_]:=(ptax[x,y,t,u,\[Tau],f,ta,fa](g))((1/(ptax[x,y,t,u,\[Tau],f,ta,fa](\[Beta])(g)))^(\[Beta]/(\[Beta]-1)))-1((1/(ptax[x,y,t,u,\[Tau],f,ta,fa](\[Beta])(g)))^(1/(\[Beta]-1)));
h[x_,y_,t_,u_,\[Tau]_,f_,ta_,fa_]:=(g)S[x,y,t,u,\[Tau],f,ta,fa]^(\[Beta]);
Density[x_,y_,t_,u_,\[Tau]_,f_,ta_,fa_]:=h[x,y,t,u,\[Tau],f,ta,fa]/q[x,y,t,u,f,ta,fa];
L[xbar_,y_,t_,u_,\[Tau]_,f_,ta_,fa_]:=\!$$\*SubsuperscriptBox[\(\[Integral]$$, $$0$$, $$xbar$$]$$\[Theta]\ x\ Density[x, y, t, u, \[Tau], f, ta, fa] \[DifferentialD]x$$\);
xavg[xbar_,pop_,y_,t_,u_,\[Tau]_,f_,ta_,fa_]:=(1/pop)*\!$$\*SubsuperscriptBox[\(\[Integral]$$, $$0$$, $$xbar$$]$$\[Theta]\ x^2\ Density[x, y, t, u, \[Tau], f, ta, fa] \[DifferentialD]x$$\); The gist of it is that I'm creating a city, where x is the distance from the central business district, p is price of housing, ptax is price of housing after property tax, q is quantity of housing demanded, S is structural density, r is rental price of land, h is floor to area ratio, density is the population density, and L is the total population. The last function is my addition.

I want to find out what the average distance is to the CBD. Xbar is where the city ends. So between the values where x is 0 and xbar there is a population of people whose distribution is defined by Density. I want to know what the average x is for these people in this interval. I thought the function would be what the L function is, but that gives the total population. What am I doing wrong?

• There seems to be multiple problems in the code. 1. What is $\alpha, \beta, g, \theta$? 2. Remove x from the definition of xavg: xavg[xbar_, pop_, y_, t_, u_, \[Tau]_, f_, ta_, fa_] := .... 3. If you care only about the numerical value, try NIntegrate instead of Integrate. Oct 14, 2021 at 11:38
• I updated the code. Oct 14, 2021 at 11:48
• The code now works and xavg gets evaluated, but it's still difficult to provide any help (the question now seems more of a "mathematical" one). Are you sure your definitions and expressions are all correct (they are probably copied from a book or an article?). Can you give an example of your calculation (e.g. xavg[5, .1, .1, .1, .1, .1, .1, .1]) that you think is incorrect, and explain what the correct answer should be? Technically speaking, your approach seems correct, i.e. $\langle x \rangle = \int x \ \rho(x) \, \mathrm{d} x$. Oct 14, 2021 at 12:01
• Yeah my question is more whether the xavg function actually gives the number I'm looking for. Whether it's the correct function to use to get the average distance from the centre. Oct 14, 2021 at 12:33