2021-12-17 14:36:38 +08:00
|
|
|
|
import pandas as pd
|
|
|
|
|
|
2021-12-14 17:30:48 +08:00
|
|
|
|
from Rating.db import find_threshold
|
2021-12-27 13:51:18 +08:00
|
|
|
|
from common.scripts import read_json_file
|
2021-12-14 17:30:48 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-20 14:23:58 +08:00
|
|
|
|
def financial_score(param1, param2):
|
2021-12-14 17:30:48 +08:00
|
|
|
|
"""
|
|
|
|
|
财务要素进行打分
|
|
|
|
|
Parameters:
|
2021-12-20 14:23:58 +08:00
|
|
|
|
param1 string 二级行业
|
2021-12-14 17:30:48 +08:00
|
|
|
|
param2 dict 财务指标
|
|
|
|
|
weights dict 财务指标权重
|
|
|
|
|
refers dict 行业阈值
|
|
|
|
|
refer list 指标阈值
|
|
|
|
|
Returns:
|
|
|
|
|
scores dict 财务要素得分
|
|
|
|
|
"""
|
|
|
|
|
# Parameters
|
2021-12-27 13:51:18 +08:00
|
|
|
|
weights = read_json_file('/Rating/static/weights.json')
|
2021-12-20 14:23:58 +08:00
|
|
|
|
refers = find_threshold(param1)[0]
|
2021-12-14 17:30:48 +08:00
|
|
|
|
|
|
|
|
|
# Returns
|
|
|
|
|
scores = dict()
|
|
|
|
|
|
|
|
|
|
# main
|
|
|
|
|
for key, value in param2.items():
|
2022-01-25 16:50:14 +08:00
|
|
|
|
# 已获利息倍数为None,分数为满分
|
2022-01-25 14:22:05 +08:00
|
|
|
|
if key == '已获利息倍数' and value is None:
|
|
|
|
|
scores[key] = 5
|
2022-01-25 16:50:14 +08:00
|
|
|
|
elif value is None:
|
|
|
|
|
scores[key] = 0
|
2021-12-14 17:30:48 +08:00
|
|
|
|
else:
|
2022-01-25 14:22:05 +08:00
|
|
|
|
refer = list(map(float, refers[key]))
|
|
|
|
|
weight = weights[key]
|
2022-02-09 16:07:12 +08:00
|
|
|
|
standard = [weight, weight*0.8, weight*0.6, weight*0.4, weight*0.2]
|
|
|
|
|
# 判断正反相关
|
2022-01-25 16:50:14 +08:00
|
|
|
|
if refer[0] > refer[1]:
|
|
|
|
|
if value >= refer[0]:
|
|
|
|
|
score = weight
|
|
|
|
|
elif value > refer[1]:
|
2022-02-09 16:07:12 +08:00
|
|
|
|
score = linear_correlation_type(value, standard[0], standard[1], refer[0], refer[1])
|
2022-01-25 16:50:14 +08:00
|
|
|
|
elif value > refer[2]:
|
2022-02-09 16:07:12 +08:00
|
|
|
|
score = linear_correlation_type(value, standard[1], standard[2], refer[1], refer[2])
|
2022-01-25 16:50:14 +08:00
|
|
|
|
elif value > refer[3]:
|
2022-02-09 16:07:12 +08:00
|
|
|
|
score = linear_correlation_type(value, standard[2], standard[3], refer[2], refer[3])
|
2022-01-25 16:50:14 +08:00
|
|
|
|
elif value > refer[4]:
|
2022-02-09 16:07:12 +08:00
|
|
|
|
score = linear_correlation_type(value, standard[3], standard[4], refer[3], refer[4])
|
2022-01-25 16:50:14 +08:00
|
|
|
|
else:
|
|
|
|
|
score = 0
|
2022-01-25 14:22:05 +08:00
|
|
|
|
else:
|
2022-01-25 16:50:14 +08:00
|
|
|
|
if value <= refer[0]:
|
|
|
|
|
score = weight
|
|
|
|
|
elif value < refer[1]:
|
2022-02-09 16:07:12 +08:00
|
|
|
|
score = linear_correlation_type(value, standard[0], standard[1], refer[0], refer[1])
|
2022-01-25 16:50:14 +08:00
|
|
|
|
elif value < refer[2]:
|
2022-02-09 16:07:12 +08:00
|
|
|
|
score = linear_correlation_type(value, standard[1], standard[2], refer[1], refer[2])
|
2022-01-25 16:50:14 +08:00
|
|
|
|
elif value < refer[3]:
|
2022-02-09 16:07:12 +08:00
|
|
|
|
score = linear_correlation_type(value, standard[2], standard[3], refer[2], refer[3])
|
2022-01-25 16:50:14 +08:00
|
|
|
|
elif value < refer[4]:
|
2022-02-09 16:07:12 +08:00
|
|
|
|
score = linear_correlation_type(value, standard[3], standard[4], refer[3], refer[4])
|
|
|
|
|
else:
|
|
|
|
|
score = 0
|
2022-01-25 14:22:05 +08:00
|
|
|
|
scores[key] = round(score, 2)
|
2021-12-14 17:30:48 +08:00
|
|
|
|
|
2021-12-17 14:36:38 +08:00
|
|
|
|
result = dict()
|
|
|
|
|
|
|
|
|
|
fin1 = ['净资产收益率', '总资产报酬率']
|
|
|
|
|
fin2 = ['总资产周转率', '应收账款周转率', '存货周转率']
|
|
|
|
|
fin3 = ['资产负债率', '已获利息倍数', '速动比率']
|
|
|
|
|
fin4 = ['营业增长率', '总资产增长率', '技术投入比率']
|
|
|
|
|
|
|
|
|
|
result['盈利能力'] = dict((key, value) for key, value in scores.items() if key in fin1)
|
2021-12-17 15:31:31 +08:00
|
|
|
|
result['资产质量'] = dict((key, value) for key, value in scores.items() if key in fin2)
|
|
|
|
|
result['债务风险'] = dict((key, value) for key, value in scores.items() if key in fin3)
|
|
|
|
|
result['经营增长'] = dict((key, value) for key, value in scores.items() if key in fin4)
|
2021-12-17 14:36:38 +08:00
|
|
|
|
|
|
|
|
|
df_scores = pd.DataFrame([scores])
|
2021-12-20 15:15:09 +08:00
|
|
|
|
result['盈利能力']['合计'] = round(float(df_scores[fin1].sum(axis=1).values[0]), 2)
|
|
|
|
|
result['资产质量']['合计'] = round(float(df_scores[fin2].sum(axis=1).values[0]), 2)
|
|
|
|
|
result['债务风险']['合计'] = round(float(df_scores[fin3].sum(axis=1).values[0]), 2)
|
|
|
|
|
result['经营增长']['合计'] = round(float(df_scores[fin4].sum(axis=1).values[0]), 2)
|
2021-12-17 14:36:38 +08:00
|
|
|
|
|
|
|
|
|
result['合计'] = round(sum(scores.values()), 2)
|
|
|
|
|
|
|
|
|
|
return result
|
2021-12-14 17:30:48 +08:00
|
|
|
|
|
|
|
|
|
|
2022-02-09 16:07:12 +08:00
|
|
|
|
def linear_correlation_type(value, standard1, standard2, refer1, refer2):
|
2021-12-14 17:30:48 +08:00
|
|
|
|
"""
|
|
|
|
|
线性相关类型 正相关/反相关
|
|
|
|
|
Parameters
|
|
|
|
|
value float 指标值
|
2022-02-09 16:07:12 +08:00
|
|
|
|
standard1 float 标准分1
|
|
|
|
|
standard2 float 标准分2
|
2021-12-14 17:30:48 +08:00
|
|
|
|
refer1 阈值1
|
|
|
|
|
refer2 阈值2
|
|
|
|
|
Returns
|
|
|
|
|
score float 得分
|
|
|
|
|
"""
|
|
|
|
|
# main
|
2022-02-09 16:07:12 +08:00
|
|
|
|
score = standard2 + (value - refer2) / (refer1 - refer2) * (standard1 - standard2)
|
2022-01-25 16:50:14 +08:00
|
|
|
|
|
|
|
|
|
return score
|
|
|
|
|
|