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

103 lines
3.5 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():
refer = list(map(float, refers[key]))
weight = weights[key]
if value is None:
score = 0
elif value > refer[0]:
score = weight
elif value > refer[1]:
standard_score = round(weight * 1, 2)
score = linear_correlation_type(value, standard_score, refer[0], refer[1])
elif value > refer[2]:
standard_score = round(weight * 0.75, 2)
score = linear_correlation_type(value, standard_score, refer[1], refer[2])
elif value > refer[3]:
standard_score = round(weight * 0.5, 2)
score = linear_correlation_type(value, standard_score, refer[2], refer[3])
elif value > refer[4]:
standard_score = round(weight * 0.25, 2)
score = linear_correlation_type(value, standard_score, 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
def linear_correlation_type(value, standard_score, refer1, refer2):
"""
线性相关类型 正相关/反相关
Parameters
value float 指标值
standard_score float 标准分
refer1 阈值1
refer2 阈值2
Returns
score float 得分
"""
# main
# 正相关打分
if refer1 > refer2:
if value <= refer2:
score = 0
elif value >= refer1:
score = standard_score
else:
score = standard_score * (1 - (refer1 - value) / (refer1 - refer2))
# 反相关打分
else:
if value <= refer2:
score = standard_score
elif value >= refer1:
score = 0
else:
score = standard_score * (1 - (value - refer2) / (refer1 - refer2))
return score