This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.metrics import accuracy_score, auc, confusion_matrix, f1_score, precision_score, recall_score, roc_curve | |
def metrics_models(y_true, y_pred): | |
from sklearn.metrics import accuracy_score, auc, confusion_matrix, f1_score, precision_score, recall_score, roc_curve | |
# Obtención de matriz de confusión | |
confusion_matrix = confusion_matrix(y_true, y_pred) | |
print("La matriz de confusión es ") | |
print(confusion_matrix) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_model_1 = pd.concat([diagnostic[categorical_1], | |
pd.get_dummies(diagnostic['a1_g1_b'], | |
prefix = 'pclass')], axis = 1) | |
data_model_2 = pd.concat([diagnostic[categorical_2], | |
pd.get_dummies(diagnostic['a1_g2'], | |
prefix = 'pclass')], axis = 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Information Value Function | |
def calculateIV(data, features, target): | |
result = pd.DataFrame(index = ['IV'], columns = features) | |
result = result.fillna(0) | |
var_target = array(data[target]) | |
for cat in features: | |
var_values = array(data[cat]) | |
var_levels = unique(var_values) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_WoE(data, var, target): | |
crosstab = pd.crosstab(data[target], data[var]) | |
print("Obteniendo el Woe para la variable", var, ":") | |
for col in crosstab.columns: | |
if crosstab[col][1] == 0: | |
print(" El WoE para", col, "[", sum(crosstab[col]), "] es infinito") | |
else: | |
print(" El WoE para", col, "[", sum(crosstab[col]), "] es", np.log(float(crosstab[col][0]) / |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#We generate a function with the above tests to calculate the Beta of each record | |
def BetaCalculated(ticker_symbol, ref_index, source_web, sdate, edate): | |
try: | |
df_stock = web.DataReader(ticker_symbol,source_web,sdate,edate) | |
df_index = web.DataReader(ref_index,source_web,sdate,edate) | |
#df_stock = pdr.get_data_yahoo(ticker_symbol, start=sdate, end=edate) | |
#df_index = pdr.get_data_yahoo(ref_index, start=sdate, end=edate) | |
# create a time-series of monthly data points | |
df_stock = df_stock.resample('M').last() #We group by Month, i.e. one record per month of each year | |
df_index = df_index.resample('M').last() #We group by Month |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(edate) | |
numerator = covariance(df['stock_returns'],df['index_returns']) | |
print("COVARIANCE(stock, benchmark) = COVARIANCE("+ticker_symbol+", "+ref_index +") = " +str(numerator)) | |
denominator = covariance(df['index_returns'],df['index_returns']) | |
print("VARIANCE(benchmark) = COVARIANCE(benchmark, benchmark) = COVARIANCE("+ref_index+", "+ref_index +") = " +str(denominator)) | |
# BETA = Covariance (stock,index) / Variance (Index) = Covariance (stock,index) / Covariance (stock,stock) | |
print("BETA = COVARIANCE(stock, benchmark) / VARIANCE(benchmark) = " + str(numerator) + " / " + str( | |
denominator) + " = " +str(covariance(df['stock_returns'],df['index_returns'])/covariance( | |
df['index_returns'],df['index_returns']))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#We define a function to calculate the covariance | |
def covariance(a, b): | |
if len(a) != len(b): | |
return | |
a_mean = np.mean(a) | |
b_mean = np.mean(b) | |
sum = 0 | |
for i in range(0, len(a)): | |
sum += ((a[i] - a_mean) * (b[i] - b_mean)) | |
return sum/(len(a)-1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Calculating returns | |
# -*- coding: utf-8 -*- | |
""" | |
reference: http://gouthamanbalaraman.com/blog/calculating-stock-beta.html | |
""" | |
df_stock['returns'] = df_stock['Adj Close']/ df_stock['Adj Close'].shift(1) -1 | |
df_stock = df_stock.dropna() | |
df_index['returns'] = df_index['Adj Close']/ df_index['Adj Close'].shift(1) -1 | |
df_index = df_index.dropna() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m = folium.Map(location=[40,-4], zoom_start=6, width=700, height=500, control_scale=True, tiles='CartoDB Positron') | |
folium.Choropleth( | |
geo_data=geojson_counties, | |
name='Comunidades Autónomas', | |
data=df_casos, | |
columns=['CCAA', 'Casos'], | |
key_on='feature.properties.texto', #'features.properties.comunidade_autonoma' | |
fill_color='YlGn', | |
fill_opacity=0.5, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m = folium.Map(location=[40,-4], zoom_start=6, width=700, height=500, control_scale=True, tiles='CartoDB Positron') | |
folium.Choropleth( | |
geo_data=geojson_counties, | |
name='Comunidades Autónomas', | |
data=df_casos, | |
columns=['CCAA', 'Casos'], | |
key_on='feature.properties.texto', #'features.properties.comunidade_autonoma' | |
fill_color='YlGn', | |
fill_opacity=0.5, |