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

106 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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