Ejemplo n.º 1
0
        /// <summary>
        /// Gets the base dollar value of the given factor
        /// </summary>
        /// <param name="tbl"></param>
        /// <returns></returns>
        public static double GetFactorBaseValue(AmericanFactorTables tbl)
        {
            DebtXml = DebtXml ?? XmlDocXrefIdentifier.GetEmbeddedXmlDoc(US_PERSONAL_DEBT,
                                                                        Assembly.GetExecutingAssembly());
            WealthXml = WealthXml ??
                        XmlDocXrefIdentifier.GetEmbeddedXmlDoc(US_PERSONAL_WEALTH, Assembly.GetExecutingAssembly());


            var xmlDoc = tbl == AmericanFactorTables.CreditCardDebt || tbl == AmericanFactorTables.HomeDebt ||
                         tbl == AmericanFactorTables.VehicleDebt || tbl == AmericanFactorTables.OtherDebt
                ? DebtXml
                : WealthXml;
            var tblName  = Enum.GetName(typeof(AmericanFactorTables), tbl);
            var tblXPath = $"//table[@name='{tblName}']";
            var tblNode  = xmlDoc.SelectSingleNode(tblXPath) as XmlElement;

            if (string.IsNullOrWhiteSpace(tblNode?.Attributes?["value"]?.Value))
            {
                return(0.0D);
            }
            if (!double.TryParse(tblNode.Attributes["value"].Value, out var dblOut))
            {
                return(0.0D);
            }
            return(dblOut);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get a random value for the given <see cref="factor"/> table.
        /// </summary>
        /// <param name="factor"></param>
        /// <param name="factorMultiplier"></param>
        /// <param name="stdDevAsPercent"></param>
        /// <param name="assignedBase">
        /// Optional value to directly assign a factor table's base value, defaults to
        /// the value from <see cref="GetFactorBaseValue"/>
        /// </param>
        /// <returns></returns>
        public static double GetRandomFactorValue(AmericanFactorTables factor, double factorMultiplier,
                                                  double stdDevAsPercent, double?assignedBase = null)
        {
            var baseValue = assignedBase.GetValueOrDefault(GetFactorBaseValue(factor));

            baseValue = Math.Round(baseValue * factorMultiplier, 2);
            var randValue = Math.Round(
                Etx.RandomValueInNormalDist(Math.Round(baseValue, 0), Math.Round(baseValue * stdDevAsPercent, 0)), 2);

            return(randValue);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the mean of the factors based on the criteria.
        /// </summary>
        /// <param name="tbl"></param>
        /// <param name="edu"></param>
        /// <param name="race"></param>
        /// <param name="region"></param>
        /// <param name="age"></param>
        /// <param name="gender"></param>
        /// <param name="maritalStatus"></param>
        /// <returns>
        /// A scalar factor above or below the national average(s) based on the given criteria
        /// </returns>
        /// <remarks>
        /// src https://www2.census.gov/programs-surveys/demo/tables/wealth/2013/wealth-asset-ownership/wealth-tables-2013.xlsx
        ///     https://www2.census.gov/programs-surveys/demo/tables/wealth/2011/wealth-asset-ownership/debt-tables-2011.xlsx
        /// </remarks>
        public static double GetFactor(AmericanFactorTables tbl, OccidentalEdu edu, NorthAmericanRace race,
                                       AmericanRegion region, int age, Gender gender, MaritalStatus maritalStatus)
        {
            DebtXml = DebtXml ?? XmlDocXrefIdentifier.GetEmbeddedXmlDoc(US_PERSONAL_DEBT,
                                                                        Assembly.GetExecutingAssembly());
            WealthXml = WealthXml ??
                        XmlDocXrefIdentifier.GetEmbeddedXmlDoc(US_PERSONAL_WEALTH, Assembly.GetExecutingAssembly());
            var xmlDoc = tbl == AmericanFactorTables.CreditCardDebt || tbl == AmericanFactorTables.HomeDebt ||
                         tbl == AmericanFactorTables.VehicleDebt
                ? DebtXml
                : WealthXml;
            var tblName    = Enum.GetName(typeof(AmericanFactorTables), tbl);
            var eduName    = GetXmlEduName(edu);
            var raceName   = Enum.GetName(typeof(NorthAmericanRace), race);
            var regionName = Enum.GetName(typeof(AmericanRegion), region);
            var genderName = Enum.GetName(typeof(Gender), gender);

            var tblXPath    = $"//table[@name='{tblName}']";
            var factorCount = 0D;
            var sum         = 0.0D;
            var hash        = new Dictionary <string, string>
            {
                { "Edu", eduName },
                { "Race", raceName },
                { "Region", regionName },
            };

            foreach (var factor in hash.Keys)
            {
                var xmlElem =
                    xmlDoc.SelectSingleNode($"{tblXPath}/factor[@name='{factor}']/add[@name='{hash[factor]}']") as
                    XmlElement;
                var factorVal = xmlElem?.Attributes["value"]?.Value;
                if (string.IsNullOrWhiteSpace(factorVal))
                {
                    continue;
                }
                if (double.TryParse(factorVal, out var dblOut))
                {
                    sum         += dblOut;
                    factorCount += 1;
                }
            }

            var ageNode = maritalStatus == MaritalStatus.Married || maritalStatus == MaritalStatus.Remarried
                ? xmlDoc.SelectSingleNode($"{tblXPath}/factor[@name='Age']/factor[@name='Married']")
                : xmlDoc.SelectSingleNode($"{tblXPath}/factor[@name='Age']/factor[@name='{genderName}']");

            factorCount = factorCount <= 0 ? 1 : factorCount;
            if (ageNode == null)
            {
                return(Math.Round(sum / factorCount, 5));
            }
            foreach (var anode in ageNode.ChildNodes)
            {
                var ageElem = anode as XmlElement;
                if (ageElem == null)
                {
                    continue;
                }
                var minAge = ageElem.Attributes["min"]?.Value;
                var maxAge = ageElem.Attributes["max"]?.Value;
                if (string.IsNullOrWhiteSpace(minAge) || string.IsNullOrWhiteSpace(maxAge))
                {
                    continue;
                }
                if (!int.TryParse(minAge, out var min) || !int.TryParse(maxAge, out var max))
                {
                    continue;
                }
                var isInRange = age >= min && age <= max;
                if (!isInRange || string.IsNullOrWhiteSpace(ageElem.Attributes["value"]?.Value))
                {
                    continue;
                }
                var factorVal = ageElem.Attributes["value"].Value;
                if (double.TryParse(factorVal, out var dblOut))
                {
                    sum         += dblOut;
                    factorCount += 1;
                }
            }
            return(Math.Round(sum / factorCount, 5));
        }