-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamic Pricing Model.py
65 lines (55 loc) · 3.15 KB
/
Dynamic Pricing Model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPRegressor
from statsmodels.tsa.arima.model import ARIMA
import numpy as np
import yfinance as yf
import requests
# Load banking data
df = pd.read_csv('banking_data.csv')
# Add more features
df['customer_segment'] = df['customer_type'].apply(lambda x: 1 if x == 'retail' else 0)
df['loan_type'] = df['loan_product'].apply(lambda x: 1 if x == 'mortgage' else 0)
df['collateral_type'] = df['collateral'].apply(lambda x: 1 if x == 'property' else 0)
df['dodd_frank_stress_test'] = df['stress_test_result'].apply(lambda x: 1 if x == 'pass' else 0)
df['ltv_ratio'] = df['loan_amount'] / df['collateral_value']
df['dti_ratio'] = df['monthly_debt'] / df['monthly_income']
# Use banking-specific data sources
fred_data = requests.get('https://api.stlouisfed.org/fred/series/observations?series_id=GS10&api_key=YOUR_API_KEY')
fred_df = pd.read_csv(fred_data.content)
df['gs10'] = fred_df['value']
# Incorporate regulatory requirements
df['basel_iii_capital'] = df['tier_1_capital'] + df['tier_2_capital']
df['dodd_frank_stress_test'] = df['stress_test_result'].apply(lambda x: 1 if x == 'pass' else 0)
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(df.drop('interest_rate', axis=1), df['interest_rate'], test_size=0.2, random_state=42)
# Train a random forest regressor model on the training data
rf_model = RandomForestRegressor(n_estimators=100, random_states=42)
rf_model.fit(X_train, y_train)
# Train a neural network regressor model on the training data
nn_model = MLPRegressor(hidden_layer_sizes=(50, 50), max_iter=1000, random_state=42)
nn_model.fit(X_train, y_train)
# Train an ARIMA model on the time-series data
arima_model = ARIMA(df['interest_rate'], order=(1, 1, 1))
arima_model_fit = arima_model.fit()
# Define a function to predict the interest rate based on the input features
def predict_interest_rate(customer_segment, loan_type, collateral_type, ltv_ratio, dti_ratio, gs10, basel_iii_capital, dodd_frank_stress_test):
input_data = pd.DataFrame({'customer_segment': [customer_segment], 'loan_type': [loan_type], 'collateral_type': [collateral_type], 'ltv_ratio': [ltv_ratio], 'dti_ratio': [dti_ratio], 'gs10': [gs10], 'basel_iii_capital': [basel_iii_capital], 'dodd_frank_stress_test': [dodd_frank_stress_test]})
rf.prediction = rf_model.predict(input_data)
mlp_prediction = mlp_model.predict(input_data)
arima_prediction = arima_model_fit.forecast(steps=1)[0]
return (rf_prediction + mlp_prediction + arima_prediction) / 3
# Example usage:
customer_segment = 1
loan_type = 1
collateral_type = 1
ltv_ratio = 0.5
dti_ratio = 0.4
gs10 = 2.5
basel_iii_capital = 100000
dodd_frank_stress_test = 1
predicted_interest_rate = predict_interest_rate(customer_segment, loan_type, collateral_type, ltv_ratio, dti_ratio, gs10, basel_iii_capital, dodd_frank_stress_test)
print(f'The predicted interest rate is: {interest_rate:.2f}%')