forked from abishekchiffon/Risk_prediction_Mutual_funds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEDA_random_forest.py
1286 lines (957 loc) · 45.4 KB
/
EDA_random_forest.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#%%
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
from pprint import pprint as pprint
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import OneHotEncoder
import pandas as pd
from sklearn.model_selection import GridSearchCV
#%%
import pandas as pd
# Sample data frame mimicking the user's data structure.
# In a real scenario, you would load the data from a CSV or Excel file.
data = pd.read_csv("data/Morningstar - European Mutual Funds.csv")
# Convert the data into a pandas DataFrame.
df = pd.DataFrame(data)
#%%
import pandas as pd
# Assuming 'df' is your pandas DataFrame
# Set pandas options to display all columns
pd.set_option('display.max_columns', None)
pd.set_option('display.expand_frame_repr', False)
#%%
len(df)
#%%
df.head()
#%%
df.describe()
#%%
pd.set_option('display.max_columns', None)
print(df.columns.to_list())
#%%
print(df.info())
#%% [markdown]
# # Question 1
#%% [markdown]
# What is the average ongoing cost of Mutual Funds with based on the rating?
#%%
average_costs_by_rating = df.groupby('rating')['ongoing_cost'].mean()
# Create a bar plot
average_costs_by_rating.plot(kind='bar', color='skyblue', figsize=(8, 6))
plt.xlabel('Rating')
plt.ylabel('Average Ongoing Cost')
plt.title('Average Ongoing Cost of Mutual Funds by Rating')
plt.xticks(rotation=0) # Rotate the x-axis labels to be horizontal
plt.show()
#%% [markdown]
# As expected the high rating funds are more cost efficient
#%% [markdown]
# A well-managed fund with slightly higher fees might be a better choice than a poorly managed low-cost fund. The expertise and track record of the fund management team can be a crucial factor.
#%% [markdown]
# # Question 2
#%% [markdown]
# How many funds have achieved a sustainability score above 80 percentile and also have a top
# quartile performance rating?
#%%
# Define the 80th percentile for sustainability score
sustainability_80th_percentile = df['sustainability_score'].quantile(0.8)
# Filter the DataFrame for funds with a sustainability score above the 80th percentile
high_sustainability_funds = df[df['sustainability_score'] > sustainability_80th_percentile]
# Further filter for funds that also have a top quartile performance rating (assuming 1 is the top quartile)
top_funds = high_sustainability_funds[high_sustainability_funds['performance_rating'] == 5]
# Count the number of such funds
number_of_top_funds = top_funds.shape[0]
number_of_top_funds
#%%
hig_sus_funds_count = high_sustainability_funds.groupby('performance_rating')["ticker"].count()
total_funds = high_sustainability_funds.shape[0]
percent_by_rating = (hig_sus_funds_count / total_funds) * 100
# Create a bar plot
percent_by_rating.plot(kind='bar', color='skyblue', figsize=(8, 6))
plt.xlabel('Performance rating')
plt.ylabel('Sustainability_score')
plt.title('No of Mutual Funds with high sustanability score for each rating')
plt.xticks(rotation=0) # Rotate the x-axis labels to be horizontal
plt.show()
#%%
percent_by_rating
#%% [markdown]
# ### Analysis:
#
# * The largest bar is above "3.0", suggesting that the majority of funds with a moderate performance rating also have high sustainability scores.
# * The second-largest group is funds with a "4.0" rating, indicating that many above-average performance-rated funds also prioritize sustainability.
#
# * Interestingly, the bar for the highest performance rating ("5.0") is the shortest, which might imply that fewer top-rated funds have high sustainability scores compared to those with moderate ratings.
# * There appears to be a trend where the moderate-performing funds (ratings "2.0" to "4.0") have more high sustainability scores than the lowest ("1.0") and highest ("5.0") rated funds.
# #### Implications:
# This could indicate that the highest-performing funds may not necessarily prioritize sustainability to the same extent as moderately rated funds, or it could reflect the distribution of sustainability initiatives across different performance levels.
# ##### Considerations for Investors :
# * For investors who prioritize sustainability, this graph suggests that they might find more options with high sustainability scores among funds with moderate performance ratings rather than the highest-rated funds.
#%% [markdown]
# # Question 3
#
#%% [markdown]
# How do funds with a high environmental score (above 70) compare in terms of ROA,
# ROE, and ROIC to funds with a low environmental score (below 30)?
#%%
percentiles_roa = df['roa'].quantile([0.25, 0.5, 0.75])
percentiles_roa
#%%
percentiles_roe = df['roe'].quantile([0.25, 0.5, 0.75])
percentiles_roe
#%%
percentiles_roic = df['roic'].quantile([0.25, 0.5, 0.75])
percentiles_roic
#%%
percentiles_env_score = df['environmental_score'].quantile([0.25, 0.5, 0.75])
percentiles_env_score
#%%
print(percentiles_env_score.iloc[2])
#%%
# Define the high and low environmental score thresholds.
high_score_threshold = percentiles_env_score.iloc[2]
low_score_threshold = percentiles_env_score.iloc[0]
# Calculate the average ROA, ROE, and ROIC for high and low environmental score funds.
high_score_funds = df[df['environmental_score'] > high_score_threshold]
low_score_funds = df[df['environmental_score'] < low_score_threshold]
# Calculate averages
high_score_averages = high_score_funds[['roa', 'roe', 'roic']].mean()
low_score_averages = low_score_funds[['roa', 'roe', 'roic']].mean()
high_score_averages, low_score_averages
#%%
# Create a bar plot
fig, ax = plt.subplots()
bar_width = 0.35
opacity = 0.8
# Convert the index to a numerical array for plotting
index = np.arange(len(high_score_averages))
bar1 = plt.bar(index, high_score_averages, bar_width, alpha=opacity, color='b', label='High Environmental Score')
bar2 = plt.bar(index + bar_width, low_score_averages, bar_width, alpha=opacity, color='g', label='Low Environmental Score')
plt.xlabel('Metrics')
plt.ylabel('Averages')
plt.title('Average ROA, ROE, ROIC by Environmental Score')
plt.xticks(index + bar_width / 2, high_score_averages.index)
plt.legend()
plt.tight_layout()
plt.show()
#%% [markdown]
# * The funds with high environmental score has lower performance compared to the funds with low environmental score as expected
#%% [markdown]
# # comparing high and low environmental score funds using boxplots
#%%
# Combine high and low score funds into a single DataFrame for plotting
high_score_funds['Score Group'] = 'High Score'
low_score_funds['Score Group'] = 'Low Score'
combined_data = pd.concat([high_score_funds, low_score_funds])
# Melting the DataFrame to have a long format suitable for seaborn boxplot
melted_data = combined_data.melt(id_vars=[ 'Score Group'], value_vars=['roa', 'roe', 'roic'],
var_name='Metric', value_name='Value')
# Creating the boxplot
plt.figure(figsize=(10, 6))
sns.boxplot(x='Metric', y='Value', hue='Score Group', data=melted_data)
plt.title('Boxplot of ROA, ROE, and ROIC for High and Low Environmental Score Funds')
plt.show()
#%%
melted_data
#%% [markdown]
# We observe the same thing.
# * The funds with high environmental score has lower performance compared to the funds with low environmental score as expected
#%% [markdown]
# # Question 4
#%% [markdown]
# What is the average fund size for each fund category?
#%%
# Group by 'fund_category' and calculate the average 'fund_size' for each group
average_fund_size_by_category = df.groupby('category')['fund_size'].mean()
average_fund_size_by_category
#%%
percent_nan = df['category'].isna().mean() * 100
#%%
percent_nan
#%%
percent_nan = df['fund_size'].isna().mean() * 100
#%%
percent_nan
#%%
df = df.dropna(subset=['fund_size'])
#%%
percent_nan = df['fund_size'].isna().mean() * 100
percent_nan
#%%
# Find the top 10 fund categories based on average fund size
top_10_categories = average_fund_size_by_category.nlargest(10)
# Find the bottom 10 fund categories based on average fund size
bottom_10_categories = average_fund_size_by_category.nsmallest(10)
top_10_categories, bottom_10_categories
#%%
# Create a bar plot
top_10_categories.plot(kind='bar', color='skyblue', figsize=(8, 6))
plt.xlabel('Categories')
plt.ylabel('Fund size')
plt.title('Funds size for top 10 category')
plt.xticks(rotation=90) # Rotate the x-axis labels to be horizontal
plt.show()
#%%
# Create a bar plot
bottom_10_categories.plot(kind='bar', color='skyblue', figsize=(8, 6))
plt.xlabel('Categories')
plt.ylabel('Fund size')
plt.title('Funds size for bottom 10 category')
plt.xticks(rotation=90) # Rotate the x-axis labels to be horizontal
plt.show()
#%% [markdown]
# Most of the largest funds are japanese fund and most of the bottom funds are from middle East.
#%% [markdown]
# # Question 5
#%% [markdown]
# How has the average Price to Earnings (P/E) ratio changed over the past five years
# across different equity styles?
#%%
percent_nan = df[['equity_style','nav_per_share','fund_return_2015','fund_return_2016','fund_return_2017','fund_return_2018','fund_return_2019']].isna().mean() * 100
percent_nan
#%%
len(df)
#%%
df = df.dropna(subset=['equity_style','nav_per_share','fund_return_2015','fund_return_2016','fund_return_2017','fund_return_2018','fund_return_2019'])
#%%
len(df)
#%%
#df = pd.DataFrame(df)
# Assuming the return can be used as a proxy for earnings growth (very speculative)
df['eps_2015'] = df['nav_per_share'] * df['fund_return_2015']
df['eps_2016'] = df['nav_per_share'] * df['fund_return_2016']
df['eps_2017'] = df['nav_per_share'] * df['fund_return_2017']
df['eps_2018'] = df['nav_per_share'] * df['fund_return_2018']
df['eps_2019'] = df['nav_per_share'] * df['fund_return_2019']
# Calculate for other years similarly...
# Now calculate P/E-like ratio for each year
df['pe_2015'] = df['nav_per_share'] / df['eps_2015']
df['pe_2016'] = df['nav_per_share'] / df['eps_2016']
df['pe_2017'] = df['nav_per_share'] / df['eps_2017']
df['pe_2018'] = df['nav_per_share'] / df['eps_2018']
df['pe_2019'] = df['nav_per_share'] / df['eps_2019']
# Calculate for other years similarly...
# Aggregate by equity style
pe_by_style_and_year = df.groupby('equity_style').agg({'pe_2015': 'mean', 'pe_2016': 'mean', 'pe_2017': 'mean', 'pe_2018': 'mean', 'pe_2019': 'mean'})
# Add other years similarly...
#%%
df[['eps_2015','eps_2016','pe_2015','pe_2016', 'pe_2017', 'pe_2018','pe_2019']].isna().mean() * 100
#%%
pe_by_style_and_year
#%%
for col in ['pe_2015', 'pe_2016', 'pe_2017', 'pe_2018','pe_2019']:
df[col].replace([np.inf, -np.inf], np.nan, inplace=True)
#%%
df[['eps_2015','eps_2016','pe_2015','pe_2016', 'pe_2017', 'pe_2018','pe_2019']].isna().mean() * 100
#%%
df = df.dropna(subset=['eps_2015','eps_2016','pe_2015','pe_2016', 'pe_2017', 'pe_2018','pe_2019'])
#%%
df[['eps_2015','eps_2016','pe_2015','pe_2016', 'pe_2017', 'pe_2018','pe_2019']].isna().mean() * 100
#%%
len(df)
#%%
pe_by_style_and_year = df.groupby('equity_style').agg({'pe_2015': 'mean', 'pe_2016': 'mean', 'pe_2017': 'mean', 'pe_2018': 'mean', 'pe_2019': 'mean'})
pe_by_style_and_year
#%%
# Convert the DataFrame from wide format to long format
pe_melted = pe_by_style_and_year.reset_index().melt(id_vars='equity_style', value_vars = ['pe_2015','pe_2016', 'pe_2017', 'pe_2018','pe_2019'], var_name='Year', value_name='PE Ratio')
# Rename the columns appropriately
#pe_melted.rename(columns={'index': 'Equity Style'}, inplace=True)
# Create a seaborn bar plot
plt.figure(figsize=(8, 6))
sns.barplot(data=pe_melted, x='equity_style', y='PE Ratio', hue='Year')
plt.title('Average PE Ratio by Equity Style')
#%%
pe_melted
#%% [markdown]
#
#
# ### Graph 1: Average PE Ratio by Equity Style
#
# #### Variation Across Years and Styles:
#
# There is variation in the average P/E ratio both across different equity styles and across the years from 2015 to 2019.
# The P/E ratios for 'Growth' and 'Value' equity styles have fluctuated over the years, whereas 'Blend' seems to have a more stable P/E ratio over time.
# #### Negative Values:
#
# There are negative P/E ratios displayed in the graph. Negative P/E ratios can occur if the earnings (denominator in the P/E calculation) are negative, meaning the company or fund reported a loss.
# Trends:
#
# For 'Blend' equity style, there was a significant dip in 2018, which suggests a decrease in price relative to earnings or an increase in earnings relative to price.
# 'Growth' style shows a notable decrease in P/E ratio from 2018 to 2019, indicating it became less expensive or earnings growth outpaced the price increase.
# 'Value' style shows an increase in P/E ratio from 2018 to 2019, indicating it may have become more expensive relative to its earnings or that the earnings growth was slower than the price increase.
#
# **We enquired and found out that these are the reasons for economic downturn in 2018**
# * The trumps's trade war with china
# * Federal reserve raising interest rate quickly
# * Inflated company earnings.
#
#%%
# Create a seaborn bar plot
plt.figure(figsize=(8, 6))
sns.barplot(data=pe_melted, x='Year', y='PE Ratio', hue='equity_style')
plt.title('Average PE Ratio by Equity Style')
#%% [markdown]
# Growth' style has a clear pattern of decreasing P/E ratios over the years, which could imply a decrease in the valuation multiples or an increase in earnings growth.
# 'Blend' and 'Value' styles do not show a clear trend over the years, suggesting inconsistent changes in their P/E ratios.
#%% [markdown]
# 2018 Anomaly:
#
# The year 2018 stands out with significantly negative P/E ratios for all equity styles. This could indicate a widespread market event that caused losses in earnings or an anomaly in the data.
# Stability in 'Value' Style:
#%% [markdown]
# # what is the funds return growth from 2015 to 2020 for different equity_size, can you code line graph
#%%
# Analyzing the funds return growth from 2015 to 2020 for different equity sizes
# Assuming the relevant columns for yearly returns are 'fund_return_2015', ..., 'fund_return_2020'
# and 'equity_size' indicates the equity size of the fund
# Check if the relevant columns exist
year_columns = ['fund_return_2015', 'fund_return_2016', 'fund_return_2017',
'fund_return_2018', 'fund_return_2019']
if 'equity_style' in df.columns and all(year in df.columns for year in year_columns):
# Grouping by equity size and calculating the average return for each year
avg_return_by_equity_size = df.groupby('equity_style')[year_columns].mean()
# Plotting the line graph
plt.figure(figsize=(12, 8))
for equity_size in avg_return_by_equity_size.index:
plt.plot(avg_return_by_equity_size.columns, avg_return_by_equity_size.loc[equity_size, :],
marker='o', label=equity_size)
plt.xlabel('Year')
plt.ylabel('Average Fund Return (%)')
plt.title('Fund Return Growth from 2015 to 2020 by Equity style')
plt.legend()
plt.grid(True)
plt.show()
else:
plt.text(0.5, 0.5, 'Some specified columns are missing from the DataFrame.',
horizontalalignment='center', verticalalignment='center')
plt.title('Data Unavailable')
plt.show()
#%% [markdown]
# 2018 has the least growth of all years.
# In 2019, growth style gives more growth as expected.
#%% [markdown]
# # Question 6
#%% [markdown]
# What is the correlation between the sustainability score and fund trailing returns over 1,
# 3, and 5 years?
#%%
correlation_data = df[['sustainability_score', 'fund_trailing_return_ytd', 'fund_trailing_return_3years', 'fund_trailing_return_5years']].corr()
# The resulting DataFrame 'correlation_data' will contain the correlation coefficients between all pairs of these variables
correlation_data
#%%
# Create the heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_data, annot=True, cmap='coolwarm')
plt.title('Correlation Heatmap')
plt.show()
#%% [markdown]
# These variables 'fund_trailing_return_ytd', 'fund_trailing_return_3years', 'fund_trailing_return_5years' are highly correlated.
#%% [markdown]
# But the sustanibility score is not correlated with them
#%% [markdown]
# Question 7
# Calculate the average fund return for each risk rating category during the last quarter
#%%
import pandas as pd
# Assuming df is your DataFrame
# Replace 'your_dataframe.csv' with the actual file name if you are reading from a CSV file
# df = pd.read_csv('your_dataframe.csv')
# Group by 'risk_rating' and calculate the mean of 'fund_return_2019'
average_returns = df.groupby('risk_rating')['fund_return_2019'].mean()
print(average_returns)
#%%
# Creating the bar graph
plt.figure(figsize=(10, 6))
plt.bar(average_returns.index, average_returns, color='skyblue')
plt.xlabel('Risk Rating')
plt.ylabel('Average Fund Return in 2019 (%)')
plt.title('Average Fund Return in 2019 by Risk Rating')
plt.xticks(list(average_returns.index))
plt.show()
#%% [markdown]
# As we can see the funds with high risk has high returns in the year 2019
#%% [markdown]
# # Question 8
#
# what are the fund percentage in each of the different credit ratings like a,aa,aaa
#%%
df.head(50).to_excel('data/first_50_rows.xlsx', index=False)
#%%
average_credit_a = df['credit_a'].mean()
average_credit_aa = df['credit_aa'].mean()
average_credit_aaa = df['credit_aaa'].mean()
#%%
credit_ratings = ['A', 'AA', 'AAA']
average_percentages = [average_credit_a, average_credit_aa, average_credit_aaa]
# Creating the bar graph
plt.figure(figsize=(10, 6))
plt.bar(credit_ratings, average_percentages, color='teal')
plt.xlabel('Credit Rating')
plt.ylabel('Average Percentage of Assets (%)')
plt.title('Average Percentage of Fund Assets in Different Credit Ratings')
plt.xticks(credit_ratings)
plt.show()
#%% [markdown]
# Most of the funds are in AAA ratings. People prefer their assets to be highly rated funds.
#%% [markdown]
# # Question 10
#%% [markdown]
# For funds with a high risk rating, what investment strategies are commonly employed?
#%%
# Filter the DataFrame for high risk rating funds
high_risk_funds = df[df['risk_rating'] == df['risk_rating'].max()]
# Extracting the investment strategies
investment_strategies_high_risk = high_risk_funds['investment_strategy'].value_counts()
pprint(investment_strategies_high_risk)
#%% [markdown]
# ### Strategies followed by the high risk rating Funds.
#
# US Growth Funds Investment Objective: This strategy seeks long-term capital appreciation, primarily through investing in securities issued by US companies and, to a lesser extent, in securities from non-US companies. The criteria for considering a company to be from a particular country or region includes its principal securities trading market location, revenue sources, or country of organization.
#
# European Property Funds Investment Objective: This strategy aims for long-term capital appreciation by investing in the equity securities of companies in the European real estate industry. This includes property development companies, those engaged in ownership of income-producing property, and collective investment vehicles with property exposure.
#
# Investing in Asian (Excluding Japan) Equities: One strategy focuses on achieving capital appreciation by investing principally in the equity securities of companies domiciled in Asia (excluding Japan) or with significant Asian operations. This may include a mix of equities, fixed income securities, and other instruments.
#
# Investing in Asian (Excluding Japanese) Equities: Another strategy involves long-term capital growth through investment in a portfolio of Asian (excluding Japanese) equities. This includes companies domiciled in, based in, or conducting a major part of their business in Asia, including both developed markets and emerging markets.
#
# These investment strategies represent a focus on geographic diversification and specific industry sectors, reflecting the varied approaches taken by high risk-rated funds to achieve capital appreciation. The count next to each strategy indicates the number of funds employing that particular strategy.
#
#%% [markdown]
# # Question 11
#%% [markdown]
# Which fund category has shown the most improvement in average return over the past three years?
#%%
# Calculating the improvement in average return for each fund category over the past three years
# Assuming the relevant columns for yearly returns are 'fund_return_2019', 'fund_return_2018', and 'fund_return_2017'
# Checking if these columns exist in the DataFrame
if all(item in df.columns for item in ['fund_return_2019', 'fund_return_2018', 'fund_return_2017']):
# Calculating average returns for each year and each category
avg_return_2019 = df.groupby('category')['fund_return_2019'].mean()
avg_return_2018 = df.groupby('category')['fund_return_2018'].mean()
avg_return_2017 = df.groupby('category')['fund_return_2017'].mean()
# Calculating improvement from 2017 to 2019
improvement = avg_return_2019 - avg_return_2017
# Identifying the category with the most improvement
most_improved_category = improvement.idxmax()
most_improvement_value = improvement.max()
else:
most_improved_category = None
most_improvement_value = None
most_improved_category, most_improvement_value
#%% [markdown]
# Russian Equity has shown the most improvement in average return over the past
# three years, 39% from 2017 to 2019
#%%
# Creating box plots to compare 'fund_return_2019' across different categorical variables
# Selecting a few categorical columns for the analysis
categorical_columns = ['category', 'fund_benchmark', 'morningstar_benchmark']
#%%
import matplotlib.pyplot as plt
import seaborn as sns
def plot_pretty_categorical_analysis(df, column, top_n=10):
"""Plot aesthetically improved box plots for the top N categories in a given column."""
top_categories = df[column].value_counts().head(top_n).index
filtered_df = df[df[column].isin(top_categories)]
plt.figure(figsize=(14, 8))
sns.boxplot(x=column, y='fund_return_2019', data=filtered_df, palette='viridis')
plt.title(f'Fund Return 2019 by {column}', fontsize=16, fontweight='bold')
plt.xlabel(column, fontsize=14)
plt.ylabel('Fund Return 2019 (%)', fontsize=14)
plt.xticks(rotation=90)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()
# List of categorical columns to plot
categorical_columns = ['category', 'fund_benchmark', 'morningstar_benchmark']
# Plotting for each categorical column with enhanced aesthetics
for col in categorical_columns:
plot_pretty_categorical_analysis(df, col)
#%% [markdown]
# ### Analysis from category
#
# * Global Large cap Growth equity performs better.
# * GBP Moderate allocation has lowest fund return
# * Japan large cap equity performs bad and indicates it has not returned back from the fall from late 1990s
#
#%% [markdown]
# # Prediction Models
#%% [markdown]
# * After the analysis, we removed the columns with more than 30% of missing values and imputed the rest with mean, median and mode accordingly.
# * we decided to predict the fund_return_2019 using regression methods.
#%%
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import skew
import statsmodels.api as sm
from sklearn.feature_selection import SelectKBest, mutual_info_classif
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from scipy.stats import pointbiserialr
m=pd.read_csv("data/Morningstar - European Mutual Funds.csv")
# Assuming df is your DataFrame
threshold = 0.3 # Set the threshold for missing values (30%)
# Calculate the percentage of missing values in each column
missing_percentages = m.isnull().mean()
# Identify columns with missing values exceeding the threshold
columns_to_drop = missing_percentages[missing_percentages > threshold].index
# Drop columns with more than 30% missing values
df = m.drop(columns=columns_to_drop)
# Alternatively, you can modify the original DataFrame in place
# df.drop(columns=columns_to_drop, inplace=True)
# Display the resulting DataFrame
#print(df)
#print(df.columns)
numerical_cols = df.select_dtypes(include=['number']).columns
# Iterate through each numerical column
for col in numerical_cols:
# Check if there are missing values in the column
if df[col].isnull().any():
# Calculate mean and median
mean_val = df[col].mean()
median_val = df[col].median()
# Determine whether to impute with mean or median
if abs(mean_val - median_val) < 0.5: # Adjust the threshold as needed
imputation_value = mean_val
imputation_method = 'mean'
else:
imputation_value = median_val
imputation_method = 'median'
# Impute missing values with either mean or median
df[col].fillna(imputation_value, inplace=True)
print(f"Imputed missing values in '{col}' with {imputation_method} ({imputation_value:.3f}).")
# Display the DataFrame with imputed values
print(df)
print(len(df.select_dtypes(include=['object']).columns))
print(len(df.select_dtypes(include=['float64', 'int64']).columns))
categorical_cols = df.select_dtypes(include=['object']).columns
# Impute missing values with mode for each categorical column
for col in categorical_cols:
mode_value = df[col].mode()[0] # Mode may have multiple values, so we take the first one
df[col].fillna(mode_value, inplace=True)
print(df.isnull().sum().sum())
#%% [markdown]
# ## RF model fund_return_2019
# ### Linear regression used for feature selection
#
# Inaddition to correlation and anova for feature selection, we also did linear regression and picked the significant columns
#%%
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import pearsonr, linregress
import statsmodels.api as sm
from statsmodels.formula.api import ols
# Assuming df is your DataFrame
cols_fr_rf = [
'asset_stock', 'asset_bond', 'involvement_abortive_contraceptive',
'involvement_animal_testing', 'fund_trailing_return_3years',
'fund_return_2018_q4', 'fund_return_2018_q2', 'fund_return_2017_q4',
'fund_return_2017_q3', 'fund_return_2017_q1'
]
# Adding the target variable
cols_fr_rf.append('fund_return_2019')
# Subset the DataFrame
df_subset = df[cols_fr_rf]
# 1. Pearson Correlation Coefficient
correlation_matrix = df_subset.corr()
print(correlation_matrix['fund_return_2019'])
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Heatmap')
plt.show()
# 2. Scatter Plots
for col in cols_fr_rf[:-1]: # Excluding the target variable itself
sns.scatterplot(x=col, y='fund_return_2019', data=df_subset)
plt.title(f"Scatter Plot: {col} vs fund_return_2019")
plt.show()
# 3. Linear Regression Analysis
for col in cols_fr_rf[:-1]: # Excluding the target variable itself
slope, intercept, r_value, p_value, std_err = linregress(df_subset[col], df_subset['fund_return_2019'])
print(f"Linear Regression for {col}: R-squared = {r_value**2:.3f}, p-value = {p_value:.3f}")
#%% [markdown]
# ### Observations
#
# * asset_stock : Has strong correlation with fund_return_2019 as expected.
# * asset_bond : Has negative correlation with fund_return_2019
# * invovlement_animal_testing, involvemnt_abortive_contraceptive : has strong correlation just like we saw in EDA.
# * All the previous year returns has good correlation with fund_return_2019.
#%%
from scipy.stats import linregress
cols_fr_rf = [
'asset_stock', 'asset_bond', 'asset_cash', 'asset_other', 'ongoing_cost',
'management_fees', 'involvement_abortive_contraceptive', 'involvement_alcohol',
'involvement_animal_testing', 'involvement_controversial_weapons',
'involvement_gambling', 'involvement_gmo', 'involvement_military_contracting',
'involvement_nuclear', 'involvement_palm_oil', 'involvement_pesticides',
'involvement_small_arms', 'involvement_thermal_coal', 'involvement_tobacco',
'shareclass_size', 'fund_size', 'fund_trailing_return_ytd',
'fund_trailing_return_3years', 'fund_return_2018_q4', 'fund_return_2018_q3',
'fund_return_2018_q2', 'fund_return_2018_q1', 'fund_return_2017_q4',
'fund_return_2017_q3', 'fund_return_2017_q2', 'fund_return_2017_q1',
'quarters_up', 'quarters_down','nav_per_share', 'fund_return_2019'
]
# Assuming df_subset is your DataFrame with selected columns
for col in cols_fr_rf[:-1]: # Excluding the target variable itself
# Perform linear regression
slope, intercept, r_value, p_value, std_err = linregress(df[col], df['fund_return_2019'])
# Print the results including the coefficient
print(f"Linear Regression for {col}: Coefficient = {slope:.3f}, R-squared = {r_value**2:.3f}, p-value = {p_value:.3f}")
#%%
import matplotlib.pyplot as plt
# Given results from linear regression
results = {
'asset_stock': {'Coefficient': 0.131, 'p-value': 0.000},
'asset_bond': {'Coefficient': -0.103, 'p-value': 0.000},
'asset_cash': {'Coefficient': -0.087, 'p-value': 0.000},
'asset_other': {'Coefficient': -0.096, 'p-value': 0.000},
'ongoing_cost': {'Coefficient': 1.432, 'p-value': 0.000},
'management_fees': {'Coefficient': 2.612, 'p-value': 0.000},
'involvement_abortive_contraceptive': {'Coefficient': 0.439, 'p-value': 0.000},
'involvement_alcohol': {'Coefficient': 0.860, 'p-value': 0.000},
'involvement_animal_testing': {'Coefficient': 0.273, 'p-value': 0.000},
'involvement_controversial_weapons': {'Coefficient': 0.946, 'p-value': 0.000},
'involvement_gambling': {'Coefficient': -0.105, 'p-value': 0.001},
'involvement_gmo': {'Coefficient': -0.798, 'p-value': 0.000},
'involvement_military_contracting': {'Coefficient': 1.170, 'p-value': 0.000},
'involvement_nuclear': {'Coefficient': 0.007, 'p-value': 0.653},
'involvement_palm_oil': {'Coefficient': -0.683, 'p-value': 0.000},
'involvement_pesticides': {'Coefficient': -0.072, 'p-value': 0.090},
'involvement_small_arms': {'Coefficient': 1.778, 'p-value': 0.000},
'involvement_thermal_coal': {'Coefficient': 0.001, 'p-value': 0.966},
'involvement_tobacco': {'Coefficient': 0.519, 'p-value': 0.000},
'shareclass_size': {'Coefficient': -0.000, 'p-value': 0.955},
'fund_size': {'Coefficient': 0.000, 'p-value': 0.000},
'fund_trailing_return_ytd': {'Coefficient': 0.151, 'p-value': 0.000},
'fund_trailing_return_3years': {'Coefficient': 0.951, 'p-value': 0.000},
'fund_return_2018_q4': {'Coefficient': -0.924, 'p-value': 0.000},
'fund_return_2018_q3': {'Coefficient': 0.621, 'p-value': 0.000},
'fund_return_2018_q2': {'Coefficient': 0.745, 'p-value': 0.000},
'fund_return_2018_q1': {'Coefficient': -0.794, 'p-value': 0.000},
'fund_return_2017_q4': {'Coefficient': 1.287, 'p-value': 0.000},
'fund_return_2017_q3': {'Coefficient': 1.240, 'p-value': 0.000},
'fund_return_2017_q2': {'Coefficient': 0.463, 'p-value': 0.000},
'fund_return_2017_q1': {'Coefficient': 1.143, 'p-value': 0.000},
'quarters_up': {'Coefficient': 0.283, 'p-value': 0.000},
'quarters_down': {'Coefficient': -0.644, 'p-value': 0.000},
'nav_per_share': {'Coefficient': -0.000, 'p-value': 0.601}
}
#%%
# Extracting coefficients and p-values for plotting
coefficients = [value['Coefficient'] for key, value in results.items()]
p_values = [value['p-value'] for key, value in results.items()]
variables = list(results.keys())
# Colors based on p-value
colors = ['green' if p <= 0.05 else 'red' for p in p_values]
# Plotting
plt.figure(figsize=(15, 8))
plt.bar(variables, coefficients, color=colors)
plt.xlabel('Variables')
plt.ylabel('Coefficients')
plt.title('Coefficients of Linear Regression Models with Significance Indication')
plt.xticks(rotation=90)
plt.show()
#%% [markdown]
# ## Observations from linear regression coeffeicients for fund_return_2019
#
# * asset_bond, asset_cash has negative impact on the fund return. As we saw in EDA
# * The fund_return_2018_q4 has a negative impact as expected because of the economic downturn
# * The ongoing cost, management fees, invovlement_animal_testing, involvemnt_abortive_contraceptive, weapons, alcochol : has as postive effect just like we saw in EDA.
# * All the previous year returns has good correlation with fund_return_2019.
# * nuclear and pesticide has statistically insignificant impact on the fund_return_2019, as the p-values are less than 0.05.
# * The genetically modified foods (GMO) and palm oil has a negative impact on the food_return_2019
# * nav_per_share strangely has no impact at all.
#%% [markdown]
## Additional EDA on the select columns
# Calculating descriptive statistics for 'fund_return_2019' and relevant numerical independent variables
descriptive_stats_columns = ['fund_return_2019', 'asset_stock', 'asset_bond', 'asset_cash']
# Check if the relevant columns exist in the DataFrame
if all(column in df.columns for column in descriptive_stats_columns):
# Calculating descriptive statistics
descriptive_stats = df[descriptive_stats_columns].describe()
else:
descriptive_stats = "Some specified columns are missing from the DataFrame."
descriptive_stats
# %%
# Histograms
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(15, 10))
# Histogram for 'fund_return_2019'
sns.histplot(df['fund_return_2019'], bins=10, kde=True, ax=axes[0, 0])
axes[0, 0].set_title('Histogram of Fund Return 2019')
axes[0, 0].set_xlabel('Fund Return 2019 (%)')
axes[0, 0].set_ylabel('Frequency')
axes[0, 0].set_xlim(left=0) # Setting the x-axis to start from 0
# Histogram for 'asset_stock'
sns.histplot(df['asset_stock'], bins=10, kde=True, ax=axes[0, 1])
axes[0, 1].set_title('Histogram of Asset Stock')
axes[0, 1].set_xlabel('Asset Stock (%)')
axes[0, 1].set_ylabel('Frequency')
axes[0, 1].set_xlim(left=0) # Setting the x-axis to start from 0
# Histogram for 'asset_bond'
sns.histplot(df['asset_bond'], bins=10, kde=True, ax=axes[1, 0])
axes[1, 0].set_title('Histogram of Asset Bond')
axes[1, 0].set_xlabel('Asset Bond (%)')
axes[1, 0].set_ylabel('Frequency')
axes[1, 0].set_xlim(left=0) # Setting the x-axis to start from 0
# Histogram for 'asset_cash'
sns.histplot(df['asset_cash'], bins=10, kde=True, ax=axes[1, 1])
axes[1, 1].set_title('Histogram of Asset Cash')
axes[1, 1].set_xlabel('Asset Cash (%)')
axes[1, 1].set_ylabel('Frequency')
axes[1, 1].set_xlim(left=0) # Setting the x-axis to start from 0
plt.tight_layout()
plt.show()
#%% [markdown]
## Asset Allocation Analysis:
# Investigate how different asset allocations (stocks, bonds, cash) impact the 'fund_return_2019'.
# * Asset_stock has somewhat a linear realtionship with fund_returns_2019
# * bond and cash has no relationship and has many outliers
# %%
# Asset allocation columns
asset_allocation_columns = ['asset_stock', 'asset_bond', 'asset_cash']
# Check if the relevant columns exist in the DataFrame
if all(column in m.columns for column in asset_allocation_columns + ['fund_return_2019']):
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(18, 6))
# Creating scatter plots for each asset allocation column and marking outliers
for i, column in enumerate(asset_allocation_columns):
# Calculate the IQR (Interquartile Range) to identify outliers
Q1 = m[column].quantile(0.25)
Q3 = m[column].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
# Identifying outliers
outliers = (m[column] < lower_bound) | (m[column] > upper_bound)
# Scatter plot without outliers
sns.scatterplot(x=m[~outliers][column], y=m[~outliers]['fund_return_2019'], ax=axes[i], color='blue')
# Scatter plot for outliers
sns.scatterplot(x=m[outliers][column], y=m[outliers]['fund_return_2019'], ax=axes[i], color='red')
axes[i].set_title(f'Fund Return 2019 vs {column}')
axes[i].set_xlabel(column)
axes[i].set_ylabel('Fund Return 2019 (%)')
plt.tight_layout()
plt.show()
else:
print('Some specified columns are missing from the DataFrame.')
# %%
import matplotlib.pyplot as plt
import seaborn as sns
# Assuming df is your DataFrame
categorical_nav_size_columns = ['nav_per_share_currency']
# Check if the relevant columns exist in the DataFrame
if all(column in df.columns for column in categorical_nav_size_columns + ['fund_return_2019']):
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(18, 6))
# Creating line plots for each categorical NAV and size related column
for i, column in enumerate(categorical_nav_size_columns):
# Grouping the data by the categorical column and calculating mean fund return for each group
grouped_data = df.groupby(column)['fund_return_2019'].mean().sort_index()