計算機科学のブログ

知識を広げる データ解析のための統計機能 乱数の生成、整数、範囲、リスト、RandomSample、平均値、各国のテレビ局数、一人当たりのGDP、欠損値、フィルタリング、Select、Cases、相関関係

ハンズ・オン・スタートMathematica® -Wolfram言語™によるプログラミング (C・ヘイスティング(著)、K・ミショー(著)、M・モリソン(著)、ウルフラム・リサーチ(翻訳)、丸善出版)の第2部(知識を広げる)、第23章(データ解析のための統計機能)の練習問題1、2、3、4、5、6、7、8、9、10の解答を求めてみる。

random integer between 1 and 100

RandomInteger[{1, 100}]
41
RandomInteger[{1, 100}, 5]
{81, 98, 33, 46, 6}
table = Range[1, 10];
RandomSample[table]
{10, 8, 3, 5, 9, 2, 6, 4, 7, 1}
CountryData["Properties"]
{AdultPopulation, AgriculturalProducts, AgriculturalValueAdded, Airports, 
 
>   AlternateNames, AlternateStandardNames, AMRadioStations, AnnualBirths, AnnualDeaths, 
 
>   AnnualHIVAIDSDeaths, ArableLandArea, ArableLandFraction, Area, BirthRateFraction, 
 
>   BorderingCountries, BordersLengths, BoundaryLength, CallingCode, CapitalCity, 
 
>   CapitalLocation, CapitalLocationLink, CellularPhones, CenterCoordinates, 
 
>   CenterLocationLink, ChildPopulation, Classes, ClimateTypes, CoastlineLength, 
 
>   ConstructionValueAdded, Continent, Coordinates, Countries, CountryCode, 
 
>   CropsLandArea, CropsLandFraction, CurrencyCode, CurrencyName, CurrencyShortName, 
 
>   CurrencyUnit, CurrentAccountBalance, DeathRateFraction, Dependencies, 
 
>   DependencyParent, EconomicAid, ElderlyPopulation, ElectricalGridFrequency, 
 
>   ElectricalGridPlugImages, ElectricalGridPlugs, ElectricalGridSocketImages, 
 
>   ElectricalGridSockets, ElectricalGridVoltages, ElectricityConsumption, 
 
>   ElectricityExports, ElectricityImports, ElectricityProduction, 
 
>   EnvironmentalAgreements, EnvironmentalIssues, EthnicGroups, EthnicGroupsFractions, 
 
>   ExchangeRate, ExpenditureFractions, ExportCommodities, ExportPartners, 
 
>   ExportPartnersFractions, ExportValue, ExternalDebt, FemaleAdultPopulation, 
 
>   FemaleChildPopulation, FemaleElderlyPopulation, FemaleInfantMortalityFraction, 
 
>   FemaleLifeExpectancy, FemaleLiteracyFraction, FemaleMedianAge, FemalePopulation, 
 
>   FiscalYearDate, FixedInvestment, Flag, FlagDescription, FMRadioStations, 
 
>   ForeignExchangeReserves, ForeignOwnedShips, ForeignRegisteredShips, FullCoordinates, 
 
>   FullName, FullNativeName, FullPolygon, GDP, GDPAtParity, GDPPerCapita, 
 
>   GDPRealGrowth, GDPSectorFractions, GiniIndex, GovernmentConsumption, GovernmentDebt, 
 
>   GovernmentExpenditures, GovernmentReceipts, GovernmentSurplus, GrossInvestment, 
 
>   Groups, HighestElevation, HighestPoint, HIVAIDSDeathRateFraction, HIVAIDSFraction, 
 
>   HIVAIDSPopulation, HouseholdConsumption, ImportCommodities, ImportPartners, 
 
>   ImportPartnersFractions, ImportValue, IndependenceDate, IndependenceYear, 
 
>   IndustrialProductionGrowth, IndustrialValueAdded, InfantMortalityFraction, 
 
>   InfectiousDiseases, InflationRate, InternationalOrganizations, 
 
>   InternationalOrganizationsObserver, InternetCode, InternetHosts, InternetUsers, 
 
>   InventoryChange, IrrigatedLandArea, IrrigatedLandFraction, ISOName, LaborForce, 
 
>   LandArea, Languages, LanguagesDialects, LanguagesFractions, LargestCities, 
 
>   LifeExpectancy, LiteracyFraction, LowestElevation, LowestPoint, MajorIndustries, 
 
>   MajorPorts, MaleAdultPopulation, MaleChildPopulation, MaleElderlyPopulation, 
 
>   MaleInfantMortalityFraction, MaleLifeExpectancy, MaleLiteracyFraction, 
 
>   MaleMedianAge, MalePopulation, ManufacturingValueAdded, MaritimeClaims, MedianAge, 
 
>   Memberships, MerchantShips, MerchantShipsDeadWeight, MerchantShipsGross, 
 
>   MerchantShipTypes, MigrationRateFraction, MilitaryAgeFemales, MilitaryAgeMales, 
 
>   MilitaryAgePopulation, MilitaryAgeRate, MilitaryExpenditureFraction, 
 
>   MilitaryExpenditures, MilitaryFitFemales, MilitaryFitMales, MilitaryFitPopulation, 
 
>   MiscellaneousValueAdded, Name, NationalIncome, NationalityName, NativeName, 
 
>   NaturalGasConsumption, NaturalGasExports, NaturalGasImports, NaturalGasProduction, 
 
>   NaturalGasReserves, NaturalHazards, NaturalResources, OilConsumption, OilExports, 
 
>   OilImports, OilProduction, OilReserves, PavedAirportLengths, PavedAirports, 
 
>   PavedRoadLength, PhoneLines, Pipelines, Polygon, Population, PopulationGrowth, 
 
>   PovertyFraction, PriceIndex, RadioStations, RailwayGaugeLengths, RailwayGaugeRules, 
 
>   RailwayLength, RegionNames, Regions, Religions, ReligionsFractions, RoadLength, 
 
>   SchematicCoordinates, SchematicPolygon, SectorLaborFractions, Shape, 
 
>   ShortWaveRadioStations, SignedEnvironmentalAgreements, StandardName, SuffrageType, 
 
>   TelevisionStations, TerrainTypes, TimeZones, TotalConsumption, TotalFertilityRate, 
 
>   TradeValueAdded, TransportationValueAdded, UNCode, UnemploymentFraction, UNNumber, 
 
>   UnpavedAirportLengths, UnpavedAirports, UnpavedRoadLength, ValueAdded, WaterArea, 
 
>   WaterwayLength}
CountryData["Japan", "TelevisionStations"]
211
CountryData["Countries", "TelevisionStations"]
Output
tvSelect[x_] := QuantityMagnitude[CountryData[x, "TelevisionStations"]] > 100;
Select[CountryData[All], tvSelect]
Output
manyStations = %
Output
manyStationsGDPGovDebt = Table[
    {country["GDPPerCapita"], country["GovernmentDebt"]},
    {country, manyStations}
]
Output
manyStationsGDPGovDebt = Table[
    {country["GDP"] / country["Population"], country["GovernmentDebt"]},
    {country, manyStations}
]
Output
Head[manyStationsGDPGovDebt[[1, 2]]]
Output
manyStationsGDPGovDebt = Cases[manyStationsGDPGovDebt, {_, _Quantity}]
Output
Dimensions[manyStationsGDPGovDebt]
{24, 2}
Length[manyStationsGDPGovDebt]
24
ListLogLogPlot[manyStationsGDPGovDebt]
Output
ListLogLogPlot[
    Table[
        Tooltip[{country["GDP"] / country["Population"], country["GovernmentDebt"]}],
        {country, manyStations}
    ]
]
Output
Mean[manyStationsGDPGovDebt[[All, 1]]]
Output
Correlation[manyStationsGDPGovDebt[[All, 1]], manyStationsGDPGovDebt[[All, 2]]]
0.305581