tfse-model-api-v0.2/Rating/scripts/financial_score.py

106 lines
4.1 KiB
Python
Raw Normal View History

2021-12-17 14:36:38 +08:00
import pandas as pd
from Rating.db import find_threshold
from common.scripts import read_json_file
2021-12-20 14:23:58 +08:00
def financial_score(param1, param2):
"""
财务要素进行打分
Parameters:
2021-12-20 14:23:58 +08:00
param1 string 二级行业
param2 dict 财务指标
weights dict 财务指标权重
refers dict 行业阈值
refer list 指标阈值
Returns:
scores dict 财务要素得分
"""
# Parameters
weights = read_json_file('/Rating/static/weights.json')
2021-12-20 14:23:58 +08:00
refers = find_threshold(param1)[0]
# Returns
scores = dict()
# main
for key, value in param2.items():
2022-01-25 16:50:14 +08:00
# 已获利息倍数为None分数为满分
if key == '已获利息倍数' and value is None:
scores[key] = 5
2022-01-25 16:50:14 +08:00
elif value is None:
scores[key] = 0
else:
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
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
scores[key] = round(score, 2)
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
2022-02-09 16:07:12 +08:00
def linear_correlation_type(value, standard1, standard2, refer1, refer2):
"""
线性相关类型 正相关/反相关
Parameters
value float 指标值
2022-02-09 16:07:12 +08:00
standard1 float 标准分1
standard2 float 标准分2
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