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

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

2824

2825

2826

2827

2828

2829

2830

2831

2832

2833

2834

2835

2836

2837

2838

2839

2840

2841

2842

2843

2844

2845

2846

2847

2848

2849

2850

2851

2852

2853

2854

2855

2856

2857

2858

2859

2860

2861

2862

2863

2864

2865

2866

2867

2868

2869

2870

2871

2872

2873

2874

2875

2876

2877

2878

2879

2880

2881

2882

2883

2884

2885

2886

2887

2888

2889

2890

2891

2892

2893

2894

2895

2896

2897

2898

2899

2900

2901

2902

2903

2904

2905

2906

2907

2908

2909

2910

2911

2912

2913

2914

2915

2916

2917

2918

2919

2920

2921

2922

2923

2924

2925

2926

2927

2928

2929

2930

2931

2932

2933

2934

2935

2936

2937

2938

2939

2940

2941

2942

2943

2944

2945

2946

2947

2948

2949

2950

2951

2952

2953

2954

2955

2956

2957

2958

2959

2960

2961

2962

2963

2964

2965

2966

2967

2968

2969

2970

2971

2972

2973

2974

2975

2976

2977

2978

2979

2980

2981

2982

2983

2984

2985

2986

2987

2988

2989

2990

2991

2992

2993

2994

2995

2996

2997

2998

2999

3000

3001

3002

3003

3004

3005

3006

3007

3008

3009

3010

3011

3012

3013

3014

3015

3016

3017

3018

3019

3020

3021

3022

3023

3024

3025

3026

3027

3028

3029

3030

3031

3032

3033

3034

3035

3036

3037

3038

3039

3040

3041

3042

3043

3044

3045

3046

3047

3048

3049

3050

3051

3052

3053

3054

3055

3056

3057

3058

3059

3060

3061

3062

3063

3064

3065

3066

3067

3068

3069

3070

3071

3072

3073

3074

3075

3076

3077

3078

3079

3080

3081

3082

3083

3084

3085

3086

3087

3088

3089

3090

3091

3092

3093

3094

3095

3096

3097

3098

3099

3100

3101

3102

3103

3104

3105

3106

3107

3108

3109

3110

3111

3112

3113

3114

3115

3116

3117

3118

3119

3120

3121

3122

3123

3124

3125

3126

3127

3128

3129

3130

3131

3132

3133

3134

3135

3136

3137

3138

3139

3140

3141

3142

3143

3144

3145

3146

3147

3148

3149

3150

3151

3152

3153

3154

3155

3156

3157

3158

3159

3160

3161

3162

3163

3164

3165

3166

3167

3168

3169

3170

3171

3172

3173

3174

3175

3176

3177

3178

3179

3180

3181

3182

3183

3184

3185

3186

3187

3188

3189

3190

3191

3192

3193

3194

3195

3196

3197

3198

3199

3200

3201

3202

3203

3204

3205

3206

3207

3208

3209

3210

3211

3212

3213

3214

3215

3216

3217

3218

3219

3220

3221

3222

3223

3224

3225

3226

3227

3228

3229

3230

3231

3232

3233

3234

3235

3236

3237

3238

3239

3240

3241

3242

3243

3244

3245

3246

3247

3248

3249

3250

3251

3252

3253

3254

3255

3256

3257

3258

3259

3260

3261

3262

3263

3264

3265

3266

3267

3268

3269

3270

3271

3272

3273

3274

3275

3276

3277

3278

3279

3280

3281

3282

3283

3284

3285

3286

3287

3288

3289

3290

3291

3292

3293

3294

3295

3296

3297

3298

3299

3300

3301

3302

3303

3304

3305

3306

3307

3308

3309

3310

3311

3312

3313

3314

3315

3316

3317

3318

3319

3320

3321

3322

3323

3324

3325

3326

3327

3328

3329

3330

3331

3332

3333

3334

3335

3336

3337

3338

3339

3340

3341

3342

3343

3344

3345

3346

3347

3348

3349

3350

3351

3352

3353

3354

3355

3356

3357

3358

3359

3360

3361

3362

3363

3364

3365

3366

3367

3368

3369

3370

3371

3372

3373

3374

3375

3376

3377

3378

3379

3380

3381

3382

3383

3384

3385

3386

3387

3388

3389

3390

3391

3392

3393

3394

3395

3396

3397

3398

3399

3400

3401

3402

3403

3404

3405

3406

3407

3408

3409

3410

3411

3412

3413

3414

3415

3416

3417

3418

3419

3420

3421

3422

3423

3424

3425

3426

3427

3428

3429

3430

3431

3432

3433

3434

3435

3436

3437

3438

3439

3440

3441

3442

3443

3444

3445

3446

3447

3448

3449

3450

3451

3452

3453

3454

3455

3456

3457

3458

3459

3460

3461

3462

3463

3464

3465

3466

3467

3468

3469

3470

3471

3472

3473

3474

3475

3476

3477

3478

3479

3480

3481

3482

3483

3484

3485

3486

3487

3488

3489

3490

3491

3492

3493

3494

3495

3496

3497

3498

3499

3500

3501

3502

3503

3504

3505

3506

3507

3508

3509

3510

3511

3512

3513

3514

3515

3516

3517

3518

3519

3520

3521

3522

3523

3524

3525

3526

3527

3528

3529

3530

3531

3532

3533

3534

3535

3536

3537

3538

3539

3540

3541

3542

3543

3544

3545

3546

3547

3548

3549

3550

3551

3552

3553

3554

3555

3556

3557

3558

3559

3560

3561

3562

3563

3564

3565

3566

3567

3568

3569

3570

3571

3572

3573

3574

3575

3576

3577

3578

3579

3580

3581

3582

3583

3584

3585

3586

3587

3588

3589

3590

3591

3592

3593

3594

3595

3596

3597

3598

3599

3600

3601

3602

3603

3604

3605

3606

3607

3608

3609

3610

3611

3612

3613

3614

3615

3616

3617

3618

3619

3620

3621

3622

3623

3624

3625

3626

3627

3628

3629

3630

3631

3632

3633

3634

3635

3636

3637

3638

3639

3640

3641

3642

3643

3644

3645

3646

3647

3648

3649

3650

3651

3652

3653

3654

3655

3656

3657

3658

3659

3660

3661

3662

3663

3664

3665

3666

3667

3668

3669

3670

3671

3672

3673

3674

3675

3676

3677

3678

3679

3680

3681

3682

3683

3684

3685

3686

3687

3688

3689

3690

3691

3692

3693

3694

3695

3696

3697

3698

3699

3700

3701

3702

3703

3704

3705

3706

3707

3708

3709

3710

3711

3712

3713

3714

3715

3716

3717

3718

3719

3720

3721

3722

3723

3724

3725

3726

3727

3728

3729

3730

3731

3732

3733

3734

3735

3736

3737

3738

3739

3740

3741

3742

3743

3744

3745

3746

3747

3748

3749

3750

3751

3752

3753

3754

3755

3756

3757

3758

3759

3760

3761

3762

3763

3764

3765

3766

3767

3768

3769

3770

3771

3772

3773

3774

3775

3776

3777

3778

3779

3780

3781

3782

3783

3784

3785

3786

3787

3788

3789

3790

3791

3792

3793

3794

3795

3796

3797

3798

3799

3800

3801

3802

3803

3804

3805

3806

3807

3808

3809

3810

3811

3812

3813

3814

3815

3816

3817

3818

3819

3820

3821

3822

3823

3824

3825

3826

3827

3828

3829

3830

3831

3832

3833

3834

3835

3836

3837

3838

3839

3840

3841

3842

3843

3844

3845

3846

3847

3848

3849

3850

3851

3852

3853

3854

3855

3856

3857

3858

3859

3860

3861

3862

3863

3864

3865

3866

3867

3868

3869

3870

3871

3872

3873

3874

3875

3876

3877

3878

3879

3880

3881

3882

3883

3884

3885

3886

3887

3888

3889

3890

3891

3892

3893

3894

3895

3896

3897

3898

3899

3900

3901

3902

3903

3904

3905

3906

3907

3908

3909

3910

3911

3912

3913

3914

3915

3916

3917

3918

3919

3920

3921

3922

3923

3924

3925

3926

3927

3928

3929

3930

3931

3932

3933

3934

3935

3936

3937

3938

3939

3940

3941

3942

3943

3944

3945

3946

3947

3948

3949

3950

3951

3952

3953

3954

3955

3956

3957

3958

3959

3960

3961

3962

3963

3964

3965

3966

3967

3968

3969

3970

3971

3972

3973

3974

3975

3976

3977

3978

3979

3980

3981

3982

3983

3984

3985

3986

3987

3988

3989

3990

3991

3992

3993

3994

3995

3996

3997

3998

3999

4000

4001

4002

4003

4004

4005

4006

4007

4008

4009

4010

4011

4012

4013

4014

4015

4016

4017

4018

4019

4020

4021

4022

4023

4024

4025

4026

4027

4028

4029

4030

4031

4032

4033

4034

4035

4036

4037

4038

4039

4040

4041

4042

4043

4044

4045

4046

4047

4048

4049

4050

4051

4052

4053

4054

4055

4056

4057

4058

4059

4060

4061

4062

4063

4064

4065

4066

4067

4068

4069

4070

4071

4072

4073

4074

4075

4076

4077

4078

4079

4080

4081

4082

4083

4084

4085

4086

4087

4088

4089

4090

4091

4092

4093

4094

4095

4096

4097

4098

4099

4100

4101

4102

4103

4104

4105

4106

4107

4108

4109

4110

4111

4112

4113

4114

4115

4116

4117

4118

4119

4120

4121

4122

4123

4124

4125

4126

4127

4128

4129

4130

4131

4132

4133

4134

4135

4136

4137

4138

4139

4140

4141

4142

4143

4144

4145

4146

4147

4148

4149

4150

4151

4152

4153

4154

4155

4156

4157

4158

4159

4160

4161

4162

4163

4164

4165

4166

4167

4168

4169

4170

4171

4172

4173

4174

4175

4176

4177

4178

4179

4180

4181

4182

4183

4184

4185

4186

4187

4188

4189

4190

4191

4192

4193

4194

4195

4196

4197

4198

4199

4200

4201

4202

4203

4204

4205

4206

4207

4208

4209

4210

4211

4212

4213

4214

4215

4216

4217

4218

4219

4220

4221

4222

4223

4224

4225

4226

4227

4228

4229

4230

4231

4232

4233

4234

4235

4236

4237

4238

4239

4240

4241

4242

4243

4244

4245

4246

4247

4248

4249

4250

4251

4252

4253

4254

4255

4256

4257

4258

4259

4260

4261

4262

4263

4264

4265

4266

4267

4268

4269

4270

4271

4272

4273

4274

4275

4276

4277

4278

4279

4280

4281

4282

4283

4284

4285

4286

4287

4288

4289

4290

4291

4292

4293

4294

4295

4296

4297

4298

4299

4300

4301

4302

4303

4304

4305

4306

4307

4308

4309

4310

4311

4312

4313

4314

4315

4316

4317

4318

4319

4320

4321

4322

4323

4324

4325

4326

4327

4328

4329

4330

4331

4332

4333

4334

4335

4336

4337

4338

4339

4340

4341

4342

4343

4344

4345

4346

4347

4348

4349

4350

4351

4352

4353

4354

4355

4356

4357

4358

4359

4360

4361

4362

4363

4364

4365

4366

4367

4368

4369

4370

4371

4372

4373

4374

4375

4376

4377

4378

4379

4380

4381

4382

4383

4384

4385

4386

4387

4388

4389

4390

4391

4392

4393

4394

4395

4396

4397

4398

4399

4400

4401

4402

4403

4404

4405

4406

4407

4408

4409

4410

4411

4412

4413

4414

4415

4416

4417

4418

4419

4420

4421

4422

4423

4424

4425

4426

4427

4428

4429

4430

4431

4432

4433

4434

4435

4436

4437

4438

4439

4440

4441

4442

4443

4444

4445

4446

4447

4448

4449

4450

4451

4452

4453

4454

4455

4456

4457

4458

4459

4460

4461

4462

4463

4464

4465

4466

4467

4468

4469

4470

4471

4472

4473

4474

4475

4476

4477

4478

4479

4480

4481

4482

4483

4484

4485

4486

4487

4488

4489

4490

4491

4492

4493

4494

4495

4496

4497

4498

4499

4500

4501

4502

4503

4504

4505

4506

4507

4508

4509

4510

4511

4512

4513

4514

4515

4516

4517

4518

4519

4520

4521

4522

4523

4524

4525

4526

4527

4528

4529

4530

4531

4532

4533

4534

4535

4536

4537

4538

4539

4540

4541

4542

4543

4544

4545

4546

4547

4548

4549

4550

4551

4552

4553

4554

4555

4556

4557

4558

4559

4560

4561

4562

4563

4564

4565

4566

4567

4568

4569

4570

4571

4572

4573

4574

4575

4576

4577

4578

4579

4580

4581

4582

4583

4584

4585

4586

4587

4588

4589

4590

4591

4592

4593

4594

4595

4596

4597

4598

4599

4600

4601

4602

4603

4604

4605

4606

4607

4608

4609

4610

4611

4612

4613

4614

4615

4616

4617

4618

4619

4620

4621

4622

4623

4624

4625

4626

4627

4628

4629

4630

4631

4632

4633

4634

4635

4636

4637

4638

4639

4640

4641

4642

4643

4644

4645

4646

4647

4648

4649

4650

4651

4652

4653

4654

4655

4656

4657

4658

4659

4660

4661

4662

4663

4664

4665

4666

4667

4668

4669

4670

4671

4672

4673

4674

4675

4676

4677

4678

4679

4680

4681

4682

4683

4684

4685

4686

4687

4688

4689

4690

4691

4692

4693

4694

4695

4696

4697

4698

4699

4700

4701

4702

4703

4704

4705

4706

4707

4708

4709

4710

4711

4712

4713

4714

4715

4716

4717

4718

4719

4720

4721

4722

4723

4724

4725

4726

4727

4728

4729

4730

4731

4732

4733

4734

4735

4736

4737

4738

4739

4740

4741

4742

4743

4744

4745

4746

4747

4748

4749

4750

4751

4752

4753

4754

4755

4756

4757

4758

4759

4760

4761

4762

4763

4764

4765

4766

4767

4768

4769

4770

4771

4772

4773

4774

4775

4776

4777

4778

4779

4780

4781

4782

4783

4784

4785

4786

4787

4788

4789

4790

4791

4792

4793

4794

4795

4796

4797

4798

4799

4800

4801

4802

4803

4804

4805

4806

4807

4808

4809

4810

4811

4812

4813

4814

4815

4816

4817

4818

4819

4820

4821

4822

4823

4824

4825

4826

4827

4828

4829

4830

4831

4832

4833

4834

4835

4836

4837

4838

4839

4840

4841

4842

4843

4844

4845

4846

4847

4848

4849

4850

4851

4852

4853

4854

4855

4856

4857

4858

4859

4860

4861

4862

4863

4864

4865

4866

4867

4868

4869

4870

4871

4872

4873

4874

4875

4876

4877

4878

4879

4880

4881

4882

4883

4884

4885

4886

4887

4888

4889

4890

4891

4892

4893

4894

4895

4896

4897

4898

4899

4900

4901

4902

4903

4904

4905

4906

4907

4908

4909

4910

4911

4912

4913

4914

4915

4916

4917

4918

4919

4920

4921

4922

4923

4924

4925

4926

4927

4928

4929

4930

4931

4932

4933

4934

4935

4936

4937

4938

4939

4940

4941

4942

4943

4944

4945

4946

4947

4948

4949

4950

4951

4952

4953

4954

4955

4956

4957

4958

4959

4960

4961

4962

4963

4964

4965

4966

4967

4968

4969

4970

4971

4972

4973

4974

4975

4976

4977

4978

4979

4980

4981

4982

4983

4984

4985

4986

4987

4988

4989

4990

4991

4992

4993

4994

4995

4996

4997

4998

4999

5000

5001

5002

5003

5004

5005

5006

5007

5008

5009

5010

5011

5012

5013

5014

5015

5016

5017

5018

5019

5020

5021

5022

5023

5024

5025

5026

5027

5028

5029

5030

5031

5032

5033

5034

5035

5036

5037

5038

5039

5040

5041

5042

5043

5044

5045

5046

5047

5048

5049

5050

5051

5052

5053

5054

5055

5056

5057

5058

5059

5060

5061

5062

5063

5064

5065

5066

5067

5068

5069

5070

5071

5072

5073

5074

5075

5076

5077

5078

5079

5080

5081

5082

5083

5084

5085

5086

5087

5088

5089

5090

5091

5092

5093

5094

5095

5096

5097

5098

5099

5100

5101

5102

5103

5104

5105

5106

5107

5108

5109

5110

5111

5112

5113

5114

5115

5116

5117

5118

5119

5120

5121

5122

5123

5124

5125

5126

5127

5128

5129

5130

5131

5132

5133

5134

5135

5136

5137

5138

5139

5140

5141

5142

5143

5144

5145

5146

5147

5148

5149

5150

5151

5152

5153

5154

5155

5156

5157

5158

5159

5160

5161

5162

5163

5164

5165

5166

5167

5168

5169

5170

5171

5172

5173

5174

5175

5176

5177

5178

5179

5180

5181

5182

5183

5184

5185

5186

5187

5188

5189

5190

5191

5192

5193

5194

5195

5196

5197

5198

5199

5200

5201

5202

5203

5204

5205

5206

5207

5208

5209

5210

5211

5212

5213

5214

5215

5216

5217

5218

5219

5220

5221

5222

5223

5224

5225

5226

5227

5228

5229

5230

5231

5232

5233

5234

5235

5236

5237

5238

5239

5240

5241

5242

5243

5244

5245

5246

5247

5248

5249

5250

5251

5252

5253

5254

5255

5256

5257

5258

5259

5260

5261

5262

5263

5264

5265

5266

5267

5268

5269

5270

5271

5272

5273

5274

5275

5276

5277

5278

5279

5280

5281

5282

5283

5284

5285

5286

5287

5288

5289

5290

5291

5292

5293

5294

5295

5296

5297

5298

5299

5300

5301

5302

5303

5304

5305

5306

5307

5308

5309

5310

5311

5312

5313

5314

5315

5316

5317

5318

5319

5320

5321

5322

5323

5324

5325

5326

5327

5328

5329

5330

5331

5332

5333

5334

5335

5336

5337

5338

5339

5340

5341

5342

5343

5344

5345

5346

5347

5348

5349

5350

5351

5352

5353

5354

5355

5356

5357

5358

5359

5360

5361

5362

5363

5364

5365

5366

5367

5368

5369

5370

5371

5372

5373

5374

5375

5376

5377

5378

5379

5380

5381

5382

5383

5384

5385

5386

5387

5388

5389

5390

5391

5392

5393

5394

5395

5396

5397

5398

5399

5400

5401

5402

5403

5404

5405

5406

5407

5408

5409

5410

5411

5412

5413

5414

5415

5416

5417

5418

5419

5420

5421

5422

5423

5424

5425

5426

5427

5428

5429

5430

5431

5432

5433

5434

5435

5436

5437

5438

5439

5440

5441

5442

5443

5444

5445

5446

5447

5448

5449

5450

5451

5452

5453

5454

5455

5456

5457

5458

5459

5460

5461

5462

5463

5464

5465

5466

5467

5468

5469

5470

5471

5472

5473

5474

5475

5476

5477

5478

5479

5480

5481

5482

5483

5484

5485

5486

5487

5488

5489

5490

5491

5492

5493

5494

5495

5496

5497

5498

5499

5500

5501

5502

5503

5504

5505

5506

5507

5508

5509

5510

5511

5512

5513

5514

5515

5516

5517

5518

5519

5520

5521

5522

5523

5524

5525

5526

5527

5528

5529

5530

5531

5532

5533

5534

5535

5536

5537

5538

5539

5540

5541

5542

5543

5544

5545

5546

5547

5548

5549

5550

5551

5552

5553

5554

5555

5556

5557

5558

5559

5560

5561

5562

5563

5564

5565

5566

5567

5568

5569

5570

5571

5572

5573

5574

5575

5576

5577

5578

5579

5580

5581

5582

5583

5584

5585

5586

5587

5588

5589

5590

5591

5592

5593

5594

5595

5596

5597

5598

5599

5600

5601

5602

5603

5604

5605

5606

5607

5608

5609

5610

5611

5612

5613

5614

5615

5616

5617

5618

5619

5620

5621

5622

5623

5624

5625

5626

5627

5628

5629

5630

5631

5632

5633

5634

5635

5636

5637

5638

5639

5640

5641

5642

5643

5644

5645

5646

5647

5648

5649

5650

5651

5652

5653

5654

5655

5656

5657

5658

5659

5660

5661

5662

5663

5664

5665

5666

5667

5668

5669

5670

5671

5672

5673

5674

5675

5676

5677

5678

5679

5680

5681

5682

5683

5684

5685

5686

5687

5688

5689

5690

5691

5692

5693

5694

5695

5696

5697

5698

5699

5700

5701

5702

5703

5704

5705

5706

5707

5708

5709

5710

5711

5712

5713

5714

5715

5716

5717

5718

5719

5720

5721

5722

5723

5724

5725

5726

5727

5728

5729

5730

5731

5732

5733

5734

5735

5736

5737

5738

5739

5740

5741

5742

5743

5744

5745

5746

5747

5748

5749

5750

5751

5752

5753

5754

5755

5756

5757

5758

5759

5760

5761

5762

5763

5764

5765

5766

5767

5768

5769

5770

5771

5772

5773

5774

5775

5776

5777

5778

5779

5780

5781

5782

5783

5784

5785

5786

5787

5788

5789

5790

5791

5792

5793

5794

5795

5796

5797

5798

5799

5800

5801

5802

5803

5804

5805

5806

5807

5808

5809

5810

5811

5812

5813

5814

5815

5816

5817

5818

5819

5820

5821

5822

5823

5824

5825

5826

5827

5828

5829

5830

5831

5832

5833

5834

5835

5836

5837

5838

5839

5840

5841

5842

5843

5844

5845

5846

5847

5848

5849

5850

5851

5852

5853

5854

5855

5856

5857

5858

5859

5860

5861

5862

5863

5864

5865

5866

5867

5868

5869

5870

5871

5872

5873

5874

5875

5876

5877

5878

5879

5880

5881

5882

5883

5884

5885

5886

5887

5888

5889

5890

5891

5892

5893

5894

5895

5896

5897

5898

5899

5900

5901

5902

5903

5904

5905

5906

5907

5908

5909

5910

5911

5912

5913

5914

5915

5916

5917

5918

5919

5920

5921

5922

5923

5924

5925

5926

5927

5928

5929

5930

5931

5932

5933

5934

5935

5936

5937

5938

5939

5940

5941

5942

5943

5944

5945

5946

5947

5948

5949

5950

5951

5952

5953

5954

5955

5956

5957

5958

5959

5960

5961

5962

5963

5964

5965

5966

5967

5968

5969

5970

5971

5972

5973

5974

5975

5976

5977

5978

5979

5980

5981

5982

5983

5984

5985

5986

5987

5988

5989

5990

5991

5992

5993

5994

5995

5996

5997

5998

5999

6000

6001

6002

6003

6004

6005

6006

6007

6008

6009

6010

6011

6012

6013

6014

6015

6016

6017

6018

6019

6020

6021

6022

6023

6024

6025

6026

6027

6028

6029

6030

6031

6032

6033

6034

6035

6036

6037

6038

6039

6040

6041

6042

6043

6044

6045

6046

6047

6048

6049

6050

6051

6052

6053

6054

6055

6056

6057

6058

6059

6060

6061

6062

6063

6064

6065

6066

6067

6068

6069

6070

6071

6072

6073

6074

6075

6076

6077

6078

6079

6080

6081

6082

6083

6084

6085

6086

6087

6088

6089

6090

6091

6092

6093

6094

6095

6096

6097

6098

6099

6100

6101

6102

6103

6104

6105

6106

6107

6108

6109

6110

6111

6112

6113

6114

6115

6116

6117

6118

6119

6120

6121

6122

6123

6124

6125

6126

6127

6128

6129

6130

6131

6132

6133

6134

6135

6136

6137

6138

6139

6140

6141

6142

6143

6144

6145

6146

6147

6148

6149

6150

6151

6152

6153

6154

6155

6156

6157

6158

6159

6160

6161

6162

6163

6164

6165

6166

6167

6168

6169

6170

6171

6172

6173

6174

6175

6176

6177

6178

6179

6180

6181

6182

6183

6184

6185

6186

6187

6188

6189

6190

6191

6192

6193

6194

6195

6196

6197

6198

6199

6200

6201

6202

6203

6204

6205

6206

6207

6208

6209

6210

6211

6212

6213

6214

6215

6216

6217

6218

6219

6220

6221

6222

6223

6224

6225

6226

6227

6228

6229

6230

6231

6232

6233

6234

6235

6236

6237

6238

6239

6240

6241

6242

6243

6244

6245

6246

6247

6248

6249

6250

6251

6252

6253

6254

6255

6256

6257

6258

6259

6260

6261

6262

6263

6264

6265

6266

6267

6268

6269

6270

6271

6272

6273

6274

6275

6276

6277

6278

6279

6280

6281

6282

6283

6284

6285

6286

6287

6288

6289

6290

6291

6292

6293

6294

6295

6296

6297

6298

6299

6300

6301

6302

6303

6304

6305

6306

6307

6308

6309

6310

6311

6312

6313

6314

6315

6316

6317

6318

6319

6320

6321

6322

6323

6324

6325

6326

6327

6328

6329

6330

6331

6332

6333

6334

6335

6336

6337

6338

6339

6340

6341

6342

6343

6344

6345

6346

6347

6348

6349

6350

6351

6352

6353

6354

6355

6356

6357

6358

6359

6360

6361

6362

6363

6364

6365

6366

6367

6368

6369

6370

6371

6372

6373

6374

6375

6376

6377

6378

6379

6380

6381

6382

6383

6384

6385

6386

6387

6388

6389

6390

6391

6392

6393

6394

6395

6396

6397

6398

6399

6400

6401

6402

6403

6404

6405

6406

6407

6408

6409

6410

6411

6412

6413

6414

6415

6416

6417

6418

6419

6420

6421

6422

6423

6424

6425

6426

6427

6428

6429

6430

6431

6432

6433

6434

6435

6436

6437

6438

6439

6440

6441

6442

6443

6444

6445

6446

6447

6448

6449

6450

6451

6452

6453

6454

6455

6456

6457

6458

6459

6460

6461

6462

6463

6464

6465

6466

6467

6468

6469

6470

6471

6472

6473

6474

6475

6476

6477

6478

6479

6480

6481

6482

6483

6484

6485

6486

6487

6488

6489

6490

6491

6492

6493

6494

6495

6496

6497

6498

6499

6500

6501

6502

6503

6504

6505

6506

6507

6508

6509

6510

6511

6512

6513

6514

6515

6516

6517

6518

6519

6520

6521

6522

6523

6524

6525

6526

6527

6528

6529

6530

6531

6532

6533

6534

6535

6536

6537

6538

6539

6540

6541

6542

6543

6544

6545

6546

6547

6548

6549

6550

6551

6552

6553

6554

6555

6556

6557

6558

6559

6560

6561

6562

6563

6564

6565

6566

6567

6568

6569

6570

6571

6572

6573

6574

6575

6576

6577

6578

6579

6580

6581

6582

6583

6584

6585

6586

6587

6588

6589

6590

6591

6592

6593

6594

6595

6596

6597

6598

6599

6600

6601

6602

6603

6604

6605

6606

6607

6608

6609

6610

6611

6612

6613

6614

6615

6616

6617

6618

6619

6620

6621

6622

6623

6624

6625

6626

6627

6628

6629

6630

6631

6632

6633

6634

6635

6636

6637

6638

6639

6640

6641

6642

6643

6644

6645

6646

6647

6648

6649

6650

6651

6652

6653

6654

6655

6656

6657

6658

6659

6660

6661

6662

6663

6664

6665

6666

6667

6668

6669

6670

6671

6672

6673

6674

6675

6676

6677

6678

6679

6680

6681

6682

6683

6684

6685

6686

6687

6688

6689

6690

6691

6692

6693

6694

6695

# 

# Author: Travis Oliphant 2002-2011 with contributions from 

# SciPy Developers 2004-2011 

# 

from __future__ import division, print_function, absolute_import 

 

import warnings 

 

import numpy as np 

 

from scipy.misc.doccer import (extend_notes_in_docstring, 

replace_notes_in_docstring) 

from scipy import optimize 

from scipy import integrate 

import scipy.special as sc 

from scipy._lib._numpy_compat import broadcast_to 

 

from . import _stats 

from ._tukeylambda_stats import (tukeylambda_variance as _tlvar, 

tukeylambda_kurtosis as _tlkurt) 

from ._distn_infrastructure import (get_distribution_names, _kurtosis, 

_lazyselect, _lazywhere, _ncx2_cdf, 

_ncx2_log_pdf, _ncx2_pdf, 

rv_continuous, _skew, valarray) 

from ._constants import _XMIN, _EULER, _ZETA3, _XMAX, _LOGXMAX 

 

 

# In numpy 1.12 and above, np.power refuses to raise integers to negative 

# powers, and `np.float_power` is a new replacement. 

try: 

float_power = np.float_power 

except AttributeError: 

float_power = np.power 

 

 

## Kolmogorov-Smirnov one-sided and two-sided test statistics 

class ksone_gen(rv_continuous): 

"""General Kolmogorov-Smirnov one-sided test. 

 

%(default)s 

 

""" 

def _cdf(self, x, n): 

return 1.0 - sc.smirnov(n, x) 

 

def _ppf(self, q, n): 

return sc.smirnovi(n, 1.0 - q) 

 

 

ksone = ksone_gen(a=0.0, name='ksone') 

 

 

class kstwobign_gen(rv_continuous): 

"""Kolmogorov-Smirnov two-sided test for large N. 

 

%(default)s 

 

""" 

def _cdf(self, x): 

return 1.0 - sc.kolmogorov(x) 

 

def _sf(self, x): 

return sc.kolmogorov(x) 

 

def _ppf(self, q): 

return sc.kolmogi(1.0 - q) 

 

 

kstwobign = kstwobign_gen(a=0.0, name='kstwobign') 

 

 

## Normal distribution 

 

# loc = mu, scale = std 

# Keep these implementations out of the class definition so they can be reused 

# by other distributions. 

_norm_pdf_C = np.sqrt(2*np.pi) 

_norm_pdf_logC = np.log(_norm_pdf_C) 

 

 

def _norm_pdf(x): 

return np.exp(-x**2/2.0) / _norm_pdf_C 

 

 

def _norm_logpdf(x): 

return -x**2 / 2.0 - _norm_pdf_logC 

 

 

def _norm_cdf(x): 

return sc.ndtr(x) 

 

 

def _norm_logcdf(x): 

return sc.log_ndtr(x) 

 

 

def _norm_ppf(q): 

return sc.ndtri(q) 

 

 

def _norm_sf(x): 

return _norm_cdf(-x) 

 

 

def _norm_logsf(x): 

return _norm_logcdf(-x) 

 

 

def _norm_isf(q): 

return -_norm_ppf(q) 

 

 

class norm_gen(rv_continuous): 

r"""A normal continuous random variable. 

 

The location (loc) keyword specifies the mean. 

The scale (scale) keyword specifies the standard deviation. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `norm` is: 

 

.. math:: 

 

f(x) = \frac{\exp(-x^2/2)}{\sqrt{2\pi}} 

 

The survival function, ``norm.sf``, is also referred to as the 

Q-function in some contexts (see, e.g., 

`Wikipedia's <https://en.wikipedia.org/wiki/Q-function>`_ definition). 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self): 

return self._random_state.standard_normal(self._size) 

 

def _pdf(self, x): 

# norm.pdf(x) = exp(-x**2/2)/sqrt(2*pi) 

return _norm_pdf(x) 

 

def _logpdf(self, x): 

return _norm_logpdf(x) 

 

def _cdf(self, x): 

return _norm_cdf(x) 

 

def _logcdf(self, x): 

return _norm_logcdf(x) 

 

def _sf(self, x): 

return _norm_sf(x) 

 

def _logsf(self, x): 

return _norm_logsf(x) 

 

def _ppf(self, q): 

return _norm_ppf(q) 

 

def _isf(self, q): 

return _norm_isf(q) 

 

def _stats(self): 

return 0.0, 1.0, 0.0, 0.0 

 

def _entropy(self): 

return 0.5*(np.log(2*np.pi)+1) 

 

@replace_notes_in_docstring(rv_continuous, notes="""\ 

This function uses explicit formulas for the maximum likelihood 

estimation of the normal distribution parameters, so the 

`optimizer` argument is ignored.\n\n""") 

def fit(self, data, **kwds): 

floc = kwds.get('floc', None) 

fscale = kwds.get('fscale', None) 

 

if floc is not None and fscale is not None: 

# This check is for consistency with `rv_continuous.fit`. 

# Without this check, this function would just return the 

# parameters that were given. 

raise ValueError("All parameters fixed. There is nothing to " 

"optimize.") 

 

data = np.asarray(data) 

 

if floc is None: 

loc = data.mean() 

else: 

loc = floc 

 

if fscale is None: 

scale = np.sqrt(((data - loc)**2).mean()) 

else: 

scale = fscale 

 

return loc, scale 

 

 

norm = norm_gen(name='norm') 

 

 

class alpha_gen(rv_continuous): 

r"""An alpha continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `alpha` is: 

 

.. math:: 

 

f(x, a) = \frac{1}{x^2 \Phi(a) \sqrt{2\pi}} * 

\exp(-\frac{1}{2} (a-1/x)^2) 

 

where ``Phi(alpha)`` is the normal CDF, ``x > 0``, and ``a > 0``. 

 

`alpha` takes ``a`` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _pdf(self, x, a): 

# alpha.pdf(x, a) = 1/(x**2*Phi(a)*sqrt(2*pi)) * exp(-1/2 * (a-1/x)**2) 

return 1.0/(x**2)/_norm_cdf(a)*_norm_pdf(a-1.0/x) 

 

def _logpdf(self, x, a): 

return -2*np.log(x) + _norm_logpdf(a-1.0/x) - np.log(_norm_cdf(a)) 

 

def _cdf(self, x, a): 

return _norm_cdf(a-1.0/x) / _norm_cdf(a) 

 

def _ppf(self, q, a): 

return 1.0/np.asarray(a-sc.ndtri(q*_norm_cdf(a))) 

 

def _stats(self, a): 

return [np.inf]*2 + [np.nan]*2 

 

 

alpha = alpha_gen(a=0.0, name='alpha') 

 

 

class anglit_gen(rv_continuous): 

r"""An anglit continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `anglit` is: 

 

.. math:: 

 

f(x) = \sin(2x + \pi/2) = \cos(2x) 

 

for :math:`-\pi/4 \le x \le \pi/4`. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x): 

# anglit.pdf(x) = sin(2*x + \pi/2) = cos(2*x) 

return np.cos(2*x) 

 

def _cdf(self, x): 

return np.sin(x+np.pi/4)**2.0 

 

def _ppf(self, q): 

return np.arcsin(np.sqrt(q))-np.pi/4 

 

def _stats(self): 

return 0.0, np.pi*np.pi/16-0.5, 0.0, -2*(np.pi**4 - 96)/(np.pi*np.pi-8)**2 

 

def _entropy(self): 

return 1-np.log(2) 

 

 

anglit = anglit_gen(a=-np.pi/4, b=np.pi/4, name='anglit') 

 

 

class arcsine_gen(rv_continuous): 

r"""An arcsine continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `arcsine` is: 

 

.. math:: 

 

f(x) = \frac{1}{\pi \sqrt{x (1-x)}} 

 

for :math:`0 \le x \le 1`. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x): 

# arcsine.pdf(x) = 1/(pi*sqrt(x*(1-x))) 

return 1.0/np.pi/np.sqrt(x*(1-x)) 

 

def _cdf(self, x): 

return 2.0/np.pi*np.arcsin(np.sqrt(x)) 

 

def _ppf(self, q): 

return np.sin(np.pi/2.0*q)**2.0 

 

def _stats(self): 

mu = 0.5 

mu2 = 1.0/8 

g1 = 0 

g2 = -3.0/2.0 

return mu, mu2, g1, g2 

 

def _entropy(self): 

return -0.24156447527049044468 

 

 

arcsine = arcsine_gen(a=0.0, b=1.0, name='arcsine') 

 

 

class FitDataError(ValueError): 

# This exception is raised by, for example, beta_gen.fit when both floc 

# and fscale are fixed and there are values in the data not in the open 

# interval (floc, floc+fscale). 

def __init__(self, distr, lower, upper): 

self.args = ( 

"Invalid values in `data`. Maximum likelihood " 

"estimation with {distr!r} requires that {lower!r} < x " 

"< {upper!r} for each x in `data`.".format( 

distr=distr, lower=lower, upper=upper), 

) 

 

 

class FitSolverError(RuntimeError): 

# This exception is raised by, for example, beta_gen.fit when 

# optimize.fsolve returns with ier != 1. 

def __init__(self, mesg): 

emsg = "Solver for the MLE equations failed to converge: " 

emsg += mesg.replace('\n', '') 

self.args = (emsg,) 

 

 

def _beta_mle_a(a, b, n, s1): 

# The zeros of this function give the MLE for `a`, with 

# `b`, `n` and `s1` given. `s1` is the sum of the logs of 

# the data. `n` is the number of data points. 

psiab = sc.psi(a + b) 

func = s1 - n * (-psiab + sc.psi(a)) 

return func 

 

 

def _beta_mle_ab(theta, n, s1, s2): 

# Zeros of this function are critical points of 

# the maximum likelihood function. Solving this system 

# for theta (which contains a and b) gives the MLE for a and b 

# given `n`, `s1` and `s2`. `s1` is the sum of the logs of the data, 

# and `s2` is the sum of the logs of 1 - data. `n` is the number 

# of data points. 

a, b = theta 

psiab = sc.psi(a + b) 

func = [s1 - n * (-psiab + sc.psi(a)), 

s2 - n * (-psiab + sc.psi(b))] 

return func 

 

 

class beta_gen(rv_continuous): 

r"""A beta continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `beta` is: 

 

.. math:: 

 

f(x, a, b) = \frac{\gamma(a+b) x^{a-1} (1-x)^{b-1}} 

{\gamma(a) \gamma(b)} 

 

for :math:`0 < x < 1`, :math:`a > 0`, :math:`b > 0`, where 

:math:`\gamma(z)` is the gamma function (`scipy.special.gamma`). 

 

`beta` takes :math:`a` and :math:`b` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self, a, b): 

return self._random_state.beta(a, b, self._size) 

 

def _pdf(self, x, a, b): 

# gamma(a+b) * x**(a-1) * (1-x)**(b-1) 

# beta.pdf(x, a, b) = ------------------------------------ 

# gamma(a)*gamma(b) 

return np.exp(self._logpdf(x, a, b)) 

 

def _logpdf(self, x, a, b): 

lPx = sc.xlog1py(b - 1.0, -x) + sc.xlogy(a - 1.0, x) 

lPx -= sc.betaln(a, b) 

return lPx 

 

def _cdf(self, x, a, b): 

return sc.btdtr(a, b, x) 

 

def _ppf(self, q, a, b): 

return sc.btdtri(a, b, q) 

 

def _stats(self, a, b): 

mn = a*1.0 / (a + b) 

var = (a*b*1.0)/(a+b+1.0)/(a+b)**2.0 

g1 = 2.0*(b-a)*np.sqrt((1.0+a+b)/(a*b)) / (2+a+b) 

g2 = 6.0*(a**3 + a**2*(1-2*b) + b**2*(1+b) - 2*a*b*(2+b)) 

g2 /= a*b*(a+b+2)*(a+b+3) 

return mn, var, g1, g2 

 

def _fitstart(self, data): 

g1 = _skew(data) 

g2 = _kurtosis(data) 

 

def func(x): 

a, b = x 

sk = 2*(b-a)*np.sqrt(a + b + 1) / (a + b + 2) / np.sqrt(a*b) 

ku = a**3 - a**2*(2*b-1) + b**2*(b+1) - 2*a*b*(b+2) 

ku /= a*b*(a+b+2)*(a+b+3) 

ku *= 6 

return [sk-g1, ku-g2] 

a, b = optimize.fsolve(func, (1.0, 1.0)) 

return super(beta_gen, self)._fitstart(data, args=(a, b)) 

 

@extend_notes_in_docstring(rv_continuous, notes="""\ 

In the special case where both `floc` and `fscale` are given, a 

`ValueError` is raised if any value `x` in `data` does not satisfy 

`floc < x < floc + fscale`.\n\n""") 

def fit(self, data, *args, **kwds): 

# Override rv_continuous.fit, so we can more efficiently handle the 

# case where floc and fscale are given. 

 

f0 = (kwds.get('f0', None) or kwds.get('fa', None) or 

kwds.get('fix_a', None)) 

f1 = (kwds.get('f1', None) or kwds.get('fb', None) or 

kwds.get('fix_b', None)) 

floc = kwds.get('floc', None) 

fscale = kwds.get('fscale', None) 

 

if floc is None or fscale is None: 

# do general fit 

return super(beta_gen, self).fit(data, *args, **kwds) 

 

if f0 is not None and f1 is not None: 

# This check is for consistency with `rv_continuous.fit`. 

raise ValueError("All parameters fixed. There is nothing to " 

"optimize.") 

 

# Special case: loc and scale are constrained, so we are fitting 

# just the shape parameters. This can be done much more efficiently 

# than the method used in `rv_continuous.fit`. (See the subsection 

# "Two unknown parameters" in the section "Maximum likelihood" of 

# the Wikipedia article on the Beta distribution for the formulas.) 

 

# Normalize the data to the interval [0, 1]. 

data = (np.ravel(data) - floc) / fscale 

if np.any(data <= 0) or np.any(data >= 1): 

raise FitDataError("beta", lower=floc, upper=floc + fscale) 

xbar = data.mean() 

 

if f0 is not None or f1 is not None: 

# One of the shape parameters is fixed. 

 

if f0 is not None: 

# The shape parameter a is fixed, so swap the parameters 

# and flip the data. We always solve for `a`. The result 

# will be swapped back before returning. 

b = f0 

data = 1 - data 

xbar = 1 - xbar 

else: 

b = f1 

 

# Initial guess for a. Use the formula for the mean of the beta 

# distribution, E[x] = a / (a + b), to generate a reasonable 

# starting point based on the mean of the data and the given 

# value of b. 

a = b * xbar / (1 - xbar) 

 

# Compute the MLE for `a` by solving _beta_mle_a. 

theta, info, ier, mesg = optimize.fsolve( 

_beta_mle_a, a, 

args=(b, len(data), np.log(data).sum()), 

full_output=True 

) 

if ier != 1: 

raise FitSolverError(mesg=mesg) 

a = theta[0] 

 

if f0 is not None: 

# The shape parameter a was fixed, so swap back the 

# parameters. 

a, b = b, a 

 

else: 

# Neither of the shape parameters is fixed. 

 

# s1 and s2 are used in the extra arguments passed to _beta_mle_ab 

# by optimize.fsolve. 

s1 = np.log(data).sum() 

s2 = sc.log1p(-data).sum() 

 

# Use the "method of moments" to estimate the initial 

# guess for a and b. 

fac = xbar * (1 - xbar) / data.var(ddof=0) - 1 

a = xbar * fac 

b = (1 - xbar) * fac 

 

# Compute the MLE for a and b by solving _beta_mle_ab. 

theta, info, ier, mesg = optimize.fsolve( 

_beta_mle_ab, [a, b], 

args=(len(data), s1, s2), 

full_output=True 

) 

if ier != 1: 

raise FitSolverError(mesg=mesg) 

a, b = theta 

 

return a, b, floc, fscale 

 

 

beta = beta_gen(a=0.0, b=1.0, name='beta') 

 

 

class betaprime_gen(rv_continuous): 

r"""A beta prime continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `betaprime` is: 

 

.. math:: 

 

f(x, a, b) = \frac{x^{a-1} (1+x)^{-a-b}}{\beta(a, b)} 

 

for ``x > 0``, ``a > 0``, ``b > 0``, where ``beta(a, b)`` is the beta 

function (see `scipy.special.beta`). 

 

`betaprime` takes ``a`` and ``b`` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _rvs(self, a, b): 

sz, rndm = self._size, self._random_state 

u1 = gamma.rvs(a, size=sz, random_state=rndm) 

u2 = gamma.rvs(b, size=sz, random_state=rndm) 

return u1 / u2 

 

def _pdf(self, x, a, b): 

# betaprime.pdf(x, a, b) = x**(a-1) * (1+x)**(-a-b) / beta(a, b) 

return np.exp(self._logpdf(x, a, b)) 

 

def _logpdf(self, x, a, b): 

return sc.xlogy(a - 1.0, x) - sc.xlog1py(a + b, x) - sc.betaln(a, b) 

 

def _cdf(self, x, a, b): 

return sc.betainc(a, b, x/(1.+x)) 

 

def _munp(self, n, a, b): 

if n == 1.0: 

return np.where(b > 1, 

a/(b-1.0), 

np.inf) 

elif n == 2.0: 

return np.where(b > 2, 

a*(a+1.0)/((b-2.0)*(b-1.0)), 

np.inf) 

elif n == 3.0: 

return np.where(b > 3, 

a*(a+1.0)*(a+2.0)/((b-3.0)*(b-2.0)*(b-1.0)), 

np.inf) 

elif n == 4.0: 

return np.where(b > 4, 

(a*(a + 1.0)*(a + 2.0)*(a + 3.0) / 

((b - 4.0)*(b - 3.0)*(b - 2.0)*(b - 1.0))), 

np.inf) 

else: 

raise NotImplementedError 

 

 

betaprime = betaprime_gen(a=0.0, name='betaprime') 

 

 

class bradford_gen(rv_continuous): 

r"""A Bradford continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `bradford` is: 

 

.. math:: 

 

f(x, c) = \frac{c}{k (1+cx)} 

 

for :math:`0 < x < 1`, :math:`c > 0` and :math:`k = \log(1+c)`. 

 

`bradford` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, c): 

# bradford.pdf(x, c) = c / (k * (1+c*x)) 

return c / (c*x + 1.0) / sc.log1p(c) 

 

def _cdf(self, x, c): 

return sc.log1p(c*x) / sc.log1p(c) 

 

def _ppf(self, q, c): 

return sc.expm1(q * sc.log1p(c)) / c 

 

def _stats(self, c, moments='mv'): 

k = np.log(1.0+c) 

mu = (c-k)/(c*k) 

mu2 = ((c+2.0)*k-2.0*c)/(2*c*k*k) 

g1 = None 

g2 = None 

if 's' in moments: 

g1 = np.sqrt(2)*(12*c*c-9*c*k*(c+2)+2*k*k*(c*(c+3)+3)) 

g1 /= np.sqrt(c*(c*(k-2)+2*k))*(3*c*(k-2)+6*k) 

if 'k' in moments: 

g2 = (c**3*(k-3)*(k*(3*k-16)+24)+12*k*c*c*(k-4)*(k-3) + 

6*c*k*k*(3*k-14) + 12*k**3) 

g2 /= 3*c*(c*(k-2)+2*k)**2 

return mu, mu2, g1, g2 

 

def _entropy(self, c): 

k = np.log(1+c) 

return k/2.0 - np.log(c/k) 

 

 

bradford = bradford_gen(a=0.0, b=1.0, name='bradford') 

 

 

class burr_gen(rv_continuous): 

r"""A Burr (Type III) continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

fisk : a special case of either `burr` or ``burr12`` with ``d = 1`` 

burr12 : Burr Type XII distribution 

 

Notes 

----- 

The probability density function for `burr` is: 

 

.. math:: 

 

f(x, c, d) = c d x^{-c-1} (1+x^{-c})^{-d-1} 

 

for :math:`x > 0`. 

 

`burr` takes :math:`c` and :math:`d` as shape parameters. 

 

This is the PDF corresponding to the third CDF given in Burr's list; 

specifically, it is equation (11) in Burr's paper [1]_. 

 

%(after_notes)s 

 

References 

---------- 

.. [1] Burr, I. W. "Cumulative frequency functions", Annals of 

Mathematical Statistics, 13(2), pp 215-232 (1942). 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _pdf(self, x, c, d): 

# burr.pdf(x, c, d) = c * d * x**(-c-1) * (1+x**(-c))**(-d-1) 

return c * d * (x**(-c - 1.0)) * ((1 + x**(-c))**(-d - 1.0)) 

 

def _cdf(self, x, c, d): 

return (1 + x**(-c))**(-d) 

 

def _ppf(self, q, c, d): 

return (q**(-1.0/d) - 1)**(-1.0/c) 

 

def _munp(self, n, c, d): 

nc = 1. * n / c 

return d * sc.beta(1.0 - nc, d + nc) 

 

 

burr = burr_gen(a=0.0, name='burr') 

 

 

class burr12_gen(rv_continuous): 

r"""A Burr (Type XII) continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

fisk : a special case of either `burr` or ``burr12`` with ``d = 1`` 

burr : Burr Type III distribution 

 

Notes 

----- 

The probability density function for `burr` is: 

 

.. math:: 

 

f(x, c, d) = c d x^{c-1} (1+x^c)^{-d-1} 

 

for :math:`x > 0`. 

 

`burr12` takes :math:`c` and :math:`d` as shape parameters. 

 

This is the PDF corresponding to the twelfth CDF given in Burr's list; 

specifically, it is equation (20) in Burr's paper [1]_. 

 

%(after_notes)s 

 

The Burr type 12 distribution is also sometimes referred to as 

the Singh-Maddala distribution from NIST [2]_. 

 

References 

---------- 

.. [1] Burr, I. W. "Cumulative frequency functions", Annals of 

Mathematical Statistics, 13(2), pp 215-232 (1942). 

 

.. [2] http://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/b12pdf.htm 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _pdf(self, x, c, d): 

# burr12.pdf(x, c, d) = c * d * x**(c-1) * (1+x**(c))**(-d-1) 

return np.exp(self._logpdf(x, c, d)) 

 

def _logpdf(self, x, c, d): 

return np.log(c) + np.log(d) + sc.xlogy(c - 1, x) + sc.xlog1py(-d-1, x**c) 

 

def _cdf(self, x, c, d): 

return -sc.expm1(self._logsf(x, c, d)) 

 

def _logcdf(self, x, c, d): 

return sc.log1p(-(1 + x**c)**(-d)) 

 

def _sf(self, x, c, d): 

return np.exp(self._logsf(x, c, d)) 

 

def _logsf(self, x, c, d): 

return sc.xlog1py(-d, x**c) 

 

def _ppf(self, q, c, d): 

# The following is an implementation of 

# ((1 - q)**(-1.0/d) - 1)**(1.0/c) 

# that does a better job handling small values of q. 

return sc.expm1(-1/d * sc.log1p(-q))**(1/c) 

 

def _munp(self, n, c, d): 

nc = 1. * n / c 

return d * sc.beta(1.0 + nc, d - nc) 

 

 

burr12 = burr12_gen(a=0.0, name='burr12') 

 

 

class fisk_gen(burr_gen): 

r"""A Fisk continuous random variable. 

 

The Fisk distribution is also known as the log-logistic distribution, and 

equals the Burr distribution with ``d == 1``. 

 

`fisk` takes :math:`c` as a shape parameter. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `fisk` is: 

 

.. math:: 

 

f(x, c) = c x^{-c-1} (1 + x^{-c})^{-2} 

 

for :math:`x > 0`. 

 

`fisk` takes :math:`c` as a shape parameters. 

 

%(after_notes)s 

 

See Also 

-------- 

burr 

 

%(example)s 

 

""" 

def _pdf(self, x, c): 

# fisk.pdf(x, c) = c * x**(-c-1) * (1 + x**(-c))**(-2) 

return burr_gen._pdf(self, x, c, 1.0) 

 

def _cdf(self, x, c): 

return burr_gen._cdf(self, x, c, 1.0) 

 

def _ppf(self, x, c): 

return burr_gen._ppf(self, x, c, 1.0) 

 

def _munp(self, n, c): 

return burr_gen._munp(self, n, c, 1.0) 

 

def _entropy(self, c): 

return 2 - np.log(c) 

 

 

fisk = fisk_gen(a=0.0, name='fisk') 

 

 

# median = loc 

class cauchy_gen(rv_continuous): 

r"""A Cauchy continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `cauchy` is: 

 

.. math:: 

 

f(x) = \frac{1}{\pi (1 + x^2)} 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x): 

# cauchy.pdf(x) = 1 / (pi * (1 + x**2)) 

return 1.0/np.pi/(1.0+x*x) 

 

def _cdf(self, x): 

return 0.5 + 1.0/np.pi*np.arctan(x) 

 

def _ppf(self, q): 

return np.tan(np.pi*q-np.pi/2.0) 

 

def _sf(self, x): 

return 0.5 - 1.0/np.pi*np.arctan(x) 

 

def _isf(self, q): 

return np.tan(np.pi/2.0-np.pi*q) 

 

def _stats(self): 

return np.nan, np.nan, np.nan, np.nan 

 

def _entropy(self): 

return np.log(4*np.pi) 

 

def _fitstart(self, data, args=None): 

# Initialize ML guesses using quartiles instead of moments. 

p25, p50, p75 = np.percentile(data, [25, 50, 75]) 

return p50, (p75 - p25)/2 

 

 

cauchy = cauchy_gen(name='cauchy') 

 

 

class chi_gen(rv_continuous): 

r"""A chi continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `chi` is: 

 

.. math:: 

 

f(x, df) = \frac{x^{df-1} \exp(-x^2/2)}{2^{df/2-1} \gamma(df/2)} 

 

for :math:`x > 0`. 

 

Special cases of `chi` are: 

 

- ``chi(1, loc, scale)`` is equivalent to `halfnorm` 

- ``chi(2, 0, scale)`` is equivalent to `rayleigh` 

- ``chi(3, 0, scale)`` is equivalent to `maxwell` 

 

`chi` takes ``df`` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

 

def _rvs(self, df): 

sz, rndm = self._size, self._random_state 

return np.sqrt(chi2.rvs(df, size=sz, random_state=rndm)) 

 

def _pdf(self, x, df): 

# x**(df-1) * exp(-x**2/2) 

# chi.pdf(x, df) = ------------------------- 

# 2**(df/2-1) * gamma(df/2) 

return np.exp(self._logpdf(x, df)) 

 

def _logpdf(self, x, df): 

l = np.log(2) - .5*np.log(2)*df - sc.gammaln(.5*df) 

return l + sc.xlogy(df - 1., x) - .5*x**2 

 

def _cdf(self, x, df): 

return sc.gammainc(.5*df, .5*x**2) 

 

def _ppf(self, q, df): 

return np.sqrt(2*sc.gammaincinv(.5*df, q)) 

 

def _stats(self, df): 

mu = np.sqrt(2)*sc.gamma(df/2.0+0.5)/sc.gamma(df/2.0) 

mu2 = df - mu*mu 

g1 = (2*mu**3.0 + mu*(1-2*df))/np.asarray(np.power(mu2, 1.5)) 

g2 = 2*df*(1.0-df)-6*mu**4 + 4*mu**2 * (2*df-1) 

g2 /= np.asarray(mu2**2.0) 

return mu, mu2, g1, g2 

 

 

chi = chi_gen(a=0.0, name='chi') 

 

 

## Chi-squared (gamma-distributed with loc=0 and scale=2 and shape=df/2) 

class chi2_gen(rv_continuous): 

r"""A chi-squared continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `chi2` is: 

 

.. math:: 

 

f(x, df) = \frac{1}{(2 \gamma(df/2)} (x/2)^{df/2-1} \exp(-x/2) 

 

`chi2` takes ``df`` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self, df): 

return self._random_state.chisquare(df, self._size) 

 

def _pdf(self, x, df): 

# chi2.pdf(x, df) = 1 / (2*gamma(df/2)) * (x/2)**(df/2-1) * exp(-x/2) 

return np.exp(self._logpdf(x, df)) 

 

def _logpdf(self, x, df): 

return sc.xlogy(df/2.-1, x) - x/2. - sc.gammaln(df/2.) - (np.log(2)*df)/2. 

 

def _cdf(self, x, df): 

return sc.chdtr(df, x) 

 

def _sf(self, x, df): 

return sc.chdtrc(df, x) 

 

def _isf(self, p, df): 

return sc.chdtri(df, p) 

 

def _ppf(self, p, df): 

return self._isf(1.0-p, df) 

 

def _stats(self, df): 

mu = df 

mu2 = 2*df 

g1 = 2*np.sqrt(2.0/df) 

g2 = 12.0/df 

return mu, mu2, g1, g2 

 

 

chi2 = chi2_gen(a=0.0, name='chi2') 

 

 

class cosine_gen(rv_continuous): 

r"""A cosine continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The cosine distribution is an approximation to the normal distribution. 

The probability density function for `cosine` is: 

 

.. math:: 

 

f(x) = \frac{1}{2\pi} (1+\cos(x)) 

 

for :math:`-\pi \le x \le \pi`. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x): 

# cosine.pdf(x) = 1/(2*pi) * (1+cos(x)) 

return 1.0/2/np.pi*(1+np.cos(x)) 

 

def _cdf(self, x): 

return 1.0/2/np.pi*(np.pi + x + np.sin(x)) 

 

def _stats(self): 

return 0.0, np.pi*np.pi/3.0-2.0, 0.0, -6.0*(np.pi**4-90)/(5.0*(np.pi*np.pi-6)**2) 

 

def _entropy(self): 

return np.log(4*np.pi)-1.0 

 

 

cosine = cosine_gen(a=-np.pi, b=np.pi, name='cosine') 

 

 

class dgamma_gen(rv_continuous): 

r"""A double gamma continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `dgamma` is: 

 

.. math:: 

 

f(x, a) = \frac{1}{2\gamma(a)} |x|^{a-1} \exp(-|x|) 

 

for :math:`a > 0`. 

 

`dgamma` takes :math:`a` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self, a): 

sz, rndm = self._size, self._random_state 

u = rndm.random_sample(size=sz) 

gm = gamma.rvs(a, size=sz, random_state=rndm) 

return gm * np.where(u >= 0.5, 1, -1) 

 

def _pdf(self, x, a): 

# dgamma.pdf(x, a) = 1 / (2*gamma(a)) * abs(x)**(a-1) * exp(-abs(x)) 

ax = abs(x) 

return 1.0/(2*sc.gamma(a))*ax**(a-1.0) * np.exp(-ax) 

 

def _logpdf(self, x, a): 

ax = abs(x) 

return sc.xlogy(a - 1.0, ax) - ax - np.log(2) - sc.gammaln(a) 

 

def _cdf(self, x, a): 

fac = 0.5*sc.gammainc(a, abs(x)) 

return np.where(x > 0, 0.5 + fac, 0.5 - fac) 

 

def _sf(self, x, a): 

fac = 0.5*sc.gammainc(a, abs(x)) 

return np.where(x > 0, 0.5-fac, 0.5+fac) 

 

def _ppf(self, q, a): 

fac = sc.gammainccinv(a, 1-abs(2*q-1)) 

return np.where(q > 0.5, fac, -fac) 

 

def _stats(self, a): 

mu2 = a*(a+1.0) 

return 0.0, mu2, 0.0, (a+2.0)*(a+3.0)/mu2-3.0 

 

 

dgamma = dgamma_gen(name='dgamma') 

 

 

class dweibull_gen(rv_continuous): 

r"""A double Weibull continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `dweibull` is: 

 

.. math:: 

 

f(x, c) = c / 2 |x|^{c-1} \exp(-|x|^c) 

 

`dweibull` takes :math:`d` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self, c): 

sz, rndm = self._size, self._random_state 

u = rndm.random_sample(size=sz) 

w = weibull_min.rvs(c, size=sz, random_state=rndm) 

return w * (np.where(u >= 0.5, 1, -1)) 

 

def _pdf(self, x, c): 

# dweibull.pdf(x, c) = c / 2 * abs(x)**(c-1) * exp(-abs(x)**c) 

ax = abs(x) 

Px = c / 2.0 * ax**(c-1.0) * np.exp(-ax**c) 

return Px 

 

def _logpdf(self, x, c): 

ax = abs(x) 

return np.log(c) - np.log(2.0) + sc.xlogy(c - 1.0, ax) - ax**c 

 

def _cdf(self, x, c): 

Cx1 = 0.5 * np.exp(-abs(x)**c) 

return np.where(x > 0, 1 - Cx1, Cx1) 

 

def _ppf(self, q, c): 

fac = 2. * np.where(q <= 0.5, q, 1. - q) 

fac = np.power(-np.log(fac), 1.0 / c) 

return np.where(q > 0.5, fac, -fac) 

 

def _munp(self, n, c): 

return (1 - (n % 2)) * sc.gamma(1.0 + 1.0 * n / c) 

 

# since we know that all odd moments are zeros, return them at once. 

# returning Nones from _stats makes the public stats call _munp 

# so overall we're saving one or two gamma function evaluations here. 

def _stats(self, c): 

return 0, None, 0, None 

 

 

dweibull = dweibull_gen(name='dweibull') 

 

 

## Exponential (gamma distributed with a=1.0, loc=loc and scale=scale) 

class expon_gen(rv_continuous): 

r"""An exponential continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `expon` is: 

 

.. math:: 

 

f(x) = \exp(-x) 

 

for :math:`x \ge 0`. 

 

%(after_notes)s 

 

A common parameterization for `expon` is in terms of the rate parameter 

``lambda``, such that ``pdf = lambda * exp(-lambda * x)``. This 

parameterization corresponds to using ``scale = 1 / lambda``. 

 

%(example)s 

 

""" 

def _rvs(self): 

return self._random_state.standard_exponential(self._size) 

 

def _pdf(self, x): 

# expon.pdf(x) = exp(-x) 

return np.exp(-x) 

 

def _logpdf(self, x): 

return -x 

 

def _cdf(self, x): 

return -sc.expm1(-x) 

 

def _ppf(self, q): 

return -sc.log1p(-q) 

 

def _sf(self, x): 

return np.exp(-x) 

 

def _logsf(self, x): 

return -x 

 

def _isf(self, q): 

return -np.log(q) 

 

def _stats(self): 

return 1.0, 1.0, 2.0, 6.0 

 

def _entropy(self): 

return 1.0 

 

@replace_notes_in_docstring(rv_continuous, notes="""\ 

This function uses explicit formulas for the maximum likelihood 

estimation of the exponential distribution parameters, so the 

`optimizer`, `loc` and `scale` keyword arguments are ignored.\n\n""") 

def fit(self, data, *args, **kwds): 

if len(args) > 0: 

raise TypeError("Too many arguments.") 

 

floc = kwds.pop('floc', None) 

fscale = kwds.pop('fscale', None) 

 

# Ignore the optimizer-related keyword arguments, if given. 

kwds.pop('loc', None) 

kwds.pop('scale', None) 

kwds.pop('optimizer', None) 

if kwds: 

raise TypeError("Unknown arguments: %s." % kwds) 

 

if floc is not None and fscale is not None: 

# This check is for consistency with `rv_continuous.fit`. 

raise ValueError("All parameters fixed. There is nothing to " 

"optimize.") 

 

data = np.asarray(data) 

data_min = data.min() 

if floc is None: 

# ML estimate of the location is the minimum of the data. 

loc = data_min 

else: 

loc = floc 

if data_min < loc: 

# There are values that are less than the specified loc. 

raise FitDataError("expon", lower=floc, upper=np.inf) 

 

if fscale is None: 

# ML estimate of the scale is the shifted mean. 

scale = data.mean() - loc 

else: 

scale = fscale 

 

# We expect the return values to be floating point, so ensure it 

# by explicitly converting to float. 

return float(loc), float(scale) 

 

 

expon = expon_gen(a=0.0, name='expon') 

 

 

## Exponentially Modified Normal (exponential distribution 

## convolved with a Normal). 

## This is called an exponentially modified gaussian on wikipedia 

class exponnorm_gen(rv_continuous): 

r"""An exponentially modified Normal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `exponnorm` is: 

 

.. math:: 

 

f(x, K) = \frac{1}{2K} \exp\left(\frac{1}{2 K^2}\right) \exp(-x / K) 

\text{erfc}\left(-\frac{x - 1/K}{\sqrt{2}}\right) 

 

where the shape parameter :math:`K > 0`. 

 

It can be thought of as the sum of a normally distributed random 

value with mean ``loc`` and sigma ``scale`` and an exponentially 

distributed random number with a pdf proportional to ``exp(-lambda * x)`` 

where ``lambda = (K * scale)**(-1)``. 

 

%(after_notes)s 

 

An alternative parameterization of this distribution (for example, in 

`Wikipedia <http://en.wikipedia.org/wiki/Exponentially_modified_Gaussian_distribution>`_) 

involves three parameters, :math:`\mu`, :math:`\lambda` and 

:math:`\sigma`. 

In the present parameterization this corresponds to having ``loc`` and 

``scale`` equal to :math:`\mu` and :math:`\sigma`, respectively, and 

shape parameter :math:`K = 1/(\sigma\lambda)`. 

 

.. versionadded:: 0.16.0 

 

%(example)s 

 

""" 

def _rvs(self, K): 

expval = self._random_state.standard_exponential(self._size) * K 

gval = self._random_state.standard_normal(self._size) 

return expval + gval 

 

def _pdf(self, x, K): 

# exponnorm.pdf(x, K) = 

# 1/(2*K) exp(1/(2 * K**2)) exp(-x / K) * erfc-(x - 1/K) / sqrt(2)) 

invK = 1.0 / K 

exparg = 0.5 * invK**2 - invK * x 

# Avoid overflows; setting np.exp(exparg) to the max float works 

# all right here 

expval = _lazywhere(exparg < _LOGXMAX, (exparg,), np.exp, _XMAX) 

return 0.5 * invK * expval * sc.erfc(-(x - invK) / np.sqrt(2)) 

 

def _logpdf(self, x, K): 

invK = 1.0 / K 

exparg = 0.5 * invK**2 - invK * x 

return exparg + np.log(0.5 * invK * sc.erfc(-(x - invK) / np.sqrt(2))) 

 

def _cdf(self, x, K): 

invK = 1.0 / K 

expval = invK * (0.5 * invK - x) 

return _norm_cdf(x) - np.exp(expval) * _norm_cdf(x - invK) 

 

def _sf(self, x, K): 

invK = 1.0 / K 

expval = invK * (0.5 * invK - x) 

return _norm_cdf(-x) + np.exp(expval) * _norm_cdf(x - invK) 

 

def _stats(self, K): 

K2 = K * K 

opK2 = 1.0 + K2 

skw = 2 * K**3 * opK2**(-1.5) 

krt = 6.0 * K2 * K2 * opK2**(-2) 

return K, opK2, skw, krt 

 

 

exponnorm = exponnorm_gen(name='exponnorm') 

 

 

class exponweib_gen(rv_continuous): 

r"""An exponentiated Weibull continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `exponweib` is: 

 

.. math:: 

 

f(x, a, c) = a c (1-\exp(-x^c))^{a-1} \exp(-x^c) x^{c-1} 

 

for :math:`x > 0`, :math:`a > 0`, :math:`c > 0`. 

 

`exponweib` takes :math:`a` and :math:`c` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, a, c): 

# exponweib.pdf(x, a, c) = 

# a * c * (1-exp(-x**c))**(a-1) * exp(-x**c)*x**(c-1) 

return np.exp(self._logpdf(x, a, c)) 

 

def _logpdf(self, x, a, c): 

negxc = -x**c 

exm1c = -sc.expm1(negxc) 

logp = (np.log(a) + np.log(c) + sc.xlogy(a - 1.0, exm1c) + 

negxc + sc.xlogy(c - 1.0, x)) 

return logp 

 

def _cdf(self, x, a, c): 

exm1c = -sc.expm1(-x**c) 

return exm1c**a 

 

def _ppf(self, q, a, c): 

return (-sc.log1p(-q**(1.0/a)))**np.asarray(1.0/c) 

 

 

exponweib = exponweib_gen(a=0.0, name='exponweib') 

 

 

class exponpow_gen(rv_continuous): 

r"""An exponential power continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `exponpow` is: 

 

.. math:: 

 

f(x, b) = b x^{b-1} \exp(1 + x^b - \exp(x^b)) 

 

for :math:`x \ge 0`, :math:`b > 0``. Note that this is a different 

distribution from the exponential power distribution that is also known 

under the names "generalized normal" or "generalized Gaussian". 

 

`exponpow` takes :math:`b` as a shape parameter. 

 

%(after_notes)s 

 

References 

---------- 

http://www.math.wm.edu/~leemis/chart/UDR/PDFs/Exponentialpower.pdf 

 

%(example)s 

 

""" 

def _pdf(self, x, b): 

# exponpow.pdf(x, b) = b * x**(b-1) * exp(1 + x**b - exp(x**b)) 

return np.exp(self._logpdf(x, b)) 

 

def _logpdf(self, x, b): 

xb = x**b 

f = 1 + np.log(b) + sc.xlogy(b - 1.0, x) + xb - np.exp(xb) 

return f 

 

def _cdf(self, x, b): 

return -sc.expm1(-sc.expm1(x**b)) 

 

def _sf(self, x, b): 

return np.exp(-sc.expm1(x**b)) 

 

def _isf(self, x, b): 

return (sc.log1p(-np.log(x)))**(1./b) 

 

def _ppf(self, q, b): 

return pow(sc.log1p(-sc.log1p(-q)), 1.0/b) 

 

 

exponpow = exponpow_gen(a=0.0, name='exponpow') 

 

 

class fatiguelife_gen(rv_continuous): 

r"""A fatigue-life (Birnbaum-Saunders) continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `fatiguelife` is: 

 

.. math:: 

 

f(x, c) = \frac{x+1}{ 2c\sqrt{2\pi x^3} \exp(-\frac{(x-1)^2}{2x c^2}} 

 

for :math:`x > 0`. 

 

`fatiguelife` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

References 

---------- 

.. [1] "Birnbaum-Saunders distribution", 

http://en.wikipedia.org/wiki/Birnbaum-Saunders_distribution 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _rvs(self, c): 

z = self._random_state.standard_normal(self._size) 

x = 0.5*c*z 

x2 = x*x 

t = 1.0 + 2*x2 + 2*x*np.sqrt(1 + x2) 

return t 

 

def _pdf(self, x, c): 

# fatiguelife.pdf(x, c) = 

# (x+1) / (2*c*sqrt(2*pi*x**3)) * exp(-(x-1)**2/(2*x*c**2)) 

return np.exp(self._logpdf(x, c)) 

 

def _logpdf(self, x, c): 

return (np.log(x+1) - (x-1)**2 / (2.0*x*c**2) - np.log(2*c) - 

0.5*(np.log(2*np.pi) + 3*np.log(x))) 

 

def _cdf(self, x, c): 

return _norm_cdf(1.0 / c * (np.sqrt(x) - 1.0/np.sqrt(x))) 

 

def _ppf(self, q, c): 

tmp = c*sc.ndtri(q) 

return 0.25 * (tmp + np.sqrt(tmp**2 + 4))**2 

 

def _stats(self, c): 

# NB: the formula for kurtosis in wikipedia seems to have an error: 

# it's 40, not 41. At least it disagrees with the one from Wolfram 

# Alpha. And the latter one, below, passes the tests, while the wiki 

# one doesn't So far I didn't have the guts to actually check the 

# coefficients from the expressions for the raw moments. 

c2 = c*c 

mu = c2 / 2.0 + 1.0 

den = 5.0 * c2 + 4.0 

mu2 = c2*den / 4.0 

g1 = 4 * c * (11*c2 + 6.0) / np.power(den, 1.5) 

g2 = 6 * c2 * (93*c2 + 40.0) / den**2.0 

return mu, mu2, g1, g2 

 

 

fatiguelife = fatiguelife_gen(a=0.0, name='fatiguelife') 

 

 

class foldcauchy_gen(rv_continuous): 

r"""A folded Cauchy continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `foldcauchy` is: 

 

.. math:: 

 

f(x, c) = \frac{1}{\pi (1+(x-c)^2)} + \frac{1}{\pi (1+(x+c)^2)} 

 

for :math:`x \ge 0``. 

 

`foldcauchy` takes :math:`c` as a shape parameter. 

 

%(example)s 

 

""" 

def _rvs(self, c): 

return abs(cauchy.rvs(loc=c, size=self._size, 

random_state=self._random_state)) 

 

def _pdf(self, x, c): 

# foldcauchy.pdf(x, c) = 1/(pi*(1+(x-c)**2)) + 1/(pi*(1+(x+c)**2)) 

return 1.0/np.pi*(1.0/(1+(x-c)**2) + 1.0/(1+(x+c)**2)) 

 

def _cdf(self, x, c): 

return 1.0/np.pi*(np.arctan(x-c) + np.arctan(x+c)) 

 

def _stats(self, c): 

return np.inf, np.inf, np.nan, np.nan 

 

 

foldcauchy = foldcauchy_gen(a=0.0, name='foldcauchy') 

 

 

class f_gen(rv_continuous): 

r"""An F continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `f` is: 

 

.. math:: 

 

f(x, df_1, df_2) = \frac{df_2^{df_2/2} df_1^{df_1/2} x^{df_1 / 2-1}} 

{(df_2+df_1 x)^{(df_1+df_2)/2} 

B(df_1/2, df_2/2)} 

 

for :math:`x > 0`. 

 

`f` takes ``dfn`` and ``dfd`` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self, dfn, dfd): 

return self._random_state.f(dfn, dfd, self._size) 

 

def _pdf(self, x, dfn, dfd): 

# df2**(df2/2) * df1**(df1/2) * x**(df1/2-1) 

# F.pdf(x, df1, df2) = -------------------------------------------- 

# (df2+df1*x)**((df1+df2)/2) * B(df1/2, df2/2) 

return np.exp(self._logpdf(x, dfn, dfd)) 

 

def _logpdf(self, x, dfn, dfd): 

n = 1.0 * dfn 

m = 1.0 * dfd 

lPx = m/2 * np.log(m) + n/2 * np.log(n) + (n/2 - 1) * np.log(x) 

lPx -= ((n+m)/2) * np.log(m + n*x) + sc.betaln(n/2, m/2) 

return lPx 

 

def _cdf(self, x, dfn, dfd): 

return sc.fdtr(dfn, dfd, x) 

 

def _sf(self, x, dfn, dfd): 

return sc.fdtrc(dfn, dfd, x) 

 

def _ppf(self, q, dfn, dfd): 

return sc.fdtri(dfn, dfd, q) 

 

def _stats(self, dfn, dfd): 

v1, v2 = 1. * dfn, 1. * dfd 

v2_2, v2_4, v2_6, v2_8 = v2 - 2., v2 - 4., v2 - 6., v2 - 8. 

 

mu = _lazywhere( 

v2 > 2, (v2, v2_2), 

lambda v2, v2_2: v2 / v2_2, 

np.inf) 

 

mu2 = _lazywhere( 

v2 > 4, (v1, v2, v2_2, v2_4), 

lambda v1, v2, v2_2, v2_4: 

2 * v2 * v2 * (v1 + v2_2) / (v1 * v2_2**2 * v2_4), 

np.inf) 

 

g1 = _lazywhere( 

v2 > 6, (v1, v2_2, v2_4, v2_6), 

lambda v1, v2_2, v2_4, v2_6: 

(2 * v1 + v2_2) / v2_6 * np.sqrt(v2_4 / (v1 * (v1 + v2_2))), 

np.nan) 

g1 *= np.sqrt(8.) 

 

g2 = _lazywhere( 

v2 > 8, (g1, v2_6, v2_8), 

lambda g1, v2_6, v2_8: (8 + g1 * g1 * v2_6) / v2_8, 

np.nan) 

g2 *= 3. / 2. 

 

return mu, mu2, g1, g2 

 

 

f = f_gen(a=0.0, name='f') 

 

 

## Folded Normal 

## abs(Z) where (Z is normal with mu=L and std=S so that c=abs(L)/S) 

## 

## note: regress docs have scale parameter correct, but first parameter 

## he gives is a shape parameter A = c * scale 

 

## Half-normal is folded normal with shape-parameter c=0. 

 

class foldnorm_gen(rv_continuous): 

r"""A folded normal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `foldnorm` is: 

 

.. math:: 

 

f(x, c) = \sqrt{2/\pi} cosh(c x) \exp(-\frac{x^2+c^2}{2}) 

 

for :math:`c \ge 0`. 

 

`foldnorm` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, c): 

return c >= 0 

 

def _rvs(self, c): 

return abs(self._random_state.standard_normal(self._size) + c) 

 

def _pdf(self, x, c): 

# foldnormal.pdf(x, c) = sqrt(2/pi) * cosh(c*x) * exp(-(x**2+c**2)/2) 

return _norm_pdf(x + c) + _norm_pdf(x-c) 

 

def _cdf(self, x, c): 

return _norm_cdf(x-c) + _norm_cdf(x+c) - 1.0 

 

def _stats(self, c): 

# Regina C. Elandt, Technometrics 3, 551 (1961) 

# http://www.jstor.org/stable/1266561 

# 

c2 = c*c 

expfac = np.exp(-0.5*c2) / np.sqrt(2.*np.pi) 

 

mu = 2.*expfac + c * sc.erf(c/np.sqrt(2)) 

mu2 = c2 + 1 - mu*mu 

 

g1 = 2. * (mu*mu*mu - c2*mu - expfac) 

g1 /= np.power(mu2, 1.5) 

 

g2 = c2 * (c2 + 6.) + 3 + 8.*expfac*mu 

g2 += (2. * (c2 - 3.) - 3. * mu**2) * mu**2 

g2 = g2 / mu2**2.0 - 3. 

 

return mu, mu2, g1, g2 

 

 

foldnorm = foldnorm_gen(a=0.0, name='foldnorm') 

 

 

class weibull_min_gen(rv_continuous): 

r"""Weibull minimum continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

weibull_max 

 

Notes 

----- 

The probability density function for `weibull_min` is: 

 

.. math:: 

 

f(x, c) = c x^{c-1} \exp(-x^c) 

 

for :math:`x > 0`, :math:`c > 0`. 

 

`weibull_min` takes ``c`` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

 

def _pdf(self, x, c): 

# frechet_r.pdf(x, c) = c * x**(c-1) * exp(-x**c) 

return c*pow(x, c-1)*np.exp(-pow(x, c)) 

 

def _logpdf(self, x, c): 

return np.log(c) + sc.xlogy(c - 1, x) - pow(x, c) 

 

def _cdf(self, x, c): 

return -sc.expm1(-pow(x, c)) 

 

def _sf(self, x, c): 

return np.exp(-pow(x, c)) 

 

def _logsf(self, x, c): 

return -pow(x, c) 

 

def _ppf(self, q, c): 

return pow(-sc.log1p(-q), 1.0/c) 

 

def _munp(self, n, c): 

return sc.gamma(1.0+n*1.0/c) 

 

def _entropy(self, c): 

return -_EULER / c - np.log(c) + _EULER + 1 

 

 

weibull_min = weibull_min_gen(a=0.0, name='weibull_min') 

 

 

class weibull_max_gen(rv_continuous): 

r"""Weibull maximum continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

weibull_min 

 

Notes 

----- 

The probability density function for `weibull_max` is: 

 

.. math:: 

 

f(x, c) = c (-x)^{c-1} \exp(-(-x)^c) 

 

for :math:`x < 0`, :math:`c > 0`. 

 

`weibull_max` takes ``c`` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, c): 

# frechet_l.pdf(x, c) = c * (-x)**(c-1) * exp(-(-x)**c) 

return c*pow(-x, c-1)*np.exp(-pow(-x, c)) 

 

def _logpdf(self, x, c): 

return np.log(c) + sc.xlogy(c-1, -x) - pow(-x, c) 

 

def _cdf(self, x, c): 

return np.exp(-pow(-x, c)) 

 

def _logcdf(self, x, c): 

return -pow(-x, c) 

 

def _sf(self, x, c): 

return -sc.expm1(-pow(-x, c)) 

 

def _ppf(self, q, c): 

return -pow(-np.log(q), 1.0/c) 

 

def _munp(self, n, c): 

val = sc.gamma(1.0+n*1.0/c) 

if int(n) % 2: 

sgn = -1 

else: 

sgn = 1 

return sgn * val 

 

def _entropy(self, c): 

return -_EULER / c - np.log(c) + _EULER + 1 

 

 

weibull_max = weibull_max_gen(b=0.0, name='weibull_max') 

 

# Public methods to be deprecated in frechet_r and frechet_l: 

# ['__call__', 'cdf', 'entropy', 'expect', 'fit', 'fit_loc_scale', 'freeze', 

# 'interval', 'isf', 'logcdf', 'logpdf', 'logsf', 'mean', 'median', 'moment', 

# 'nnlf', 'pdf', 'ppf', 'rvs', 'sf', 'stats', 'std', 'var'] 

 

_frechet_r_deprec_msg = """\ 

The distribution `frechet_r` is a synonym for `weibull_min`; this historical 

usage is deprecated because of possible confusion with the (quite different) 

Frechet distribution. To preserve the existing behavior of the program, use 

`scipy.stats.weibull_min`. For the Frechet distribution (i.e. the Type II 

extreme value distribution), use `scipy.stats.invweibull`.""" 

 

class frechet_r_gen(weibull_min_gen): 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def __call__(self, *args, **kwargs): 

return weibull_min_gen.__call__(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def cdf(self, *args, **kwargs): 

return weibull_min_gen.cdf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def entropy(self, *args, **kwargs): 

return weibull_min_gen.entropy(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def expect(self, *args, **kwargs): 

return weibull_min_gen.expect(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def fit(self, *args, **kwargs): 

return weibull_min_gen.fit(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def fit_loc_scale(self, *args, **kwargs): 

return weibull_min_gen.fit_loc_scale(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def freeze(self, *args, **kwargs): 

return weibull_min_gen.freeze(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def interval(self, *args, **kwargs): 

return weibull_min_gen.interval(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def isf(self, *args, **kwargs): 

return weibull_min_gen.isf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def logcdf(self, *args, **kwargs): 

return weibull_min_gen.logcdf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def logpdf(self, *args, **kwargs): 

return weibull_min_gen.logpdf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def logsf(self, *args, **kwargs): 

return weibull_min_gen.logsf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def mean(self, *args, **kwargs): 

return weibull_min_gen.mean(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def median(self, *args, **kwargs): 

return weibull_min_gen.median(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def moment(self, *args, **kwargs): 

return weibull_min_gen.moment(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def nnlf(self, *args, **kwargs): 

return weibull_min_gen.nnlf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def pdf(self, *args, **kwargs): 

return weibull_min_gen.pdf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def ppf(self, *args, **kwargs): 

return weibull_min_gen.ppf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def rvs(self, *args, **kwargs): 

return weibull_min_gen.rvs(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def sf(self, *args, **kwargs): 

return weibull_min_gen.sf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def stats(self, *args, **kwargs): 

return weibull_min_gen.stats(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def std(self, *args, **kwargs): 

return weibull_min_gen.std(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_r', message=_frechet_r_deprec_msg) 

def var(self, *args, **kwargs): 

return weibull_min_gen.var(self, *args, **kwargs) 

 

 

frechet_r = frechet_r_gen(a=0.0, name='frechet_r') 

 

 

_frechet_l_deprec_msg = """\ 

The distribution `frechet_l` is a synonym for `weibull_max`; this historical 

usage is deprecated because of possible confusion with the (quite different) 

Frechet distribution. To preserve the existing behavior of the program, use 

`scipy.stats.weibull_max`. For the Frechet distribution (i.e. the Type II 

extreme value distribution), use `scipy.stats.invweibull`.""" 

 

class frechet_l_gen(weibull_max_gen): 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def __call__(self, *args, **kwargs): 

return weibull_max_gen.__call__(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def cdf(self, *args, **kwargs): 

return weibull_max_gen.cdf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def entropy(self, *args, **kwargs): 

return weibull_max_gen.entropy(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def expect(self, *args, **kwargs): 

return weibull_max_gen.expect(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def fit(self, *args, **kwargs): 

return weibull_max_gen.fit(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def fit_loc_scale(self, *args, **kwargs): 

return weibull_max_gen.fit_loc_scale(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def freeze(self, *args, **kwargs): 

return weibull_max_gen.freeze(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def interval(self, *args, **kwargs): 

return weibull_max_gen.interval(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def isf(self, *args, **kwargs): 

return weibull_max_gen.isf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def logcdf(self, *args, **kwargs): 

return weibull_max_gen.logcdf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def logpdf(self, *args, **kwargs): 

return weibull_max_gen.logpdf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def logsf(self, *args, **kwargs): 

return weibull_max_gen.logsf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def mean(self, *args, **kwargs): 

return weibull_max_gen.mean(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def median(self, *args, **kwargs): 

return weibull_max_gen.median(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def moment(self, *args, **kwargs): 

return weibull_max_gen.moment(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def nnlf(self, *args, **kwargs): 

return weibull_max_gen.nnlf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def pdf(self, *args, **kwargs): 

return weibull_max_gen.pdf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def ppf(self, *args, **kwargs): 

return weibull_max_gen.ppf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def rvs(self, *args, **kwargs): 

return weibull_max_gen.rvs(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def sf(self, *args, **kwargs): 

return weibull_max_gen.sf(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def stats(self, *args, **kwargs): 

return weibull_max_gen.stats(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def std(self, *args, **kwargs): 

return weibull_max_gen.std(self, *args, **kwargs) 

 

@np.deprecate(old_name='frechet_l', message=_frechet_l_deprec_msg) 

def var(self, *args, **kwargs): 

return weibull_max_gen.var(self, *args, **kwargs) 

 

 

frechet_l = frechet_l_gen(b=0.0, name='frechet_l') 

 

 

class genlogistic_gen(rv_continuous): 

r"""A generalized logistic continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `genlogistic` is: 

 

.. math:: 

 

f(x, c) = c \frac{\exp(-x)} 

{(1 + \exp(-x))^{c+1}} 

 

for :math:`x > 0`, :math:`c > 0`. 

 

`genlogistic` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, c): 

# genlogistic.pdf(x, c) = c * exp(-x) / (1 + exp(-x))**(c+1) 

return np.exp(self._logpdf(x, c)) 

 

def _logpdf(self, x, c): 

return np.log(c) - x - (c+1.0)*sc.log1p(np.exp(-x)) 

 

def _cdf(self, x, c): 

Cx = (1+np.exp(-x))**(-c) 

return Cx 

 

def _ppf(self, q, c): 

vals = -np.log(pow(q, -1.0/c)-1) 

return vals 

 

def _stats(self, c): 

mu = _EULER + sc.psi(c) 

mu2 = np.pi*np.pi/6.0 + sc.zeta(2, c) 

g1 = -2*sc.zeta(3, c) + 2*_ZETA3 

g1 /= np.power(mu2, 1.5) 

g2 = np.pi**4/15.0 + 6*sc.zeta(4, c) 

g2 /= mu2**2.0 

return mu, mu2, g1, g2 

 

 

genlogistic = genlogistic_gen(name='genlogistic') 

 

 

class genpareto_gen(rv_continuous): 

r"""A generalized Pareto continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `genpareto` is: 

 

.. math:: 

 

f(x, c) = (1 + c x)^{-1 - 1/c} 

 

defined for :math:`x \ge 0` if :math:`c \ge 0`, and for 

:math:`0 \le x \le -1/c` if :math:`c < 0`. 

 

`genpareto` takes :math:`c` as a shape parameter. 

 

For ``c == 0``, `genpareto` reduces to the exponential 

distribution, `expon`: 

 

.. math:: 

 

f(x, c=0) = \exp(-x) 

 

For ``c == -1``, `genpareto` is uniform on ``[0, 1]``: 

 

.. math:: 

 

f(x, c=-1) = x 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, c): 

c = np.asarray(c) 

self.b = _lazywhere(c < 0, (c,), 

lambda c: -1. / c, 

np.inf) 

return True 

 

def _pdf(self, x, c): 

# genpareto.pdf(x, c) = (1 + c * x)**(-1 - 1/c) 

return np.exp(self._logpdf(x, c)) 

 

def _logpdf(self, x, c): 

return _lazywhere((x == x) & (c != 0), (x, c), 

lambda x, c: -sc.xlog1py(c + 1., c*x) / c, 

-x) 

 

def _cdf(self, x, c): 

return -sc.inv_boxcox1p(-x, -c) 

 

def _sf(self, x, c): 

return sc.inv_boxcox(-x, -c) 

 

def _logsf(self, x, c): 

return _lazywhere((x == x) & (c != 0), (x, c), 

lambda x, c: -sc.log1p(c*x) / c, 

-x) 

 

def _ppf(self, q, c): 

return -sc.boxcox1p(-q, -c) 

 

def _isf(self, q, c): 

return -sc.boxcox(q, -c) 

 

def _munp(self, n, c): 

def __munp(n, c): 

val = 0.0 

k = np.arange(0, n + 1) 

for ki, cnk in zip(k, sc.comb(n, k)): 

val = val + cnk * (-1) ** ki / (1.0 - c * ki) 

return np.where(c * n < 1, val * (-1.0 / c) ** n, np.inf) 

return _lazywhere(c != 0, (c,), 

lambda c: __munp(n, c), 

sc.gamma(n + 1)) 

 

def _entropy(self, c): 

return 1. + c 

 

 

genpareto = genpareto_gen(a=0.0, name='genpareto') 

 

 

class genexpon_gen(rv_continuous): 

r"""A generalized exponential continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `genexpon` is: 

 

.. math:: 

 

f(x, a, b, c) = (a + b (1 - \exp(-c x))) 

\exp(-a x - b x + \frac{b}{c} (1-\exp(-c x))) 

 

for :math:`x \ge 0`, :math:`a, b, c > 0`. 

 

`genexpon` takes :math:`a`, :math:`b` and :math:`c` as shape parameters. 

 

%(after_notes)s 

 

References 

---------- 

H.K. Ryu, "An Extension of Marshall and Olkin's Bivariate Exponential 

Distribution", Journal of the American Statistical Association, 1993. 

 

N. Balakrishnan, "The Exponential Distribution: Theory, Methods and 

Applications", Asit P. Basu. 

 

%(example)s 

 

""" 

def _pdf(self, x, a, b, c): 

# genexpon.pdf(x, a, b, c) = (a + b * (1 - exp(-c*x))) * \ 

# exp(-a*x - b*x + b/c * (1-exp(-c*x))) 

return (a + b*(-sc.expm1(-c*x)))*np.exp((-a-b)*x + 

b*(-sc.expm1(-c*x))/c) 

 

def _cdf(self, x, a, b, c): 

return -sc.expm1((-a-b)*x + b*(-sc.expm1(-c*x))/c) 

 

def _logpdf(self, x, a, b, c): 

return np.log(a+b*(-sc.expm1(-c*x))) + (-a-b)*x+b*(-sc.expm1(-c*x))/c 

 

 

genexpon = genexpon_gen(a=0.0, name='genexpon') 

 

 

class genextreme_gen(rv_continuous): 

r"""A generalized extreme value continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

gumbel_r 

 

Notes 

----- 

For :math:`c=0`, `genextreme` is equal to `gumbel_r`. 

The probability density function for `genextreme` is: 

 

.. math:: 

 

f(x, c) = \begin{cases} 

\exp(-\exp(-x)) \exp(-x) &\text{for } c = 0\\ 

\exp(-(1-c x)^{1/c}) (1-c x)^{1/c-1} &\text{for } 

x \le 1/c, c > 0 

\end{cases} 

 

 

Note that several sources and software packages use the opposite 

convention for the sign of the shape parameter :math:`c`. 

 

`genextreme` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, c): 

self.b = np.where(c > 0, 1.0 / np.maximum(c, _XMIN), np.inf) 

self.a = np.where(c < 0, 1.0 / np.minimum(c, -_XMIN), -np.inf) 

return np.where(abs(c) == np.inf, 0, 1) 

 

def _loglogcdf(self, x, c): 

return _lazywhere((x == x) & (c != 0), (x, c), 

lambda x, c: sc.log1p(-c*x)/c, -x) 

 

def _pdf(self, x, c): 

# genextreme.pdf(x, c) = 

# exp(-exp(-x))*exp(-x), for c==0 

# exp(-(1-c*x)**(1/c))*(1-c*x)**(1/c-1), for x \le 1/c, c > 0 

return np.exp(self._logpdf(x, c)) 

 

def _logpdf(self, x, c): 

cx = _lazywhere((x == x) & (c != 0), (x, c), lambda x, c: c*x, 0.0) 

logex2 = sc.log1p(-cx) 

logpex2 = self._loglogcdf(x, c) 

pex2 = np.exp(logpex2) 

# Handle special cases 

np.putmask(logpex2, (c == 0) & (x == -np.inf), 0.0) 

logpdf = np.where((cx == 1) | (cx == -np.inf), 

-np.inf, 

-pex2+logpex2-logex2) 

np.putmask(logpdf, (c == 1) & (x == 1), 0.0) 

return logpdf 

 

def _logcdf(self, x, c): 

return -np.exp(self._loglogcdf(x, c)) 

 

def _cdf(self, x, c): 

return np.exp(self._logcdf(x, c)) 

 

def _sf(self, x, c): 

return -sc.expm1(self._logcdf(x, c)) 

 

def _ppf(self, q, c): 

x = -np.log(-np.log(q)) 

return _lazywhere((x == x) & (c != 0), (x, c), 

lambda x, c: -sc.expm1(-c * x) / c, x) 

 

def _isf(self, q, c): 

x = -np.log(-sc.log1p(-q)) 

return _lazywhere((x == x) & (c != 0), (x, c), 

lambda x, c: -sc.expm1(-c * x) / c, x) 

 

def _stats(self, c): 

g = lambda n: sc.gamma(n*c + 1) 

g1 = g(1) 

g2 = g(2) 

g3 = g(3) 

g4 = g(4) 

g2mg12 = np.where(abs(c) < 1e-7, (c*np.pi)**2.0/6.0, g2-g1**2.0) 

gam2k = np.where(abs(c) < 1e-7, np.pi**2.0/6.0, 

sc.expm1(sc.gammaln(2.0*c+1.0)-2*sc.gammaln(c + 1.0))/c**2.0) 

eps = 1e-14 

gamk = np.where(abs(c) < eps, -_EULER, sc.expm1(sc.gammaln(c + 1))/c) 

 

m = np.where(c < -1.0, np.nan, -gamk) 

v = np.where(c < -0.5, np.nan, g1**2.0*gam2k) 

 

# skewness 

sk1 = _lazywhere(c >= -1./3, 

(c, g1, g2, g3, g2mg12), 

lambda c, g1, g2, g3, g2gm12: 

np.sign(c)*(-g3 + (g2 + 2*g2mg12)*g1)/g2mg12**1.5, 

fillvalue=np.nan) 

sk = np.where(abs(c) <= eps**0.29, 12*np.sqrt(6)*_ZETA3/np.pi**3, sk1) 

 

# kurtosis 

ku1 = _lazywhere(c >= -1./4, 

(g1, g2, g3, g4, g2mg12), 

lambda g1, g2, g3, g4, g2mg12: 

(g4 + (-4*g3 + 3*(g2 + g2mg12)*g1)*g1)/g2mg12**2, 

fillvalue=np.nan) 

ku = np.where(abs(c) <= (eps)**0.23, 12.0/5.0, ku1-3.0) 

return m, v, sk, ku 

 

def _fitstart(self, data): 

# This is better than the default shape of (1,). 

g = _skew(data) 

if g < 0: 

a = 0.5 

else: 

a = -0.5 

return super(genextreme_gen, self)._fitstart(data, args=(a,)) 

 

def _munp(self, n, c): 

k = np.arange(0, n+1) 

vals = 1.0/c**n * np.sum( 

sc.comb(n, k) * (-1)**k * sc.gamma(c*k + 1), 

axis=0) 

return np.where(c*n > -1, vals, np.inf) 

 

def _entropy(self, c): 

return _EULER*(1 - c) + 1 

 

 

genextreme = genextreme_gen(name='genextreme') 

 

 

def _digammainv(y): 

# Inverse of the digamma function (real positive arguments only). 

# This function is used in the `fit` method of `gamma_gen`. 

# The function uses either optimize.fsolve or optimize.newton 

# to solve `sc.digamma(x) - y = 0`. There is probably room for 

# improvement, but currently it works over a wide range of y: 

# >>> y = 64*np.random.randn(1000000) 

# >>> y.min(), y.max() 

# (-311.43592651416662, 351.77388222276869) 

# x = [_digammainv(t) for t in y] 

# np.abs(sc.digamma(x) - y).max() 

# 1.1368683772161603e-13 

# 

_em = 0.5772156649015328606065120 

func = lambda x: sc.digamma(x) - y 

if y > -0.125: 

x0 = np.exp(y) + 0.5 

if y < 10: 

# Some experimentation shows that newton reliably converges 

# must faster than fsolve in this y range. For larger y, 

# newton sometimes fails to converge. 

value = optimize.newton(func, x0, tol=1e-10) 

return value 

elif y > -3: 

x0 = np.exp(y/2.332) + 0.08661 

else: 

x0 = 1.0 / (-y - _em) 

 

value, info, ier, mesg = optimize.fsolve(func, x0, xtol=1e-11, 

full_output=True) 

if ier != 1: 

raise RuntimeError("_digammainv: fsolve failed, y = %r" % y) 

 

return value[0] 

 

 

## Gamma (Use MATLAB and MATHEMATICA (b=theta=scale, a=alpha=shape) definition) 

 

## gamma(a, loc, scale) with a an integer is the Erlang distribution 

## gamma(1, loc, scale) is the Exponential distribution 

## gamma(df/2, 0, 2) is the chi2 distribution with df degrees of freedom. 

 

class gamma_gen(rv_continuous): 

r"""A gamma continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

erlang, expon 

 

Notes 

----- 

The probability density function for `gamma` is: 

 

.. math:: 

 

f(x, a) = \frac{x^{a-1} \exp(-x)}{\Gamma(a)} 

 

for :math:`x \ge 0`, :math:`a > 0`. Here :math:`\Gamma(a)` refers to the 

gamma function. 

 

`gamma` has a shape parameter `a` which needs to be set explicitly. 

 

When :math:`a` is an integer, `gamma` reduces to the Erlang 

distribution, and when :math:`a=1` to the exponential distribution. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self, a): 

return self._random_state.standard_gamma(a, self._size) 

 

def _pdf(self, x, a): 

# gamma.pdf(x, a) = x**(a-1) * exp(-x) / gamma(a) 

return np.exp(self._logpdf(x, a)) 

 

def _logpdf(self, x, a): 

return sc.xlogy(a-1.0, x) - x - sc.gammaln(a) 

 

def _cdf(self, x, a): 

return sc.gammainc(a, x) 

 

def _sf(self, x, a): 

return sc.gammaincc(a, x) 

 

def _ppf(self, q, a): 

return sc.gammaincinv(a, q) 

 

def _stats(self, a): 

return a, a, 2.0/np.sqrt(a), 6.0/a 

 

def _entropy(self, a): 

return sc.psi(a)*(1-a) + a + sc.gammaln(a) 

 

def _fitstart(self, data): 

# The skewness of the gamma distribution is `4 / np.sqrt(a)`. 

# We invert that to estimate the shape `a` using the skewness 

# of the data. The formula is regularized with 1e-8 in the 

# denominator to allow for degenerate data where the skewness 

# is close to 0. 

a = 4 / (1e-8 + _skew(data)**2) 

return super(gamma_gen, self)._fitstart(data, args=(a,)) 

 

@extend_notes_in_docstring(rv_continuous, notes="""\ 

When the location is fixed by using the argument `floc`, this 

function uses explicit formulas or solves a simpler numerical 

problem than the full ML optimization problem. So in that case, 

the `optimizer`, `loc` and `scale` arguments are ignored.\n\n""") 

def fit(self, data, *args, **kwds): 

f0 = (kwds.get('f0', None) or kwds.get('fa', None) or 

kwds.get('fix_a', None)) 

floc = kwds.get('floc', None) 

fscale = kwds.get('fscale', None) 

 

if floc is None: 

# loc is not fixed. Use the default fit method. 

return super(gamma_gen, self).fit(data, *args, **kwds) 

 

# Special case: loc is fixed. 

 

if f0 is not None and fscale is not None: 

# This check is for consistency with `rv_continuous.fit`. 

# Without this check, this function would just return the 

# parameters that were given. 

raise ValueError("All parameters fixed. There is nothing to " 

"optimize.") 

 

# Fixed location is handled by shifting the data. 

data = np.asarray(data) 

if np.any(data <= floc): 

raise FitDataError("gamma", lower=floc, upper=np.inf) 

if floc != 0: 

# Don't do the subtraction in-place, because `data` might be a 

# view of the input array. 

data = data - floc 

xbar = data.mean() 

 

# Three cases to handle: 

# * shape and scale both free 

# * shape fixed, scale free 

# * shape free, scale fixed 

 

if fscale is None: 

# scale is free 

if f0 is not None: 

# shape is fixed 

a = f0 

else: 

# shape and scale are both free. 

# The MLE for the shape parameter `a` is the solution to: 

# np.log(a) - sc.digamma(a) - np.log(xbar) + 

# np.log(data.mean) = 0 

s = np.log(xbar) - np.log(data).mean() 

func = lambda a: np.log(a) - sc.digamma(a) - s 

aest = (3-s + np.sqrt((s-3)**2 + 24*s)) / (12*s) 

xa = aest*(1-0.4) 

xb = aest*(1+0.4) 

a = optimize.brentq(func, xa, xb, disp=0) 

 

# The MLE for the scale parameter is just the data mean 

# divided by the shape parameter. 

scale = xbar / a 

else: 

# scale is fixed, shape is free 

# The MLE for the shape parameter `a` is the solution to: 

# sc.digamma(a) - np.log(data).mean() + np.log(fscale) = 0 

c = np.log(data).mean() - np.log(fscale) 

a = _digammainv(c) 

scale = fscale 

 

return a, floc, scale 

 

 

gamma = gamma_gen(a=0.0, name='gamma') 

 

 

class erlang_gen(gamma_gen): 

"""An Erlang continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

gamma 

 

Notes 

----- 

The Erlang distribution is a special case of the Gamma distribution, with 

the shape parameter `a` an integer. Note that this restriction is not 

enforced by `erlang`. It will, however, generate a warning the first time 

a non-integer value is used for the shape parameter. 

 

Refer to `gamma` for examples. 

 

""" 

 

def _argcheck(self, a): 

allint = np.all(np.floor(a) == a) 

allpos = np.all(a > 0) 

if not allint: 

# An Erlang distribution shouldn't really have a non-integer 

# shape parameter, so warn the user. 

warnings.warn( 

'The shape parameter of the erlang distribution ' 

'has been given a non-integer value %r.' % (a,), 

RuntimeWarning) 

return allpos 

 

def _fitstart(self, data): 

# Override gamma_gen_fitstart so that an integer initial value is 

# used. (Also regularize the division, to avoid issues when 

# _skew(data) is 0 or close to 0.) 

a = int(4.0 / (1e-8 + _skew(data)**2)) 

return super(gamma_gen, self)._fitstart(data, args=(a,)) 

 

# Trivial override of the fit method, so we can monkey-patch its 

# docstring. 

def fit(self, data, *args, **kwds): 

return super(erlang_gen, self).fit(data, *args, **kwds) 

 

if fit.__doc__ is not None: 

fit.__doc__ = (rv_continuous.fit.__doc__ + 

""" 

Notes 

----- 

The Erlang distribution is generally defined to have integer values 

for the shape parameter. This is not enforced by the `erlang` class. 

When fitting the distribution, it will generally return a non-integer 

value for the shape parameter. By using the keyword argument 

`f0=<integer>`, the fit method can be constrained to fit the data to 

a specific integer shape parameter. 

""") 

 

 

erlang = erlang_gen(a=0.0, name='erlang') 

 

 

class gengamma_gen(rv_continuous): 

r"""A generalized gamma continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `gengamma` is: 

 

.. math:: 

 

f(x, a, c) = \frac{|c| x^{c a-1} \exp(-x^c)}{\gamma(a)} 

 

for :math:`x \ge 0`, :math:`a > 0`, and :math:`c \ne 0`. 

 

`gengamma` takes :math:`a` and :math:`c` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, a, c): 

return (a > 0) & (c != 0) 

 

def _pdf(self, x, a, c): 

# gengamma.pdf(x, a, c) = abs(c) * x**(c*a-1) * exp(-x**c) / gamma(a) 

return np.exp(self._logpdf(x, a, c)) 

 

def _logpdf(self, x, a, c): 

return np.log(abs(c)) + sc.xlogy(c*a - 1, x) - x**c - sc.gammaln(a) 

 

def _cdf(self, x, a, c): 

xc = x**c 

val1 = sc.gammainc(a, xc) 

val2 = sc.gammaincc(a, xc) 

return np.where(c > 0, val1, val2) 

 

def _sf(self, x, a, c): 

xc = x**c 

val1 = sc.gammainc(a, xc) 

val2 = sc.gammaincc(a, xc) 

return np.where(c > 0, val2, val1) 

 

def _ppf(self, q, a, c): 

val1 = sc.gammaincinv(a, q) 

val2 = sc.gammainccinv(a, q) 

return np.where(c > 0, val1, val2)**(1.0/c) 

 

def _isf(self, q, a, c): 

val1 = sc.gammaincinv(a, q) 

val2 = sc.gammainccinv(a, q) 

return np.where(c > 0, val2, val1)**(1.0/c) 

 

def _munp(self, n, a, c): 

# Pochhammer symbol: sc.pocha,n) = gamma(a+n)/gamma(a) 

return sc.poch(a, n*1.0/c) 

 

def _entropy(self, a, c): 

val = sc.psi(a) 

return a*(1-val) + 1.0/c*val + sc.gammaln(a) - np.log(abs(c)) 

 

 

gengamma = gengamma_gen(a=0.0, name='gengamma') 

 

 

class genhalflogistic_gen(rv_continuous): 

r"""A generalized half-logistic continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `genhalflogistic` is: 

 

.. math:: 

 

f(x, c) = \frac{2 (1 - c x)^{1/(c-1)}}{[1 + (1 - c x)^{1/c}]^2} 

 

for :math:`0 \le x \le 1/c`, and :math:`c > 0`. 

 

`genhalflogistic` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, c): 

self.b = 1.0 / c 

return c > 0 

 

def _pdf(self, x, c): 

# genhalflogistic.pdf(x, c) = 

# 2 * (1-c*x)**(1/c-1) / (1+(1-c*x)**(1/c))**2 

limit = 1.0/c 

tmp = np.asarray(1-c*x) 

tmp0 = tmp**(limit-1) 

tmp2 = tmp0*tmp 

return 2*tmp0 / (1+tmp2)**2 

 

def _cdf(self, x, c): 

limit = 1.0/c 

tmp = np.asarray(1-c*x) 

tmp2 = tmp**(limit) 

return (1.0-tmp2) / (1+tmp2) 

 

def _ppf(self, q, c): 

return 1.0/c*(1-((1.0-q)/(1.0+q))**c) 

 

def _entropy(self, c): 

return 2 - (2*c+1)*np.log(2) 

 

 

genhalflogistic = genhalflogistic_gen(a=0.0, name='genhalflogistic') 

 

 

class gompertz_gen(rv_continuous): 

r"""A Gompertz (or truncated Gumbel) continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `gompertz` is: 

 

.. math:: 

 

f(x, c) = c \exp(x) \exp(-c (e^x-1)) 

 

for :math:`x \ge 0`, :math:`c > 0`. 

 

`gompertz` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, c): 

# gompertz.pdf(x, c) = c * exp(x) * exp(-c*(exp(x)-1)) 

return np.exp(self._logpdf(x, c)) 

 

def _logpdf(self, x, c): 

return np.log(c) + x - c * sc.expm1(x) 

 

def _cdf(self, x, c): 

return -sc.expm1(-c * sc.expm1(x)) 

 

def _ppf(self, q, c): 

return sc.log1p(-1.0 / c * sc.log1p(-q)) 

 

def _entropy(self, c): 

return 1.0 - np.log(c) - np.exp(c)*sc.expn(1, c) 

 

 

gompertz = gompertz_gen(a=0.0, name='gompertz') 

 

 

class gumbel_r_gen(rv_continuous): 

r"""A right-skewed Gumbel continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

gumbel_l, gompertz, genextreme 

 

Notes 

----- 

The probability density function for `gumbel_r` is: 

 

.. math:: 

 

f(x) = \exp(-(x + e^{-x})) 

 

The Gumbel distribution is sometimes referred to as a type I Fisher-Tippett 

distribution. It is also related to the extreme value distribution, 

log-Weibull and Gompertz distributions. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x): 

# gumbel_r.pdf(x) = exp(-(x + exp(-x))) 

return np.exp(self._logpdf(x)) 

 

def _logpdf(self, x): 

return -x - np.exp(-x) 

 

def _cdf(self, x): 

return np.exp(-np.exp(-x)) 

 

def _logcdf(self, x): 

return -np.exp(-x) 

 

def _ppf(self, q): 

return -np.log(-np.log(q)) 

 

def _stats(self): 

return _EULER, np.pi*np.pi/6.0, 12*np.sqrt(6)/np.pi**3 * _ZETA3, 12.0/5 

 

def _entropy(self): 

# http://en.wikipedia.org/wiki/Gumbel_distribution 

return _EULER + 1. 

 

 

gumbel_r = gumbel_r_gen(name='gumbel_r') 

 

 

class gumbel_l_gen(rv_continuous): 

r"""A left-skewed Gumbel continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

gumbel_r, gompertz, genextreme 

 

Notes 

----- 

The probability density function for `gumbel_l` is: 

 

.. math:: 

 

f(x) = \exp(x - e^x) 

 

The Gumbel distribution is sometimes referred to as a type I Fisher-Tippett 

distribution. It is also related to the extreme value distribution, 

log-Weibull and Gompertz distributions. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x): 

# gumbel_l.pdf(x) = exp(x - exp(x)) 

return np.exp(self._logpdf(x)) 

 

def _logpdf(self, x): 

return x - np.exp(x) 

 

def _cdf(self, x): 

return -sc.expm1(-np.exp(x)) 

 

def _ppf(self, q): 

return np.log(-sc.log1p(-q)) 

 

def _logsf(self, x): 

return -np.exp(x) 

 

def _sf(self, x): 

return np.exp(-np.exp(x)) 

 

def _isf(self, x): 

return np.log(-np.log(x)) 

 

def _stats(self): 

return -_EULER, np.pi*np.pi/6.0, \ 

-12*np.sqrt(6)/np.pi**3 * _ZETA3, 12.0/5 

 

def _entropy(self): 

return _EULER + 1. 

 

 

gumbel_l = gumbel_l_gen(name='gumbel_l') 

 

 

class halfcauchy_gen(rv_continuous): 

r"""A Half-Cauchy continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `halfcauchy` is: 

 

.. math:: 

 

f(x) = \frac{2}{\pi (1 + x^2)} 

 

for :math:`x \ge 0`. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x): 

# halfcauchy.pdf(x) = 2 / (pi * (1 + x**2)) 

return 2.0/np.pi/(1.0+x*x) 

 

def _logpdf(self, x): 

return np.log(2.0/np.pi) - sc.log1p(x*x) 

 

def _cdf(self, x): 

return 2.0/np.pi*np.arctan(x) 

 

def _ppf(self, q): 

return np.tan(np.pi/2*q) 

 

def _stats(self): 

return np.inf, np.inf, np.nan, np.nan 

 

def _entropy(self): 

return np.log(2*np.pi) 

 

 

halfcauchy = halfcauchy_gen(a=0.0, name='halfcauchy') 

 

 

class halflogistic_gen(rv_continuous): 

r"""A half-logistic continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `halflogistic` is: 

 

.. math:: 

 

f(x) = \frac{ 2 e^{-x} }{ (1+e^{-x})^2 } = \frac{1}{2} sech(x/2)^2 

 

for :math:`x \ge 0`. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x): 

# halflogistic.pdf(x) = 2 * exp(-x) / (1+exp(-x))**2 

# = 1/2 * sech(x/2)**2 

return np.exp(self._logpdf(x)) 

 

def _logpdf(self, x): 

return np.log(2) - x - 2. * sc.log1p(np.exp(-x)) 

 

def _cdf(self, x): 

return np.tanh(x/2.0) 

 

def _ppf(self, q): 

return 2*np.arctanh(q) 

 

def _munp(self, n): 

if n == 1: 

return 2*np.log(2) 

if n == 2: 

return np.pi*np.pi/3.0 

if n == 3: 

return 9*_ZETA3 

if n == 4: 

return 7*np.pi**4 / 15.0 

return 2*(1-pow(2.0, 1-n))*sc.gamma(n+1)*sc.zeta(n, 1) 

 

def _entropy(self): 

return 2-np.log(2) 

 

 

halflogistic = halflogistic_gen(a=0.0, name='halflogistic') 

 

 

class halfnorm_gen(rv_continuous): 

r"""A half-normal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `halfnorm` is: 

 

.. math:: 

 

f(x) = \sqrt{2/\pi} e^{-\frac{x^2}{2}} 

 

for :math:`x > 0`. 

 

`halfnorm` is a special case of :math`\chi` with ``df == 1``. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self): 

return abs(self._random_state.standard_normal(size=self._size)) 

 

def _pdf(self, x): 

# halfnorm.pdf(x) = sqrt(2/pi) * exp(-x**2/2) 

return np.sqrt(2.0/np.pi)*np.exp(-x*x/2.0) 

 

def _logpdf(self, x): 

return 0.5 * np.log(2.0/np.pi) - x*x/2.0 

 

def _cdf(self, x): 

return _norm_cdf(x)*2-1.0 

 

def _ppf(self, q): 

return sc.ndtri((1+q)/2.0) 

 

def _stats(self): 

return (np.sqrt(2.0/np.pi), 

1-2.0/np.pi, 

np.sqrt(2)*(4-np.pi)/(np.pi-2)**1.5, 

8*(np.pi-3)/(np.pi-2)**2) 

 

def _entropy(self): 

return 0.5*np.log(np.pi/2.0)+0.5 

 

 

halfnorm = halfnorm_gen(a=0.0, name='halfnorm') 

 

 

class hypsecant_gen(rv_continuous): 

r"""A hyperbolic secant continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `hypsecant` is: 

 

.. math:: 

 

f(x) = \frac{1}{\pi} sech(x) 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x): 

# hypsecant.pdf(x) = 1/pi * sech(x) 

return 1.0/(np.pi*np.cosh(x)) 

 

def _cdf(self, x): 

return 2.0/np.pi*np.arctan(np.exp(x)) 

 

def _ppf(self, q): 

return np.log(np.tan(np.pi*q/2.0)) 

 

def _stats(self): 

return 0, np.pi*np.pi/4, 0, 2 

 

def _entropy(self): 

return np.log(2*np.pi) 

 

 

hypsecant = hypsecant_gen(name='hypsecant') 

 

 

class gausshyper_gen(rv_continuous): 

r"""A Gauss hypergeometric continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `gausshyper` is: 

 

.. math:: 

 

f(x, a, b, c, z) = C x^{a-1} (1-x)^{b-1} (1+zx)^{-c} 

 

for :math:`0 \le x \le 1`, :math:`a > 0`, :math:`b > 0`, and 

:math:`C = \frac{1}{B(a, b) F[2, 1](c, a; a+b; -z)}` 

 

`gausshyper` takes :math:`a`, :math:`b`, :math:`c` and :math:`z` as shape 

parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, a, b, c, z): 

return (a > 0) & (b > 0) & (c == c) & (z == z) 

 

def _pdf(self, x, a, b, c, z): 

# gausshyper.pdf(x, a, b, c, z) = 

# C * x**(a-1) * (1-x)**(b-1) * (1+z*x)**(-c) 

Cinv = sc.gamma(a)*sc.gamma(b)/sc.gamma(a+b)*sc.hyp2f1(c, a, a+b, -z) 

return 1.0/Cinv * x**(a-1.0) * (1.0-x)**(b-1.0) / (1.0+z*x)**c 

 

def _munp(self, n, a, b, c, z): 

fac = sc.beta(n+a, b) / sc.beta(a, b) 

num = sc.hyp2f1(c, a+n, a+b+n, -z) 

den = sc.hyp2f1(c, a, a+b, -z) 

return fac*num / den 

 

 

gausshyper = gausshyper_gen(a=0.0, b=1.0, name='gausshyper') 

 

 

class invgamma_gen(rv_continuous): 

r"""An inverted gamma continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `invgamma` is: 

 

.. math:: 

 

f(x, a) = \frac{x^{-a-1}}{\gamma(a)} \exp(-\frac{1}{x}) 

 

for :math:`x > 0`, :math:`a > 0`. 

 

`invgamma` takes :math:`a` as a shape parameter. 

 

`invgamma` is a special case of `gengamma` with ``c == -1``. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _pdf(self, x, a): 

# invgamma.pdf(x, a) = x**(-a-1) / gamma(a) * exp(-1/x) 

return np.exp(self._logpdf(x, a)) 

 

def _logpdf(self, x, a): 

return -(a+1) * np.log(x) - sc.gammaln(a) - 1.0/x 

 

def _cdf(self, x, a): 

return sc.gammaincc(a, 1.0 / x) 

 

def _ppf(self, q, a): 

return 1.0 / sc.gammainccinv(a, q) 

 

def _sf(self, x, a): 

return sc.gammainc(a, 1.0 / x) 

 

def _isf(self, q, a): 

return 1.0 / sc.gammaincinv(a, q) 

 

def _stats(self, a, moments='mvsk'): 

m1 = _lazywhere(a > 1, (a,), lambda x: 1. / (x - 1.), np.inf) 

m2 = _lazywhere(a > 2, (a,), lambda x: 1. / (x - 1.)**2 / (x - 2.), 

np.inf) 

 

g1, g2 = None, None 

if 's' in moments: 

g1 = _lazywhere( 

a > 3, (a,), 

lambda x: 4. * np.sqrt(x - 2.) / (x - 3.), np.nan) 

if 'k' in moments: 

g2 = _lazywhere( 

a > 4, (a,), 

lambda x: 6. * (5. * x - 11.) / (x - 3.) / (x - 4.), np.nan) 

return m1, m2, g1, g2 

 

def _entropy(self, a): 

return a - (a+1.0) * sc.psi(a) + sc.gammaln(a) 

 

 

invgamma = invgamma_gen(a=0.0, name='invgamma') 

 

 

# scale is gamma from DATAPLOT and B from Regress 

class invgauss_gen(rv_continuous): 

r"""An inverse Gaussian continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `invgauss` is: 

 

.. math:: 

 

f(x, \mu) = \frac{1}{\sqrt{2 \pi x^3}} 

\exp(-\frac{(x-\mu)^2}{2 x \mu^2}) 

 

for :math:`x > 0`. 

 

`invgauss` takes :math:`\mu` as a shape parameter. 

 

%(after_notes)s 

 

When :math:`\mu` is too small, evaluating the cumulative distribution 

function will be inaccurate due to ``cdf(mu -> 0) = inf * 0``. 

NaNs are returned for :math:`\mu \le 0.0028`. 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _rvs(self, mu): 

return self._random_state.wald(mu, 1.0, size=self._size) 

 

def _pdf(self, x, mu): 

# invgauss.pdf(x, mu) = 

# 1 / sqrt(2*pi*x**3) * exp(-(x-mu)**2/(2*x*mu**2)) 

return 1.0/np.sqrt(2*np.pi*x**3.0)*np.exp(-1.0/(2*x)*((x-mu)/mu)**2) 

 

def _logpdf(self, x, mu): 

return -0.5*np.log(2*np.pi) - 1.5*np.log(x) - ((x-mu)/mu)**2/(2*x) 

 

def _cdf(self, x, mu): 

fac = np.sqrt(1.0/x) 

# Numerical accuracy for small `mu` is bad. See #869. 

C1 = _norm_cdf(fac*(x-mu)/mu) 

C1 += np.exp(1.0/mu) * _norm_cdf(-fac*(x+mu)/mu) * np.exp(1.0/mu) 

return C1 

 

def _stats(self, mu): 

return mu, mu**3.0, 3*np.sqrt(mu), 15*mu 

 

 

invgauss = invgauss_gen(a=0.0, name='invgauss') 

 

 

class norminvgauss_gen(rv_continuous): 

r"""A Normal Inverse Gaussian continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `norminvgauss` is: 

 

.. math:: 

 

f(x; a, b) = (a \exp(\sqrt{a^2 - b^2} + b x)) / 

(\pi \sqrt{1 + x^2} \, K_1(a * \sqrt{1 + x^2})) 

 

where `x` is a real number, the parameter `a` is the tail heaviness 

and `b` is the asymmetry parameter satisfying `a > 0` and `abs(b) <= a`. 

`K_1` is the modified Bessel function of second kind (`scipy.special.k1`). 

 

%(after_notes)s 

 

A normal inverse Gaussian random variable with parameters `a` and `b` can 

be expressed as `X = b * V + sqrt(V) * X` where `X` is `norm(0,1)` 

and `V` is `invgauss(mu=1/sqrt(a**2 - b**2))`. This representation is used 

to generate random variates. 

 

References 

---------- 

O. Barndorff-Nielsen, "Hyperbolic Distributions and Distributions on 

Hyperbolae", Scandinavian Journal of Statistics, Vol. 5(3), 

pp. 151-157, 1978. 

 

O. Barndorff-Nielsen, "Normal Inverse Gaussian Distributions and Stochastic 

Volatility Modelling", Scandinavian Journal of Statistics, Vol. 24, 

pp. 1–13, 1997. 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _argcheck(self, a, b): 

return (a > 0) & (np.absolute(b) < a) 

 

def _pdf(self, x, a, b): 

gamma = np.sqrt(a**2 - b**2) 

fac1 = a / np.pi * np.exp(gamma) 

sq = np.hypot(1, x) # reduce overflows 

return fac1 * sc.k1e(a * sq) * np.exp(b*x - a*sq) / sq 

 

def _rvs(self, a, b): 

# note: X = b * V + sqrt(V) * X is norminvgaus(a,b) if X is standard 

# normal and V is invgauss(mu=1/sqrt(a**2 - b**2)) 

gamma = np.sqrt(a**2 - b**2) 

sz, rndm = self._size, self._random_state 

ig = invgauss.rvs(mu=1/gamma, size=sz, random_state=rndm) 

return b * ig + np.sqrt(ig) * norm.rvs(size=sz, random_state=rndm) 

 

def _stats(self, a, b): 

gamma = np.sqrt(a**2 - b**2) 

mean = b / gamma 

variance = a**2 / gamma**3 

skewness = 3.0 * b / (a * np.sqrt(gamma)) 

kurtosis = 3.0 * (1 + 4 * b**2 / a**2) / gamma 

return mean, variance, skewness, kurtosis 

 

 

norminvgauss = norminvgauss_gen(name="norminvgauss") 

 

 

class invweibull_gen(rv_continuous): 

r"""An inverted Weibull continuous random variable. 

 

This distribution is also known as the Fréchet distribution or the 

type II extreme value distribution. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `invweibull` is: 

 

.. math:: 

 

f(x, c) = c x^{-c-1} \exp(-x^{-c}) 

 

for :math:`x > 0``, :math:`c > 0``. 

 

`invweibull` takes :math:`c`` as a shape parameter. 

 

%(after_notes)s 

 

References 

---------- 

F.R.S. de Gusmao, E.M.M Ortega and G.M. Cordeiro, "The generalized inverse 

Weibull distribution", Stat. Papers, vol. 52, pp. 591-619, 2011. 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _pdf(self, x, c): 

# invweibull.pdf(x, c) = c * x**(-c-1) * exp(-x**(-c)) 

xc1 = np.power(x, -c - 1.0) 

xc2 = np.power(x, -c) 

xc2 = np.exp(-xc2) 

return c * xc1 * xc2 

 

def _cdf(self, x, c): 

xc1 = np.power(x, -c) 

return np.exp(-xc1) 

 

def _ppf(self, q, c): 

return np.power(-np.log(q), -1.0/c) 

 

def _munp(self, n, c): 

return sc.gamma(1 - n / c) 

 

def _entropy(self, c): 

return 1+_EULER + _EULER / c - np.log(c) 

 

 

invweibull = invweibull_gen(a=0, name='invweibull') 

 

 

class johnsonsb_gen(rv_continuous): 

r"""A Johnson SB continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

johnsonsu 

 

Notes 

----- 

The probability density function for `johnsonsb` is: 

 

.. math:: 

 

f(x, a, b) = \frac{b}{x(1-x)} \phi(a + b \log \frac{x}{1-x} ) 

 

for :math:`0 < x < 1` and :math:`a, b > 0`, and :math:`\phi` is the normal 

pdf. 

 

`johnsonsb` takes :math:`a` and :math:`b` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _argcheck(self, a, b): 

return (b > 0) & (a == a) 

 

def _pdf(self, x, a, b): 

# johnsonsb.pdf(x, a, b) = b / (x*(1-x)) * phi(a + b * log(x/(1-x))) 

trm = _norm_pdf(a + b*np.log(x/(1.0-x))) 

return b*1.0/(x*(1-x))*trm 

 

def _cdf(self, x, a, b): 

return _norm_cdf(a + b*np.log(x/(1.0-x))) 

 

def _ppf(self, q, a, b): 

return 1.0 / (1 + np.exp(-1.0 / b * (_norm_ppf(q) - a))) 

 

 

johnsonsb = johnsonsb_gen(a=0.0, b=1.0, name='johnsonsb') 

 

 

class johnsonsu_gen(rv_continuous): 

r"""A Johnson SU continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

johnsonsb 

 

Notes 

----- 

The probability density function for `johnsonsu` is: 

 

.. math:: 

 

f(x, a, b) = \frac{b}{\sqrt{x^2 + 1}} 

\phi(a + b \log(x + \sqrt{x^2 + 1})) 

 

for all :math:`x, a, b > 0`, and :math:`\phi` is the normal pdf. 

 

`johnsonsu` takes :math:`a` and :math:`b` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, a, b): 

return (b > 0) & (a == a) 

 

def _pdf(self, x, a, b): 

# johnsonsu.pdf(x, a, b) = b / sqrt(x**2 + 1) * 

# phi(a + b * log(x + sqrt(x**2 + 1))) 

x2 = x*x 

trm = _norm_pdf(a + b * np.log(x + np.sqrt(x2+1))) 

return b*1.0/np.sqrt(x2+1.0)*trm 

 

def _cdf(self, x, a, b): 

return _norm_cdf(a + b * np.log(x + np.sqrt(x*x + 1))) 

 

def _ppf(self, q, a, b): 

return np.sinh((_norm_ppf(q) - a) / b) 

 

 

johnsonsu = johnsonsu_gen(name='johnsonsu') 

 

 

class laplace_gen(rv_continuous): 

r"""A Laplace continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `laplace` is: 

 

.. math:: 

 

f(x) = \frac{1}{2} \exp(-|x|) 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self): 

return self._random_state.laplace(0, 1, size=self._size) 

 

def _pdf(self, x): 

# laplace.pdf(x) = 1/2 * exp(-abs(x)) 

return 0.5*np.exp(-abs(x)) 

 

def _cdf(self, x): 

return np.where(x > 0, 1.0-0.5*np.exp(-x), 0.5*np.exp(x)) 

 

def _ppf(self, q): 

return np.where(q > 0.5, -np.log(2*(1-q)), np.log(2*q)) 

 

def _stats(self): 

return 0, 2, 0, 3 

 

def _entropy(self): 

return np.log(2)+1 

 

 

laplace = laplace_gen(name='laplace') 

 

 

class levy_gen(rv_continuous): 

r"""A Levy continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

levy_stable, levy_l 

 

Notes 

----- 

The probability density function for `levy` is: 

 

.. math:: 

 

f(x) = \frac{1}{x \sqrt{2\pi x}) \exp(-\frac{1}{2x}} 

 

for :math:`x > 0`. 

 

This is the same as the Levy-stable distribution with :math:`a=1/2` and 

:math:`b=1`. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _pdf(self, x): 

# levy.pdf(x) = 1 / (x * sqrt(2*pi*x)) * exp(-1/(2*x)) 

return 1 / np.sqrt(2*np.pi*x) / x * np.exp(-1/(2*x)) 

 

def _cdf(self, x): 

# Equivalent to 2*norm.sf(np.sqrt(1/x)) 

return sc.erfc(np.sqrt(0.5 / x)) 

 

def _ppf(self, q): 

# Equivalent to 1.0/(norm.isf(q/2)**2) or 0.5/(erfcinv(q)**2) 

val = -sc.ndtri(q/2) 

return 1.0 / (val * val) 

 

def _stats(self): 

return np.inf, np.inf, np.nan, np.nan 

 

 

levy = levy_gen(a=0.0, name="levy") 

 

 

class levy_l_gen(rv_continuous): 

r"""A left-skewed Levy continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

levy, levy_stable 

 

Notes 

----- 

The probability density function for `levy_l` is: 

 

.. math:: 

 

f(x) = \frac{1}{|x| \sqrt{2\pi |x|}} \exp(-\frac{1}{2 |x|}) 

 

for :math:`x < 0`. 

 

This is the same as the Levy-stable distribution with :math:`a=1/2` and 

:math:`b=-1`. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _pdf(self, x): 

# levy_l.pdf(x) = 1 / (abs(x) * sqrt(2*pi*abs(x))) * exp(-1/(2*abs(x))) 

ax = abs(x) 

return 1/np.sqrt(2*np.pi*ax)/ax*np.exp(-1/(2*ax)) 

 

def _cdf(self, x): 

ax = abs(x) 

return 2 * _norm_cdf(1 / np.sqrt(ax)) - 1 

 

def _ppf(self, q): 

val = _norm_ppf((q + 1.0) / 2) 

return -1.0 / (val * val) 

 

def _stats(self): 

return np.inf, np.inf, np.nan, np.nan 

 

 

levy_l = levy_l_gen(b=0.0, name="levy_l") 

 

 

class levy_stable_gen(rv_continuous): 

r"""A Levy-stable continuous random variable. 

 

%(before_notes)s 

 

See Also 

-------- 

levy, levy_l 

 

Notes 

----- 

Levy-stable distribution (only random variates available -- ignore other 

docs) 

 

""" 

 

def _rvs(self, alpha, beta): 

 

def alpha1func(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W): 

return (2/np.pi*(np.pi/2 + bTH)*tanTH - 

beta*np.log((np.pi/2*W*cosTH)/(np.pi/2 + bTH))) 

 

def beta0func(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W): 

return (W/(cosTH/np.tan(aTH) + np.sin(TH)) * 

((np.cos(aTH) + np.sin(aTH)*tanTH)/W)**(1.0/alpha)) 

 

def otherwise(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W): 

# alpha is not 1 and beta is not 0 

val0 = beta*np.tan(np.pi*alpha/2) 

th0 = np.arctan(val0)/alpha 

val3 = W/(cosTH/np.tan(alpha*(th0 + TH)) + np.sin(TH)) 

res3 = val3*((np.cos(aTH) + np.sin(aTH)*tanTH - 

val0*(np.sin(aTH) - np.cos(aTH)*tanTH))/W)**(1.0/alpha) 

return res3 

 

def alphanot1func(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W): 

res = _lazywhere(beta == 0, 

(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W), 

beta0func, f2=otherwise) 

return res 

 

sz = self._size 

alpha = broadcast_to(alpha, sz) 

beta = broadcast_to(beta, sz) 

TH = uniform.rvs(loc=-np.pi/2.0, scale=np.pi, size=sz, 

random_state=self._random_state) 

W = expon.rvs(size=sz, random_state=self._random_state) 

aTH = alpha*TH 

bTH = beta*TH 

cosTH = np.cos(TH) 

tanTH = np.tan(TH) 

res = _lazywhere(alpha == 1, 

(alpha, beta, TH, aTH, bTH, cosTH, tanTH, W), 

alpha1func, f2=alphanot1func) 

return res 

 

def _argcheck(self, alpha, beta): 

return (alpha > 0) & (alpha <= 2) & (beta <= 1) & (beta >= -1) 

 

def _pdf(self, x, alpha, beta): 

raise NotImplementedError 

 

 

levy_stable = levy_stable_gen(name='levy_stable') 

 

 

class logistic_gen(rv_continuous): 

r"""A logistic (or Sech-squared) continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `logistic` is: 

 

.. math:: 

 

f(x) = \frac{\exp(-x)} 

{(1+exp(-x))^2} 

 

`logistic` is a special case of `genlogistic` with ``c == 1``. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self): 

return self._random_state.logistic(size=self._size) 

 

def _pdf(self, x): 

# logistic.pdf(x) = exp(-x) / (1+exp(-x))**2 

return np.exp(self._logpdf(x)) 

 

def _logpdf(self, x): 

return -x - 2. * sc.log1p(np.exp(-x)) 

 

def _cdf(self, x): 

return sc.expit(x) 

 

def _ppf(self, q): 

return sc.logit(q) 

 

def _sf(self, x): 

return sc.expit(-x) 

 

def _isf(self, q): 

return -sc.logit(q) 

 

def _stats(self): 

return 0, np.pi*np.pi/3.0, 0, 6.0/5.0 

 

def _entropy(self): 

# http://en.wikipedia.org/wiki/Logistic_distribution 

return 2.0 

 

 

logistic = logistic_gen(name='logistic') 

 

 

class loggamma_gen(rv_continuous): 

r"""A log gamma continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `loggamma` is: 

 

.. math:: 

 

f(x, c) = \frac{\exp(c x - \exp(x))} 

{\gamma(c)} 

 

for all :math:`x, c > 0`. 

 

`loggamma` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self, c): 

return np.log(self._random_state.gamma(c, size=self._size)) 

 

def _pdf(self, x, c): 

# loggamma.pdf(x, c) = exp(c*x-exp(x)) / gamma(c) 

return np.exp(c*x-np.exp(x)-sc.gammaln(c)) 

 

def _cdf(self, x, c): 

return sc.gammainc(c, np.exp(x)) 

 

def _ppf(self, q, c): 

return np.log(sc.gammaincinv(c, q)) 

 

def _stats(self, c): 

# See, for example, "A Statistical Study of Log-Gamma Distribution", by 

# Ping Shing Chan (thesis, McMaster University, 1993). 

mean = sc.digamma(c) 

var = sc.polygamma(1, c) 

skewness = sc.polygamma(2, c) / np.power(var, 1.5) 

excess_kurtosis = sc.polygamma(3, c) / (var*var) 

return mean, var, skewness, excess_kurtosis 

 

 

loggamma = loggamma_gen(name='loggamma') 

 

 

class loglaplace_gen(rv_continuous): 

r"""A log-Laplace continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `loglaplace` is: 

 

.. math:: 

 

f(x, c) = \begin{cases}\frac{c}{2} x^{ c-1} &\text{for } 0 < x < 1\\ 

\frac{c}{2} x^{-c-1} &\text{for } x \ge 1 

\end{cases} 

 

for ``c > 0``. 

 

`loglaplace` takes ``c`` as a shape parameter. 

 

%(after_notes)s 

 

References 

---------- 

T.J. Kozubowski and K. Podgorski, "A log-Laplace growth rate model", 

The Mathematical Scientist, vol. 28, pp. 49-60, 2003. 

 

%(example)s 

 

""" 

def _pdf(self, x, c): 

# loglaplace.pdf(x, c) = c / 2 * x**(c-1), for 0 < x < 1 

# = c / 2 * x**(-c-1), for x >= 1 

cd2 = c/2.0 

c = np.where(x < 1, c, -c) 

return cd2*x**(c-1) 

 

def _cdf(self, x, c): 

return np.where(x < 1, 0.5*x**c, 1-0.5*x**(-c)) 

 

def _ppf(self, q, c): 

return np.where(q < 0.5, (2.0*q)**(1.0/c), (2*(1.0-q))**(-1.0/c)) 

 

def _munp(self, n, c): 

return c**2 / (c**2 - n**2) 

 

def _entropy(self, c): 

return np.log(2.0/c) + 1.0 

 

 

loglaplace = loglaplace_gen(a=0.0, name='loglaplace') 

 

 

def _lognorm_logpdf(x, s): 

return _lazywhere(x != 0, (x, s), 

lambda x, s: -np.log(x)**2 / (2*s**2) - np.log(s*x*np.sqrt(2*np.pi)), 

-np.inf) 

 

 

class lognorm_gen(rv_continuous): 

r"""A lognormal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `lognorm` is: 

 

.. math:: 

 

f(x, s) = \frac{1}{s x \sqrt{2\pi}} 

\exp(-\frac{1}{2} (\frac{\log(x)}{s})^2) 

 

for ``x > 0``, ``s > 0``. 

 

`lognorm` takes ``s`` as a shape parameter. 

 

%(after_notes)s 

 

A common parametrization for a lognormal random variable ``Y`` is in 

terms of the mean, ``mu``, and standard deviation, ``sigma``, of the 

unique normally distributed random variable ``X`` such that exp(X) = Y. 

This parametrization corresponds to setting ``s = sigma`` and ``scale = 

exp(mu)``. 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _rvs(self, s): 

return np.exp(s * self._random_state.standard_normal(self._size)) 

 

def _pdf(self, x, s): 

# lognorm.pdf(x, s) = 1 / (s*x*sqrt(2*pi)) * exp(-1/2*(log(x)/s)**2) 

return np.exp(self._logpdf(x, s)) 

 

def _logpdf(self, x, s): 

return _lognorm_logpdf(x, s) 

 

def _cdf(self, x, s): 

return _norm_cdf(np.log(x) / s) 

 

def _logcdf(self, x, s): 

return _norm_logcdf(np.log(x) / s) 

 

def _ppf(self, q, s): 

return np.exp(s * _norm_ppf(q)) 

 

def _sf(self, x, s): 

return _norm_sf(np.log(x) / s) 

 

def _logsf(self, x, s): 

return _norm_logsf(np.log(x) / s) 

 

def _stats(self, s): 

p = np.exp(s*s) 

mu = np.sqrt(p) 

mu2 = p*(p-1) 

g1 = np.sqrt((p-1))*(2+p) 

g2 = np.polyval([1, 2, 3, 0, -6.0], p) 

return mu, mu2, g1, g2 

 

def _entropy(self, s): 

return 0.5 * (1 + np.log(2*np.pi) + 2 * np.log(s)) 

 

@extend_notes_in_docstring(rv_continuous, notes="""\ 

When the location parameter is fixed by using the `floc` argument, 

this function uses explicit formulas for the maximum likelihood 

estimation of the log-normal shape and scale parameters, so the 

`optimizer`, `loc` and `scale` keyword arguments are ignored.\n\n""") 

def fit(self, data, *args, **kwds): 

floc = kwds.get('floc', None) 

if floc is None: 

# loc is not fixed. Use the default fit method. 

return super(lognorm_gen, self).fit(data, *args, **kwds) 

 

f0 = (kwds.get('f0', None) or kwds.get('fs', None) or 

kwds.get('fix_s', None)) 

fscale = kwds.get('fscale', None) 

 

if len(args) > 1: 

raise TypeError("Too many input arguments.") 

for name in ['f0', 'fs', 'fix_s', 'floc', 'fscale', 'loc', 'scale', 

'optimizer']: 

kwds.pop(name, None) 

if kwds: 

raise TypeError("Unknown arguments: %s." % kwds) 

 

# Special case: loc is fixed. Use the maximum likelihood formulas 

# instead of the numerical solver. 

 

if f0 is not None and fscale is not None: 

# This check is for consistency with `rv_continuous.fit`. 

raise ValueError("All parameters fixed. There is nothing to " 

"optimize.") 

 

data = np.asarray(data) 

floc = float(floc) 

if floc != 0: 

# Shifting the data by floc. Don't do the subtraction in-place, 

# because `data` might be a view of the input array. 

data = data - floc 

if np.any(data <= 0): 

raise FitDataError("lognorm", lower=floc, upper=np.inf) 

lndata = np.log(data) 

 

# Three cases to handle: 

# * shape and scale both free 

# * shape fixed, scale free 

# * shape free, scale fixed 

 

if fscale is None: 

# scale is free. 

scale = np.exp(lndata.mean()) 

if f0 is None: 

# shape is free. 

shape = lndata.std() 

else: 

# shape is fixed. 

shape = float(f0) 

else: 

# scale is fixed, shape is free 

scale = float(fscale) 

shape = np.sqrt(((lndata - np.log(scale))**2).mean()) 

 

return shape, floc, scale 

 

 

lognorm = lognorm_gen(a=0.0, name='lognorm') 

 

 

class gilbrat_gen(rv_continuous): 

r"""A Gilbrat continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `gilbrat` is: 

 

.. math:: 

 

f(x) = \frac{1}{x \sqrt{2\pi}} \exp(-\frac{1}{2} (\log(x))^2) 

 

`gilbrat` is a special case of `lognorm` with ``s = 1``. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _rvs(self): 

return np.exp(self._random_state.standard_normal(self._size)) 

 

def _pdf(self, x): 

# gilbrat.pdf(x) = 1/(x*sqrt(2*pi)) * exp(-1/2*(log(x))**2) 

return np.exp(self._logpdf(x)) 

 

def _logpdf(self, x): 

return _lognorm_logpdf(x, 1.0) 

 

def _cdf(self, x): 

return _norm_cdf(np.log(x)) 

 

def _ppf(self, q): 

return np.exp(_norm_ppf(q)) 

 

def _stats(self): 

p = np.e 

mu = np.sqrt(p) 

mu2 = p * (p - 1) 

g1 = np.sqrt((p - 1)) * (2 + p) 

g2 = np.polyval([1, 2, 3, 0, -6.0], p) 

return mu, mu2, g1, g2 

 

def _entropy(self): 

return 0.5 * np.log(2 * np.pi) + 0.5 

 

 

gilbrat = gilbrat_gen(a=0.0, name='gilbrat') 

 

 

class maxwell_gen(rv_continuous): 

r"""A Maxwell continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

A special case of a `chi` distribution, with ``df = 3``, ``loc = 0.0``, 

and given ``scale = a``, where ``a`` is the parameter used in the 

Mathworld description [1]_. 

 

The probability density function for `maxwell` is: 

 

.. math:: 

 

f(x) = \sqrt{2/\pi}x^2 \exp(-x^2/2) 

 

for ``x > 0``. 

 

%(after_notes)s 

 

References 

---------- 

.. [1] http://mathworld.wolfram.com/MaxwellDistribution.html 

 

%(example)s 

""" 

def _rvs(self): 

return chi.rvs(3.0, size=self._size, random_state=self._random_state) 

 

def _pdf(self, x): 

# maxwell.pdf(x) = sqrt(2/pi)x**2 * exp(-x**2/2) 

return np.sqrt(2.0/np.pi)*x*x*np.exp(-x*x/2.0) 

 

def _cdf(self, x): 

return sc.gammainc(1.5, x*x/2.0) 

 

def _ppf(self, q): 

return np.sqrt(2*sc.gammaincinv(1.5, q)) 

 

def _stats(self): 

val = 3*np.pi-8 

return (2*np.sqrt(2.0/np.pi), 

3-8/np.pi, 

np.sqrt(2)*(32-10*np.pi)/val**1.5, 

(-12*np.pi*np.pi + 160*np.pi - 384) / val**2.0) 

 

def _entropy(self): 

return _EULER + 0.5*np.log(2*np.pi)-0.5 

 

 

maxwell = maxwell_gen(a=0.0, name='maxwell') 

 

 

class mielke_gen(rv_continuous): 

r"""A Mielke's Beta-Kappa continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `mielke` is: 

 

.. math:: 

 

f(x, k, s) = \frac{k x^{k-1}}{(1+x^s)^{1+k/s}} 

 

for ``x > 0``. 

 

`mielke` takes ``k`` and ``s`` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, k, s): 

# mielke.pdf(x, k, s) = k * x**(k-1) / (1+x**s)**(1+k/s) 

return k*x**(k-1.0) / (1.0+x**s)**(1.0+k*1.0/s) 

 

def _cdf(self, x, k, s): 

return x**k / (1.0+x**s)**(k*1.0/s) 

 

def _ppf(self, q, k, s): 

qsk = pow(q, s*1.0/k) 

return pow(qsk/(1.0-qsk), 1.0/s) 

 

 

mielke = mielke_gen(a=0.0, name='mielke') 

 

 

class kappa4_gen(rv_continuous): 

r"""Kappa 4 parameter distribution. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for kappa4 is: 

 

.. math:: 

 

f(x, h, k) = (1 - k x)^{1/k - 1} (1 - h (1 - k x)^{1/k})^{1/h-1} 

 

if :math:`h` and :math:`k` are not equal to 0. 

 

If :math:`h` or :math:`k` are zero then the pdf can be simplified: 

 

h = 0 and k != 0:: 

 

kappa4.pdf(x, h, k) = (1.0 - k*x)**(1.0/k - 1.0)* 

exp(-(1.0 - k*x)**(1.0/k)) 

 

h != 0 and k = 0:: 

 

kappa4.pdf(x, h, k) = exp(-x)*(1.0 - h*exp(-x))**(1.0/h - 1.0) 

 

h = 0 and k = 0:: 

 

kappa4.pdf(x, h, k) = exp(-x)*exp(-exp(-x)) 

 

kappa4 takes :math:`h` and :math:`k` as shape parameters. 

 

The kappa4 distribution returns other distributions when certain 

:math:`h` and :math:`k` values are used. 

 

+------+-------------+----------------+------------------+ 

| h | k=0.0 | k=1.0 | -inf<=k<=inf | 

+======+=============+================+==================+ 

| -1.0 | Logistic | | Generalized | 

| | | | Logistic(1) | 

| | | | | 

| | logistic(x) | | | 

+------+-------------+----------------+------------------+ 

| 0.0 | Gumbel | Reverse | Generalized | 

| | | Exponential(2) | Extreme Value | 

| | | | | 

| | gumbel_r(x) | | genextreme(x, k) | 

+------+-------------+----------------+------------------+ 

| 1.0 | Exponential | Uniform | Generalized | 

| | | | Pareto | 

| | | | | 

| | expon(x) | uniform(x) | genpareto(x, -k) | 

+------+-------------+----------------+------------------+ 

 

(1) There are at least five generalized logistic distributions. 

Four are described here: 

https://en.wikipedia.org/wiki/Generalized_logistic_distribution 

The "fifth" one is the one kappa4 should match which currently 

isn't implemented in scipy: 

https://en.wikipedia.org/wiki/Talk:Generalized_logistic_distribution 

http://www.mathwave.com/help/easyfit/html/analyses/distributions/gen_logistic.html 

(2) This distribution is currently not in scipy. 

 

References 

---------- 

J.C. Finney, "Optimization of a Skewed Logistic Distribution With Respect 

to the Kolmogorov-Smirnov Test", A Dissertation Submitted to the Graduate 

Faculty of the Louisiana State University and Agricultural and Mechanical 

College, (August, 2004), 

http://digitalcommons.lsu.edu/cgi/viewcontent.cgi?article=4671&context=gradschool_dissertations 

 

J.R.M. Hosking, "The four-parameter kappa distribution". IBM J. Res. 

Develop. 38 (3), 25 1-258 (1994). 

 

B. Kumphon, A. Kaew-Man, P. Seenoi, "A Rainfall Distribution for the Lampao 

Site in the Chi River Basin, Thailand", Journal of Water Resource and 

Protection, vol. 4, 866-869, (2012). 

http://file.scirp.org/pdf/JWARP20121000009_14676002.pdf 

 

C. Winchester, "On Estimation of the Four-Parameter Kappa Distribution", A 

Thesis Submitted to Dalhousie University, Halifax, Nova Scotia, (March 

2000). 

http://www.nlc-bnc.ca/obj/s4/f2/dsk2/ftp01/MQ57336.pdf 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, h, k): 

condlist = [np.logical_and(h > 0, k > 0), 

np.logical_and(h > 0, k == 0), 

np.logical_and(h > 0, k < 0), 

np.logical_and(h <= 0, k > 0), 

np.logical_and(h <= 0, k == 0), 

np.logical_and(h <= 0, k < 0)] 

 

def f0(h, k): 

return (1.0 - float_power(h, -k))/k 

 

def f1(h, k): 

return np.log(h) 

 

def f3(h, k): 

a = np.empty(np.shape(h)) 

a[:] = -np.inf 

return a 

 

def f5(h, k): 

return 1.0/k 

 

self.a = _lazyselect(condlist, 

[f0, f1, f0, f3, f3, f5], 

[h, k], 

default=np.nan) 

 

def f0(h, k): 

return 1.0/k 

 

def f1(h, k): 

a = np.empty(np.shape(h)) 

a[:] = np.inf 

return a 

 

self.b = _lazyselect(condlist, 

[f0, f1, f1, f0, f1, f1], 

[h, k], 

default=np.nan) 

return h == h 

 

def _pdf(self, x, h, k): 

# kappa4.pdf(x, h, k) = (1.0 - k*x)**(1.0/k - 1.0)* 

# (1.0 - h*(1.0 - k*x)**(1.0/k))**(1.0/h-1) 

return np.exp(self._logpdf(x, h, k)) 

 

def _logpdf(self, x, h, k): 

condlist = [np.logical_and(h != 0, k != 0), 

np.logical_and(h == 0, k != 0), 

np.logical_and(h != 0, k == 0), 

np.logical_and(h == 0, k == 0)] 

 

def f0(x, h, k): 

'''pdf = (1.0 - k*x)**(1.0/k - 1.0)*( 

1.0 - h*(1.0 - k*x)**(1.0/k))**(1.0/h-1.0) 

logpdf = ... 

''' 

return (sc.xlog1py(1.0/k - 1.0, -k*x) + 

sc.xlog1py(1.0/h - 1.0, -h*(1.0 - k*x)**(1.0/k))) 

 

def f1(x, h, k): 

'''pdf = (1.0 - k*x)**(1.0/k - 1.0)*np.exp(-( 

1.0 - k*x)**(1.0/k)) 

logpdf = ... 

''' 

return sc.xlog1py(1.0/k - 1.0, -k*x) - (1.0 - k*x)**(1.0/k) 

 

def f2(x, h, k): 

'''pdf = np.exp(-x)*(1.0 - h*np.exp(-x))**(1.0/h - 1.0) 

logpdf = ... 

''' 

return -x + sc.xlog1py(1.0/h - 1.0, -h*np.exp(-x)) 

 

def f3(x, h, k): 

'''pdf = np.exp(-x-np.exp(-x)) 

logpdf = ... 

''' 

return -x - np.exp(-x) 

 

return _lazyselect(condlist, 

[f0, f1, f2, f3], 

[x, h, k], 

default=np.nan) 

 

def _cdf(self, x, h, k): 

return np.exp(self._logcdf(x, h, k)) 

 

def _logcdf(self, x, h, k): 

condlist = [np.logical_and(h != 0, k != 0), 

np.logical_and(h == 0, k != 0), 

np.logical_and(h != 0, k == 0), 

np.logical_and(h == 0, k == 0)] 

 

def f0(x, h, k): 

'''cdf = (1.0 - h*(1.0 - k*x)**(1.0/k))**(1.0/h) 

logcdf = ... 

''' 

return (1.0/h)*sc.log1p(-h*(1.0 - k*x)**(1.0/k)) 

 

def f1(x, h, k): 

'''cdf = np.exp(-(1.0 - k*x)**(1.0/k)) 

logcdf = ... 

''' 

return -(1.0 - k*x)**(1.0/k) 

 

def f2(x, h, k): 

'''cdf = (1.0 - h*np.exp(-x))**(1.0/h) 

logcdf = ... 

''' 

return (1.0/h)*sc.log1p(-h*np.exp(-x)) 

 

def f3(x, h, k): 

'''cdf = np.exp(-np.exp(-x)) 

logcdf = ... 

''' 

return -np.exp(-x) 

 

return _lazyselect(condlist, 

[f0, f1, f2, f3], 

[x, h, k], 

default=np.nan) 

 

def _ppf(self, q, h, k): 

condlist = [np.logical_and(h != 0, k != 0), 

np.logical_and(h == 0, k != 0), 

np.logical_and(h != 0, k == 0), 

np.logical_and(h == 0, k == 0)] 

 

def f0(q, h, k): 

return 1.0/k*(1.0 - ((1.0 - (q**h))/h)**k) 

 

def f1(q, h, k): 

return 1.0/k*(1.0 - (-np.log(q))**k) 

 

def f2(q, h, k): 

'''ppf = -np.log((1.0 - (q**h))/h) 

''' 

return -sc.log1p(-(q**h)) + np.log(h) 

 

def f3(q, h, k): 

return -np.log(-np.log(q)) 

 

return _lazyselect(condlist, 

[f0, f1, f2, f3], 

[q, h, k], 

default=np.nan) 

 

def _stats(self, h, k): 

if h >= 0 and k >= 0: 

maxr = 5 

elif h < 0 and k >= 0: 

maxr = int(-1.0/h*k) 

elif k < 0: 

maxr = int(-1.0/k) 

else: 

maxr = 5 

 

outputs = [None if r < maxr else np.nan for r in range(1, 5)] 

return outputs[:] 

 

 

kappa4 = kappa4_gen(name='kappa4') 

 

 

class kappa3_gen(rv_continuous): 

r"""Kappa 3 parameter distribution. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `kappa` is: 

 

.. math:: 

 

f(x, a) = \begin{cases} 

a [a + x^a]^{-(a + 1)/a}, &\text{for } x > 0\\ 

0.0, &\text{for } x \le 0 

\end{cases} 

 

`kappa3` takes :math:`a` as a shape parameter and :math:`a > 0`. 

 

References 

---------- 

P.W. Mielke and E.S. Johnson, "Three-Parameter Kappa Distribution Maximum 

Likelihood and Likelihood Ratio Tests", Methods in Weather Research, 

701-707, (September, 1973), 

http://docs.lib.noaa.gov/rescue/mwr/101/mwr-101-09-0701.pdf 

 

B. Kumphon, "Maximum Entropy and Maximum Likelihood Estimation for the 

Three-Parameter Kappa Distribution", Open Journal of Statistics, vol 2, 

415-419 (2012) 

http://file.scirp.org/pdf/OJS20120400011_95789012.pdf 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, a): 

return a > 0 

 

def _pdf(self, x, a): 

# kappa3.pdf(x, a) = 

# a*[a + x**a]**(-(a + 1)/a), for x > 0 

# 0.0, for x <= 0 

return a*(a + x**a)**(-1.0/a-1) 

 

def _cdf(self, x, a): 

return x*(a + x**a)**(-1.0/a) 

 

def _ppf(self, q, a): 

return (a/(q**-a - 1.0))**(1.0/a) 

 

def _stats(self, a): 

outputs = [None if i < a else np.nan for i in range(1, 5)] 

return outputs[:] 

 

 

kappa3 = kappa3_gen(a=0.0, name='kappa3') 

 

class moyal_gen(rv_continuous): 

r"""A Moyal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `moyal` is: 

 

.. math:: 

 

f(x) = \exp(-(x + \exp(-x))/2) / \sqrt{2\pi} 

 

%(after_notes)s 

 

This distribution has utility in high-energy physics and radiation 

detection. It describes the energy loss of a charged relativistic 

particle due to ionization of the medium [1]_. It also provides an 

approximation for the Landau distribution. For an in depth description 

see [2]_. For additional description, see [3]_. 

 

References 

---------- 

.. [1] J.E. Moyal, "XXX. Theory of ionization fluctuations", 

The London, Edinburgh, and Dublin Philosophical Magazine 

and Journal of Science, vol 46, 263-280, (1955). 

https://doi.org/10.1080/14786440308521076 (gated) 

.. [2] G. Cordeiro et al., "The beta Moyal: a useful skew distribution", 

International Journal of Research and Reviews in Applied Sciences, 

vol 10, 171-192, (2012). 

http://www.arpapress.com/Volumes/Vol10Issue2/IJRRAS_10_2_02.pdf 

.. [3] C. Walck, "Handbook on Statistical Distributions for 

Experimentalists; International Report SUF-PFY/96-01", Chapter 26, 

University of Stockholm: Stockholm, Sweden, (2007). 

www.stat.rice.edu/~dobelman/textfiles/DistributionsHandbook.pdf 

 

.. versionadded:: 1.1.0 

 

%(example)s 

 

""" 

def _rvs(self): 

sz, rndm = self._size, self._random_state 

u1 = gamma.rvs(a = 0.5, scale = 2, size=sz, random_state=rndm) 

return -np.log(u1) 

 

def _pdf(self, x): 

return np.exp(-0.5 * (x + np.exp(-x))) / np.sqrt(2*np.pi) 

 

def _cdf(self, x): 

return sc.erfc(np.exp(-0.5 * x) / np.sqrt(2)) 

 

def _sf(self, x): 

return sc.erf(np.exp(-0.5 * x) / np.sqrt(2)) 

 

def _ppf(self, x): 

return -np.log(2 * sc.erfcinv(x)**2) 

 

def _stats(self): 

mu = np.log(2) + np.euler_gamma 

mu2 = np.pi**2 / 2 

g1 = 28 * np.sqrt(2) * sc.zeta(3) / np.pi**3 

g2 = 4. 

return mu, mu2, g1, g2 

 

def _munp(self, n): 

if n == 1.0: 

return np.log(2) + np.euler_gamma 

elif n == 2.0: 

return np.pi**2 / 2 + (np.log(2) + np.euler_gamma)**2 

elif n == 3.0: 

tmp1 = 1.5 * np.pi**2 * (np.log(2)+np.euler_gamma) 

tmp2 = (np.log(2)+np.euler_gamma)**3 

tmp3 = 14 * sc.zeta(3) 

return tmp1 + tmp2 + tmp3 

elif n == 4.0: 

tmp1 = 4 * 14 * sc.zeta(3) * (np.log(2) + np.euler_gamma) 

tmp2 = 3 * np.pi**2 * (np.log(2) + np.euler_gamma)**2 

tmp3 = (np.log(2) + np.euler_gamma)**4 

tmp4 = 7 * np.pi**4 / 4 

return tmp1 + tmp2 + tmp3 + tmp4 

else: 

# return generic for higher moments 

# return rv_continuous._mom1_sc(self, n, b) 

return self._mom1_sc(n) 

 

 

moyal = moyal_gen(name="moyal") 

 

 

class nakagami_gen(rv_continuous): 

r"""A Nakagami continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `nakagami` is: 

 

.. math:: 

 

f(x, nu) = \frac{2 \nu^\nu}{\Gamma(\nu)} x^{2\nu-1} \exp(-\nu x^2) 

 

for ``x > 0``, ``nu > 0``. 

 

`nakagami` takes ``nu`` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, nu): 

# nakagami.pdf(x, nu) = 2 * nu**nu / gamma(nu) * 

# x**(2*nu-1) * exp(-nu*x**2) 

return 2*nu**nu/sc.gamma(nu)*(x**(2*nu-1.0))*np.exp(-nu*x*x) 

 

def _cdf(self, x, nu): 

return sc.gammainc(nu, nu*x*x) 

 

def _ppf(self, q, nu): 

return np.sqrt(1.0/nu*sc.gammaincinv(nu, q)) 

 

def _stats(self, nu): 

mu = sc.gamma(nu+0.5)/sc.gamma(nu)/np.sqrt(nu) 

mu2 = 1.0-mu*mu 

g1 = mu * (1 - 4*nu*mu2) / 2.0 / nu / np.power(mu2, 1.5) 

g2 = -6*mu**4*nu + (8*nu-2)*mu**2-2*nu + 1 

g2 /= nu*mu2**2.0 

return mu, mu2, g1, g2 

 

 

nakagami = nakagami_gen(a=0.0, name="nakagami") 

 

 

class ncx2_gen(rv_continuous): 

r"""A non-central chi-squared continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `ncx2` is: 

 

.. math:: 

 

f(x, df, nc) = \exp(-\frac{nc+x}{2}) \frac{1}{2} (x/nc)^{(df-2)/4} 

I[(df-2)/2](\sqrt{nc x}) 

 

for :math:`x > 0`. 

 

`ncx2` takes ``df`` and ``nc`` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self, df, nc): 

return self._random_state.noncentral_chisquare(df, nc, self._size) 

 

def _logpdf(self, x, df, nc): 

return _ncx2_log_pdf(x, df, nc) 

 

def _pdf(self, x, df, nc): 

# ncx2.pdf(x, df, nc) = exp(-(nc+x)/2) * 1/2 * (x/nc)**((df-2)/4) 

# * I[(df-2)/2](sqrt(nc*x)) 

return _ncx2_pdf(x, df, nc) 

 

def _cdf(self, x, df, nc): 

return _ncx2_cdf(x, df, nc) 

 

def _ppf(self, q, df, nc): 

return sc.chndtrix(q, df, nc) 

 

def _stats(self, df, nc): 

val = df + 2.0*nc 

return (df + nc, 

2*val, 

np.sqrt(8)*(val+nc)/val**1.5, 

12.0*(val+2*nc)/val**2.0) 

 

 

ncx2 = ncx2_gen(a=0.0, name='ncx2') 

 

 

class ncf_gen(rv_continuous): 

r"""A non-central F distribution continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `ncf` is: 

 

.. math:: 

 

f(x, n_1, n_2, \lambda) = 

\exp(\frac{\lambda}{2} + \lambda n_1 \frac{x}{2(n_1 x+n_2)}) 

n_1^{n_1/2} n_2^{n_2/2} x^{n_1/2 - 1} \\ 

(n_2+n_1 x)^{-(n_1+n_2)/2} 

\gamma(n_1/2) \gamma(1+n_2/2) \\ 

\frac{L^{\frac{v_1}{2}-1}_{v_2/2} 

(-\lambda v_1 \frac{x}{2(v_1 x+v_2)})} 

{(B(v_1/2, v_2/2) \gamma(\frac{v_1+v_2}{2})} 

 

for :math:`n_1 > 1`, :math:`n_2, \lambda > 0`. Here :math:`n_1` is the 

degrees of freedom in the numerator, :math:`n_2` the degrees of freedom in 

the denominator, :math:`\lambda` the non-centrality parameter, 

:math:`\gamma` is the logarithm of the Gamma function, :math:`L_n^k` is a 

generalized Laguerre polynomial and :math:`B` is the beta function. 

 

`ncf` takes ``df1``, ``df2`` and ``nc`` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self, dfn, dfd, nc): 

return self._random_state.noncentral_f(dfn, dfd, nc, self._size) 

 

def _pdf_skip(self, x, dfn, dfd, nc): 

# ncf.pdf(x, df1, df2, nc) = exp(nc/2 + nc*df1*x/(2*(df1*x+df2))) * 

# df1**(df1/2) * df2**(df2/2) * x**(df1/2-1) * 

# (df2+df1*x)**(-(df1+df2)/2) * 

# gamma(df1/2)*gamma(1+df2/2) * 

# L^{v1/2-1}^{v2/2}(-nc*v1*x/(2*(v1*x+v2))) / 

# (B(v1/2, v2/2) * gamma((v1+v2)/2)) 

n1, n2 = dfn, dfd 

term = -nc/2+nc*n1*x/(2*(n2+n1*x)) + sc.gammaln(n1/2.)+sc.gammaln(1+n2/2.) 

term -= sc.gammaln((n1+n2)/2.0) 

Px = np.exp(term) 

Px *= n1**(n1/2) * n2**(n2/2) * x**(n1/2-1) 

Px *= (n2+n1*x)**(-(n1+n2)/2) 

Px *= sc.assoc_laguerre(-nc*n1*x/(2.0*(n2+n1*x)), n2/2, n1/2-1) 

Px /= sc.beta(n1/2, n2/2) 

# This function does not have a return. Drop it for now, the generic 

# function seems to work OK. 

 

def _cdf(self, x, dfn, dfd, nc): 

return sc.ncfdtr(dfn, dfd, nc, x) 

 

def _ppf(self, q, dfn, dfd, nc): 

return sc.ncfdtri(dfn, dfd, nc, q) 

 

def _munp(self, n, dfn, dfd, nc): 

val = (dfn * 1.0/dfd)**n 

term = sc.gammaln(n+0.5*dfn) + sc.gammaln(0.5*dfd-n) - sc.gammaln(dfd*0.5) 

val *= np.exp(-nc / 2.0+term) 

val *= sc.hyp1f1(n+0.5*dfn, 0.5*dfn, 0.5*nc) 

return val 

 

def _stats(self, dfn, dfd, nc): 

mu = np.where(dfd <= 2, np.inf, dfd / (dfd-2.0)*(1+nc*1.0/dfn)) 

mu2 = np.where(dfd <= 4, np.inf, 2*(dfd*1.0/dfn)**2.0 * 

((dfn+nc/2.0)**2.0 + (dfn+nc)*(dfd-2.0)) / 

((dfd-2.0)**2.0 * (dfd-4.0))) 

return mu, mu2, None, None 

 

 

ncf = ncf_gen(a=0.0, name='ncf') 

 

 

class t_gen(rv_continuous): 

r"""A Student's T continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `t` is: 

 

.. math:: 

 

f(x, df) = \frac{\gamma((df+1)/2)} 

{\sqrt{\pi*df} \gamma(df/2) (1+x^2/df)^{(df+1)/2}} 

 

for ``df > 0``. 

 

`t` takes ``df`` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _rvs(self, df): 

return self._random_state.standard_t(df, size=self._size) 

 

def _pdf(self, x, df): 

# gamma((df+1)/2) 

# t.pdf(x, df) = --------------------------------------------------- 

# sqrt(pi*df) * gamma(df/2) * (1+x**2/df)**((df+1)/2) 

r = np.asarray(df*1.0) 

Px = np.exp(sc.gammaln((r+1)/2)-sc.gammaln(r/2)) 

Px /= np.sqrt(r*np.pi)*(1+(x**2)/r)**((r+1)/2) 

return Px 

 

def _logpdf(self, x, df): 

r = df*1.0 

lPx = sc.gammaln((r+1)/2)-sc.gammaln(r/2) 

lPx -= 0.5*np.log(r*np.pi) + (r+1)/2*np.log(1+(x**2)/r) 

return lPx 

 

def _cdf(self, x, df): 

return sc.stdtr(df, x) 

 

def _sf(self, x, df): 

return sc.stdtr(df, -x) 

 

def _ppf(self, q, df): 

return sc.stdtrit(df, q) 

 

def _isf(self, q, df): 

return -sc.stdtrit(df, q) 

 

def _stats(self, df): 

mu2 = _lazywhere(df > 2, (df,), 

lambda df: df / (df-2.0), 

np.inf) 

g1 = np.where(df > 3, 0.0, np.nan) 

g2 = _lazywhere(df > 4, (df,), 

lambda df: 6.0 / (df-4.0), 

np.nan) 

return 0, mu2, g1, g2 

 

 

t = t_gen(name='t') 

 

 

class nct_gen(rv_continuous): 

r"""A non-central Student's T continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `nct` is: 

 

.. math:: 

 

f(x, df, nc) = \frac{df^{df/2} \gamma(df+1)}{2^{df} 

\exp(nc^2 / 2) (df+x^2)^{df/2} \gamma(df/2)} 

 

for ``df > 0``. 

 

`nct` takes ``df`` and ``nc`` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, df, nc): 

return (df > 0) & (nc == nc) 

 

def _rvs(self, df, nc): 

sz, rndm = self._size, self._random_state 

n = norm.rvs(loc=nc, size=sz, random_state=rndm) 

c2 = chi2.rvs(df, size=sz, random_state=rndm) 

return n * np.sqrt(df) / np.sqrt(c2) 

 

def _pdf(self, x, df, nc): 

# nct.pdf(x, df, nc) = 

# df**(df/2) * gamma(df+1) 

# ---------------------------------------------------- 

# 2**df*exp(nc**2/2) * (df+x**2)**(df/2) * gamma(df/2) 

n = df*1.0 

nc = nc*1.0 

x2 = x*x 

ncx2 = nc*nc*x2 

fac1 = n + x2 

trm1 = n/2.*np.log(n) + sc.gammaln(n+1) 

trm1 -= n*np.log(2)+nc*nc/2.+(n/2.)*np.log(fac1)+sc.gammaln(n/2.) 

Px = np.exp(trm1) 

valF = ncx2 / (2*fac1) 

trm1 = np.sqrt(2)*nc*x*sc.hyp1f1(n/2+1, 1.5, valF) 

trm1 /= np.asarray(fac1*sc.gamma((n+1)/2)) 

trm2 = sc.hyp1f1((n+1)/2, 0.5, valF) 

trm2 /= np.asarray(np.sqrt(fac1)*sc.gamma(n/2+1)) 

Px *= trm1+trm2 

return Px 

 

def _cdf(self, x, df, nc): 

return sc.nctdtr(df, nc, x) 

 

def _ppf(self, q, df, nc): 

return sc.nctdtrit(df, nc, q) 

 

def _stats(self, df, nc, moments='mv'): 

# 

# See D. Hogben, R.S. Pinkham, and M.B. Wilk, 

# 'The moments of the non-central t-distribution' 

# Biometrika 48, p. 465 (2961). 

# e.g. http://www.jstor.org/stable/2332772 (gated) 

# 

mu, mu2, g1, g2 = None, None, None, None 

 

gfac = sc.gamma(df/2.-0.5) / sc.gamma(df/2.) 

c11 = np.sqrt(df/2.) * gfac 

c20 = df / (df-2.) 

c22 = c20 - c11*c11 

mu = np.where(df > 1, nc*c11, np.inf) 

mu2 = np.where(df > 2, c22*nc*nc + c20, np.inf) 

if 's' in moments: 

c33t = df * (7.-2.*df) / (df-2.) / (df-3.) + 2.*c11*c11 

c31t = 3.*df / (df-2.) / (df-3.) 

mu3 = (c33t*nc*nc + c31t) * c11*nc 

g1 = np.where(df > 3, mu3 / np.power(mu2, 1.5), np.nan) 

# kurtosis 

if 'k' in moments: 

c44 = df*df / (df-2.) / (df-4.) 

c44 -= c11*c11 * 2.*df*(5.-df) / (df-2.) / (df-3.) 

c44 -= 3.*c11**4 

c42 = df / (df-4.) - c11*c11 * (df-1.) / (df-3.) 

c42 *= 6.*df / (df-2.) 

c40 = 3.*df*df / (df-2.) / (df-4.) 

 

mu4 = c44 * nc**4 + c42*nc**2 + c40 

g2 = np.where(df > 4, mu4/mu2**2 - 3., np.nan) 

return mu, mu2, g1, g2 

 

 

nct = nct_gen(name="nct") 

 

 

class pareto_gen(rv_continuous): 

r"""A Pareto continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `pareto` is: 

 

.. math:: 

 

f(x, b) = \frac{b}{x^{b+1}} 

 

for :math:`x \ge 1`, :math:`b > 0`. 

 

`pareto` takes :math:`b` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, b): 

# pareto.pdf(x, b) = b / x**(b+1) 

return b * x**(-b-1) 

 

def _cdf(self, x, b): 

return 1 - x**(-b) 

 

def _ppf(self, q, b): 

return pow(1-q, -1.0/b) 

 

def _sf(self, x, b): 

return x**(-b) 

 

def _stats(self, b, moments='mv'): 

mu, mu2, g1, g2 = None, None, None, None 

if 'm' in moments: 

mask = b > 1 

bt = np.extract(mask, b) 

mu = valarray(np.shape(b), value=np.inf) 

np.place(mu, mask, bt / (bt-1.0)) 

if 'v' in moments: 

mask = b > 2 

bt = np.extract(mask, b) 

mu2 = valarray(np.shape(b), value=np.inf) 

np.place(mu2, mask, bt / (bt-2.0) / (bt-1.0)**2) 

if 's' in moments: 

mask = b > 3 

bt = np.extract(mask, b) 

g1 = valarray(np.shape(b), value=np.nan) 

vals = 2 * (bt + 1.0) * np.sqrt(bt - 2.0) / ((bt - 3.0) * np.sqrt(bt)) 

np.place(g1, mask, vals) 

if 'k' in moments: 

mask = b > 4 

bt = np.extract(mask, b) 

g2 = valarray(np.shape(b), value=np.nan) 

vals = (6.0*np.polyval([1.0, 1.0, -6, -2], bt) / 

np.polyval([1.0, -7.0, 12.0, 0.0], bt)) 

np.place(g2, mask, vals) 

return mu, mu2, g1, g2 

 

def _entropy(self, c): 

return 1 + 1.0/c - np.log(c) 

 

 

pareto = pareto_gen(a=1.0, name="pareto") 

 

 

class lomax_gen(rv_continuous): 

r"""A Lomax (Pareto of the second kind) continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The Lomax distribution is a special case of the Pareto distribution, with 

(loc=-1.0). 

 

The probability density function for `lomax` is: 

 

.. math:: 

 

f(x, c) = \frac{c}{(1+x)^{c+1}} 

 

for :math:`x \ge 0`, ``c > 0``. 

 

`lomax` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, c): 

# lomax.pdf(x, c) = c / (1+x)**(c+1) 

return c*1.0/(1.0+x)**(c+1.0) 

 

def _logpdf(self, x, c): 

return np.log(c) - (c+1)*sc.log1p(x) 

 

def _cdf(self, x, c): 

return -sc.expm1(-c*sc.log1p(x)) 

 

def _sf(self, x, c): 

return np.exp(-c*sc.log1p(x)) 

 

def _logsf(self, x, c): 

return -c*sc.log1p(x) 

 

def _ppf(self, q, c): 

return sc.expm1(-sc.log1p(-q)/c) 

 

def _stats(self, c): 

mu, mu2, g1, g2 = pareto.stats(c, loc=-1.0, moments='mvsk') 

return mu, mu2, g1, g2 

 

def _entropy(self, c): 

return 1+1.0/c-np.log(c) 

 

 

lomax = lomax_gen(a=0.0, name="lomax") 

 

 

class pearson3_gen(rv_continuous): 

r"""A pearson type III continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `pearson3` is: 

 

.. math:: 

 

f(x, skew) = \frac{|\beta|}{\gamma(\alpha)} 

(\beta (x - \zeta))^{alpha - 1} \exp(-\beta (x - \zeta)) 

 

where: 

 

.. math:: 

 

\beta = \frac{2}{skew stddev} 

\alpha = (stddev \beta)^2 

\zeta = loc - \frac{\alpha}{\beta} 

 

`pearson3` takes ``skew`` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

References 

---------- 

R.W. Vogel and D.E. McMartin, "Probability Plot Goodness-of-Fit and 

Skewness Estimation Procedures for the Pearson Type 3 Distribution", Water 

Resources Research, Vol.27, 3149-3158 (1991). 

 

L.R. Salvosa, "Tables of Pearson's Type III Function", Ann. Math. Statist., 

Vol.1, 191-198 (1930). 

 

"Using Modern Computing Tools to Fit the Pearson Type III Distribution to 

Aviation Loads Data", Office of Aviation Research (2003). 

 

""" 

def _preprocess(self, x, skew): 

# The real 'loc' and 'scale' are handled in the calling pdf(...). The 

# local variables 'loc' and 'scale' within pearson3._pdf are set to 

# the defaults just to keep them as part of the equations for 

# documentation. 

loc = 0.0 

scale = 1.0 

 

# If skew is small, return _norm_pdf. The divide between pearson3 

# and norm was found by brute force and is approximately a skew of 

# 0.000016. No one, I hope, would actually use a skew value even 

# close to this small. 

norm2pearson_transition = 0.000016 

 

ans, x, skew = np.broadcast_arrays([1.0], x, skew) 

ans = ans.copy() 

 

# mask is True where skew is small enough to use the normal approx. 

mask = np.absolute(skew) < norm2pearson_transition 

invmask = ~mask 

 

beta = 2.0 / (skew[invmask] * scale) 

alpha = (scale * beta)**2 

zeta = loc - alpha / beta 

 

transx = beta * (x[invmask] - zeta) 

return ans, x, transx, mask, invmask, beta, alpha, zeta 

 

def _argcheck(self, skew): 

# The _argcheck function in rv_continuous only allows positive 

# arguments. The skew argument for pearson3 can be zero (which I want 

# to handle inside pearson3._pdf) or negative. So just return True 

# for all skew args. 

return np.ones(np.shape(skew), dtype=bool) 

 

def _stats(self, skew): 

_, _, _, _, _, beta, alpha, zeta = ( 

self._preprocess([1], skew)) 

m = zeta + alpha / beta 

v = alpha / (beta**2) 

s = 2.0 / (alpha**0.5) * np.sign(beta) 

k = 6.0 / alpha 

return m, v, s, k 

 

def _pdf(self, x, skew): 

# pearson3.pdf(x, skew) = abs(beta) / gamma(alpha) * 

# (beta * (x - zeta))**(alpha - 1) * exp(-beta*(x - zeta)) 

# Do the calculation in _logpdf since helps to limit 

# overflow/underflow problems 

ans = np.exp(self._logpdf(x, skew)) 

if ans.ndim == 0: 

if np.isnan(ans): 

return 0.0 

return ans 

ans[np.isnan(ans)] = 0.0 

return ans 

 

def _logpdf(self, x, skew): 

# PEARSON3 logpdf GAMMA logpdf 

# np.log(abs(beta)) 

# + (alpha - 1)*np.log(beta*(x - zeta)) + (a - 1)*np.log(x) 

# - beta*(x - zeta) - x 

# - sc.gammalnalpha) - sc.gammalna) 

ans, x, transx, mask, invmask, beta, alpha, _ = ( 

self._preprocess(x, skew)) 

 

ans[mask] = np.log(_norm_pdf(x[mask])) 

ans[invmask] = np.log(abs(beta)) + gamma._logpdf(transx, alpha) 

return ans 

 

def _cdf(self, x, skew): 

ans, x, transx, mask, invmask, _, alpha, _ = ( 

self._preprocess(x, skew)) 

 

ans[mask] = _norm_cdf(x[mask]) 

ans[invmask] = gamma._cdf(transx, alpha) 

return ans 

 

def _rvs(self, skew): 

skew = broadcast_to(skew, self._size) 

ans, _, _, mask, invmask, beta, alpha, zeta = ( 

self._preprocess([0], skew)) 

 

nsmall = mask.sum() 

nbig = mask.size - nsmall 

ans[mask] = self._random_state.standard_normal(nsmall) 

ans[invmask] = (self._random_state.standard_gamma(alpha, nbig)/beta + 

zeta) 

 

if self._size == (): 

ans = ans[0] 

return ans 

 

def _ppf(self, q, skew): 

ans, q, _, mask, invmask, beta, alpha, zeta = ( 

self._preprocess(q, skew)) 

ans[mask] = _norm_ppf(q[mask]) 

ans[invmask] = sc.gammaincinv(alpha, q[invmask])/beta + zeta 

return ans 

 

 

pearson3 = pearson3_gen(name="pearson3") 

 

 

class powerlaw_gen(rv_continuous): 

r"""A power-function continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `powerlaw` is: 

 

.. math:: 

 

f(x, a) = a x^{a-1} 

 

for :math:`0 \le x \le 1`, :math:`a > 0`. 

 

`powerlaw` takes :math:`a` as a shape parameter. 

 

%(after_notes)s 

 

`powerlaw` is a special case of `beta` with ``b == 1``. 

 

%(example)s 

 

""" 

def _pdf(self, x, a): 

# powerlaw.pdf(x, a) = a * x**(a-1) 

return a*x**(a-1.0) 

 

def _logpdf(self, x, a): 

return np.log(a) + sc.xlogy(a - 1, x) 

 

def _cdf(self, x, a): 

return x**(a*1.0) 

 

def _logcdf(self, x, a): 

return a*np.log(x) 

 

def _ppf(self, q, a): 

return pow(q, 1.0/a) 

 

def _stats(self, a): 

return (a / (a + 1.0), 

a / (a + 2.0) / (a + 1.0) ** 2, 

-2.0 * ((a - 1.0) / (a + 3.0)) * np.sqrt((a + 2.0) / a), 

6 * np.polyval([1, -1, -6, 2], a) / (a * (a + 3.0) * (a + 4))) 

 

def _entropy(self, a): 

return 1 - 1.0/a - np.log(a) 

 

 

powerlaw = powerlaw_gen(a=0.0, b=1.0, name="powerlaw") 

 

 

class powerlognorm_gen(rv_continuous): 

r"""A power log-normal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `powerlognorm` is: 

 

.. math:: 

 

f(x, c, s) = \frac{c}{x s} \phi(\log(x)/s) 

(\Phi(-\log(x)/s))^{c-1} 

 

where :math:`\phi` is the normal pdf, and :math:`\Phi` is the normal cdf, 

and :math:`x > 0`, :math:`s, c > 0`. 

 

`powerlognorm` takes :math:`c` and :math:`s` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _pdf(self, x, c, s): 

# powerlognorm.pdf(x, c, s) = c / (x*s) * phi(log(x)/s) * 

# (Phi(-log(x)/s))**(c-1), 

return (c/(x*s) * _norm_pdf(np.log(x)/s) * 

pow(_norm_cdf(-np.log(x)/s), c*1.0-1.0)) 

 

def _cdf(self, x, c, s): 

return 1.0 - pow(_norm_cdf(-np.log(x)/s), c*1.0) 

 

def _ppf(self, q, c, s): 

return np.exp(-s * _norm_ppf(pow(1.0 - q, 1.0 / c))) 

 

 

powerlognorm = powerlognorm_gen(a=0.0, name="powerlognorm") 

 

 

class powernorm_gen(rv_continuous): 

r"""A power normal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `powernorm` is: 

 

.. math:: 

 

f(x, c) = c \phi(x) (\Phi(-x))^{c-1} 

 

where :math:`\phi` is the normal pdf, and :math:`\Phi` is the normal cdf, 

and :math:`x > 0`, :math:`c > 0`. 

 

`powernorm` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, c): 

# powernorm.pdf(x, c) = c * phi(x) * (Phi(-x))**(c-1) 

return c*_norm_pdf(x) * (_norm_cdf(-x)**(c-1.0)) 

 

def _logpdf(self, x, c): 

return np.log(c) + _norm_logpdf(x) + (c-1)*_norm_logcdf(-x) 

 

def _cdf(self, x, c): 

return 1.0-_norm_cdf(-x)**(c*1.0) 

 

def _ppf(self, q, c): 

return -_norm_ppf(pow(1.0 - q, 1.0 / c)) 

 

 

powernorm = powernorm_gen(name='powernorm') 

 

 

class rdist_gen(rv_continuous): 

r"""An R-distributed continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `rdist` is: 

 

.. math:: 

 

f(x, c) = \frac{(1-x^2)^{c/2-1}}{B(1/2, c/2)} 

 

for :math:`-1 \le x \le 1`, :math:`c > 0`. 

 

`rdist` takes :math:`c` as a shape parameter. 

 

This distribution includes the following distribution kernels as 

special cases:: 

 

c = 2: uniform 

c = 4: Epanechnikov (parabolic) 

c = 6: quartic (biweight) 

c = 8: triweight 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x, c): 

# rdist.pdf(x, c) = (1-x**2)**(c/2-1) / B(1/2, c/2) 

return np.power((1.0 - x**2), c / 2.0 - 1) / sc.beta(0.5, c / 2.0) 

 

def _cdf(self, x, c): 

term1 = x / sc.beta(0.5, c / 2.0) 

res = 0.5 + term1 * sc.hyp2f1(0.5, 1 - c / 2.0, 1.5, x**2) 

# There's an issue with hyp2f1, it returns nans near x = +-1, c > 100. 

# Use the generic implementation in that case. See gh-1285 for 

# background. 

if np.any(np.isnan(res)): 

return rv_continuous._cdf(self, x, c) 

return res 

 

def _munp(self, n, c): 

numerator = (1 - (n % 2)) * sc.beta((n + 1.0) / 2, c / 2.0) 

return numerator / sc.beta(1. / 2, c / 2.) 

 

 

rdist = rdist_gen(a=-1.0, b=1.0, name="rdist") 

 

 

class rayleigh_gen(rv_continuous): 

r"""A Rayleigh continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `rayleigh` is: 

 

.. math:: 

 

f(r) = r \exp(-r^2/2) 

 

for :math:`x \ge 0`. 

 

`rayleigh` is a special case of `chi` with ``df == 2``. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _rvs(self): 

return chi.rvs(2, size=self._size, random_state=self._random_state) 

 

def _pdf(self, r): 

# rayleigh.pdf(r) = r * exp(-r**2/2) 

return np.exp(self._logpdf(r)) 

 

def _logpdf(self, r): 

return np.log(r) - 0.5 * r * r 

 

def _cdf(self, r): 

return -sc.expm1(-0.5 * r**2) 

 

def _ppf(self, q): 

return np.sqrt(-2 * sc.log1p(-q)) 

 

def _sf(self, r): 

return np.exp(self._logsf(r)) 

 

def _logsf(self, r): 

return -0.5 * r * r 

 

def _isf(self, q): 

return np.sqrt(-2 * np.log(q)) 

 

def _stats(self): 

val = 4 - np.pi 

return (np.sqrt(np.pi/2), 

val/2, 

2*(np.pi-3)*np.sqrt(np.pi)/val**1.5, 

6*np.pi/val-16/val**2) 

 

def _entropy(self): 

return _EULER/2.0 + 1 - 0.5*np.log(2) 

 

 

rayleigh = rayleigh_gen(a=0.0, name="rayleigh") 

 

 

class reciprocal_gen(rv_continuous): 

r"""A reciprocal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `reciprocal` is: 

 

.. math:: 

 

f(x, a, b) = \frac{1}{x \log(b/a)} 

 

for :math:`a \le x \le b`, :math:`a, b > 0`. 

 

`reciprocal` takes :math:`a` and :math:`b` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, a, b): 

self.a = a 

self.b = b 

self.d = np.log(b*1.0 / a) 

return (a > 0) & (b > 0) & (b > a) 

 

def _pdf(self, x, a, b): 

# reciprocal.pdf(x, a, b) = 1 / (x*log(b/a)) 

return 1.0 / (x * self.d) 

 

def _logpdf(self, x, a, b): 

return -np.log(x) - np.log(self.d) 

 

def _cdf(self, x, a, b): 

return (np.log(x)-np.log(a)) / self.d 

 

def _ppf(self, q, a, b): 

return a*pow(b*1.0/a, q) 

 

def _munp(self, n, a, b): 

return 1.0/self.d / n * (pow(b*1.0, n) - pow(a*1.0, n)) 

 

def _entropy(self, a, b): 

return 0.5*np.log(a*b)+np.log(np.log(b/a)) 

 

 

reciprocal = reciprocal_gen(name="reciprocal") 

 

 

class rice_gen(rv_continuous): 

r"""A Rice continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `rice` is: 

 

.. math:: 

 

f(x, b) = x \exp(- \frac{x^2 + b^2}{2}) I[0](x b) 

 

for :math:`x > 0`, :math:`b > 0`. 

 

`rice` takes :math:`b` as a shape parameter. 

 

%(after_notes)s 

 

The Rice distribution describes the length, :math:`r`, of a 2-D vector with 

components :math:`(U+u, V+v)`, where :math:`U, V` are constant, :math:`u, 

v` are independent Gaussian random variables with standard deviation 

:math:`s`. Let :math:`R = \sqrt{U^2 + V^2}`. Then the pdf of :math:`r` is 

``rice.pdf(x, R/s, scale=s)``. 

 

%(example)s 

 

""" 

def _argcheck(self, b): 

return b >= 0 

 

def _rvs(self, b): 

# http://en.wikipedia.org/wiki/Rice_distribution 

t = b/np.sqrt(2) + self._random_state.standard_normal(size=(2,) + 

self._size) 

return np.sqrt((t*t).sum(axis=0)) 

 

def _cdf(self, x, b): 

return sc.chndtr(np.square(x), 2, np.square(b)) 

 

def _ppf(self, q, b): 

return np.sqrt(sc.chndtrix(q, 2, np.square(b))) 

 

def _pdf(self, x, b): 

# rice.pdf(x, b) = x * exp(-(x**2+b**2)/2) * I[0](x*b) 

# 

# We use (x**2 + b**2)/2 = ((x-b)**2)/2 + xb. 

# The factor of np.exp(-xb) is then included in the i0e function 

# in place of the modified Bessel function, i0, improving 

# numerical stability for large values of xb. 

return x * np.exp(-(x-b)*(x-b)/2.0) * sc.i0e(x*b) 

 

def _munp(self, n, b): 

nd2 = n/2.0 

n1 = 1 + nd2 

b2 = b*b/2.0 

return (2.0**(nd2) * np.exp(-b2) * sc.gamma(n1) * 

sc.hyp1f1(n1, 1, b2)) 

 

 

rice = rice_gen(a=0.0, name="rice") 

 

 

# FIXME: PPF does not work. 

class recipinvgauss_gen(rv_continuous): 

r"""A reciprocal inverse Gaussian continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `recipinvgauss` is: 

 

.. math:: 

 

f(x, \mu) = \frac{1}{\sqrt{2\pi x}} \frac{\exp(-(1-\mu x)^2}{2x\mu^2)} 

 

for :math:`x \ge 0`. 

 

`recipinvgauss` takes :math:`\mu` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

 

def _pdf(self, x, mu): 

# recipinvgauss.pdf(x, mu) = 

# 1/sqrt(2*pi*x) * exp(-(1-mu*x)**2/(2*x*mu**2)) 

return 1.0/np.sqrt(2*np.pi*x)*np.exp(-(1-mu*x)**2.0 / (2*x*mu**2.0)) 

 

def _logpdf(self, x, mu): 

return -(1-mu*x)**2.0 / (2*x*mu**2.0) - 0.5*np.log(2*np.pi*x) 

 

def _cdf(self, x, mu): 

trm1 = 1.0/mu - x 

trm2 = 1.0/mu + x 

isqx = 1.0/np.sqrt(x) 

return 1.0-_norm_cdf(isqx*trm1)-np.exp(2.0/mu)*_norm_cdf(-isqx*trm2) 

 

def _rvs(self, mu): 

return 1.0/self._random_state.wald(mu, 1.0, size=self._size) 

 

 

recipinvgauss = recipinvgauss_gen(a=0.0, name='recipinvgauss') 

 

 

class semicircular_gen(rv_continuous): 

r"""A semicircular continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `semicircular` is: 

 

.. math:: 

 

f(x) = \frac{2}{\pi} \sqrt{1-x^2} 

 

for :math:`-1 \le x \le 1`. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _pdf(self, x): 

# semicircular.pdf(x) = 2/pi * sqrt(1-x**2) 

return 2.0/np.pi*np.sqrt(1-x*x) 

 

def _cdf(self, x): 

return 0.5+1.0/np.pi*(x*np.sqrt(1-x*x) + np.arcsin(x)) 

 

def _stats(self): 

return 0, 0.25, 0, -1.0 

 

def _entropy(self): 

return 0.64472988584940017414 

 

 

semicircular = semicircular_gen(a=-1.0, b=1.0, name="semicircular") 

 

 

class skew_norm_gen(rv_continuous): 

r"""A skew-normal random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The pdf is:: 

 

skewnorm.pdf(x, a) = 2 * norm.pdf(x) * norm.cdf(a*x) 

 

`skewnorm` takes :math:`a` as a skewness parameter 

When ``a = 0`` the distribution is identical to a normal distribution. 

rvs implements the method of [1]_. 

 

%(after_notes)s 

 

%(example)s 

 

References 

---------- 

.. [1] A. Azzalini and A. Capitanio (1999). Statistical applications of the 

multivariate skew-normal distribution. J. Roy. Statist. Soc., B 61, 579-602. 

http://azzalini.stat.unipd.it/SN/faq-r.html 

 

""" 

def _argcheck(self, a): 

return np.isfinite(a) 

 

def _pdf(self, x, a): 

return 2.*_norm_pdf(x)*_norm_cdf(a*x) 

 

def _cdf_single(self, x, *args): 

if x <= 0: 

cdf = integrate.quad(self._pdf, self.a, x, args=args)[0] 

else: 

t1 = integrate.quad(self._pdf, self.a, 0, args=args)[0] 

t2 = integrate.quad(self._pdf, 0, x, args=args)[0] 

cdf = t1 + t2 

if cdf > 1: 

# Presumably numerical noise, e.g. 1.0000000000000002 

cdf = 1.0 

return cdf 

 

def _sf(self, x, a): 

return self._cdf(-x, -a) 

 

def _rvs(self, a): 

u0 = self._random_state.normal(size=self._size) 

v = self._random_state.normal(size=self._size) 

d = a/np.sqrt(1 + a**2) 

u1 = d*u0 + v*np.sqrt(1 - d**2) 

return np.where(u0 >= 0, u1, -u1) 

 

def _stats(self, a, moments='mvsk'): 

output = [None, None, None, None] 

const = np.sqrt(2/np.pi) * a/np.sqrt(1 + a**2) 

 

if 'm' in moments: 

output[0] = const 

if 'v' in moments: 

output[1] = 1 - const**2 

if 's' in moments: 

output[2] = ((4 - np.pi)/2) * (const/np.sqrt(1 - const**2))**3 

if 'k' in moments: 

output[3] = (2*(np.pi - 3)) * (const**4/(1 - const**2)**2) 

 

return output 

 

 

skewnorm = skew_norm_gen(name='skewnorm') 

 

 

class trapz_gen(rv_continuous): 

r"""A trapezoidal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The trapezoidal distribution can be represented with an up-sloping line 

from ``loc`` to ``(loc + c*scale)``, then constant to ``(loc + d*scale)`` 

and then downsloping from ``(loc + d*scale)`` to ``(loc+scale)``. 

 

`trapz` takes :math:`c` and :math:`d` as shape parameters. 

 

%(after_notes)s 

 

The standard form is in the range [0, 1] with c the mode. 

The location parameter shifts the start to `loc`. 

The scale parameter changes the width from 1 to `scale`. 

 

%(example)s 

 

""" 

def _argcheck(self, c, d): 

return (c >= 0) & (c <= 1) & (d >= 0) & (d <= 1) & (d >= c) 

 

def _pdf(self, x, c, d): 

u = 2 / (d-c+1) 

 

return _lazyselect([x < c, 

(c <= x) & (x <= d), 

x > d], 

[lambda x, c, d, u: u * x / c, 

lambda x, c, d, u: u, 

lambda x, c, d, u: u * (1-x) / (1-d)], 

(x, c, d, u)) 

 

def _cdf(self, x, c, d): 

return _lazyselect([x < c, 

(c <= x) & (x <= d), 

x > d], 

[lambda x, c, d: x**2 / c / (d-c+1), 

lambda x, c, d: (c + 2 * (x-c)) / (d-c+1), 

lambda x, c, d: 1-((1-x) ** 2 

/ (d-c+1) / (1-d))], 

(x, c, d)) 

 

def _ppf(self, q, c, d): 

qc, qd = self._cdf(c, c, d), self._cdf(d, c, d) 

condlist = [q < qc, q <= qd, q > qd] 

choicelist = [np.sqrt(q * c * (1 + d - c)), 

0.5 * q * (1 + d - c) + 0.5 * c, 

1 - np.sqrt((1 - q) * (d - c + 1) * (1 - d))] 

return np.select(condlist, choicelist) 

 

 

trapz = trapz_gen(a=0.0, b=1.0, name="trapz") 

 

 

class triang_gen(rv_continuous): 

r"""A triangular continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The triangular distribution can be represented with an up-sloping line from 

``loc`` to ``(loc + c*scale)`` and then downsloping for ``(loc + c*scale)`` 

to ``(loc+scale)``. 

 

`triang` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

The standard form is in the range [0, 1] with c the mode. 

The location parameter shifts the start to `loc`. 

The scale parameter changes the width from 1 to `scale`. 

 

%(example)s 

 

""" 

def _rvs(self, c): 

return self._random_state.triangular(0, c, 1, self._size) 

 

def _argcheck(self, c): 

return (c >= 0) & (c <= 1) 

 

def _pdf(self, x, c): 

# 0: edge case where c=0 

# 1: generalised case for x < c, don't use x <= c, as it doesn't cope 

# with c = 0. 

# 2: generalised case for x >= c, but doesn't cope with c = 1 

# 3: edge case where c=1 

r = _lazyselect([c == 0, 

x < c, 

(x >= c) & (c != 1), 

c == 1], 

[lambda x, c: 2 - 2 * x, 

lambda x, c: 2 * x / c, 

lambda x, c: 2 * (1 - x) / (1 - c), 

lambda x, c: 2 * x], 

(x, c)) 

return r 

 

def _cdf(self, x, c): 

r = _lazyselect([c == 0, 

x < c, 

(x >= c) & (c != 1), 

c == 1], 

[lambda x, c: 2*x - x*x, 

lambda x, c: x * x / c, 

lambda x, c: (x*x - 2*x + c) / (c-1), 

lambda x, c: x * x], 

(x, c)) 

return r 

 

def _ppf(self, q, c): 

return np.where(q < c, np.sqrt(c * q), 1-np.sqrt((1-c) * (1-q))) 

 

def _stats(self, c): 

return ((c+1.0)/3.0, 

(1.0-c+c*c)/18, 

np.sqrt(2)*(2*c-1)*(c+1)*(c-2) / (5*np.power((1.0-c+c*c), 1.5)), 

-3.0/5.0) 

 

def _entropy(self, c): 

return 0.5-np.log(2) 

 

 

triang = triang_gen(a=0.0, b=1.0, name="triang") 

 

 

class truncexpon_gen(rv_continuous): 

r"""A truncated exponential continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `truncexpon` is: 

 

.. math:: 

 

f(x, b) = \frac{\exp(-x)}{1 - \exp(-b)} 

 

for :math:`0 < x < b`. 

 

`truncexpon` takes :math:`b` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, b): 

self.b = b 

return b > 0 

 

def _pdf(self, x, b): 

# truncexpon.pdf(x, b) = exp(-x) / (1-exp(-b)) 

return np.exp(-x)/(-sc.expm1(-b)) 

 

def _logpdf(self, x, b): 

return -x - np.log(-sc.expm1(-b)) 

 

def _cdf(self, x, b): 

return sc.expm1(-x)/sc.expm1(-b) 

 

def _ppf(self, q, b): 

return -sc.log1p(q*sc.expm1(-b)) 

 

def _munp(self, n, b): 

# wrong answer with formula, same as in continuous.pdf 

# return sc.gamman+1)-sc.gammainc1+n, b) 

if n == 1: 

return (1-(b+1)*np.exp(-b))/(-sc.expm1(-b)) 

elif n == 2: 

return 2*(1-0.5*(b*b+2*b+2)*np.exp(-b))/(-sc.expm1(-b)) 

else: 

# return generic for higher moments 

# return rv_continuous._mom1_sc(self, n, b) 

return self._mom1_sc(n, b) 

 

def _entropy(self, b): 

eB = np.exp(b) 

return np.log(eB-1)+(1+eB*(b-1.0))/(1.0-eB) 

 

 

truncexpon = truncexpon_gen(a=0.0, name='truncexpon') 

 

 

class truncnorm_gen(rv_continuous): 

r"""A truncated normal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The standard form of this distribution is a standard normal truncated to 

the range [a, b] --- notice that a and b are defined over the domain of the 

standard normal. To convert clip values for a specific mean and standard 

deviation, use:: 

 

a, b = (myclip_a - my_mean) / my_std, (myclip_b - my_mean) / my_std 

 

`truncnorm` takes :math:`a` and :math:`b` as shape parameters. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, a, b): 

self.a = a 

self.b = b 

self._nb = _norm_cdf(b) 

self._na = _norm_cdf(a) 

self._sb = _norm_sf(b) 

self._sa = _norm_sf(a) 

self._delta = np.where(self.a > 0, 

-(self._sb - self._sa), 

self._nb - self._na) 

self._logdelta = np.log(self._delta) 

return a != b 

 

def _pdf(self, x, a, b): 

return _norm_pdf(x) / self._delta 

 

def _logpdf(self, x, a, b): 

return _norm_logpdf(x) - self._logdelta 

 

def _cdf(self, x, a, b): 

return (_norm_cdf(x) - self._na) / self._delta 

 

def _ppf(self, q, a, b): 

# XXX Use _lazywhere... 

ppf = np.where(self.a > 0, 

_norm_isf(q*self._sb + self._sa*(1.0-q)), 

_norm_ppf(q*self._nb + self._na*(1.0-q))) 

return ppf 

 

def _stats(self, a, b): 

nA, nB = self._na, self._nb 

d = nB - nA 

pA, pB = _norm_pdf(a), _norm_pdf(b) 

mu = (pA - pB) / d # correction sign 

mu2 = 1 + (a*pA - b*pB) / d - mu*mu 

return mu, mu2, None, None 

 

 

truncnorm = truncnorm_gen(name='truncnorm') 

 

 

# FIXME: RVS does not work. 

class tukeylambda_gen(rv_continuous): 

r"""A Tukey-Lamdba continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

A flexible distribution, able to represent and interpolate between the 

following distributions: 

 

- Cauchy (lam=-1) 

- logistic (lam=0.0) 

- approx Normal (lam=0.14) 

- u-shape (lam = 0.5) 

- uniform from -1 to 1 (lam = 1) 

 

`tukeylambda` takes ``lam`` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, lam): 

return np.ones(np.shape(lam), dtype=bool) 

 

def _pdf(self, x, lam): 

Fx = np.asarray(sc.tklmbda(x, lam)) 

Px = Fx**(lam-1.0) + (np.asarray(1-Fx))**(lam-1.0) 

Px = 1.0/np.asarray(Px) 

return np.where((lam <= 0) | (abs(x) < 1.0/np.asarray(lam)), Px, 0.0) 

 

def _cdf(self, x, lam): 

return sc.tklmbda(x, lam) 

 

def _ppf(self, q, lam): 

return sc.boxcox(q, lam) - sc.boxcox1p(-q, lam) 

 

def _stats(self, lam): 

return 0, _tlvar(lam), 0, _tlkurt(lam) 

 

def _entropy(self, lam): 

def integ(p): 

return np.log(pow(p, lam-1)+pow(1-p, lam-1)) 

return integrate.quad(integ, 0, 1)[0] 

 

 

tukeylambda = tukeylambda_gen(name='tukeylambda') 

 

 

class FitUniformFixedScaleDataError(FitDataError): 

def __init__(self, ptp, fscale): 

self.args = ( 

"Invalid values in `data`. Maximum likelihood estimation with " 

"the uniform distribution and fixed scale requires that " 

"data.ptp() <= fscale, but data.ptp() = %r and fscale = %r." % 

(ptp, fscale), 

) 

 

 

class uniform_gen(rv_continuous): 

r"""A uniform continuous random variable. 

 

This distribution is constant between `loc` and ``loc + scale``. 

 

%(before_notes)s 

 

%(example)s 

 

""" 

def _rvs(self): 

return self._random_state.uniform(0.0, 1.0, self._size) 

 

def _pdf(self, x): 

return 1.0*(x == x) 

 

def _cdf(self, x): 

return x 

 

def _ppf(self, q): 

return q 

 

def _stats(self): 

return 0.5, 1.0/12, 0, -1.2 

 

def _entropy(self): 

return 0.0 

 

def fit(self, data, *args, **kwds): 

""" 

Maximum likelihood estimate for the location and scale parameters. 

 

`uniform.fit` uses only the following parameters. Because exact 

formulas are used, the parameters related to optimization that are 

available in the `fit` method of other distributions are ignored 

here. The only positional argument accepted is `data`. 

 

Parameters 

---------- 

data : array_like 

Data to use in calculating the maximum likelihood estimate. 

floc : float, optional 

Hold the location parameter fixed to the specified value. 

fscale : float, optional 

Hold the scale parameter fixed to the specified value. 

 

Returns 

------- 

loc, scale : float 

Maximum likelihood estimates for the location and scale. 

 

Notes 

----- 

An error is raised if `floc` is given and any values in `data` are 

less than `floc`, or if `fscale` is given and `fscale` is less 

than ``data.max() - data.min()``. An error is also raised if both 

`floc` and `fscale` are given. 

 

Examples 

-------- 

>>> from scipy.stats import uniform 

 

We'll fit the uniform distribution to `x`: 

 

>>> x = np.array([2, 2.5, 3.1, 9.5, 13.0]) 

 

For a uniform distribution MLE, the location is the minimum of the 

data, and the scale is the maximum minus the minimum. 

 

>>> loc, scale = uniform.fit(x) 

>>> loc 

2.0 

>>> scale 

11.0 

 

If we know the data comes from a uniform distribution where the support 

starts at 0, we can use `floc=0`: 

 

>>> loc, scale = uniform.fit(x, floc=0) 

>>> loc 

0.0 

>>> scale 

13.0 

 

Alternatively, if we know the length of the support is 12, we can use 

`fscale=12`: 

 

>>> loc, scale = uniform.fit(x, fscale=12) 

>>> loc 

1.5 

>>> scale 

12.0 

 

In that last example, the support interval is [1.5, 13.5]. This 

solution is not unique. For example, the distribution with ``loc=2`` 

and ``scale=12`` has the same likelihood as the one above. When 

`fscale` is given and it is larger than ``data.max() - data.min()``, 

the parameters returned by the `fit` method center the support over 

the interval ``[data.min(), data.max()]``. 

 

""" 

if len(args) > 0: 

raise TypeError("Too many arguments.") 

 

floc = kwds.pop('floc', None) 

fscale = kwds.pop('fscale', None) 

 

# Ignore the optimizer-related keyword arguments, if given. 

kwds.pop('loc', None) 

kwds.pop('scale', None) 

kwds.pop('optimizer', None) 

if kwds: 

raise TypeError("Unknown arguments: %s." % kwds) 

 

if floc is not None and fscale is not None: 

# This check is for consistency with `rv_continuous.fit`. 

raise ValueError("All parameters fixed. There is nothing to " 

"optimize.") 

 

data = np.asarray(data) 

 

# MLE for the uniform distribution 

# -------------------------------- 

# The PDF is 

# 

# f(x, loc, scale) = {1/scale for loc <= x <= loc + scale 

# {0 otherwise} 

# 

# The likelihood function is 

# L(x, loc, scale) = (1/scale)**n 

# where n is len(x), assuming loc <= x <= loc + scale for all x. 

# The log-likelihood is 

# l(x, loc, scale) = -n*log(scale) 

# The log-likelihood is maximized by making scale as small as possible, 

# while keeping loc <= x <= loc + scale. So if neither loc nor scale 

# are fixed, the log-likelihood is maximized by choosing 

# loc = x.min() 

# scale = x.ptp() 

# If loc is fixed, it must be less than or equal to x.min(), and then 

# the scale is 

# scale = x.max() - loc 

# If scale is fixed, it must not be less than x.ptp(). If scale is 

# greater than x.ptp(), the solution is not unique. Note that the 

# likelihood does not depend on loc, except for the requirement that 

# loc <= x <= loc + scale. All choices of loc for which 

# x.max() - scale <= loc <= x.min() 

# have the same log-likelihood. In this case, we choose loc such that 

# the support is centered over the interval [data.min(), data.max()]: 

# loc = x.min() = 0.5*(scale - x.ptp()) 

 

if fscale is None: 

# scale is not fixed. 

if floc is None: 

# loc is not fixed, scale is not fixed. 

loc = data.min() 

scale = data.ptp() 

else: 

# loc is fixed, scale is not fixed. 

loc = floc 

scale = data.max() - loc 

if data.min() < loc: 

raise FitDataError("uniform", lower=loc, upper=loc + scale) 

else: 

# loc is not fixed, scale is fixed. 

ptp = data.ptp() 

if ptp > fscale: 

raise FitUniformFixedScaleDataError(ptp=ptp, fscale=fscale) 

# If ptp < fscale, the ML estimate is not unique; see the comments 

# above. We choose the distribution for which the support is 

# centered over the interval [data.min(), data.max()]. 

loc = data.min() - 0.5*(fscale - ptp) 

scale = fscale 

 

# We expect the return values to be floating point, so ensure it 

# by explicitly converting to float. 

return float(loc), float(scale) 

 

 

uniform = uniform_gen(a=0.0, b=1.0, name='uniform') 

 

 

class vonmises_gen(rv_continuous): 

r"""A Von Mises continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

If `x` is not in range or `loc` is not in range it assumes they are angles 

and converts them to [-\pi, \pi] equivalents. 

 

The probability density function for `vonmises` is: 

 

.. math:: 

 

f(x, \kappa) = \frac{ \exp(\kappa \cos(x)) }{ 2 \pi I[0](\kappa) } 

 

for :math:`-\pi \le x \le \pi`, :math:`\kappa > 0`. 

 

`vonmises` takes :math:`\kappa` as a shape parameter. 

 

%(after_notes)s 

 

See Also 

-------- 

vonmises_line : The same distribution, defined on a [-\pi, \pi] segment 

of the real line. 

 

%(example)s 

 

""" 

def _rvs(self, kappa): 

return self._random_state.vonmises(0.0, kappa, size=self._size) 

 

def _pdf(self, x, kappa): 

# vonmises.pdf(x, \kappa) = exp(\kappa * cos(x)) / (2*pi*I[0](\kappa)) 

return np.exp(kappa * np.cos(x)) / (2*np.pi*sc.i0(kappa)) 

 

def _cdf(self, x, kappa): 

return _stats.von_mises_cdf(kappa, x) 

 

def _stats_skip(self, kappa): 

return 0, None, 0, None 

 

def _entropy(self, kappa): 

return (-kappa * sc.i1(kappa) / sc.i0(kappa) + 

np.log(2 * np.pi * sc.i0(kappa))) 

 

 

vonmises = vonmises_gen(name='vonmises') 

vonmises_line = vonmises_gen(a=-np.pi, b=np.pi, name='vonmises_line') 

 

 

class wald_gen(invgauss_gen): 

r"""A Wald continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `wald` is: 

 

.. math:: 

 

f(x) = \frac{1}{\sqrt{2\pi x^3}} \exp(- \frac{ (x-1)^2 }{ 2x }) 

 

for :math:`x > 0`. 

 

`wald` is a special case of `invgauss` with ``mu == 1``. 

 

%(after_notes)s 

 

%(example)s 

""" 

_support_mask = rv_continuous._open_support_mask 

 

def _rvs(self): 

return self._random_state.wald(1.0, 1.0, size=self._size) 

 

def _pdf(self, x): 

# wald.pdf(x) = 1/sqrt(2*pi*x**3) * exp(-(x-1)**2/(2*x)) 

return invgauss._pdf(x, 1.0) 

 

def _logpdf(self, x): 

return invgauss._logpdf(x, 1.0) 

 

def _cdf(self, x): 

return invgauss._cdf(x, 1.0) 

 

def _stats(self): 

return 1.0, 1.0, 3.0, 15.0 

 

 

wald = wald_gen(a=0.0, name="wald") 

 

 

class wrapcauchy_gen(rv_continuous): 

r"""A wrapped Cauchy continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `wrapcauchy` is: 

 

.. math:: 

 

f(x, c) = \frac{1-c^2}{2\pi (1+c^2 - 2c \cos(x))} 

 

for :math:`0 \le x \le 2\pi`, :math:`0 < c < 1`. 

 

`wrapcauchy` takes :math:`c` as a shape parameter. 

 

%(after_notes)s 

 

%(example)s 

 

""" 

def _argcheck(self, c): 

return (c > 0) & (c < 1) 

 

def _pdf(self, x, c): 

# wrapcauchy.pdf(x, c) = (1-c**2) / (2*pi*(1+c**2-2*c*cos(x))) 

return (1.0-c*c)/(2*np.pi*(1+c*c-2*c*np.cos(x))) 

 

def _cdf(self, x, c): 

output = np.zeros(x.shape, dtype=x.dtype) 

val = (1.0+c)/(1.0-c) 

c1 = x < np.pi 

c2 = 1-c1 

xp = np.extract(c1, x) 

xn = np.extract(c2, x) 

if np.any(xn): 

valn = np.extract(c2, np.ones_like(x)*val) 

xn = 2*np.pi - xn 

yn = np.tan(xn/2.0) 

on = 1.0-1.0/np.pi*np.arctan(valn*yn) 

np.place(output, c2, on) 

if np.any(xp): 

valp = np.extract(c1, np.ones_like(x)*val) 

yp = np.tan(xp/2.0) 

op = 1.0/np.pi*np.arctan(valp*yp) 

np.place(output, c1, op) 

return output 

 

def _ppf(self, q, c): 

val = (1.0-c)/(1.0+c) 

rcq = 2*np.arctan(val*np.tan(np.pi*q)) 

rcmq = 2*np.pi-2*np.arctan(val*np.tan(np.pi*(1-q))) 

return np.where(q < 1.0/2, rcq, rcmq) 

 

def _entropy(self, c): 

return np.log(2*np.pi*(1-c*c)) 

 

 

wrapcauchy = wrapcauchy_gen(a=0.0, b=2*np.pi, name='wrapcauchy') 

 

 

class gennorm_gen(rv_continuous): 

r"""A generalized normal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `gennorm` is [1]_:: 

 

beta 

gennorm.pdf(x, beta) = --------------- exp(-|x|**beta) 

2 gamma(1/beta) 

 

`gennorm` takes :math:`\beta` as a shape parameter. 

For :math:`\beta = 1`, it is identical to a Laplace distribution. 

For ``\beta = 2``, it is identical to a normal distribution 

(with :math:`scale=1/\sqrt{2}`). 

 

See Also 

-------- 

laplace : Laplace distribution 

norm : normal distribution 

 

References 

---------- 

 

.. [1] "Generalized normal distribution, Version 1", 

https://en.wikipedia.org/wiki/Generalized_normal_distribution#Version_1 

 

%(example)s 

 

""" 

 

def _pdf(self, x, beta): 

return np.exp(self._logpdf(x, beta)) 

 

def _logpdf(self, x, beta): 

return np.log(0.5*beta) - sc.gammaln(1.0/beta) - abs(x)**beta 

 

def _cdf(self, x, beta): 

c = 0.5 * np.sign(x) 

# evaluating (.5 + c) first prevents numerical cancellation 

return (0.5 + c) - c * sc.gammaincc(1.0/beta, abs(x)**beta) 

 

def _ppf(self, x, beta): 

c = np.sign(x - 0.5) 

# evaluating (1. + c) first prevents numerical cancellation 

return c * sc.gammainccinv(1.0/beta, (1.0 + c) - 2.0*c*x)**(1.0/beta) 

 

def _sf(self, x, beta): 

return self._cdf(-x, beta) 

 

def _isf(self, x, beta): 

return -self._ppf(x, beta) 

 

def _stats(self, beta): 

c1, c3, c5 = sc.gammaln([1.0/beta, 3.0/beta, 5.0/beta]) 

return 0., np.exp(c3 - c1), 0., np.exp(c5 + c1 - 2.0*c3) - 3. 

 

def _entropy(self, beta): 

return 1. / beta - np.log(.5 * beta) + sc.gammaln(1. / beta) 

 

 

gennorm = gennorm_gen(name='gennorm') 

 

 

class halfgennorm_gen(rv_continuous): 

r"""The upper half of a generalized normal continuous random variable. 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `halfgennorm` is: 

 

.. math:: 

 

f(x, \beta) = \frac{\beta}{\gamma(1/\beta)} \exp(-|x|^\beta) 

 

`gennorm` takes :math:`\beta` as a shape parameter. 

For :math:`\beta = 1`, it is identical to an exponential distribution. 

For :math:`\beta = 2`, it is identical to a half normal distribution 

(with :math:`scale=1/\sqrt{2}`). 

 

See Also 

-------- 

gennorm : generalized normal distribution 

expon : exponential distribution 

halfnorm : half normal distribution 

 

References 

---------- 

 

.. [1] "Generalized normal distribution, Version 1", 

https://en.wikipedia.org/wiki/Generalized_normal_distribution#Version_1 

 

%(example)s 

 

""" 

 

def _pdf(self, x, beta): 

# beta 

# halfgennorm.pdf(x, beta) = ------------- exp(-|x|**beta) 

# gamma(1/beta) 

return np.exp(self._logpdf(x, beta)) 

 

def _logpdf(self, x, beta): 

return np.log(beta) - sc.gammaln(1.0/beta) - x**beta 

 

def _cdf(self, x, beta): 

return sc.gammainc(1.0/beta, x**beta) 

 

def _ppf(self, x, beta): 

return sc.gammaincinv(1.0/beta, x)**(1.0/beta) 

 

def _sf(self, x, beta): 

return sc.gammaincc(1.0/beta, x**beta) 

 

def _isf(self, x, beta): 

return sc.gammainccinv(1.0/beta, x)**(1.0/beta) 

 

def _entropy(self, beta): 

return 1.0/beta - np.log(beta) + sc.gammaln(1.0/beta) 

 

 

halfgennorm = halfgennorm_gen(a=0, name='halfgennorm') 

 

 

class crystalball_gen(rv_continuous): 

r""" 

Crystalball distribution 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `crystalball` is: 

 

.. math:: 

 

f(x, \beta, m) = \begin{cases} 

N \exp(-x^2 / 2), &\text{for } x > -\beta\\ 

N A (B - x)^{-m} &\text{for } x \le -\beta 

\end{cases} 

 

where :math:`A = (m / |beta|)**n * exp(-beta**2 / 2)`, 

:math:`B = m/|beta| - |beta|` and :math:`N` is a normalisation constant. 

 

`crystalball` takes :math:`\beta` and :math:`m` as shape parameters. 

:math:`\beta` defines the point where the pdf changes from a power-law to a 

gaussian distribution :math:`m` is power of the power-law tail. 

 

References 

---------- 

.. [1] "Crystal Ball Function", 

https://en.wikipedia.org/wiki/Crystal_Ball_function 

 

%(after_notes)s 

 

.. versionadded:: 0.19.0 

 

%(example)s 

""" 

def _pdf(self, x, beta, m): 

""" 

Return PDF of the crystalball function. 

 

-- 

| exp(-x**2 / 2), for x > -beta 

crystalball.pdf(x, beta, m) = N * | 

| A * (B - x)**(-m), for x <= -beta 

-- 

""" 

N = 1.0 / (m/beta / (m-1) * np.exp(-beta**2 / 2.0) + _norm_pdf_C * _norm_cdf(beta)) 

rhs = lambda x, beta, m: np.exp(-x**2 / 2) 

lhs = lambda x, beta, m: (m/beta)**m * np.exp(-beta**2 / 2.0) * (m/beta - beta - x)**(-m) 

return N * _lazywhere(np.atleast_1d(x > -beta), (x, beta, m), f=rhs, f2=lhs) 

 

def _cdf(self, x, beta, m): 

""" 

Return CDF of the crystalball function 

""" 

N = 1.0 / (m/beta / (m-1) * np.exp(-beta**2 / 2.0) + _norm_pdf_C * _norm_cdf(beta)) 

rhs = lambda x, beta, m: (m/beta) * np.exp(-beta**2 / 2.0) / (m-1) + _norm_pdf_C * (_norm_cdf(x) - _norm_cdf(-beta)) 

lhs = lambda x, beta, m: (m/beta)**m * np.exp(-beta**2 / 2.0) * (m/beta - beta - x)**(-m+1) / (m-1) 

return N * _lazywhere(np.atleast_1d(x > -beta), (x, beta, m), f=rhs, f2=lhs) 

 

def _munp(self, n, beta, m): 

""" 

Returns the n-th non-central moment of the crystalball function. 

""" 

N = 1.0 / (m/beta / (m-1) * np.exp(-beta**2 / 2.0) + _norm_pdf_C * _norm_cdf(beta)) 

 

def n_th_moment(n, beta, m): 

""" 

Returns n-th moment. Defined only if n+1 < m 

Function cannot broadcast due to the loop over n 

""" 

A = (m/beta)**m * np.exp(-beta**2 / 2.0) 

B = m/beta - beta 

rhs = 2**((n-1)/2.0) * sc.gamma((n+1)/2) * (1.0 + (-1)**n * sc.gammainc((n+1)/2, beta**2 / 2)) 

lhs = np.zeros(rhs.shape) 

for k in range(n + 1): 

lhs += sc.binom(n, k) * B**(n-k) * (-1)**k / (m - k - 1) * (m/beta)**(-m + k + 1) 

return A * lhs + rhs 

 

return N * _lazywhere(np.atleast_1d(n + 1 < m), 

(n, beta, m), 

np.vectorize(n_th_moment, otypes=[np.float]), 

np.inf) 

 

def _argcheck(self, beta, m): 

""" 

In HEP crystal-ball is also defined for m = 1 (see plot on wikipedia) 

But the function doesn't have a finite integral in this corner case, 

and isn't a PDF anymore (but can still be used on a finite range). 

Here we restrict the function to m > 1. 

In addition we restrict beta to be positive 

""" 

return (m > 1) & (beta > 0) 

 

 

crystalball = crystalball_gen(name='crystalball', longname="A Crystalball Function") 

 

 

def _argus_phi(chi): 

""" 

Utility function for the argus distribution 

used in the CDF and norm of the Argus Funktion 

""" 

return _norm_cdf(chi) - chi * _norm_pdf(chi) - 0.5 

 

 

class argus_gen(rv_continuous): 

r""" 

Argus distribution 

 

%(before_notes)s 

 

Notes 

----- 

The probability density function for `argus` is: 

 

.. math:: 

 

f(x, \chi) = \frac{\chi^3}{\sqrt{2\pi} \Psi(\chi)} x \sqrt{1-x^2} 

\exp(- 0.5 \chi^2 (1 - x^2)) 

 

where: 

 

.. math:: 

 

\Psi(\chi) = \Phi(\chi) - \chi \phi(\chi) - 1/2 

 

with :math:`\Phi` and :math:`\phi` being the CDF and PDF of a standard 

normal distribution, respectively. 

 

`argus` takes :math:`\chi` as shape a parameter. 

 

References 

---------- 

 

.. [1] "ARGUS distribution", 

https://en.wikipedia.org/wiki/ARGUS_distribution 

 

%(after_notes)s 

 

.. versionadded:: 0.19.0 

 

%(example)s 

""" 

def _pdf(self, x, chi): 

""" 

Return PDF of the argus function 

 

argus.pdf(x, chi) = chi**3 / (sqrt(2*pi) * Psi(chi)) * x * 

sqrt(1-x**2) * exp(- 0.5 * chi**2 * (1 - x**2)) 

""" 

y = 1.0 - x**2 

return chi**3 / (_norm_pdf_C * _argus_phi(chi)) * x * np.sqrt(y) * np.exp(-chi**2 * y / 2) 

 

def _cdf(self, x, chi): 

""" 

Return CDF of the argus function 

""" 

return 1.0 - self._sf(x, chi) 

 

def _sf(self, x, chi): 

""" 

Return survival function of the argus function 

""" 

return _argus_phi(chi * np.sqrt(1 - x**2)) / _argus_phi(chi) 

 

 

argus = argus_gen(name='argus', longname="An Argus Function", a=0.0, b=1.0) 

 

 

class rv_histogram(rv_continuous): 

""" 

Generates a distribution given by a histogram. 

This is useful to generate a template distribution from a binned 

datasample. 

 

As a subclass of the `rv_continuous` class, `rv_histogram` inherits from it 

a collection of generic methods (see `rv_continuous` for the full list), 

and implements them based on the properties of the provided binned 

datasample. 

 

Parameters 

---------- 

histogram : tuple of array_like 

Tuple containing two array_like objects 

The first containing the content of n bins 

The second containing the (n+1) bin boundaries 

In particular the return value np.histogram is accepted 

 

Notes 

----- 

There are no additional shape parameters except for the loc and scale. 

The pdf is defined as a stepwise function from the provided histogram 

The cdf is a linear interpolation of the pdf. 

 

.. versionadded:: 0.19.0 

 

Examples 

-------- 

 

Create a scipy.stats distribution from a numpy histogram 

 

>>> import scipy.stats 

>>> import numpy as np 

>>> data = scipy.stats.norm.rvs(size=100000, loc=0, scale=1.5, random_state=123) 

>>> hist = np.histogram(data, bins=100) 

>>> hist_dist = scipy.stats.rv_histogram(hist) 

 

Behaves like an ordinary scipy rv_continuous distribution 

 

>>> hist_dist.pdf(1.0) 

0.20538577847618705 

>>> hist_dist.cdf(2.0) 

0.90818568543056499 

 

PDF is zero above (below) the highest (lowest) bin of the histogram, 

defined by the max (min) of the original dataset 

 

>>> hist_dist.pdf(np.max(data)) 

0.0 

>>> hist_dist.cdf(np.max(data)) 

1.0 

>>> hist_dist.pdf(np.min(data)) 

7.7591907244498314e-05 

>>> hist_dist.cdf(np.min(data)) 

0.0 

 

PDF and CDF follow the histogram 

 

>>> import matplotlib.pyplot as plt 

>>> X = np.linspace(-5.0, 5.0, 100) 

>>> plt.title("PDF from Template") 

>>> plt.hist(data, density=True, bins=100) 

>>> plt.plot(X, hist_dist.pdf(X), label='PDF') 

>>> plt.plot(X, hist_dist.cdf(X), label='CDF') 

>>> plt.show() 

 

""" 

_support_mask = rv_continuous._support_mask 

 

def __init__(self, histogram, *args, **kwargs): 

""" 

Create a new distribution using the given histogram 

 

Parameters 

---------- 

histogram : tuple of array_like 

Tuple containing two array_like objects 

The first containing the content of n bins 

The second containing the (n+1) bin boundaries 

In particular the return value np.histogram is accepted 

""" 

self._histogram = histogram 

if len(histogram) != 2: 

raise ValueError("Expected length 2 for parameter histogram") 

self._hpdf = np.asarray(histogram[0]) 

self._hbins = np.asarray(histogram[1]) 

if len(self._hpdf) + 1 != len(self._hbins): 

raise ValueError("Number of elements in histogram content " 

"and histogram boundaries do not match, " 

"expected n and n+1.") 

self._hbin_widths = self._hbins[1:] - self._hbins[:-1] 

self._hpdf = self._hpdf / float(np.sum(self._hpdf * self._hbin_widths)) 

self._hcdf = np.cumsum(self._hpdf * self._hbin_widths) 

self._hpdf = np.hstack([0.0, self._hpdf, 0.0]) 

self._hcdf = np.hstack([0.0, self._hcdf]) 

# Set support 

kwargs['a'] = self._hbins[0] 

kwargs['b'] = self._hbins[-1] 

super(rv_histogram, self).__init__(*args, **kwargs) 

 

def _pdf(self, x): 

""" 

PDF of the histogram 

""" 

return self._hpdf[np.searchsorted(self._hbins, x, side='right')] 

 

def _cdf(self, x): 

""" 

CDF calculated from the histogram 

""" 

return np.interp(x, self._hbins, self._hcdf) 

 

def _ppf(self, x): 

""" 

Percentile function calculated from the histogram 

""" 

return np.interp(x, self._hcdf, self._hbins) 

 

def _munp(self, n): 

"""Compute the n-th non-central moment.""" 

integrals = (self._hbins[1:]**(n+1) - self._hbins[:-1]**(n+1)) / (n+1) 

return np.sum(self._hpdf[1:-1] * integrals) 

 

def _entropy(self): 

"""Compute entropy of distribution""" 

res = _lazywhere(self._hpdf[1:-1] > 0.0, 

(self._hpdf[1:-1],), 

np.log, 

0.0) 

return -np.sum(self._hpdf[1:-1] * res * self._hbin_widths) 

 

def _updated_ctor_param(self): 

""" 

Set the histogram as additional constructor argument 

""" 

dct = super(rv_histogram, self)._updated_ctor_param() 

dct['histogram'] = self._histogram 

return dct 

 

 

# Collect names of classes and objects in this module. 

pairs = list(globals().items()) 

_distn_names, _distn_gen_names = get_distribution_names(pairs, rv_continuous) 

 

__all__ = _distn_names + _distn_gen_names + ['rv_histogram']