|
| 1 | +/* |
| 2 | + * Copyright 2019 The FATE Authors. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.webank.ai.fate.serving.federatedml.model; |
| 18 | + |
| 19 | +import com.webank.ai.fate.serving.common.model.MergeInferenceAware; |
| 20 | +import com.webank.ai.fate.serving.core.bean.Context; |
| 21 | +import com.webank.ai.fate.serving.core.bean.Dict; |
| 22 | +import com.webank.ai.fate.serving.core.constant.StatusCode; |
| 23 | +import com.webank.ai.fate.serving.core.exceptions.GuestMergeException; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +public class Scorecard extends BaseComponent implements Returnable, MergeInferenceAware{ |
| 32 | + |
| 33 | + private static final Logger logger = LoggerFactory.getLogger(Scorecard.class); |
| 34 | + private static final double FLOAT_ZERO = 1e-8; |
| 35 | + |
| 36 | + @Override |
| 37 | + public int initModel(byte[] protoMeta, byte[] protoParam) { |
| 38 | + return 0; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public Object getParam() { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Map<String, Object> localInference(Context context, List<Map<String, Object>> input |
| 48 | + ) { |
| 49 | + // do nothing, calc in merge |
| 50 | + return new HashMap<>(0); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Map<String, Object> mergeRemoteInference(Context context, List<Map<String, Object>> localData, Map<String, Object> remoteData) { |
| 55 | + logger.error("score card merge input local {}, remote {}", localData, remoteData); |
| 56 | + Map<String, Object> data = localData.get(0); |
| 57 | + Map<String, Object> result = this.handleRemoteReturnData(remoteData); |
| 58 | + double localScore = 0; |
| 59 | + if ((int) result.get(Dict.RET_CODE) == StatusCode.SUCCESS) { |
| 60 | + if (data.get(Dict.SCORE) != null) { |
| 61 | + localScore = ((Number) data.get(Dict.SCORE)).doubleValue(); |
| 62 | + } else { |
| 63 | + throw new GuestMergeException("local result is invalid "); |
| 64 | + } |
| 65 | + result.put(Dict.SCORE, this.scorecardCalc(localScore)); |
| 66 | + } |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + private double scorecardCalc(double score) { |
| 71 | + int upper_limit_ratio = 3; |
| 72 | + int offset = 500; |
| 73 | + int upper_limit_value = upper_limit_ratio * offset; |
| 74 | + int lower_limit_value = 0; |
| 75 | + |
| 76 | + double credit_score; |
| 77 | + if(Math.abs(score - 0) <= FLOAT_ZERO && score >= 0){ |
| 78 | + credit_score = upper_limit_value; |
| 79 | + }else if(Math.abs(score - 1) <= FLOAT_ZERO && score > 0){ |
| 80 | + credit_score = lower_limit_value; |
| 81 | + }else if(score > 1 || score < 0){ |
| 82 | + credit_score = -1; |
| 83 | + } else{ |
| 84 | + double odds = (double) ((1 - score) / score); |
| 85 | + int factor = 20; |
| 86 | + int factor_base = 2; |
| 87 | + credit_score = offset + factor / Math.log(factor_base) * Math.log(odds); |
| 88 | + } |
| 89 | + if (credit_score > upper_limit_value) { |
| 90 | + credit_score = upper_limit_value; |
| 91 | + } |
| 92 | + if(credit_score < lower_limit_value){ |
| 93 | + credit_score = lower_limit_value; |
| 94 | + } |
| 95 | + credit_score = Math.round(credit_score * 100.0) / 100.0; |
| 96 | + return credit_score; |
| 97 | + } |
| 98 | +} |
0 commit comments