#In ORDER TO FIND THE BEST MUTUAL FUND WE NEED TO KNOW FEW THINGS FIRST 
#MIN SIP FOR INVESTMENT , AVERAGE EXPENSE RATIO, FUND MANAGER , SD-BETA RATIO, RETURNS IN 1,3 AND 5 YEARS

# Filtering out mutual funds with ratings 1, 2, 3, 4, and 5 stars
comprehensive_mutual_funds_data<-read.csv("D:\\LPU\\Sem 4\\Data Exploration\\archive\\comprehensive_mutual_funds_data.csv")


library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.5
## ✔ ggplot2   3.5.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)


filtered_data <- filter(comprehensive_mutual_funds_data, rating == 1 | rating == 2 | rating == 3 | rating == 4 | rating == 5)


ggplot(filtered_data, aes(x = factor(rating))) +
  geom_bar(fill = "skyblue", color = "black") +
  labs(x = "Rating", y = "Count", title = "Number of Mutual Funds by Rating") +
  theme_minimal()

 #What is the average expense ratio of the mutual funds in the dataset?

library(dplyr)
library(tidyverse)


average_expense_ratio <- comprehensive_mutual_funds_data %>%
  summarise(avg_expense_ratio = mean(expense_ratio))
average_expense_ratio
##   avg_expense_ratio
## 1          0.713231
# finding the best mutual funds from the data set 
library(dplyr)
library(tidyverse)

best_mutual_fund <- comprehensive_mutual_funds_data %>%
  arrange(desc(returns_5yr)) %>%
  slice(1)

best_mutual_fund
##            scheme_name min_sip min_lumpsum expense_ratio fund_size_cr
## 1 Quant Small Cap Fund    1000        5000          0.64         3301
##   fund_age_yr   fund_manager sortino alpha    sd beta sharpe risk_level
## 1          10 Sanjeev Sharma    3.71 19.16 24.75 0.99   2.11          6
##            amc_name rating category           sub_category returns_1yr
## 1 Quant Mutual Fund      5   Equity Small Cap Mutual Funds         5.4
##   returns_3yr returns_5yr
## 1        71.4        23.2
#which are the best mutual funds in case some one wish to invest in sip and lumpsum and minimum amount 
library(dplyr)
library(tidyverse)


filtered_data <- comprehensive_mutual_funds_data %>%
  filter(min_sip <= 100 & min_lumpsum <= 100)


selected_data <- filtered_data %>% 
  slice(1:25)  
print(selected_data)
##                                              scheme_name min_sip min_lumpsum
## 1   Aditya Birla SL Active Debt Multi-Mgr FoF-Dir Growth     100         100
## 2                Aditya Birla SL Balanced Advantage Fund     100         100
## 3                    Aditya Birla SL Corporate Bond Fund     100         100
## 4                       Aditya Birla SL Credit Risk Fund     100         100
## 5                 Aditya Birla SL Equity Hybrid ’95 Fund     100         100
## 6                         Aditya Birla SL Flexi Cap Fund     100         100
## 7    Aditya Birla SL FP FoF – Aggressive – Direct Growth     100         100
## 8  Aditya Birla SL FP FoF – Conservative – Direct Growth     100         100
## 9      Aditya Birla SL FP FoF – Moderate – Direct Growth     100         100
## 10                 Aditya Birla SL Frontline Equity Fund     100         100
## 11             Aditya Birla SL Global Emerging Oppt Fund     100         100
## 12                             Aditya Birla SL Gold Fund     100         100
## 13                     Aditya Birla SL Low Duration Fund     100         100
## 14                      AXIS FTP – Series 104 – 1112Days       0           0
## 15                       AXIS FTP – Series 109 – 111Days       0           0
## 16                                   HDFC Arbitrage Fund     100         100
## 17                          HDFC Balanced Advantage Fund     100         100
## 18                        HDFC Banking and PSU Debt Fund     100         100
## 19                       HDFC Capital Builder Value Fund     100         100
## 20                             HDFC Children’s Gift Fund     100         100
## 21                              HDFC Corporate Bond Fund     100         100
## 22                            HDFC Credit Risk Debt Fund     100         100
## 23                                HDFC Dynamic Debt Fund     100         100
## 24                            HDFC Dynamic PE Ratio Fund     100         100
## 25                              HDFC Equity Savings Fund     100         100
##    expense_ratio fund_size_cr fund_age_yr          fund_manager sortino alpha
## 1           0.27           10          10        Kaustubh Gupta    0.32  2.24
## 2           0.61         6386          10          Mohit Sharma    3.69  1.99
## 3           0.31        12120          10        Kaustubh Gupta    2.28  2.38
## 4           0.69         1032           8      Sunaina da Cunha    1.82  3.01
## 5           1.01         7046          10          Mahesh Patil    2.54  1.43
## 6           0.86        15449          10             Anil Shah     3.4 -1.46
## 7           0.22          159          10    Vinod Narayan Bhat    3.13  1.65
## 8           0.34           14          10    Vinod Narayan Bhat    3.82  1.94
## 9           0.36           28          10    Vinod Narayan Bhat    3.09  1.69
## 10          1.03        21131          10          Mahesh Patil    3.36  0.76
## 11          0.70          261          10    Vinod Narayan Bhat    0.98     -
## 12          0.15          278          10      Lovelish Solanki    1.07 -0.57
## 13          0.39        10035          10          Mohit Sharma    4.76  3.33
## 14          0.13          110           4           Devang Shah    7.27  5.29
## 15          0.00          565           1           Devang Shah       -     -
## 16          0.43         5886           9          Arun Agarwal    0.99  1.24
## 17          0.88        51042          10         Gopal Agrawal    3.38  7.38
## 18          0.39         4805           9          Anil Bamboli    1.74   3.4
## 19          1.04         5227          10         Gopal Agrawal    3.26  3.69
## 20          0.96         6157          10       Chirag Setalvad    4.11  7.62
## 21          0.32        23486          10          Anupam Joshi    1.59  1.91
## 22          0.95         8437           9      Shobhit Mehrotra     1.5  2.63
## 23          0.49          527          10          Anil Bamboli     2.3  2.71
## 24          0.22           30          10 Srinivasan Ramamurthy    4.11  6.38
## 25          1.10         2524          10          Anil Bamboli    3.67  4.42
##       sd  beta sharpe risk_level                          amc_name rating
## 1   9.39  0.01   0.24          3 Aditya Birla Sun Life Mutual Fund      3
## 2  10.38  0.68   1.39          6 Aditya Birla Sun Life Mutual Fund      4
## 3   1.93  0.78   1.35          2 Aditya Birla Sun Life Mutual Fund      4
## 4    2.9 -0.95   1.47          4 Aditya Birla Sun Life Mutual Fund      4
## 5  12.84  0.85   1.33          6 Aditya Birla Sun Life Mutual Fund      2
## 6   16.6  0.96   1.27          6 Aditya Birla Sun Life Mutual Fund      3
## 7  11.73   0.8    1.4          6 Aditya Birla Sun Life Mutual Fund      3
## 8   5.49  0.36   1.57          4 Aditya Birla Sun Life Mutual Fund      4
## 9   8.56  0.58   1.44          5 Aditya Birla Sun Life Mutual Fund      3
## 10 16.02  0.95   1.39          6 Aditya Birla Sun Life Mutual Fund      3
## 11 16.64     -    0.7          6 Aditya Birla Sun Life Mutual Fund      0
## 12 13.89  0.97   0.45          5 Aditya Birla Sun Life Mutual Fund      2
## 13  0.97     1   2.05          3 Aditya Birla Sun Life Mutual Fund      4
## 14     -  2.36   2.19          2                  Axis Mutual Fund      0
## 15     -     -      -          3                  Axis Mutual Fund      0
## 16   0.7   0.5   0.82          1                  HDFC Mutual Fund      2
## 17 14.82  0.88    1.6          6                  HDFC Mutual Fund      4
## 18  2.02  1.06   0.98          2                  HDFC Mutual Fund      4
## 19 16.41  0.95   1.53          6                  HDFC Mutual Fund      2
## 20 11.58  0.76   1.87          6                  HDFC Mutual Fund      0
## 21  2.19  0.94   0.99          3                  HDFC Mutual Fund      3
## 22  2.56 -0.52    1.3          4                  HDFC Mutual Fund      3
## 23  3.62  0.55   0.79          3                  HDFC Mutual Fund      4
## 24  9.63   0.6    1.8          5                  HDFC Mutual Fund      4
## 25  7.09  0.98   1.61          4                  HDFC Mutual Fund      4
##             category                                   sub_category returns_1yr
## 1              Other                                  FoFs Domestic         4.0
## 2             Hybrid Dynamic Asset Allocation or Balanced Advantage         4.5
## 3               Debt                    Corporate Bond Mutual Funds         4.9
## 4               Debt                              Credit Risk Funds         8.7
## 5             Hybrid                 Aggressive Hybrid Mutual Funds        -5.7
## 6             Equity                                Flexi Cap Funds        -2.1
## 7              Other                                  FoFs Domestic         2.5
## 8              Other                                  FoFs Domestic         4.7
## 9              Other                                  FoFs Domestic         3.7
## 10            Equity                         Large Cap Mutual Funds         1.6
## 11             Other                                  FoFs Overseas        -5.7
## 12             Other                                  FoFs Domestic        11.8
## 13              Debt                             Low Duration Funds         5.7
## 14              Debt                           Fixed Maturity Plans        11.2
## 15              Debt                           Fixed Maturity Plans        11.2
## 16            Hybrid                         Arbitrage Mutual Funds         5.6
## 17            Hybrid Dynamic Asset Allocation or Balanced Advantage        14.4
## 18              Debt                   Banking and PSU Mutual Funds         4.3
## 19            Equity                                    Value Funds        -0.7
## 20 Solution Oriented                                Childrens Funds        10.5
## 21              Debt                    Corporate Bond Mutual Funds         4.1
## 22              Debt                              Credit Risk Funds         4.6
## 23              Debt                                   Dynamic Bond         4.6
## 24             Other                                  FoFs Domestic         8.6
## 25            Hybrid                    Equity Savings Mutual Funds         5.1
##    returns_3yr returns_5yr
## 1          6.5         6.9
## 2         18.6         9.7
## 3          7.1         7.4
## 4          9.0         7.2
## 5         20.4         7.7
## 6         25.0        10.4
## 7         21.7        10.7
## 8         13.3         8.2
## 9         17.3         9.4
## 10        26.3        10.6
## 11        18.8         8.3
## 12        12.1        13.2
## 13         6.5         7.0
## 14        14.5        14.0
## 15        14.5        14.0
## 16         4.5         5.2
## 17        29.3        12.9
## 18         6.8         7.2
## 19        33.7        11.3
## 20        26.8        12.9
## 21         6.9         7.3
## 22         8.0         7.7
## 23         7.3         6.1
## 24        21.9        11.1
## 25        16.0         8.8
# Create the scatter plot of 3yrs return and 5 yrs returns

library(ggplot2)
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.3.3
## corrplot 0.92 loaded
ggplot(comprehensive_mutual_funds_data, aes(x = returns_3yr, y = returns_5yr)) +
  geom_point(color = "red") +
  labs(x = "3 Years Returns", y = "5 Years Returns", title = "5 Years Returns vs. 3 Years Returns of Mutual Funds")
## Warning: Removed 167 rows containing missing values or values outside the scale range
## (`geom_point()`).

#mutual funds above 4 star
library(dplyr)
library(tidyverse)
library(ggplot2)



filtered_data <- filter(comprehensive_mutual_funds_data, rating >= 4)


ggplot(filtered_data, aes(x = factor(rating))) +
  geom_bar(fill = "skyblue", color = "black") +
  labs(x = "Rating", y = "Count", title = "Number of Mutual Funds with Rating > 4") +
  theme_minimal()

#finding by category wise
library(dplyr)
library(tidyverse)
library(ggplot2)

category_counts <- table(comprehensive_mutual_funds_data$category)


category_data <- as.data.frame(category_counts)


colnames(category_data) <- c("Category", "Count")

ggplot(category_data, aes(x = Category, y = Count, fill = Category)) +
  geom_bar(stat = "identity") +
  labs(x = "Category", y = "Count", fill = "Category", title = "Distribution of Mutual Funds by Category") 

#found some mutual funds by the mutual fund manager names
library(dplyr)
library(tidyverse)

selected_managers <- c("Kaustubh Gupta", "Dhaval Gala", "Lovelish Solanki") 

filtered_data <- filter(comprehensive_mutual_funds_data, fund_manager %in% selected_managers)


print(filtered_data)
##                                             scheme_name min_sip min_lumpsum
## 1  Aditya Birla SL Active Debt Multi-Mgr FoF-Dir Growth     100         100
## 2                        Aditya Birla SL Arbitrage Fund    1000        1000
## 3  Aditya Birla SL Banking&Financial Services-DirGrowth    1000        1000
## 4                 Aditya Birla SL Banking&PSU Debt Fund    1000        1000
## 5                   Aditya Birla SL Corporate Bond Fund     100         100
## 6                   Aditya Birla SL Dividend Yield Fund    1000        1000
## 7                    Aditya Birla SL Floating Rate Fund    1000        1000
## 8                             Aditya Birla SL Gold Fund     100         100
## 9                           Aditya Birla SL Liquid Fund     500         500
## 10                  Aditya Birla SL Nifty 50 Index Fund    1000         100
## 11                       Aditya Birla SL Overnight Fund       0        5000
## 12                         Aditya Birla SL Savings Fund    1000        1000
## 13                      Aditya Birla SL Short Term Fund    1000        1000
##    expense_ratio fund_size_cr fund_age_yr     fund_manager sortino alpha    sd
## 1           0.27           10          10   Kaustubh Gupta    0.32  2.24  9.39
## 2           0.36         4288          10 Lovelish Solanki    1.33  1.53  0.72
## 3           1.17         2384           9      Dhaval Gala    2.07  1.24 25.53
## 4           0.37         7994          10   Kaustubh Gupta    1.92  4.46  1.91
## 5           0.31        12120          10   Kaustubh Gupta    2.28  2.38  1.93
## 6           1.73          850          10      Dhaval Gala    3.78  5.04 15.77
## 7           0.23        12364          10   Kaustubh Gupta    2.81  3.73  1.14
## 8           0.15          278          10 Lovelish Solanki    1.07 -0.57 13.89
## 9           0.21        28071          10   Kaustubh Gupta    5.28  1.49  0.39
## 10          0.32          494          10 Lovelish Solanki    3.42 -0.87 16.65
## 11          0.07        13399           4   Kaustubh Gupta    0.11  0.58  0.35
## 12          0.34        14457          10   Kaustubh Gupta    4.23  2.64  0.76
## 13          0.38         5045          10   Kaustubh Gupta     3.7  4.91  2.05
##    beta sharpe risk_level                          amc_name rating category
## 1  0.01   0.24          3 Aditya Birla Sun Life Mutual Fund      3    Other
## 2  0.56    1.1          1 Aditya Birla Sun Life Mutual Fund      3   Hybrid
## 3  0.96   0.97          6 Aditya Birla Sun Life Mutual Fund      2   Equity
## 4  1.78   1.09          3 Aditya Birla Sun Life Mutual Fund      4     Debt
## 5  0.78   1.35          2 Aditya Birla Sun Life Mutual Fund      4     Debt
## 6  0.85   1.58          6 Aditya Birla Sun Life Mutual Fund      0   Equity
## 7  1.37   1.66          2 Aditya Birla Sun Life Mutual Fund      5     Debt
## 8  0.97   0.45          5 Aditya Birla Sun Life Mutual Fund      2    Other
## 9   0.8   1.06          3 Aditya Birla Sun Life Mutual Fund      4     Debt
## 10 0.99   1.29          6 Aditya Birla Sun Life Mutual Fund      3    Other
## 11 0.43   0.02          1 Aditya Birla Sun Life Mutual Fund      3     Debt
## 12 0.86   1.96          3 Aditya Birla Sun Life Mutual Fund      4     Debt
## 13 1.27   1.57          3 Aditya Birla Sun Life Mutual Fund      4     Debt
##                        sub_category returns_1yr returns_3yr returns_5yr
## 1                     FoFs Domestic         4.0         6.5         6.9
## 2            Arbitrage Mutual Funds         5.6         4.8         5.5
## 3  Sectoral / Thematic Mutual Funds         5.3        24.6         9.2
## 4      Banking and PSU Mutual Funds         4.5         6.8         7.3
## 5       Corporate Bond Mutual Funds         4.9         7.1         7.4
## 6              Dividend Yield Funds         6.2        30.5        10.0
## 7              Floater Mutual Funds         5.8         6.4         6.8
## 8                     FoFs Domestic        11.8        12.1        13.2
## 9               Liquid Mutual Funds         5.7         4.4         5.4
## 10                      Index Funds         0.7        25.5        11.6
## 11           Overnight Mutual Funds         5.4         3.9          NA
## 12       Ultra Short Duration Funds         5.5         5.8         6.5
## 13             Short Duration Funds         5.5         7.8         7.6
#best mutual funds according to fund size
library(dplyr)
library(tidyverse)

best_fund_size <- filtered_data %>%
  arrange(desc(fund_size_cr)) %>%
  slice(1)


print(best_fund_size)
##                   scheme_name min_sip min_lumpsum expense_ratio fund_size_cr
## 1 Aditya Birla SL Liquid Fund     500         500          0.21        28071
##   fund_age_yr   fund_manager sortino alpha   sd beta sharpe risk_level
## 1          10 Kaustubh Gupta    5.28  1.49 0.39  0.8   1.06          3
##                            amc_name rating category        sub_category
## 1 Aditya Birla Sun Life Mutual Fund      4     Debt Liquid Mutual Funds
##   returns_1yr returns_3yr returns_5yr
## 1         5.7         4.4         5.4
#best mutual funds in debt category
library(dplyr)
library(tidyverse)


debt_funds <- comprehensive_mutual_funds_data %>%
  filter(category == "Debt")


best_debt_fund <- debt_funds %>%
  arrange(desc(returns_5yr)) %>%
  slice(1)
print(best_debt_fund)
##                        scheme_name min_sip min_lumpsum expense_ratio
## 1 AXIS FTP – Series 104 – 1112Days       0           0          0.13
##   fund_size_cr fund_age_yr fund_manager sortino alpha sd beta sharpe risk_level
## 1          110           4  Devang Shah    7.27  5.29  - 2.36   2.19          2
##           amc_name rating category         sub_category returns_1yr returns_3yr
## 1 Axis Mutual Fund      0     Debt Fixed Maturity Plans        11.2        14.5
##   returns_5yr
## 1          14
#find the best MUTUAL FUNDS
library(dplyr)
library(tidyverse)

filtered_funds <- comprehensive_mutual_funds_data %>%
  filter(min_sip >= 100 & 
           min_lumpsum >= 100 & 
           fund_size_cr > 10000 & 
           returns_1yr > 6 & 
           returns_3yr > 7 & 
           returns_5yr > 8 &
           rating >= 4 &
           fund_age_yr >= 10 &
           sharpe > 1.2) %>%
  select(scheme_name, min_sip, min_lumpsum, fund_size_cr, returns_1yr, returns_3yr, returns_5yr, rating)

print(filtered_funds)
##                                   scheme_name min_sip min_lumpsum fund_size_cr
## 1                HDFC Balanced Advantage Fund     100         100        51042
## 2                         HDFC Flexi Cap Fund     100         100        31672
## 3              ICICI Pru Asset Allocator Fund    1000        5000        18393
## 4           ICICI Pru Balanced Advantage Fund     100         500        44515
## 5                ICICI Pru Equity & Debt Fund     100        5000        21232
## 6                  ICICI Pru Multi-Asset Fund     100        5000        16449
## 7              ICICI Pru Value Discovery Fund     100        1000        27449
## 8                  Kotak Emerging Equity Fund     100         100        23962
## 9  Kotak Equity Opportunities – Direct Growth     500         500        11608
## 10                Nippon India Small Cap Fund     100        5000        23910
## 11                         SBI Small Cap Fund     500        5000        15395
##    returns_1yr returns_3yr returns_5yr rating
## 1         14.4        29.3        12.9      4
## 2         12.4        34.5        14.0      4
## 3          8.8        21.6        12.8      4
## 4          6.7        19.7        10.4      4
## 5          6.8        30.6        14.3      5
## 6         11.3        31.3        14.2      4
## 7          8.9        36.9        14.9      4
## 8          6.6        34.0        15.2      4
## 9          6.2        28.8        13.9      4
## 10         8.7        47.6        16.8      4
## 11         8.2        38.3        15.3      4
#find whose sharp ratio is above 2.5
library(dplyr)
library(tidyverse)


filtered_funds <- comprehensive_mutual_funds_data %>%
  filter(sharpe >= 2.5) %>%
  select(scheme_name, fund_size_cr, returns_1yr, returns_3yr, returns_5yr, sharpe, risk_level)


print(filtered_funds)
##                         scheme_name fund_size_cr returns_1yr returns_3yr
## 1 Quant Liquid Plan – Direct Growth         1561         5.6         4.9
##   returns_5yr sharpe risk_level
## 1         5.9   3.52          2
# corelation and corrplot between sd and beta
library(dplyr)
library(tidyverse)
library(corrplot)

data<-comprehensive_mutual_funds_data
data$sortino <- as.numeric(data$sortino)
## Warning: NAs introduced by coercion
data$beta <- as.numeric(data$beta)
## Warning: NAs introduced by coercion
data$sd <- as.numeric(data$sd)
## Warning: NAs introduced by coercion
data$alpha <- as.numeric(data$alpha)
## Warning: NAs introduced by coercion
data$sharpe <- as.numeric(data$sharpe)
## Warning: NAs introduced by coercion
data <- replace(data, is.na(data), 0)

correlation_coefficient <- cor(data$sd,data$beta)


print(correlation_coefficient)
## [1] -0.2730955
corrplot(cor(cor(data[, c("sd", "beta")]), method = "pearson"), method = "circle")

# count amc
library(dplyr)
library(tidyverse)
library(ggplot2)

amc_counts <- table(comprehensive_mutual_funds_data$amc_name)


amc_counts_df <- as.data.frame(amc_counts)


ggplot(amc_counts_df, aes(x = Var1, y = Freq)) +
  geom_bar(stat = "identity", fill = "skyblue", color = "black") +
 labs(x = "AMC Name", y = "Count", title = "Count of Mutual Funds by AMC") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

#co relation of fund size and 5yrs return
library(dplyr)
library(tidyverse)
library(corrplot)

data<-comprehensive_mutual_funds_data

data$sortino <- as.numeric(data$sortino)
## Warning: NAs introduced by coercion
data$beta <- as.numeric(data$beta)
## Warning: NAs introduced by coercion
data$sd <- as.numeric(data$sd)
## Warning: NAs introduced by coercion
data$alpha <- as.numeric(data$alpha)
## Warning: NAs introduced by coercion
data$sharpe <- as.numeric(data$sharpe)
## Warning: NAs introduced by coercion
data <- replace(data, is.na(data), 0)
correlation <- cor(data$fund_size_cr,data$returns_5yr)

print(correlation)
## [1] 0.1569896
#finding the median of 3yrs return

library(dplyr)
library(tidyverse)
median_return_3yr <- median(comprehensive_mutual_funds_data$returns_3yr, na.rm = TRUE)
print(median_return_3yr)
## [1] 18.5