@@ -940,6 +940,8 @@ public override XElement ToXElement()
940940 result . SetAttributeValue ( nameof ( Type ) , Type . ToString ( ) ) ;
941941 result . SetAttributeValue ( nameof ( XTransform ) , XTransform . ToString ( ) ) ;
942942 result . SetAttributeValue ( nameof ( ProbabilityTransform ) , ProbabilityTransform . ToString ( ) ) ;
943+ result . SetAttributeValue ( nameof ( MinimumOfRandomVariables ) , MinimumOfRandomVariables . ToString ( ) ) ;
944+ result . SetAttributeValue ( nameof ( Dependency ) , Dependency . ToString ( ) ) ;
943945 result . SetAttributeValue ( nameof ( Distributions ) , String . Join ( "|" , Distributions . Select ( x => x . Type ) ) ) ;
944946 // Parameters
945947 var parms = GetParameters ;
@@ -949,6 +951,36 @@ public override XElement ToXElement()
949951 parmStrings [ i ] = parms [ i ] . ToString ( "G17" , CultureInfo . InvariantCulture ) ;
950952 }
951953 result . SetAttributeValue ( "Parameters" , String . Join ( "|" , parmStrings ) ) ;
954+
955+ // Correlation matrix
956+ var corrMatrixElement = new XElement ( nameof ( CorrelationMatrix ) ) ;
957+ if ( CorrelationMatrix != null
958+ && CorrelationMatrix . GetLength ( 0 ) == Distributions . Count
959+ && CorrelationMatrix . GetLength ( 1 ) == Distributions . Count )
960+ {
961+ int rows = Distributions . Count ;
962+ int cols = Distributions . Count ;
963+ var row = new double [ cols ] ;
964+
965+ for ( int i = 0 ; i < rows ; i ++ )
966+ {
967+ var corrRowElement = new XElement ( "Correlation_Row" ) ;
968+
969+ // collect one row of the 2D array
970+ for ( int j = 0 ; j < cols ; j ++ )
971+ {
972+ row [ j ] = _correlationMatrix [ i , j ] ;
973+ }
974+
975+ // format each double to "G17" with invariant culture
976+ var formatted = row . Select ( v => v . ToString ( "G17" , CultureInfo . InvariantCulture ) ) ;
977+ // join with '|' and set as the element's text
978+ corrRowElement . Value = string . Join ( "|" , formatted ) ;
979+ corrMatrixElement . Add ( corrRowElement ) ;
980+ }
981+ }
982+ result . Add ( corrMatrixElement ) ;
983+
952984 return result ;
953985 }
954986
@@ -989,6 +1021,18 @@ public static CompetingRisks FromXElement(XElement xElement)
9891021 Enum . TryParse ( xElement . Attribute ( nameof ( ProbabilityTransform ) ) . Value , out Transform probabilityTransform ) ;
9901022 competingRisks . ProbabilityTransform = probabilityTransform ;
9911023 }
1024+ if ( xElement . Attribute ( nameof ( MinimumOfRandomVariables ) ) != null )
1025+ {
1026+ bool . TryParse ( xElement . Attribute ( nameof ( MinimumOfRandomVariables ) ) . Value , out bool minOfValues ) ;
1027+ competingRisks . MinimumOfRandomVariables = minOfValues ;
1028+ }
1029+ if ( xElement . Attribute ( nameof ( Dependency ) ) != null )
1030+ {
1031+ Enum . TryParse ( xElement . Attribute ( nameof ( Dependency ) ) . Value , out Probability . DependencyType dependency ) ;
1032+ competingRisks . Dependency = dependency ;
1033+ }
1034+
1035+ // Parameters
9921036 if ( xElement . Attribute ( "Parameters" ) != null )
9931037 {
9941038 var vals = xElement . Attribute ( "Parameters" ) . Value . Split ( '|' ) ;
@@ -1001,12 +1045,46 @@ public static CompetingRisks FromXElement(XElement xElement)
10011045 competingRisks . SetParameters ( parameters ) ;
10021046 }
10031047
1048+ // Correlation matrix
1049+ var corrMatrixElement = xElement . Element ( nameof ( CorrelationMatrix ) ) ;
1050+ if ( corrMatrixElement != null )
1051+ {
1052+ var _corrMatrix = new double [ competingRisks . Distributions . Count , competingRisks . Distributions . Count ] ;
1053+ int counter = 0 ;
1054+ foreach ( var rowEl in corrMatrixElement . Elements ( "Correlation_Row" ) )
1055+ {
1056+ if ( counter >= competingRisks . Distributions . Count )
1057+ break ;
1058+
1059+ // Split on '|' to get each stringified value
1060+ var parts = rowEl . Value . Split ( '|' ) ;
1061+ int maxCols = Math . Min ( parts . Length , competingRisks . Distributions . Count ) ;
1062+
1063+ for ( int j = 0 ; j < maxCols ; j ++ )
1064+ {
1065+ // Try to parse each part; if it fails, leave as 0 or assign NaN if you prefer
1066+ if ( double . TryParse ( parts [ j ] , NumberStyles . Any , CultureInfo . InvariantCulture , out var p ) )
1067+ {
1068+ _corrMatrix [ counter , j ] = p ;
1069+ }
1070+ else
1071+ {
1072+ _corrMatrix [ counter , j ] = double . NaN ;
1073+ }
1074+ }
1075+
1076+ counter ++ ;
1077+ }
1078+ competingRisks . CorrelationMatrix = _corrMatrix ;
1079+ }
1080+
10041081 return competingRisks ;
10051082 }
10061083 else
10071084 {
10081085 return null ;
10091086 }
10101087 }
1088+
10111089 }
10121090}
0 commit comments