Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for ogrn in set_ogrn:
- if pd.isna(ogrn) != True:
- # result_migr = {}
- result_migr = defaultdict(list)
- time.sleep(0.01)
- my_bar.progress(int(100 * counter / len(set_ogrn)), text=progress_text)
- counter += 1
- # result = {}
- # result_migr = {}
- pr = data_1.loc[data_1[col_ogrn] == ogrn].reset_index(drop=True).sort_values(col_date)
- # start_dates = pr[col_date][0]
- start_dates = start_date
- # end_dates = pr[col_date][len(pr) - 1]
- while start_dates < (datetime.strptime(end_dates, "%Y-%m-%d")).strftime('%Y-%m-%d'):
- data_prev = pr[(pr[col_date] < start_dates)]
- end_date = (datetime.strptime(start_dates, "%Y-%m-%d") + curent_step).strftime('%Y-%m-%d')
- temp_df = pr[(pr[col_date] >= start_dates) & (pr[col_date] <= end_date)].reset_index().drop(columns=['index']).sort_values(col_date)
- first = pr[col_rating].iloc[0] # Если только одна запись, берем ее
- if len(pr) > 1:
- transitions = []
- prev_rating = pr[col_rating].iloc[0] # Начинаем с первого рейтинга
- for i in range(1, len(pr)): # Проходим по записям начиная со второго элемента
- cur_rating = pr[col_rating].iloc[i]
- prev_year = pd.to_datetime(pr[col_date].iloc[i - 1]).year
- cur_year = pd.to_datetime(pr[col_date].iloc[i]).year
- if cur_year - prev_year >= 1:
- # print(ogrn, 'yes')
- pass # Пропускаем большие разрывы во времени
- else:
- result_migr[prev_rating].append(cur_rating)
- # transitions.append((prev_rating, cur_rating)) # Запоминаем переход
- prev_rating = cur_rating # Обновляем "предыдущее" значение
- else:
- if first in agency_dict:
- result_migr[first].append(first)
- transition_counts = defaultdict(Counter)
- #
- # if first == 'D':
- # print(ogrn, result_migr)
- for start_rating in full_dict.keys():
- if start_rating in result_migr:
- for end_rating in result_migr[start_rating]:
- if start_rating == "CCC-C" and end_rating == "D":
- counter_def += 1
- transition_counts[start_rating][end_rating] += 1
- full_[start_rating][end_rating] += 1
- else:
- for end_rating_ in full_dict.keys():
- transition_counts[start_rating][end_rating_] += 0
- full_[start_rating][end_rating_] += 0
- transition_df = pd.DataFrame.from_dict(transition_counts, orient='index').fillna(0)
- # print(ogrn, transition_df)
- transition_df["Total"] = transition_df.sum(axis=1)
- df_sum = pd.DataFrame.from_dict(full_, orient='index').fillna(0)
- df_sum["Total"] = df_sum.sum(axis=1)
- # st.write(df_sum)
- # st.write(transition_df["Total"])
- for key, value_d in full_dict.items():
- for key_d, value_in_d in value_d.items():
- if key_d in result_migr.values():
- counter_CC_D += 1
- n_ = df_sum.at[key, "Total"]
- if key in result_migr:
- if key_d in result_migr[key]:
- # if key_d == key:
- # # print(key_d, key)
- old_rank = agency_dict[key]
- new_rank = agency_dict[key_d]
- penalty = abs((new_rank - old_rank))
- if penalty == 0:
- penalty = 1
- t_alpha = value_in_d["alpha"]
- t_beta = value_in_d["beta"]
- full_dict[key][key_d]["alpha"] = t_alpha + transition_counts[key][key_d]
- full_dict[key][key_d]["beta"] = t_beta + (transition_df.at[key, "Total"] - transition_counts[key][key_d])
- weight = 1 / (1 + np.exp(-tau * (n_ - n0)))
- if transition_counts[key][key_d] == 0:
- old_rank = agency_dict[key]
- new_rank = agency_dict[key_d]
- # penalty = (new_rank - old_rank) ** 2
- penalty = abs((new_rank - old_rank))
- if penalty == 0:
- penalty = 1
- full_dict[key][key_d]["beta"] += np.exp(penalty) * (1 - weight)
- start_dates = (datetime.strptime(start_dates, "%Y-%m-%d") + full_step).strftime('%Y-%m-%d')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement