Python+经济学:成本函数与图像

Python+经济学:成本函数与图像

前言:

本文可以说是上一篇文章的“姊妹篇”,同样利用一元二次函数图像的性质,构造边际成本函数(Marginal Cost Function),然后在此基础上还原各种成本函数。为了让函数构造更加精准。笔者首先进行了一个简单的函数绘图作为调试环节。

调试环节——简单的一元二次函数与其轴对称图形绘制:

函数1: [公式]

函数2: [公式]

函数3: [公式]

Y1与Y2关于x轴对称,而Y3则确保Y2上移后最小值大于零。

调试环节的代码实现:

import matplotlib.pyplot as plt

import numpy as np

#调试语句,判断Y3图形位置

n = 200

X = np.linspace(0,50,n)

Y1 = []

Y2 = []

Y3 = []

for i in range(len(X)):

    x = X[i]

    y1 = -(x**2)+50*x

    y2 = (x**2)-50*x

    y3 = (x**2)-50*x+750

    Y1.append(y1)

    Y2.append(y2)

    Y3.append(y3)

fig = plt.figure(1)

ax1 = plt.subplot(111)

ax1.plot(X, Y1,'r',label='y1=-x^2+50x')

ax1.plot(X, Y2,'g',label='y2=x^2-50x')

ax1.plot(X, Y3,'b',label='y3=x^2-50x+750')

ax1.set_xlabel('X')

ax1.set_ylabel('Y')

ax1.spines['top'].set_visible(False)

ax1.spines['right'].set_visible(False)

ax1.spines['bottom'].set_position(('data', 0))

ax1.spines['left'].set_position(('data',0))

plt.legend(['y1=-x^2+50x','y2=x^2-50x','y3=x^2-50x+750'])

plt.show()

调试环节效果:


接下来将函数3作为边际成本函数(MC),并加上固定(不变成本),构造TC/TFC/TVC/AC/AVC/AFC等相关函数。

边际成本: [公式]

总不变成本: [公式]

成本函数代码实现:

import matplotlib.pyplot as plt

import numpy as np

#成本函数参数设定

m = 100

Quantity = np.linspace(1,50,m)

MC = []

TC = []

TFC = []

AC = []

AVC = []

AFC = []

for j in range(len(Quantity)):

    q = Quantity[j]

    tfc = 1000

    mc = (q**2) -50*q+750

    tvc =(1/3)*q**3-25*q**2+750*q

    tc = tvc + tfc

    afc = tfc/q

    avc = tvc/q

    ac = tc/q

    MC.append(mc)

    TC.append(tc)

    TFC.append(tfc)

    AC.append(ac)

    AVC.append(avc)

    AFC.append(afc)

#第一个图,输出总成本、边际成本、平均成本、平均可变成本

fig = plt.figure(2,figsize=(8,8))

ax2 = plt.subplot(211)

#总成本线为红色

ax2.plot(Quantity, TC,'red',label='Total Cost')

ax2.set_xlabel('Quantity')

ax2.set_ylabel('Cost')

ax2.spines['top'].set_visible(False)

ax2.spines['right'].set_visible(False)

ax2.spines['bottom'].set_position(('data', 0))

ax2.spines['left'].set_position(('data',0))

plt.legend(['Total Cost'])

ax3 = plt.subplot(212)

#边际成本线为蓝色

ax3.plot(Quantity, MC,'blue',label='Marginal Cost')

#平均成本线为绿色

ax3.plot(Quantity, AC,'green',label='Average Cost')

#平均可变成本线为深粉色

ax3.plot(Quantity, AVC,'hotpink',label='Average Variable Cost')

ax3.set_xlabel('Quantity')

ax3.set_ylabel('Cost')

ax3.spines['top'].set_visible(False)

ax3.spines['right'].set_visible(False)

ax3.spines['bottom'].set_position(('data', 0))

ax3.spines['left'].set_position(('data',0))

plt.legend(['Marginal Cost','Average Cost','Average Variable Cost'])

#第二个图,输出总不变成本与平均不变成本

fig = plt.figure(3)

ax4 = plt.subplot(111)

#总不变成本线为棕色

ax4.plot(Quantity, TFC,'brown',label='Total Fixed Cost')

#平均不变成本线为金黄色

ax4.plot(Quantity, AFC,'gold',label='Average Fixed Cost')

ax4.set_xlabel('Quantity')

ax4.set_ylabel('Cost')

ax4.spines['top'].set_visible(False)

ax4.spines['right'].set_visible(False)

ax4.spines['bottom'].set_position(('data', 0))

ax4.spines['left'].set_position(('data',0))

plt.legend(['Total Fixed Cost','Average Fixed Cost'])

plt.show()

成本函数图形:


MC曲线从下往上依次穿过AVC曲线和AC曲线

总不变成本与平均不变成本,AFC的大小代表AVC和AC曲线之间的距离

编辑于 2022-02-19 22:27

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容